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.

📦 Turbo Core f3ade8d · on v1.0.1 · k33g · 12h ago
submenu.go · 163 lines · 4.7 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
package ui

// Submenus: the second drop-down that an item with Items opens beside the
// first. One level deep is all there is — see MenuItem.Items for why.

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

	"rickub.com/turbo-editors/turbo-core/theme"
)

// submenuMarker is drawn at the right of an item that opens a submenu. It is
// one column wide and three bytes long, so its width is counted in runes.
const submenuMarker = "▶"

// submenuOpen reports whether a second drop-down is showing.
func (b *MenuBar) submenuOpen() bool { return b.openItem >= 0 }

// openSubmenu shows the submenu of drop-down item i.
func (b *MenuBar) openSubmenu(i int) {
	items := b.menus[b.openMenu].Items
	if i < 0 || i >= len(items) || !items[i].hasSubmenu() {
		return
	}

	b.highlight = i
	b.openItem = i
	b.subHighlight = b.nextUsableIn(items[i].Items, -1, +1)
}

// closeSubmenu puts the second drop-down away, leaving the first showing.
func (b *MenuBar) closeSubmenu() {
	b.openItem = -1
	b.subHighlight = 0
}

// submenuItems returns the items of the open submenu, or nil when none is.
func (b *MenuBar) submenuItems() []*MenuItem {
	if !b.submenuOpen() {
		return nil
	}
	return b.menus[b.openMenu].Items[b.openItem].Items
}

// submenuBounds returns where the open submenu goes: to the right of the
// drop-down, level with the item that opened it.
//
// It flips to the left of the drop-down when there is not room on the right,
// because a menu drawn past the edge of the screen is a menu nobody can read.
func (b *MenuBar) submenuBounds() Rect {
	parent := b.dropdownBounds()
	items := b.menus[b.openMenu].Items[b.openItem].Items

	width := 0
	for _, item := range items {
		width = max(width, item.width())
	}
	// Capped to the screen as well as flipped, because a panel wider than the
	// terminal cannot be made to fit by moving it. Long labels are then clipped
	// by the painter, which is better than a frame with no right-hand edge.
	width = min(width+4, b.Bounds().W)

	x := parent.Right()
	if x+width+ShadowWidth > b.Bounds().Right() {
		x = parent.X - width
	}
	rightmost := max(b.Bounds().Right()-width, b.Bounds().X)
	x = min(max(x, b.Bounds().X), rightmost)

	return Rect{X: x, Y: parent.Y + b.openItem, W: width, H: len(items) + 2}
}

// drawSubmenu paints the second drop-down, on top of the first.
func (b *MenuBar) drawSubmenu(p *Painter, th *theme.Theme) {
	bounds := b.submenuBounds()
	DrawShadow(p, bounds, th.Style(theme.KeyShadow))

	panel := p.Sub(bounds)
	panel.Clear(th.Style(theme.KeyMenuItem))
	DrawFrame(panel, panel.Size(), FrameSingle, th.Style(theme.KeyMenuItem))

	for i, item := range b.submenuItems() {
		b.drawLine(panel, th, i, item, i == b.subHighlight)
	}
}

// stepRight opens the highlighted item's submenu, or moves to the next menu
// when it has none.
//
// That order is what makes right mean "further in" wherever you are, rather
// than sometimes skipping past a branch you were about to open.
func (b *MenuBar) stepRight() {
	items := b.menus[b.openMenu].Items
	if b.highlight >= 0 && b.highlight < len(items) && items[b.highlight].hasSubmenu() {
		b.openSubmenu(b.highlight)
		return
	}
	b.OpenMenu((b.openMenu + 1) % len(b.menus))
}

// handleSubmenuKey works the second drop-down.
//
// Left and Escape differ here: left steps back out to the parent, while Escape
// puts the whole menu away — which is what "cancel" should do from anywhere.
func (b *MenuBar) handleSubmenuKey(ev *tcell.EventKey) bool {
	items := b.submenuItems()

	switch ev.Key() {
	case tcell.KeyEscape:
		b.Close()
	case tcell.KeyLeft:
		b.closeSubmenu()
	case tcell.KeyUp:
		b.subHighlight = b.nextUsableIn(items, b.subHighlight, -1)
	case tcell.KeyDown:
		b.subHighlight = b.nextUsableIn(items, b.subHighlight, +1)
	case tcell.KeyRight, tcell.KeyEnter:
		b.activateSubmenu(b.subHighlight)
	case tcell.KeyRune:
		return b.selectSubmenuByHotKey(ev.Rune())
	default:
		return false
	}
	return true
}

// selectSubmenuByHotKey runs the submenu item whose hot key was typed.
func (b *MenuBar) selectSubmenuByHotKey(key rune) bool {
	for i, item := range b.submenuItems() {
		if item.enabled() && MatchesHotKey(item.Label, key) {
			b.activateSubmenu(i)
			return true
		}
	}
	return true
}

// activateSubmenu closes the whole menu and runs a submenu item's action.
func (b *MenuBar) activateSubmenu(i int) {
	items := b.submenuItems()
	if i < 0 || i >= len(items) || !items[i].enabled() {
		return
	}

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

// handleSubmenuMouse reacts to the pointer over the second drop-down.
func (b *MenuBar) handleSubmenuMouse(x, y int, pressed bool) bool {
	index := y - b.submenuBounds().Y - 1
	items := b.submenuItems()
	if index < 0 || index >= len(items) || !items[index].enabled() {
		return true
	}

	b.subHighlight = index
	if pressed {
		b.activateSubmenu(index)
	}
	return true
}