Dropdown no longer embeds tomo.ContainerBox

This commit is contained in:
Sasha Koshka 2024-08-24 19:33:16 -04:00
parent 0c4e098680
commit df2e8f1b07

View File

@ -6,10 +6,12 @@ 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(Dropdown)
// Dropdown is a non-editable text input that allows the user to pick a value
// from a list.
type Dropdown struct {
tomo.ContainerBox
box tomo.ContainerBox
label *Label
value string
@ -24,28 +26,40 @@ type Dropdown struct {
// NewDropdown creates a new dropdown input with the specified items
func NewDropdown (items ...string) *Dropdown {
dropdown := &Dropdown {
ContainerBox: tomo.NewContainerBox(),
box: tomo.NewContainerBox(),
label: NewLabel(""),
}
dropdown.SetRole(tomo.R("objects", "Dropdown"))
dropdown.SetAttr(tomo.ALayout(layouts.Row { true, false }))
dropdown.Add(dropdown.label)
dropdown.Add(NewIcon(tomo.IconListChoose, tomo.IconSizeSmall))
dropdown.box.SetRole(tomo.R("objects", "Dropdown"))
dropdown.box.SetAttr(tomo.ALayout(layouts.Row { true, false }))
dropdown.box.Add(dropdown.label)
dropdown.box.Add(NewIcon(tomo.IconListChoose, tomo.IconSizeSmall))
dropdown.SetItems(items...)
if len(items) > 0 {
dropdown.SetValue(items[0])
}
dropdown.SetInputMask(true)
dropdown.OnButtonDown(dropdown.handleButtonDown)
dropdown.OnButtonUp(dropdown.handleButtonUp)
dropdown.OnKeyDown(dropdown.handleKeyDown)
dropdown.OnKeyUp(dropdown.handleKeyUp)
dropdown.SetFocusable(true)
dropdown.box.SetInputMask(true)
dropdown.box.OnButtonDown(dropdown.handleButtonDown)
dropdown.box.OnButtonUp(dropdown.handleButtonUp)
dropdown.box.OnKeyDown(dropdown.handleKeyDown)
dropdown.box.OnKeyUp(dropdown.handleKeyUp)
dropdown.box.SetFocusable(true)
return dropdown
}
// GetBox returns the underlying box.
func (this *Dropdown) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this dropdown has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Dropdown) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the value of the dropdown. This does not necissarily have to be
// in the list of items.
func (this *Dropdown) Value () string {
@ -107,7 +121,7 @@ func (this *Dropdown) handleButtonDown (button input.Button) bool {
func (this *Dropdown) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true