Improvements to menus

Major progress on #4
This commit is contained in:
Sasha Koshka 2024-08-14 11:44:47 -04:00
parent 8068036219
commit eb0bf58961
2 changed files with 29 additions and 19 deletions

30
menu.go
View File

@ -18,22 +18,27 @@ type Menu struct {
}
// NewMenu creates a new menu with the specified items. The menu will appear
// directly under the anchor Object. If the anchor is nil, it will appear
// directly under the mouse pointer instead.
func NewMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
// directly under the mouse pointer.
func NewMenu (parent tomo.Window, items ...tomo.Object) (*Menu, error) {
bounds := (image.Rectangle { }).Add(parent.MousePosition())
return newMenu(parent, bounds, items...)
}
// NewAnchoredMenu creates a new menu with the specified items. The menu will
// appear directly under the anchor.
func NewAnchoredMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
parent := anchor.GetBox().Window()
bounds := menuBoundsFromAnchor(anchor)
return newMenu(parent, bounds, items...)
}
func newMenu (parent tomo.Window, bounds image.Rectangle, items ...tomo.Object) (*Menu, error) {
menu := &Menu { }
if anchor == nil {
// TODO: *actually* put it under the mouse
window, err := tomo.NewWindow(menu.bounds)
if err != nil { return nil, err }
menu.Window = window
} else {
menu.bounds = menuBoundsFromAnchor(anchor)
menu.parent = anchor.GetBox().Window()
menu.bounds = bounds
menu.parent = parent
window, err := menu.parent.NewMenu(menu.bounds)
if err != nil { return nil, err }
menu.Window = window
}
menu.rootContainer = tomo.NewContainerBox()
menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical))
@ -74,6 +79,7 @@ func (this *Menu) TearOff () {
this.Window.Close()
this.rootContainer.Remove(this.tearLine)
this.rootContainer.SetTag("torn", true)
this.Window = window
this.Window.SetRoot(this.rootContainer)

View File

@ -20,10 +20,15 @@ type MenuItem struct {
// 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("", tomo.IconSizeSmall),
icon: NewIcon(icon, tomo.IconSizeSmall),
}
box.SetRole(tomo.R("objects", "MenuItem"))
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
@ -49,8 +54,7 @@ func (this *MenuItem) SetText (text string) {
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
// remove it.
func (this *MenuItem) SetIcon (id tomo.Icon) {
if this.icon != nil { this.Remove(this.icon) }
this.Insert(NewIcon(id, tomo.IconSizeSmall), this.label)
this.icon.SetIcon(id, tomo.IconSizeSmall)
}
// OnClick specifies a function to be called when the menu item is clicked.