mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-26 13:49:01 +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
|
||||
group *RadioGroup
|
||||
selected bool
|
||||
|
||||
onChange func(bool)
|
||||
}
|
||||
|
||||
/*
|
||||
@ -40,6 +42,8 @@ func CreateRadio(parent Control, width int, title string, scale int) *Radio {
|
||||
c.SetTabStop(true)
|
||||
c.SetScale(scale)
|
||||
|
||||
c.onChange = nil
|
||||
|
||||
if parent != nil {
|
||||
parent.AddChild(c)
|
||||
}
|
||||
@ -104,7 +108,7 @@ func (c *Radio) ProcessEvent(event Event) bool {
|
||||
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 {
|
||||
c.SetSelected(true)
|
||||
} else {
|
||||
@ -120,6 +124,10 @@ func (c *Radio) ProcessEvent(event Event) bool {
|
||||
// the method directly, it is for RadioGroup control
|
||||
func (c *Radio) SetSelected(val bool) {
|
||||
c.selected = val
|
||||
|
||||
if c.onChange != nil {
|
||||
go c.onChange(val)
|
||||
}
|
||||
}
|
||||
|
||||
// Selected returns if the radio is selected
|
||||
@ -131,3 +139,12 @@ func (c *Radio) Selected() bool {
|
||||
func (c *Radio) SetGroup(group *RadioGroup) {
|
||||
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