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"
|
|
|
|
|
2024-08-24 13:41:47 -06:00
|
|
|
var _ tomo.Object = new(Checkbox)
|
|
|
|
|
2024-05-07 11:45:06 -06:00
|
|
|
// Checkbox is a control that can be toggled.
|
2024-08-25 00:36:05 -06:00
|
|
|
//
|
|
|
|
// Tags:
|
|
|
|
// - [checked] The checkbox's value is true.
|
|
|
|
// - [unchecked] The checkbox's value is false.
|
2024-05-07 11:45:06 -06:00
|
|
|
type Checkbox struct {
|
2024-08-24 13:41:47 -06:00
|
|
|
box tomo.Box
|
2024-05-07 11:45:06 -06:00
|
|
|
value bool
|
|
|
|
on struct {
|
|
|
|
valueChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox creates a new checkbox with the specified value.
|
|
|
|
func NewCheckbox (value bool) *Checkbox {
|
2024-08-24 13:41:47 -06:00
|
|
|
checkbox := &Checkbox {
|
|
|
|
box: tomo.NewBox(),
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
2024-08-24 13:41:47 -06:00
|
|
|
checkbox.box.SetRole(tomo.R("objects", "Checkbox"))
|
|
|
|
checkbox.SetValue(value)
|
2024-05-07 11:45:06 -06:00
|
|
|
|
2024-08-24 13:41:47 -06:00
|
|
|
checkbox.box.OnButtonDown(checkbox.handleButtonDown)
|
|
|
|
checkbox.box.OnButtonUp(checkbox.handleButtonUp)
|
|
|
|
checkbox.box.OnKeyDown(checkbox.handleKeyDown)
|
|
|
|
checkbox.box.OnKeyUp(checkbox.handleKeyUp)
|
|
|
|
checkbox.box.SetFocusable(true)
|
|
|
|
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)
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// Value returns the value of the checkbox.
|
|
|
|
func (this *Checkbox) Value () bool {
|
|
|
|
return this.value
|
|
|
|
}
|
|
|
|
|
2024-05-07 11:45:06 -06:00
|
|
|
// SetValue sets the value of the checkbox.
|
|
|
|
func (this *Checkbox) SetValue (value bool) {
|
|
|
|
this.value = value
|
2024-07-21 09:48:28 -06:00
|
|
|
// the theme shall decide what checked and unchecked states look like
|
2024-08-24 13:41:47 -06:00
|
|
|
this.box.SetTag("checked", value)
|
|
|
|
this.box.SetTag("unchecked", !value)
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle toggles the value of the checkbox between true and false.
|
|
|
|
func (this *Checkbox) Toggle () {
|
|
|
|
this.SetValue(!this.Value())
|
|
|
|
}
|
|
|
|
|
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 *Checkbox) OnValueChange (callback func ()) event.Cookie {
|
|
|
|
return this.on.valueChange.Connect(callback)
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Checkbox) handleKeyDown (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { return false }
|
2024-05-07 11:45:06 -06:00
|
|
|
this.Toggle()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { 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 *Checkbox) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Checkbox) handleButtonUp (button input.Button) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-08-24 13:41:47 -06:00
|
|
|
if this.box.Window().MousePosition().In(this.box.Bounds()) {
|
2024-05-07 11:45:06 -06:00
|
|
|
this.Toggle()
|
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-05-07 11:45:06 -06:00
|
|
|
}
|