turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

menu.go · 139 lines · 4.1 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 18h ago1package ui
2
3// The menu bar's types and its state. What it draws is in menu_draw.go, what it
4// answers to is in menu_events.go, and the second-level panels are in
5// submenu.go.
6
7// MenuItem is one line of a drop-down menu.
8//
9// Label may carry a hot key written with tildes, as in "~O~pen…". Shortcut is
10// the key combination shown right-aligned; the menu only displays it, since
11// the shortcut itself is handled wherever global keys are.
12type MenuItem struct {
13 Label string
14 Shortcut string
15 Action func()
16 Enabled func() bool
17 Separator bool
18
19 // Items makes this a submenu: choosing it opens a second drop-down beside
20 // the first rather than running anything, and Action is ignored.
21 //
22 // One level deep is all there is. The menus that need it — snippets
23 // grouped by kind — are exactly one level, and a general depth would mean
24 // replacing the bar's two indices with a path for a need nobody has.
25 Items []*MenuItem
26}
27
28// hasSubmenu reports whether choosing this item opens another drop-down.
29func (m *MenuItem) hasSubmenu() bool { return len(m.Items) > 0 }
30
31// enabled reports whether the item can be chosen right now. An item with no
32// Enabled function is always available, which is the common case.
33func (m *MenuItem) enabled() bool {
34 if m.Separator {
35 return false
36 }
37 if m.Action == nil && !m.hasSubmenu() {
38 return false
39 }
40 return m.Enabled == nil || m.Enabled()
41}
42
43// width returns how many cells the item needs, shortcut or marker included.
44func (m *MenuItem) width() int {
45 if m.Separator {
46 return 0
47 }
48
49 width := LabelWidth(m.Label)
50 switch {
51 case m.Shortcut != "":
52 width += len([]rune(m.Shortcut)) + 3
53 case m.hasSubmenu():
54 width += len([]rune(submenuMarker)) + 2
55 }
56 return width
57}
58
59// Menu is one drop-down: a title on the bar and the items under it.
60type Menu struct {
61 Label string
62 Items []*MenuItem
63
64 // OnOpen is called just before the menu drops down, so its owner can
65 // refill Items first.
66 //
67 // A menu built from a file, or from what the front window holds, cannot be
68 // decided once at start-up. This is the hook for that, and it runs at the
69 // only moment the contents are about to be seen.
70 OnOpen func()
71}
72
73// MenuBar is the row of menu titles across the top of the screen, and the
74// drop-down currently open under it.
75//
76// It is drawn last and offered events first, so an open menu takes precedence
77// over everything behind it.
78type MenuBar struct {
79 Box
80 menus []*Menu
81
82 openMenu int // -1 when nothing is open
83 highlight int
84
85 // openItem is the index of the drop-down item whose submenu is showing, or
86 // -1 when none is, and subHighlight is the highlighted line inside it.
87 openItem int
88 subHighlight int
89}
90
91// NewMenuBar returns a closed menu bar holding the given menus.
92func NewMenuBar(menus ...*Menu) *MenuBar {
93 return &MenuBar{menus: menus, openMenu: -1, openItem: -1}
94}
95
96// Menus returns the menus on the bar.
97func (b *MenuBar) Menus() []*Menu { return b.menus }
98
99// SetMenus replaces the menus on the bar, closing whatever was open.
100//
101// Menu.OnOpen refills one menu; this is for the rarer case where the *set* of
102// menus changes — a project file that names menus of its own can add or remove
103// one while the editor is running. Closing first matters: the open index refers
104// to the old slice, and keeping it would drop down whichever menu happened to
105// land at that position.
106//
107// bar.SetMenus(append(fixedMenus(), toolMenus()...))
108func (b *MenuBar) SetMenus(menus []*Menu) {
109 b.Close()
110 b.menus = menus
111}
112
113// Open reports whether a drop-down is currently showing.
114func (b *MenuBar) Open() bool { return b.openMenu >= 0 }
115
116// OpenMenu drops down menu i and highlights its first usable item.
117//
118// The menu's OnOpen runs first, so a menu whose contents come from elsewhere is
119// filled in before anyone sees it.
120func (b *MenuBar) OpenMenu(i int) {
121 if i < 0 || i >= len(b.menus) {
122 return
123 }
124 if b.menus[i].OnOpen != nil {
125 b.menus[i].OnOpen()
126 }
127
128 b.openMenu = i
129 b.openItem = -1
130 b.highlight = b.nextUsableIn(b.menus[i].Items, -1, +1)
131}
132
133// Close puts the open drop-down away, and the submenu with it.
134func (b *MenuBar) Close() {
135 b.openMenu = -1
136 b.openItem = -1
137 b.highlight = 0
138 b.subHighlight = 0
139}