objects/menu.go

133 lines
3.6 KiB
Go
Raw Normal View History

2024-06-06 23:59:29 -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"
// Menu is a menu window.
//
// Sub-components:
// - TearLine is a horizontal line at the top of the menu that, when clicked,
// causes the menu to be "torn off" into a movable window.
2024-06-06 23:59:29 -06:00
type Menu struct {
tomo.Window
parent tomo.Window
bounds image.Rectangle
rootContainer tomo.ContainerBox
2024-07-25 10:58:38 -06:00
tearLine tomo.Object
2024-06-06 23:59:29 -06:00
torn bool
}
// NewMenu creates a new menu with the specified items. The menu will appear
// 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...)
}
2024-06-06 23:59:29 -06:00
// 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
2024-06-06 23:59:29 -06:00
menu.rootContainer = tomo.NewContainerBox()
2024-07-25 10:58:38 -06:00
menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical))
2024-06-06 23:59:29 -06:00
if !menu.torn {
2024-07-25 10:58:38 -06:00
menu.tearLine = menu.newTearLine()
2024-06-06 23:59:29 -06:00
menu.rootContainer.Add(menu.tearLine)
}
for _, item := range items {
menu.rootContainer.Add(item)
if item, ok := item.(*MenuItem); ok {
item.OnClick(func () {
if !menu.torn {
menu.Close()
}
})
}
}
2024-07-21 09:48:28 -06:00
menu.rootContainer.SetRole(tomo.R("objects", "Container"))
menu.rootContainer.SetTag("menu", true)
2024-06-06 23:59:29 -06:00
menu.Window.SetRoot(menu.rootContainer)
return menu, nil
}
// TearOff converts this menu into a tear-off menu.
func (this *Menu) TearOff () {
if this.torn { return }
if this.parent == nil { return }
this.torn = true
window, err := this.parent.NewChild(this.bounds)
2024-08-23 23:00:34 -06:00
window.SetIcon(tomo.IconListChoose)
2024-06-06 23:59:29 -06:00
if err != nil { return }
visible := this.Window.Visible()
this.Window.SetRoot(nil)
this.Window.Close()
this.rootContainer.Remove(this.tearLine)
this.rootContainer.SetTag("torn", true)
2024-06-06 23:59:29 -06:00
this.Window = window
this.Window.SetRoot(this.rootContainer)
this.Window.SetVisible(visible)
}
2024-07-25 10:58:38 -06:00
func (this *Menu) newTearLine () tomo.Object {
tearLine := tomo.NewBox()
tearLine.SetRole(tomo.R("objects", "TearLine"))
tearLine.SetFocusable(true)
2024-08-24 12:42:08 -06:00
tearLine.OnMouseEnter(func () {
tearLine.SetFocused(true)
})
tearLine.OnMouseLeave(func () {
tearLine.SetFocused(false)
})
2024-07-25 10:58:38 -06:00
tearLine.OnKeyDown(func (key input.Key, numberPad bool) bool {
2024-08-24 12:32:19 -06:00
if !isClickingKey(key) { return false }
2024-07-25 10:58:38 -06:00
return true
})
tearLine.OnKeyUp(func (key input.Key, numberPad bool) bool {
2024-08-24 12:32:19 -06:00
if !isClickingKey(key) { return false }
2024-07-25 10:58:38 -06:00
this.TearOff()
return true
})
tearLine.OnButtonDown(func (button input.Button) bool {
2024-08-24 12:32:19 -06:00
if !isClickingButton(button) { return false }
2024-07-25 10:58:38 -06:00
return true
})
tearLine.OnButtonUp(func (button input.Button) bool {
2024-08-24 12:32:19 -06:00
if !isClickingButton(button) { return false }
2024-07-25 10:58:38 -06:00
if tearLine.Window().MousePosition().In(tearLine.Bounds()) {
this.TearOff()
}
return true
})
return tearLine
}
2024-06-06 23:59:29 -06:00
func menuBoundsFromAnchor (anchor tomo.Object) image.Rectangle {
bounds := anchor.GetBox().Bounds()
return image.Rect (
bounds.Min.X, bounds.Max.Y,
bounds.Max.X, bounds.Max.Y)//.Add(windowBounds.Min)
}