-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_items.go
86 lines (73 loc) · 1.73 KB
/
menu_items.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"slices"
"github.com/gdamore/tcell/v2"
)
type MenuItem interface {
Validate() error
GetID() string
AsFolder() *MenuFolder
GetParentPath() string
GetParent() MenuItem
GetPath() string
Compare(MenuItem) int
OnChangedFunc()
OnSelectedFunc()
OnDoneFunc()
CurrentMenuInputCapture(event *tcell.EventKey) *tcell.EventKey
CurrentFocusInputCapture(event *tcell.EventKey) *tcell.EventKey
}
type MenuItems map[string]MenuItem
func (m MenuItems) Add(menuItem MenuItem) error {
if err := menuItem.Validate(); err != nil {
return err
}
m[menuItem.GetPath()] = menuItem
return nil
}
func (m MenuItems) Delete(menuItem MenuItem) {
childs := m.GetChilds(menuItem)
for _, child := range childs {
menuItems.Delete(child)
}
delete(m, menuItem.GetPath())
}
func (m MenuItems) MustAdd(menuItem MenuItem) {
if err := m.Add(menuItem); err != nil {
panic(err)
}
}
func (m MenuItems) GetChilds(parent MenuItem) []MenuItem {
childs := []MenuItem{}
for _, menuItem := range m {
if parent == nil {
if menuItem.GetParent() == nil {
childs = append(childs, menuItem)
} else {
continue
}
} else if menuItem.GetParent() == parent {
childs = append(childs, menuItem)
}
}
slices.SortStableFunc(childs, func(left, right MenuItem) int {
return left.Compare(right)
})
return childs
}
func (m MenuItems) GetByParentAndID(parent MenuItem, name string) MenuItem {
for _, menuItem := range m.GetChilds(parent) {
if menuItem.GetID() == name {
return menuItem
}
}
return nil
}
var (
globalKeys = []string{"<q> Quit", "<ctrl+s> Save"}
menuItems = MenuItems{}
currentMenuItem MenuItem
currentMenuItemKeys = []string{}
currentMenuFocus MenuItem
currentFocusKeys = []string{}
)