2018-08-02 02:52:09 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-11 00:19:47 +00:00
|
|
|
"strconv"
|
2018-08-02 02:52:09 +00:00
|
|
|
|
2018-12-30 22:22:50 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/ui/theme"
|
2018-08-02 02:52:09 +00:00
|
|
|
)
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// CheckButton implements a checkbox and radiobox widget. It's based on a
|
|
|
|
// Button and holds a boolean or string pointer (boolean for checkbox,
|
|
|
|
// string for radio).
|
2018-08-02 02:52:09 +00:00
|
|
|
type CheckButton struct {
|
|
|
|
Button
|
2018-08-11 00:19:47 +00:00
|
|
|
BoolVar *bool
|
|
|
|
StringVar *string
|
|
|
|
Value string
|
2018-08-02 02:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckButton creates a new CheckButton.
|
|
|
|
func NewCheckButton(name string, boolVar *bool, child Widget) *CheckButton {
|
|
|
|
w := &CheckButton{
|
|
|
|
BoolVar: boolVar,
|
|
|
|
}
|
|
|
|
w.Button.child = child
|
|
|
|
w.IDFunc(func() string {
|
|
|
|
return fmt.Sprintf("CheckButton<%s %+v>", name, w.BoolVar)
|
|
|
|
})
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
w.setup()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRadioButton creates a CheckButton bound to a string variable.
|
|
|
|
func NewRadioButton(name string, stringVar *string, value string, child Widget) *CheckButton {
|
|
|
|
w := &CheckButton{
|
|
|
|
StringVar: stringVar,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
w.Button.child = child
|
|
|
|
w.IDFunc(func() string {
|
|
|
|
return fmt.Sprintf(`RadioButton<%s "%s" %s>`, name, w.Value, strconv.FormatBool(*w.StringVar == w.Value))
|
|
|
|
})
|
|
|
|
w.setup()
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute to re-evaluate the button state (in the case of radio buttons where
|
|
|
|
// a different button will affect the state of this one when clicked).
|
|
|
|
func (w *CheckButton) Compute(e render.Engine) {
|
|
|
|
if w.StringVar != nil {
|
|
|
|
// Radio button, always re-assign the border style in case a sister
|
|
|
|
// radio button has changed the value.
|
|
|
|
if *w.StringVar == w.Value {
|
|
|
|
w.SetBorderStyle(BorderSunken)
|
|
|
|
} else {
|
|
|
|
w.SetBorderStyle(BorderRaised)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Button.Compute(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the common things between checkboxes and radioboxes.
|
|
|
|
func (w *CheckButton) setup() {
|
2018-08-02 02:52:09 +00:00
|
|
|
var borderStyle BorderStyle = BorderRaised
|
|
|
|
if w.BoolVar != nil {
|
|
|
|
if *w.BoolVar == true {
|
|
|
|
borderStyle = BorderSunken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Configure(Config{
|
|
|
|
BorderSize: 2,
|
|
|
|
BorderStyle: borderStyle,
|
|
|
|
OutlineSize: 1,
|
|
|
|
OutlineColor: theme.ButtonOutlineColor,
|
|
|
|
Background: theme.ButtonBackgroundColor,
|
|
|
|
})
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseOver, func(p render.Point) {
|
2018-08-02 02:52:09 +00:00
|
|
|
w.hovering = true
|
|
|
|
w.SetBackground(theme.ButtonHoverColor)
|
|
|
|
})
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseOut, func(p render.Point) {
|
2018-08-02 02:52:09 +00:00
|
|
|
w.hovering = false
|
|
|
|
w.SetBackground(theme.ButtonBackgroundColor)
|
|
|
|
})
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseDown, func(p render.Point) {
|
2018-08-02 02:52:09 +00:00
|
|
|
w.clicked = true
|
|
|
|
w.SetBorderStyle(BorderSunken)
|
|
|
|
})
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseUp, func(p render.Point) {
|
2018-08-02 02:52:09 +00:00
|
|
|
w.clicked = false
|
|
|
|
})
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(Click, func(p render.Point) {
|
2018-08-11 00:19:47 +00:00
|
|
|
var sunken bool
|
2018-08-02 02:52:09 +00:00
|
|
|
if w.BoolVar != nil {
|
|
|
|
if *w.BoolVar {
|
|
|
|
*w.BoolVar = false
|
|
|
|
} else {
|
|
|
|
*w.BoolVar = true
|
2018-08-11 00:19:47 +00:00
|
|
|
sunken = true
|
2018-08-02 02:52:09 +00:00
|
|
|
}
|
2018-08-11 00:19:47 +00:00
|
|
|
} else if w.StringVar != nil {
|
|
|
|
*w.StringVar = w.Value
|
|
|
|
sunken = true
|
2018-08-02 02:52:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
if sunken {
|
|
|
|
w.SetBorderStyle(BorderSunken)
|
|
|
|
} else {
|
|
|
|
w.SetBorderStyle(BorderRaised)
|
|
|
|
}
|
|
|
|
})
|
2018-08-02 02:52:09 +00:00
|
|
|
}
|