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"
|
|
|
|
|
2024-08-24 17:50:29 -06:00
|
|
|
var _ tomo.Object = new(LabelCheckbox)
|
|
|
|
|
2024-05-07 11:45:06 -06:00
|
|
|
// LabelCheckbox is a checkbox with a label.
|
|
|
|
type LabelCheckbox struct {
|
2024-08-24 17:50:29 -06:00
|
|
|
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 {
|
2024-08-24 17:50:29 -06:00
|
|
|
labelCheckbox := &LabelCheckbox {
|
|
|
|
box: tomo.NewContainerBox(),
|
|
|
|
checkbox: NewCheckbox(value),
|
|
|
|
label: NewLabel(text),
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
2024-08-24 17:50:29 -06:00
|
|
|
labelCheckbox.box.SetRole(tomo.R("objects", "LabelCheckbox"))
|
2024-08-24 17:56:58 -06:00
|
|
|
labelCheckbox.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
|
|
|
labelCheckbox.label.GetBox().(tomo.TextBox).SetSelectable(false)
|
|
|
|
labelCheckbox.label.GetBox().(tomo.TextBox).SetFocusable(false)
|
2024-08-24 17:50:29 -06:00
|
|
|
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
|
|
|
|
2024-08-24 17:50:29 -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
|
|
|
}
|
|
|
|
|
2024-08-24 18:03:48 -06:00
|
|
|
// SetText sets the text label of the checkbox.
|
|
|
|
func (this *LabelCheckbox) SetText (text string) {
|
|
|
|
this.label.SetText(text)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// 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 {
|
2024-08-16 14:15:52 -06:00
|
|
|
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 {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-08-24 17:50:29 -06:00
|
|
|
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
|
|
|
}
|