-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.go
136 lines (105 loc) · 3.04 KB
/
buttons.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
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
package ui
import (
"fmt"
"github.com/darkhz/tview"
"github.com/gdamore/tcell/v2"
)
// Button stores a button's label and the function to be
// executed when it is selected.
type Button struct {
label string
selectedFunc func()
}
// ButtonDisplay stores the layout for the currently configured buttons.
type ButtonDisplay struct {
Layout *tview.TextView
currentButtons []Button
lastFocus tview.Primitive
arrangeFunc func(label string) bool
}
var buttonDisplay ButtonDisplay
// ButtonView returns a display area to show buttons.
func ButtonView() *tview.TextView {
if buttonDisplay.Layout != nil {
goto ShowButtonLayout
}
buttonDisplay.Layout = tview.NewTextView()
buttonDisplay.Layout.SetRegions(true)
buttonDisplay.Layout.SetDynamicColors(true)
buttonDisplay.Layout.SetTextAlign(tview.AlignRight)
buttonDisplay.Layout.SetBackgroundColor(tcell.ColorDefault)
buttonDisplay.Layout.SetBlurFunc(func() {
buttonDisplay.Layout.Highlight("")
})
buttonDisplay.Layout.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft, tcell.KeyRight:
if event.Modifiers() == tcell.ModCtrl {
switchButtons(event.Key() == tcell.KeyLeft)
goto Event
}
case tcell.KeyEnter:
var selected string
highlights := buttonDisplay.Layout.GetHighlights()
if highlights != nil {
selected = highlights[0]
}
for _, button := range buttonDisplay.currentButtons {
if button.label == selected {
button.selectedFunc()
buttonDisplay.Layout.Highlight("")
goto Event
}
}
}
if buttonDisplay.lastFocus != nil {
App.SetFocus(buttonDisplay.lastFocus)
buttonDisplay.Layout.Highlight("")
}
Event:
return event
})
ShowButtonLayout:
return buttonDisplay.Layout
}
// UpdateButtonView updates the button display area. If addCondition is provided,
// each button label is checked against addCondition before displaying it.
func UpdateButtonView(buttons []Button, addCondition ...func(label string) bool) {
var buttonText string
if buttons == nil {
return
}
buttonDisplay.currentButtons = buttons
ButtonView().Clear()
for _, button := range buttons {
if addCondition != nil {
buttonDisplay.arrangeFunc = addCondition[0]
}
if buttonDisplay.arrangeFunc != nil && !buttonDisplay.arrangeFunc(button.label) {
continue
}
buttonText += fmt.Sprintf("[\"%s\"][blue::b][%s[][-:-:-][\"\"] ", button.label, button.label)
}
ButtonView().SetText(buttonText)
}
// switchButtons cycles between the button selection.
func switchButtons(reverse bool) {
SwitchTabView(reverse, buttonDisplay.Layout)
}
// focusButtonView brings the button display area into focus.
func focusButtonView() {
buttonDisplay.lastFocus = App.GetFocus()
App.SetFocus(ButtonView())
switchButtons(false)
}
// buttonEventHandler handles key events for the button display area.
func buttonEventHandler(event *tcell.EventKey) bool {
if event.Modifiers() != tcell.ModCtrl {
return false
}
switch event.Key() {
case tcell.KeyLeft, tcell.KeyRight:
focusButtonView()
}
return true
}