objects/labelcheckbox.go

70 lines
1.9 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-07-21 09:48:28 -06:00
box.SetRole(tomo.R("objects", "LabelCheckbox"))
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
box.label.SetSelectable(false)
box.label.SetFocusable(false)
2024-05-07 11:45:06 -06:00
box.Add(box.checkbox)
box.Add(box.label)
2024-07-25 10:58:38 -06:00
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
2024-05-07 11:45:06 -06:00
2024-07-21 09:48:28 -06:00
box.OnButtonDown(box.handleButtonDown)
box.OnButtonUp(box.handleButtonUp)
2024-05-07 11:45:06 -06:00
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)
}
2024-07-25 10:58:38 -06:00
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
2024-07-25 10:58:38 -06:00
return true
2024-07-21 09:48:28 -06:00
}
2024-07-25 10:58:38 -06:00
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
2024-07-21 09:48:28 -06:00
if this.Window().MousePosition().In(this.Bounds()) {
2024-05-07 11:45:06 -06:00
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}
2024-07-25 10:58:38 -06:00
return true
2024-05-07 11:45:06 -06:00
}