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

38
menu.go
View File

@ -18,23 +18,28 @@ type Menu struct {
} }
// NewMenu creates a new menu with the specified items. The menu will appear // 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.
// directly under the mouse pointer instead. func NewMenu (parent tomo.Window, items ...tomo.Object) (*Menu, error) {
func NewMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) { bounds := (image.Rectangle { }).Add(parent.MousePosition())
menu := &Menu { } return newMenu(parent, bounds, items...)
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()
window, err := menu.parent.NewMenu(menu.bounds)
if err != nil { return nil, err }
menu.Window = window
}
// 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 { }
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 = tomo.NewContainerBox()
menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical)) menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical))
@ -74,6 +79,7 @@ func (this *Menu) TearOff () {
this.Window.Close() this.Window.Close()
this.rootContainer.Remove(this.tearLine) this.rootContainer.Remove(this.tearLine)
this.rootContainer.SetTag("torn", true)
this.Window = window this.Window = window
this.Window.SetRoot(this.rootContainer) this.Window.SetRoot(this.rootContainer)

View File

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