2024-06-06 23:59:29 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
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-06-25 00:33:36 -06:00
|
|
|
// MenuItem is a selectable memu item.
|
2024-06-06 23:59:29 -06:00
|
|
|
type MenuItem struct {
|
|
|
|
tomo.ContainerBox
|
|
|
|
|
|
|
|
label *Label
|
|
|
|
icon *Icon
|
|
|
|
labelActive bool
|
|
|
|
|
|
|
|
on struct {
|
|
|
|
click event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMenuItem creates a new menu item with the specified text.
|
|
|
|
func NewMenuItem (text string) *MenuItem {
|
2024-08-14 09:44:47 -06:00
|
|
|
return NewIconMenuItem(tomo.IconUnknown, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIconMenuItem creates a new menu item with the specified icon and text.
|
|
|
|
func NewIconMenuItem (icon tomo.Icon, text string) *MenuItem {
|
2024-06-06 23:59:29 -06:00
|
|
|
box := &MenuItem {
|
|
|
|
ContainerBox: tomo.NewContainerBox(),
|
|
|
|
label: NewLabel(text),
|
2024-08-14 09:44:47 -06:00
|
|
|
icon: NewIcon(icon, tomo.IconSizeSmall),
|
2024-06-06 23:59:29 -06:00
|
|
|
}
|
2024-07-21 09:48:28 -06:00
|
|
|
box.SetRole(tomo.R("objects", "MenuItem"))
|
2024-08-24 17:56:58 -06:00
|
|
|
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
|
2024-07-25 10:58:38 -06:00
|
|
|
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
|
2024-06-06 23:59:29 -06:00
|
|
|
|
|
|
|
box.Add(box.icon)
|
|
|
|
box.Add(box.label)
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
box.SetInputMask(true)
|
2024-08-24 12:37:44 -06:00
|
|
|
box.OnMouseEnter(box.handleMouseEnter)
|
|
|
|
box.OnMouseLeave(box.handleMouseLeave)
|
2024-07-21 09:48:28 -06:00
|
|
|
box.OnButtonDown(box.handleButtonDown)
|
|
|
|
box.OnButtonUp(box.handleButtonUp)
|
2024-07-25 10:58:38 -06:00
|
|
|
box.OnKeyDown(box.handleKeyDown)
|
2024-06-06 23:59:29 -06:00
|
|
|
box.OnKeyUp(box.handleKeyUp)
|
|
|
|
box.SetFocusable(true)
|
|
|
|
return box
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the text of the items's label.
|
|
|
|
func (this *MenuItem) SetText (text string) {
|
|
|
|
this.label.SetText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
|
|
|
|
// remove it.
|
|
|
|
func (this *MenuItem) SetIcon (id tomo.Icon) {
|
2024-08-14 09:44:47 -06:00
|
|
|
this.icon.SetIcon(id, tomo.IconSizeSmall)
|
2024-06-06 23:59:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnClick specifies a function to be called when the menu item is clicked.
|
|
|
|
func (this *MenuItem) OnClick (callback func ()) event.Cookie {
|
|
|
|
return this.on.click.Connect(callback)
|
|
|
|
}
|
|
|
|
|
2024-08-24 12:37:44 -06:00
|
|
|
func (this *MenuItem) handleMouseEnter () {
|
|
|
|
this.SetFocused(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *MenuItem) handleMouseLeave () {
|
|
|
|
this.SetFocused(false)
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *MenuItem) handleKeyDown (key input.Key, numberPad bool) bool {
|
|
|
|
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *MenuItem) handleKeyUp (key input.Key, numberPad bool) bool {
|
|
|
|
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
2024-06-06 23:59:29 -06:00
|
|
|
this.on.click.Broadcast()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-06-06 23:59:29 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *MenuItem) handleButtonDown (button input.Button) bool {
|
|
|
|
if button != input.ButtonLeft { return false }
|
|
|
|
return true
|
2024-07-21 09:48:28 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *MenuItem) handleButtonUp (button input.Button) bool {
|
|
|
|
if button != input.ButtonLeft { return false }
|
2024-07-21 09:48:28 -06:00
|
|
|
if this.Window().MousePosition().In(this.Bounds()) {
|
2024-06-06 23:59:29 -06:00
|
|
|
this.on.click.Broadcast()
|
|
|
|
}
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-06-06 23:59:29 -06:00
|
|
|
}
|