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"
|
|
|
|
|
|
|
|
// Dropdown is a non-editable text input that allows the user to pick a value
|
|
|
|
// from a list.
|
|
|
|
type Dropdown struct {
|
|
|
|
tomo.ContainerBox
|
|
|
|
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 {
|
|
|
|
ContainerBox: tomo.NewContainerBox(),
|
|
|
|
label: NewLabel(""),
|
|
|
|
}
|
|
|
|
dropdown.SetRole(tomo.R("objects", "Dropdown"))
|
|
|
|
dropdown.SetAttr(tomo.ALayout(layouts.Row { true, false }))
|
|
|
|
dropdown.Add(dropdown.label)
|
|
|
|
// TODO: replace IconValueDecrement with a drop-down expand icon once
|
|
|
|
// we get that
|
|
|
|
dropdown.Add(NewIcon(tomo.IconValueDecrement, 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)
|
|
|
|
return dropdown
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-14 17:06:41 -06:00
|
|
|
if this.Window().MousePosition().In(this.Bounds()) {
|
2024-08-16 14:15:52 -06:00
|
|
|
this.Choose()
|
2024-08-14 17:06:41 -06:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|