98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
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"
|
|
|
|
// MenuItem is a selectable memu item.
|
|
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 {
|
|
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 {
|
|
box := &MenuItem {
|
|
ContainerBox: tomo.NewContainerBox(),
|
|
label: NewLabel(text),
|
|
icon: NewIcon(icon, tomo.IconSizeSmall),
|
|
}
|
|
box.SetRole(tomo.R("objects", "MenuItem"))
|
|
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
|
box.SetAttr(tomo.ALayout(layouts.Row { false, true }))
|
|
|
|
box.Add(box.icon)
|
|
box.Add(box.label)
|
|
|
|
box.SetInputMask(true)
|
|
box.OnMouseEnter(box.handleMouseEnter)
|
|
box.OnMouseLeave(box.handleMouseLeave)
|
|
box.OnButtonDown(box.handleButtonDown)
|
|
box.OnButtonUp(box.handleButtonUp)
|
|
box.OnKeyDown(box.handleKeyDown)
|
|
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) {
|
|
this.icon.SetIcon(id, tomo.IconSizeSmall)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
func (this *MenuItem) handleMouseEnter () {
|
|
this.SetFocused(true)
|
|
}
|
|
|
|
func (this *MenuItem) handleMouseLeave () {
|
|
this.SetFocused(false)
|
|
}
|
|
|
|
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 }
|
|
this.on.click.Broadcast()
|
|
return true
|
|
}
|
|
|
|
func (this *MenuItem) handleButtonDown (button input.Button) bool {
|
|
if button != input.ButtonLeft { return false }
|
|
return true
|
|
}
|
|
|
|
func (this *MenuItem) handleButtonUp (button input.Button) bool {
|
|
if button != input.ButtonLeft { return false }
|
|
if this.Window().MousePosition().In(this.Bounds()) {
|
|
this.on.click.Broadcast()
|
|
}
|
|
return true
|
|
}
|