Merge pull request #111 from enciris/master

Add OnChange() callback function on radio buttons
This commit is contained in:
Vladimir Markelov 2018-10-26 20:10:41 -07:00 committed by GitHub
commit 80da317c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,8 @@ type Radio struct {
BaseControl BaseControl
group *RadioGroup group *RadioGroup
selected bool selected bool
onChange func(bool)
} }
/* /*
@ -40,6 +42,8 @@ func CreateRadio(parent Control, width int, title string, scale int) *Radio {
c.SetTabStop(true) c.SetTabStop(true)
c.SetScale(scale) c.SetScale(scale)
c.onChange = nil
if parent != nil { if parent != nil {
parent.AddChild(c) parent.AddChild(c)
} }
@ -104,7 +108,7 @@ func (c *Radio) ProcessEvent(event Event) bool {
return false return false
} }
if (event.Type == EventKey && event.Key == term.KeySpace) || event.Type == EventMouse { if (event.Type == EventKey && event.Key == term.KeySpace) || event.Type == EventClick {
if c.group == nil { if c.group == nil {
c.SetSelected(true) c.SetSelected(true)
} else { } else {
@ -120,6 +124,10 @@ func (c *Radio) ProcessEvent(event Event) bool {
// the method directly, it is for RadioGroup control // the method directly, it is for RadioGroup control
func (c *Radio) SetSelected(val bool) { func (c *Radio) SetSelected(val bool) {
c.selected = val c.selected = val
if c.onChange != nil {
go c.onChange(val)
}
} }
// Selected returns if the radio is selected // Selected returns if the radio is selected
@ -131,3 +139,12 @@ func (c *Radio) Selected() bool {
func (c *Radio) SetGroup(group *RadioGroup) { func (c *Radio) SetGroup(group *RadioGroup) {
c.group = group c.group = group
} }
// OnChange sets the callback that is called whenever the state
// of the Radio is changed. Argument of callback is the current
func (c *Radio) OnChange(fn func(bool)) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.onChange = fn
}