forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
widget_selectplaylist.go
249 lines (237 loc) · 6.2 KB
/
widget_selectplaylist.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type PlaylistSelectionWidget struct {
Root *tview.Flex
ui *Ui
inputField *tview.InputField
overwrite *tview.Checkbox
accept *tview.Button
cancel *tview.Button
overwriteEnabled bool
visible bool
}
// createPlaylistSelectionWidget creates the widget and sets up all of the
// behaviors, including the key bindings.
func (ui *Ui) createPlaylistSelectionWidget() (m *PlaylistSelectionWidget) {
m = &PlaylistSelectionWidget{
ui: ui,
}
m.overwrite = tview.NewCheckbox()
m.overwrite.SetDisabled(true)
m.overwriteEnabled = false
m.overwrite.SetLabel("Overwrite?").SetFieldTextColor(tcell.ColorBlack)
m.overwrite.SetBackgroundColor(tcell.ColorGray)
m.overwrite.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == ' ' {
m.overwrite.SetChecked(!m.overwrite.IsChecked())
m.accept.SetDisabled(!m.overwrite.IsChecked())
return nil
}
return event
})
m.accept = tview.NewButton("Accept").SetLabelColor(tcell.ColorBlack)
m.cancel = tview.NewButton("Cancel").SetLabelColor(tcell.ColorBlack)
m.inputField = tview.NewInputField().SetAutocompleteFunc(func(current string) []string {
rv := make([]string, 0)
var exactMatch bool
for _, p := range ui.playlists {
if strings.Contains(p.Name, current) {
rv = append(rv, p.Name)
}
if p.Name == current {
exactMatch = true
}
}
if exactMatch {
m.overwrite.SetDisabled(false)
m.overwriteEnabled = true
m.accept.SetDisabled(!m.overwrite.IsChecked())
} else {
m.overwrite.SetDisabled(true)
m.overwriteEnabled = false
m.accept.SetDisabled(false)
}
return rv
}).SetFieldTextColor(tcell.ColorBlack)
m.inputField.SetDoneFunc(func(key tcell.Key) {
m.focusNext(nil)
})
// FIXME with this code in place, the list isn't navigable
// m.inputField.SetAutocompletedFunc(func(text string, index int, source int) bool {
// m.inputField.SetText(text)
// for _, p := range ui.playlists {
// if p.Name == text {
// m.overwrite.SetDisabled(false)
// m.overwriteEnabled = true
// m.focusNext(nil)
// return false
// }
// }
// m.overwrite.SetDisabled(true)
// m.overwriteEnabled = false
// return false
// })
acceptFunc := func() {
inputText := m.inputField.GetText()
if !m.overwrite.IsChecked() {
for _, p := range ui.playlists {
if p.Name == inputText {
return
}
}
}
ui.queuePage.saveQueue(inputText)
ui.CloseSelectPlaylist()
}
m.accept.SetSelectedFunc(acceptFunc)
cancelFunc := func() {
m.inputField.SetText("")
m.overwrite.SetDisabled(true)
m.overwriteEnabled = false
m.overwrite.SetChecked(false)
m.accept.SetDisabled(!m.overwrite.IsChecked())
ui.CloseSelectPlaylist()
}
m.cancel.SetSelectedFunc(cancelFunc)
buttons := tview.NewFlex().
SetDirection(tview.FlexColumn).
AddItem(tview.NewFlex(), 0, 1, false).
AddItem(m.accept, 0, 4, false).
AddItem(tview.NewFlex(), 0, 1, false).
AddItem(m.cancel, 0, 4, false).
AddItem(tview.NewFlex(), 0, 1, false)
m.Root = tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(m.inputField, 1, 1, true).
AddItem(m.overwrite, 1, 1, false).
AddItem(buttons, 0, 1, false)
m.Root.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if m.ui.app.GetFocus() == m.inputField {
switch event.Key() {
case tcell.KeyTab:
if event.Modifiers()&tcell.ModShift != 0 {
return m.focusPrev(event)
} else {
return m.focusNext(event)
}
case tcell.KeyBacktab:
return m.focusPrev(event)
case tcell.KeyESC:
cancelFunc()
}
return event
}
if event.Rune() == ' ' {
focused := m.ui.app.GetFocus()
if focused == m.accept {
acceptFunc()
return nil
}
if focused == m.cancel {
cancelFunc()
return nil
}
return event
}
switch event.Key() {
case tcell.KeyESC:
cancelFunc()
return nil
case tcell.KeyCR:
focused := m.ui.app.GetFocus()
if focused == m.accept {
acceptFunc()
return nil
} else if focused == m.cancel {
cancelFunc()
return nil
}
m.focusNext(event)
return event
case tcell.KeyTab:
if event.Modifiers()&tcell.ModShift != 0 {
return m.focusPrev(event)
} else {
return m.focusNext(event)
}
case tcell.KeyBacktab:
return m.focusPrev(event)
case tcell.KeyDown:
return m.focusNext(event)
case tcell.KeyUp:
return m.focusPrev(event)
default:
m.ui.logger.Printf("non-input key = %d", event.Key())
}
return event
})
m.Root.Box.SetBorder(true).SetTitle(" Playlist Name ")
return
}
func (m *PlaylistSelectionWidget) focusNext(event *tcell.EventKey) *tcell.EventKey {
switch m.ui.app.GetFocus() {
case m.inputField:
st := m.inputField.GetText()
found := false
for _, p := range m.ui.playlists {
if p.Name == st {
m.overwrite.SetDisabled(false)
m.overwriteEnabled = true
m.accept.SetDisabled(!m.overwrite.IsChecked())
m.ui.app.SetFocus(m.overwrite)
found = true
}
}
if !found {
m.overwrite.SetDisabled(true)
m.overwriteEnabled = false
m.accept.SetDisabled(false)
m.ui.app.SetFocus(m.accept)
}
case m.overwrite:
if m.overwrite.IsChecked() {
m.ui.app.SetFocus(m.accept)
} else {
m.ui.app.SetFocus(m.cancel)
}
case m.accept:
m.ui.app.SetFocus(m.cancel)
case m.cancel:
m.ui.app.SetFocus(m.inputField)
default:
return event
}
return nil
}
func (m PlaylistSelectionWidget) focusPrev(event *tcell.EventKey) *tcell.EventKey {
switch m.ui.app.GetFocus() {
case m.inputField:
m.ui.app.SetFocus(m.cancel)
case m.overwrite:
m.ui.app.SetFocus(m.inputField)
case m.accept:
if m.overwriteEnabled {
m.ui.app.SetFocus(m.overwrite)
} else {
m.ui.app.SetFocus(m.inputField)
}
case m.cancel:
// FIXME There's some bug in back-tabbing from cancel; _something_ is disabling the overwriteEnabled field, and I can't find it. Tabbing forward works fine, but tabbing backward fails to work properly when the playlist name matches an existing playlist
if m.overwriteEnabled {
if m.overwrite.IsChecked() {
m.ui.app.SetFocus(m.accept)
} else {
m.ui.app.SetFocus(m.overwrite)
}
} else {
m.ui.app.SetFocus(m.accept)
}
default:
return event
}
return nil
}