objects/labelcheckbox.go

84 lines
2.5 KiB
Go

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)
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
box 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 {
labelCheckbox := &LabelCheckbox {
box: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
}
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
}
// 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)
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// 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.
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
return this.checkbox.OnValueChange(callback)
}
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
if !isClickingButton(button) { return false }
return true
}
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}
return true
}