Checkbox no longer embeds tomo.ContainerBox

This commit is contained in:
Sasha Koshka 2024-08-24 15:41:47 -04:00
parent 4e8823ef9f
commit fc51e7ab9f

View File

@ -4,9 +4,11 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
var _ tomo.Object = new(Checkbox)
// Checkbox is a control that can be toggled. // Checkbox is a control that can be toggled.
type Checkbox struct { type Checkbox struct {
tomo.Box box tomo.Box
value bool value bool
on struct { on struct {
valueChange event.FuncBroadcaster valueChange event.FuncBroadcaster
@ -15,18 +17,30 @@ type Checkbox struct {
// NewCheckbox creates a new checkbox with the specified value. // NewCheckbox creates a new checkbox with the specified value.
func NewCheckbox (value bool) *Checkbox { func NewCheckbox (value bool) *Checkbox {
box := &Checkbox { checkbox := &Checkbox {
Box: tomo.NewBox(), box: tomo.NewBox(),
} }
box.SetRole(tomo.R("objects", "Checkbox")) checkbox.box.SetRole(tomo.R("objects", "Checkbox"))
box.SetValue(value) checkbox.SetValue(value)
box.OnButtonDown(box.handleButtonDown) checkbox.box.OnButtonDown(checkbox.handleButtonDown)
box.OnButtonUp(box.handleButtonUp) checkbox.box.OnButtonUp(checkbox.handleButtonUp)
box.OnKeyDown(box.handleKeyDown) checkbox.box.OnKeyDown(checkbox.handleKeyDown)
box.OnKeyUp(box.handleKeyUp) checkbox.box.OnKeyUp(checkbox.handleKeyUp)
box.SetFocusable(true) checkbox.box.SetFocusable(true)
return box return checkbox
}
// GetBox returns the underlying box.
func (this *Checkbox) 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 *Checkbox) SetFocused (focused bool) {
this.box.SetFocused(focused)
} }
// Value returns the value of the checkbox. // Value returns the value of the checkbox.
@ -38,8 +52,8 @@ func (this *Checkbox) Value () bool {
func (this *Checkbox) SetValue (value bool) { func (this *Checkbox) SetValue (value bool) {
this.value = value this.value = value
// the theme shall decide what checked and unchecked states look like // the theme shall decide what checked and unchecked states look like
this.SetTag("checked", value) this.box.SetTag("checked", value)
this.SetTag("unchecked", !value) this.box.SetTag("unchecked", !value)
} }
// Toggle toggles the value of the checkbox between true and false. // Toggle toggles the value of the checkbox between true and false.
@ -71,7 +85,7 @@ func (this *Checkbox) handleButtonDown (button input.Button) bool {
func (this *Checkbox) handleButtonUp (button input.Button) bool { func (this *Checkbox) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false } if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) { if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Toggle() this.Toggle()
} }
return true return true