2024-08-14 17:06:41 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
// import "image"
|
|
|
|
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:33:16 -06:00
|
|
|
var _ tomo.Object = new(Dropdown)
|
|
|
|
|
2024-08-14 17:06:41 -06:00
|
|
|
// Dropdown is a non-editable text input that allows the user to pick a value
|
|
|
|
// from a list.
|
|
|
|
type Dropdown struct {
|
2024-08-24 17:33:16 -06:00
|
|
|
box tomo.ContainerBox
|
2024-08-14 17:06:41 -06:00
|
|
|
label *Label
|
|
|
|
|
|
|
|
value string
|
|
|
|
items []string
|
|
|
|
menu *Menu
|
|
|
|
|
|
|
|
on struct {
|
|
|
|
valueChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDropdown creates a new dropdown input with the specified items
|
|
|
|
func NewDropdown (items ...string) *Dropdown {
|
|
|
|
dropdown := &Dropdown {
|
2024-08-24 17:33:16 -06:00
|
|
|
box: tomo.NewContainerBox(),
|
2024-08-14 17:06:41 -06:00
|
|
|
label: NewLabel(""),
|
|
|
|
}
|
2024-08-24 17:33:16 -06:00
|
|
|
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))
|
2024-08-14 17:06:41 -06:00
|
|
|
|
|
|
|
dropdown.SetItems(items...)
|
|
|
|
if len(items) > 0 {
|
|
|
|
dropdown.SetValue(items[0])
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:33:16 -06:00
|
|
|
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)
|
2024-08-14 17:06:41 -06:00
|
|
|
return dropdown
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:33:16 -06:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:06:41 -06:00
|
|
|
// Value returns the value of the dropdown. This does not necissarily have to be
|
|
|
|
// in the list of items.
|
|
|
|
func (this *Dropdown) Value () string {
|
|
|
|
return this.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets the value of the dropdown. This does not necissarily have to be
|
|
|
|
// in the list of items.
|
|
|
|
func (this *Dropdown) SetValue (value string) {
|
|
|
|
this.value = value
|
|
|
|
this.label.SetText(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetItems sets the items from which the user is able to pick.
|
|
|
|
func (this *Dropdown) SetItems (items ...string) {
|
|
|
|
this.items = items
|
|
|
|
}
|
|
|
|
|
2024-08-16 14:15:52 -06:00
|
|
|
// Choose creates a menu that allows the user to pick a value.
|
|
|
|
func (this *Dropdown) Choose () {
|
|
|
|
if this.menu != nil {
|
|
|
|
this.menu.Close()
|
|
|
|
}
|
|
|
|
menu, err := NewAnchoredMenu(this, this.itemList()...)
|
|
|
|
if err != nil { return }
|
|
|
|
this.menu = menu
|
|
|
|
menu.SetVisible(true)
|
|
|
|
}
|
|
|
|
|
2024-08-14 17:06:41 -06:00
|
|
|
func (this *Dropdown) itemList () []tomo.Object {
|
|
|
|
items := make([]tomo.Object, len(this.items))
|
|
|
|
for index, value := range this.items {
|
|
|
|
value := value
|
|
|
|
item := NewMenuItem(value)
|
|
|
|
item.OnClick(func () {
|
|
|
|
this.SetValue(value)
|
|
|
|
this.on.valueChange.Broadcast()
|
|
|
|
})
|
|
|
|
items[index] = item
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Dropdown) handleKeyDown (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { return false }
|
|
|
|
this.Choose()
|
2024-08-14 17:06:41 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Dropdown) handleKeyUp (key input.Key, numberPad bool) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingKey(key) { return false }
|
2024-08-14 17:06:41 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Dropdown) handleButtonDown (button input.Button) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-08-14 17:06:41 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Dropdown) handleButtonUp (button input.Button) bool {
|
2024-08-16 14:15:52 -06:00
|
|
|
if !isClickingButton(button) { return false }
|
2024-08-24 17:33:16 -06:00
|
|
|
if this.box.Window().MousePosition().In(this.box.Bounds()) {
|
2024-08-16 14:15:52 -06:00
|
|
|
this.Choose()
|
2024-08-14 17:06:41 -06:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|