objects/labelcheckbox.go

62 lines
1.7 KiB
Go
Raw Normal View History

2024-05-07 11:45:06 -06:00
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
tomo.ContainerBox
checkbox *Checkbox
label *Label
}
// NewLabelCheckbox creates a new labeled checkbox with the specified value and
// label text.
func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
box := &LabelCheckbox {
ContainerBox: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
}
2024-06-03 19:13:18 -06:00
box.SetRole(tomo.R("objects", "LabelCheckbox", ""))
2024-05-07 11:45:06 -06:00
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
box.Add(box.checkbox)
box.Add(box.label)
2024-06-22 16:44:26 -06:00
box.SetLayout(layouts.Row { false, true })
2024-05-07 11:45:06 -06:00
box.OnMouseUp(box.handleMouseUp)
box.label.OnMouseUp(box.handleMouseUp)
return box
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
2024-05-07 11:45:06 -06:00
// SetValue sets the value of the checkbox.
func (this *LabelCheckbox) SetValue (value bool) {
this.checkbox.SetValue(value)
}
// Toggle toggles the value of the checkbox between true and false.
func (this *LabelCheckbox) Toggle () {
this.checkbox.Toggle()
}
// OnValueChange specifies a function to be called when the user checks or
// unchecks the checkbox.
2024-05-07 11:45:06 -06:00
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
return this.checkbox.OnValueChange(callback)
}
func (this *LabelCheckbox) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}
}