File: menu.go

package info (click to toggle)
termshark 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,800 kB
  • sloc: sh: 98; makefile: 9
file content (214 lines) | stat: -rw-r--r-- 5,422 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2019-2021 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package ui contains user-interface functions and helpers for termshark.
package ui

import (
	"fmt"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/button"
	"github.com/gcla/gowid/widgets/cellmod"
	"github.com/gcla/gowid/widgets/columns"
	"github.com/gcla/gowid/widgets/divider"
	"github.com/gcla/gowid/widgets/framed"
	"github.com/gcla/gowid/widgets/hpadding"
	"github.com/gcla/gowid/widgets/keypress"
	"github.com/gcla/gowid/widgets/menu"
	"github.com/gcla/gowid/widgets/pile"
	"github.com/gcla/gowid/widgets/selectable"
	"github.com/gcla/gowid/widgets/styled"
	"github.com/gcla/gowid/widgets/text"
	"github.com/gcla/termshark/v2/widgets/appkeys"
	"github.com/gdamore/tcell"
)

//======================================================================

type SimpleMenuItem struct {
	Txt string
	Key gowid.IKey
	CB  gowid.WidgetChangedFunction
}

func MakeMenuDivider() SimpleMenuItem {
	return SimpleMenuItem{}
}

func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {
	menu1Widgets := make([]gowid.IWidget, len(items))
	menu1HotKeys := make([]gowid.IWidget, len(items))

	// Figure out the length of the longest hotkey string representation
	max := 0
	for _, w := range items {
		if w.Txt != "" {
			k := fmt.Sprintf("%v", w.Key)
			if len(k) > max {
				max = len(k)
			}
		}
	}

	// Construct the hotkey widget and menu item widget for each menu entry
	for i, w := range items {
		if w.Txt != "" {
			load1B := button.NewBare(text.New(w.Txt))
			var ks string
			if w.Key != nil {
				ks = fmt.Sprintf("%v", w.Key)
			}
			load1K := button.NewBare(text.New(ks))
			load1CB := gowid.MakeWidgetCallback("cb", w.CB)
			load1B.OnClick(load1CB)
			if w.Key != nil {
				load1K.OnClick(load1CB)
			}
			menu1Widgets[i] = load1B
			menu1HotKeys[i] = load1K
		}
	}
	for i, w := range menu1Widgets {
		if w != nil {
			menu1Widgets[i] = styled.NewInvertedFocus(selectable.New(w), gowid.MakePaletteRef("default"))
		}
	}
	for i, w := range menu1HotKeys {
		if w != nil {
			menu1HotKeys[i] = styled.NewInvertedFocus(w, gowid.MakePaletteRef("default"))
		}
	}

	// Build the menu "row" for each menu entry
	menu1Widgets2 := make([]gowid.IWidget, len(menu1Widgets))
	for i, w := range menu1Widgets {
		if w == nil {
			menu1Widgets2[i] = divider.NewUnicode()
		} else {
			menu1Widgets2[i] = columns.New(
				[]gowid.IContainerWidget{
					&gowid.ContainerWidget{
						IWidget: hpadding.New(
							// size is translated from flowwith{20} to fixed; fixed gives size 6, flowwith aligns right to 12
							hpadding.New(
								selectable.NewUnselectable( // don't want to be able to navigate to the hotkey itself
									menu1HotKeys[i],
								),
								gowid.HAlignRight{},
								fixed,
							),
							gowid.HAlignLeft{},
							gowid.RenderFlowWith{C: max},
						),
						D: fixed,
					},
					&gowid.ContainerWidget{
						IWidget: text.New("| "),
						D:       fixed,
					},
					&gowid.ContainerWidget{
						IWidget: w,
						D:       fixed,
					},
				},
				columns.Options{
					StartColumn: 2,
				},
			)
		}
	}

	menu1cwidgets := make([]gowid.IContainerWidget, len(menu1Widgets2))
	for i, w := range menu1Widgets2 {
		var dim gowid.IWidgetDimension
		if menu1Widgets[i] != nil {
			dim = fixed
		} else {
			dim = gowid.RenderFlow{}
		}
		menu1cwidgets[i] = &gowid.ContainerWidget{
			IWidget: w,
			D:       dim,
		}
	}

	keys := make([]gowid.IKey, 0)
	for _, i := range items {
		if i.Key != nil {
			keys = append(keys, i.Key)
		}
	}

	// Surround the menu with a widget that captures the hotkey keypresses
	menuListBox1 := keypress.New(
		cellmod.Opaque(
			styled.New(
				framed.NewUnicode(
					pile.New(menu1cwidgets, pile.Options{
						Wrap: true,
					}),
				),
				gowid.MakePaletteRef("default"),
			),
		),
		keypress.Options{
			Keys: keys,
		},
	)

	menuListBox1.OnKeyPress(keypress.MakeCallback("key1", func(app gowid.IApp, w gowid.IWidget, k gowid.IKey) {
		for _, r := range items {
			if r.Key != nil && gowid.KeysEqual(k, r.Key) {
				r.CB(app, w)
				break
			}
		}
	}))

	return menuListBox1
}

//======================================================================

type NextMenu struct {
	Cur       *menu.Widget
	Next      *menu.Widget // nil if menu is nil
	Site      *menu.SiteWidget
	Container gowid.IFocus // container holding menu buttons, etc
	Focus     int          // index of next menu in container
}

func MakeMenuNavigatingKeyPress(left *NextMenu, right *NextMenu) appkeys.KeyInputFn {
	return func(evk *tcell.EventKey, app gowid.IApp) bool {
		return MenuNavigatingKeyPress(evk, left, right, app)
	}
}

func MenuNavigatingKeyPress(evk *tcell.EventKey, left *NextMenu, right *NextMenu, app gowid.IApp) bool {
	res := false
	switch evk.Key() {
	case tcell.KeyLeft:
		if left != nil {
			left.Cur.Close(app)
			left.Next.Open(left.Site, app)
			left.Container.SetFocus(app, left.Focus) // highlight next menu selector
			res = true
		}
	case tcell.KeyRight:
		if right != nil {
			right.Cur.Close(app)
			right.Next.Open(right.Site, app)
			right.Container.SetFocus(app, right.Focus) // highlight next menu selector
			res = true
		}
	}
	return res
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: