objects/labelcheckbox.go

89 lines
2.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"
var _ tomo.Object = new(LabelCheckbox)
2024-05-07 11:45:06 -06:00
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
box tomo.ContainerBox
2024-05-07 11:45:06 -06:00
checkbox *Checkbox
label *Label
}
// NewLabelCheckbox creates a new labeled checkbox with the specified value and
// label text.
func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
labelCheckbox := &LabelCheckbox {
box: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
2024-05-07 11:45:06 -06:00
}
labelCheckbox.box.SetRole(tomo.R("objects", "LabelCheckbox"))
labelCheckbox.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
labelCheckbox.label.GetBox().(tomo.TextBox).SetSelectable(false)
labelCheckbox.label.GetBox().(tomo.TextBox).SetFocusable(false)
labelCheckbox.box.Add(labelCheckbox.checkbox)
labelCheckbox.box.Add(labelCheckbox.label)
labelCheckbox.box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
labelCheckbox.box.OnButtonDown(labelCheckbox.handleButtonDown)
labelCheckbox.box.OnButtonUp(labelCheckbox.handleButtonUp)
return labelCheckbox
}
// GetBox returns the underlying box.
func (this *LabelCheckbox) GetBox () tomo.Box {
return this.box
}
2024-05-07 11:45:06 -06:00
// SetFocused sets whether or not this checkbox has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *LabelCheckbox) SetFocused (focused bool) {
this.checkbox.SetFocused(focused)
2024-05-07 11:45:06 -06:00
}
// SetText sets the text label of the checkbox.
func (this *LabelCheckbox) SetText (text string) {
this.label.SetText(text)
}
// 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 }
if this.box.Window().MousePosition().In(this.box.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
}