Checkbox: SetText and better BoolVar watch for CheckButton

master
Noah 2022-10-09 17:41:59 -07:00
parent c99e79d9b0
commit d82ef0b751
2 changed files with 18 additions and 0 deletions

View File

@ -61,6 +61,14 @@ func (w *CheckButton) Compute(e render.Engine) {
} else {
w.SetBorderStyle(BorderRaised)
}
} else if w.BoolVar != nil {
// Checkbutton, always re-assign the border style in case the caller
// has flipped the boolean behind our back.
if *w.BoolVar {
w.SetBorderStyle(BorderSunken)
} else {
w.SetBorderStyle(BorderRaised)
}
}
w.Button.Compute(e)
}

View File

@ -1,5 +1,7 @@
package ui
import "errors"
// Checkbox combines a CheckButton with a widget like a Label.
type Checkbox struct {
Frame
@ -60,6 +62,14 @@ func (w *Checkbox) Child() Widget {
return w.child
}
// SetText conveniently sets the button text, for Label children only.
func (w *Checkbox) SetText(text string) error {
if label, ok := w.child.(*Label); ok {
label.Text = text
}
return errors.New("child is not a Label widget")
}
// Pass event handlers on to descendents.
func (w *Checkbox) Handle(e Event, fn func(EventData) error) {
w.button.Handle(e, fn)