-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_field.go
270 lines (235 loc) · 6.71 KB
/
form_field.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Package ui consists of console ui components
Copyright 2021 Michael Bungenstock
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package ui
import (
"github.com/fuxs/aepctl/util"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type focusedInputField struct {
*tview.InputField
Focused func()
Parent *FocusFlex
}
func newFocusedInputField() *focusedInputField {
return &focusedInputField{
InputField: tview.NewInputField(),
}
}
func (f *focusedInputField) SetFocusedFunc(ff *FocusFlex, focused func()) *focusedInputField {
f.Focused = focused
f.Parent = ff
return f
}
func (f *focusedInputField) Focus(delegate func(p tview.Primitive)) {
f.InputField.Focus(delegate)
f.Parent.Last = f
if f.Focused != nil {
f.Focused()
}
}
type focusedButton struct {
*tview.Button
Focused func()
Parent *FocusFlex
}
func newFocusedButton(label string) *focusedButton {
return &focusedButton{
Button: tview.NewButton(label),
}
}
func (f *focusedButton) SetFocusedFunc(ff *FocusFlex, focused func()) *focusedButton {
f.Focused = focused
f.Parent = ff
return f
}
func (f *focusedButton) Focus(delegate func(p tview.Primitive)) {
f.Button.Focus(delegate)
f.Parent.Last = f
if f.Focused != nil {
f.Focused()
}
}
// FormField consists of an input fiels and a button.
type FormField struct {
*tview.Box
Input *focusedInputField
Button *focusedButton
ButtonFirst bool
next tview.Primitive
prev tview.Primitive
}
// SetNext sets the next tview.Primitive in the navigation order
func (f *FormField) SetNext(next Tabable) {
f.next = next.Primitive()
}
// SetPrev sets the previous tview.Primitive in the navigation order
func (f *FormField) SetPrev(prev Tabable) {
f.prev = prev.Primitive()
}
// Primitive returns the this object with the tview.Primitive interface
func (f *FormField) Primitive() tview.Primitive {
return f
}
// SetFocusedFunc sets the Focused functions for the InpurtForm and the Button
func (f *FormField) SetFocusedFunc(ff *FocusFlex, focused ...func()) *FormField {
l := len(focused)
if l == 1 {
f.Input.SetFocusedFunc(ff, focused[0])
f.Button.SetFocusedFunc(ff, focused[0])
} else if l > 1 {
f.Input.SetFocusedFunc(ff, focused[0])
f.Button.SetFocusedFunc(ff, focused[1])
}
return f
}
// Focus sets the focus to the InputForm or Button
func (f *FormField) Focus(delegate func(p tview.Primitive)) {
if f.ButtonFirst {
delegate(f.Button)
} else {
delegate(f.Input)
}
}
// Blur forwards the call to the Inputform and Button
func (f *FormField) Blur() {
f.Input.Blur()
f.Button.Blur()
}
// After connects this button with the previous Tabable
func (f *FormField) After(previous Tabable) *FormField {
ConnectTabable(previous, f)
return f
}
// HasFocus returns true if this primitve has the focus
func (f *FormField) HasFocus() bool {
return f.Button.HasFocus() || f.Input.HasFocus()
}
// SetText sets the text
func (f *FormField) SetText(text string) *FormField {
f.Input.SetText(text)
return f
}
// GetText returns the text of the InputField
func (f *FormField) GetText() string {
return f.Input.GetText()
}
// Draw draws this primitive
func (f *FormField) Draw(screen tcell.Screen) {
f.Box.DrawForSubclass(screen, f)
x, y, width, height := f.GetInnerRect()
l := len(f.Button.GetLabel()) + 4
f.Input.SetRect(x, y, width-(l+1), height)
f.Input.Draw(screen)
f.Button.SetRect(x+width-l, y, l, 1)
f.Button.Draw(screen)
}
// InputHandler handles the keyboard input
func (f *FormField) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return f.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
key := event.Key()
switch key {
case tcell.KeyUp, tcell.KeyBacktab:
setFocus(f.prev)
return
case tcell.KeyDown, tcell.KeyTab:
setFocus(f.next)
return
default:
}
if f.Button.HasFocus() {
switch key {
case tcell.KeyRight:
setFocus(f.next)
case tcell.KeyLeft:
setFocus(f.Input)
case tcell.KeyEnter:
f.Button.InputHandler()(event, setFocus)
}
} else {
if key == tcell.KeyEnter {
setFocus(f.next)
}
f.Input.InputHandler()(event, setFocus)
}
})
}
// MouseHandler handles mouse input
func (f *FormField) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return f.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
if !f.InRect(event.Position()) {
return false, nil
}
consumed, capture = f.Input.MouseHandler()(action, event, setFocus)
if consumed {
setFocus(f.Input)
return
}
if action == tview.MouseLeftClick {
setFocus(f.Button)
}
consumed, capture = f.Button.MouseHandler()(action, event, setFocus)
return
})
}
/*
// GetFieldWidth returns the width
func (f *FormField) GetFieldWidth() int {
return 0
}
// GetLabel returns the label
func (f *FormField) GetLabel() string {
return f.Input.GetLabel()
}
// SetFormAttributes set form specific attributes
func (f *FormField) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) tview.FormItem {
f.Input.SetFormAttributes(labelWidth, labelColor, bgColor, fieldTextColor, fieldBgColor)
f.Button.SetBackgroundColor(fieldBgColor)
f.Button.SetLabelColor(fieldTextColor)
return f
}
func (f *FormField) SetFinishedFunc(handler func(key tcell.Key)) tview.FormItem {
f.Input.SetFinishedFunc(handler)
return f
}*/
// NewPasteField returns a textfield with a past button
func NewPasteField(label string) *FormField {
field := newFocusedInputField()
field.SetLabel(label)
button := newFocusedButton("Paste")
button.SetSelectedFunc(func() {
field.SetText(util.Paste())
})
result := &FormField{
Box: tview.NewBox(),
Input: field,
Button: button,
}
return result
}
// NewFileField returns a textfield with a file
func NewFileField(label string, pages *FocusPages) *FormField {
field := newFocusedInputField()
field.SetLabel(label)
button := newFocusedButton("Select File")
button.SetSelectedFunc(func() {
OpenFileDialog(pages, func(s string) { field.SetText(s) })
})
result := &FormField{
Box: tview.NewBox(),
ButtonFirst: true,
Input: field,
Button: button,
}
return result
}