turbo-editors/turbo-corepublic Fork 0
main
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.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
menu_events.go · 181 lines · 4.2 KBGo Blame HistoryRaw
  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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ui

// The keyboard and the mouse on the menu bar.

import "github.com/gdamore/tcell/v2"

// HandleKey drives the bar: F10 and Alt-letter open it, and once open the
// arrow keys, Enter and Escape work it.
func (b *MenuBar) HandleKey(ev *tcell.EventKey) bool {
	if !b.Open() {
		return b.handleClosedKey(ev)
	}
	return b.handleOpenKey(ev)
}

// handleClosedKey opens the bar on F10 or on the Alt-letter of a menu.
func (b *MenuBar) handleClosedKey(ev *tcell.EventKey) bool {
	if ev.Key() == tcell.KeyF10 {
		b.OpenMenu(0)
		return true
	}
	if ev.Modifiers()&tcell.ModAlt == 0 || ev.Key() != tcell.KeyRune {
		return false
	}

	for i, menu := range b.menus {
		if MatchesHotKey(menu.Label, ev.Rune()) {
			b.OpenMenu(i)
			return true
		}
	}
	return false
}

// handleOpenKey works whichever drop-down the keyboard is in.
func (b *MenuBar) handleOpenKey(ev *tcell.EventKey) bool {
	if b.submenuOpen() {
		return b.handleSubmenuKey(ev)
	}

	switch ev.Key() {
	case tcell.KeyEscape:
		b.Close()
	case tcell.KeyLeft:
		b.OpenMenu((b.openMenu - 1 + len(b.menus)) % len(b.menus))
	case tcell.KeyRight:
		b.stepRight()
	case tcell.KeyUp:
		b.highlight = b.nextUsableIn(b.menus[b.openMenu].Items, b.highlight, -1)
	case tcell.KeyDown:
		b.highlight = b.nextUsableIn(b.menus[b.openMenu].Items, b.highlight, +1)
	case tcell.KeyEnter:
		b.activate(b.highlight)
	case tcell.KeyRune:
		return b.selectByHotKey(ev.Rune())
	default:
		return false
	}
	return true
}

// selectByHotKey runs the drop-down item whose hot key was typed.
func (b *MenuBar) selectByHotKey(key rune) bool {
	for i, item := range b.menus[b.openMenu].Items {
		if item.enabled() && MatchesHotKey(item.Label, key) {
			b.activate(i)
			return true
		}
	}
	return true // an open menu swallows stray typing rather than letting it through
}

// activate opens item i's submenu, or closes the menu and runs its action.
func (b *MenuBar) activate(i int) {
	items := b.menus[b.openMenu].Items
	if i < 0 || i >= len(items) || !items[i].enabled() {
		return
	}
	if items[i].hasSubmenu() {
		b.openSubmenu(i)
		return
	}

	action := items[i].Action
	b.Close()
	action()
}

// nextUsableIn returns the index of the next item of a list that can be
// chosen, walking in the given direction and wrapping round. It returns from
// where it started if the list holds nothing usable at all.
func (b *MenuBar) nextUsableIn(items []*MenuItem, from, step int) int {
	if len(items) == 0 {
		return 0
	}

	index := from
	for range len(items) {
		index = (index + step + len(items)) % len(items)
		if items[index].enabled() {
			return index
		}
	}
	return max(from, 0)
}

// HandleMouse opens, walks and runs menus with the pointer.
func (b *MenuBar) HandleMouse(ev *tcell.EventMouse) bool {
	x, y := ev.Position()
	pressed := ev.Buttons() == tcell.Button1

	if b.Bounds().Contains(x, y) {
		return b.handleBarMouse(x, pressed)
	}
	if !b.Open() {
		return false
	}
	if b.submenuOpen() && b.submenuBounds().Contains(x, y) {
		return b.handleSubmenuMouse(x, y, pressed)
	}
	return b.handleDropdownMouse(x, y, pressed)
}

// handleBarMouse reacts to the pointer on the row of titles.
func (b *MenuBar) handleBarMouse(x int, pressed bool) bool {
	for i, menu := range b.menus {
		start := b.menuX(i)
		if x < start-1 || x > start+LabelWidth(menu.Label) {
			continue
		}
		if pressed {
			b.toggle(i)
		}
		return true
	}
	if pressed {
		b.Close()
	}
	return true
}

// toggle opens menu i, or closes it when it is already the open one.
func (b *MenuBar) toggle(i int) {
	if b.openMenu == i {
		b.Close()
		return
	}
	b.OpenMenu(i)
}

// handleDropdownMouse reacts to the pointer over an open drop-down.
func (b *MenuBar) handleDropdownMouse(x, y int, pressed bool) bool {
	bounds := b.dropdownBounds()
	if !bounds.Contains(x, y) {
		if pressed {
			b.Close() // a click anywhere else puts the menu away
		}
		return pressed
	}

	index := y - bounds.Y - 1
	items := b.menus[b.openMenu].Items
	if index < 0 || index >= len(items) || !items[index].enabled() {
		return true
	}

	b.highlight = index
	// Moving onto a branch opens it and moving off it closes it, so the pointer
	// never leaves a submenu showing for an item it has left.
	switch {
	case items[index].hasSubmenu():
		b.openSubmenu(index)
	case b.submenuOpen():
		b.closeSubmenu()
	}

	if pressed {
		b.activate(index)
	}
	return true
}