mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-28 13:48:50 +08:00
Merge pull request #111 from enciris/master
Add OnChange() callback function on radio buttons
This commit is contained in:
commit
80da317c70
19
radio.go
19
radio.go
@ -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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user