forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.go
323 lines (284 loc) · 8.83 KB
/
dialog.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package clui
import (
term "github.com/nsf/termbox-go"
)
// ConfirmationDialog is a simple dialog to get a user
// choice or confirmation. The dialog can contain upto
// three button with custom titles. There are a few
// predefined button sets: see Buttons* constants.
// The dialog is modal, so a user cannot interact other
// Views until the user closes the dialog
type ConfirmationDialog struct {
View *Window
result int
onClose func()
}
// SelectDialog allows to user to select an item from
// the list. Items can be displayed in ListBox or in
// RadioGroup.
// The dialog is modal, so a user cannot interact other
// Views until the user closes the dialog
type SelectDialog struct {
View *Window
result int
value int
edtResult string
rg *RadioGroup
list *ListBox
edit *EditField
typ SelectDialogType
onClose func()
}
// CreateAlertDialog creates a new alert dialog.
// title is a dialog title
// message is a text inside dialog for user to be notified of a fact
// button is a title for button inside dialog.
func CreateAlertDialog(title, message string, button string) *ConfirmationDialog {
return CreateConfirmationDialog(title, message, []string{button}, 0)
}
// CreateConfirmationDialog creates new confirmation dialog.
// c is a composer that manages the dialog
// title is a dialog title
// question is a text inside dialog for user to explain what happens
// buttons is a titles for button inside dialog. If the list is empty,
// the dialog will have only one button 'OK'. If the list has more
// than 3 button then only first three items will be used in the dialog
// defaultButton is the number of button that is active right after
// dialog is created. If the number is greater than the number of
// buttons, no button is active
func CreateConfirmationDialog(title, question string, buttons []string, defaultButton int) *ConfirmationDialog {
dlg := new(ConfirmationDialog)
if len(buttons) == 0 {
buttons = []string{"OK"}
}
cw, ch := term.Size()
dlg.View = AddWindow(cw/2-12, ch/2-8, 30, 3, title)
WindowManager().BeginUpdate()
defer WindowManager().EndUpdate()
dlg.View.SetConstraints(30, 3)
dlg.View.SetModal(true)
dlg.View.SetPack(Vertical)
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
fbtn := CreateFrame(dlg.View, 1, 1, BorderNone, 1)
CreateFrame(fbtn, 1, 1, BorderNone, Fixed)
lb := CreateLabel(fbtn, 10, 3, question, 1)
lb.SetMultiline(true)
CreateFrame(fbtn, 1, 1, BorderNone, Fixed)
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
frm1 := CreateFrame(dlg.View, 16, 4, BorderNone, Fixed)
CreateFrame(frm1, 1, 1, BorderNone, 1)
bText := buttons[0]
btn1 := CreateButton(frm1, AutoSize, AutoSize, bText, Fixed)
btn1.OnClick(func(ev Event) {
dlg.result = DialogButton1
WindowManager().DestroyWindow(dlg.View)
WindowManager().BeginUpdate()
closeFunc := dlg.onClose
WindowManager().EndUpdate()
if closeFunc != nil {
closeFunc()
}
})
var btn2, btn3 *Button
if len(buttons) > 1 {
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn2 = CreateButton(frm1, AutoSize, AutoSize, buttons[1], Fixed)
btn2.OnClick(func(ev Event) {
dlg.result = DialogButton2
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
})
}
if len(buttons) > 2 {
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn3 = CreateButton(frm1, AutoSize, AutoSize, buttons[2], Fixed)
btn3.OnClick(func(ev Event) {
dlg.result = DialogButton3
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
})
}
CreateFrame(frm1, 1, 1, BorderNone, 1)
if defaultButton == DialogButton2 && len(buttons) > 1 {
ActivateControl(dlg.View, btn2)
} else if defaultButton == DialogButton3 && len(buttons) > 2 {
ActivateControl(dlg.View, btn3)
} else {
ActivateControl(dlg.View, btn1)
}
dlg.View.OnClose(func(ev Event) bool {
if dlg.result == DialogAlive {
dlg.result = DialogClosed
if ev.X != 1 {
WindowManager().DestroyWindow(dlg.View)
}
if dlg.onClose != nil {
dlg.onClose()
}
}
return true
})
return dlg
}
// OnClose sets the callback that is called when the
// dialog is closed
func (d *ConfirmationDialog) OnClose(fn func()) {
WindowManager().BeginUpdate()
defer WindowManager().EndUpdate()
d.onClose = fn
}
// Result returns what button closed the dialog.
// See DialogButton constants. It can equal DialogAlive
// that means that the dialog is still visible and a
// user still does not click any button
func (d *ConfirmationDialog) Result() int {
return d.result
}
// ------------------------ Selection Dialog ---------------------
func CreateEditDialog(title, message, initialText string) *SelectDialog {
return CreateSelectDialog(title, []string{message, initialText}, 0, SelectDialogEdit)
}
// NewSelectDialog creates new dialog to select an item from list.
// c is a composer that manages the dialog
// title is a dialog title
// items is a list of items to select from
// selectedItem is the index of the item that is selected after
// the dialog is created
// typ is a selection type: ListBox or RadioGroup
// Returns nil in case of creation process fails, e.g, if item list is empty
func CreateSelectDialog(title string, items []string, selectedItem int, typ SelectDialogType) *SelectDialog {
dlg := new(SelectDialog)
if len(items) == 0 {
// Item list must contain at least 1 item
return nil
}
cw, ch := term.Size()
dlg.typ = typ
dlg.View = AddWindow(cw/2-12, ch/2-8, 20, 10, title)
WindowManager().BeginUpdate()
defer WindowManager().EndUpdate()
dlg.View.SetModal(true)
dlg.View.SetPack(Vertical)
if typ == SelectDialogList {
fList := CreateFrame(dlg.View, 1, 1, BorderNone, 1)
fList.SetPaddings(1, 1)
fList.SetGaps(0, 0)
dlg.list = CreateListBox(fList, 15, 5, 1)
for _, item := range items {
dlg.list.AddItem(item)
}
if selectedItem >= 0 && selectedItem < len(items) {
dlg.list.SelectItem(selectedItem)
}
} else if typ == SelectDialogEdit {
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
lb := CreateLabel(dlg.View, 10, 3, items[0], 1)
lb.SetMultiline(true)
fWidth, _ := dlg.View.Size()
dlg.edit = CreateEditField(dlg.View, fWidth-2, items[1], AutoSize)
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
dlg.edit.OnKeyPress(func(key term.Key, r rune) bool {
var input string
if key == term.KeyEnter {
input = dlg.edit.Title()
dlg.edtResult = input
dlg.value = -1
dlg.result = DialogButton1
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
}
// returning false so that other keypresses work as usual
return false
})
} else {
fRadio := CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
fRadio.SetPaddings(1, 1)
fRadio.SetGaps(0, 0)
fRadio.SetPack(Vertical)
dlg.rg = CreateRadioGroup()
for _, item := range items {
r := CreateRadio(fRadio, AutoSize, item, Fixed)
dlg.rg.AddItem(r)
}
if selectedItem >= 0 && selectedItem < len(items) {
dlg.rg.SetSelected(selectedItem)
}
}
frm1 := CreateFrame(dlg.View, 16, 4, BorderNone, Fixed)
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn1 := CreateButton(frm1, AutoSize, AutoSize, "OK", Fixed)
btn1.OnClick(func(ev Event) {
dlg.result = DialogButton1
if dlg.typ == SelectDialogList {
dlg.value = dlg.list.SelectedItem()
} else if dlg.typ == SelectDialogEdit {
dlg.edtResult = dlg.edit.Title()
dlg.value = -1
} else {
dlg.value = dlg.rg.Selected()
}
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
})
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn2 := CreateButton(frm1, AutoSize, AutoSize, "Cancel", Fixed)
btn2.OnClick(func(ev Event) {
dlg.result = DialogButton2
dlg.edtResult = ""
dlg.value = -1
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
dlg.onClose()
}
})
if typ == SelectDialogEdit {
ActivateControl(dlg.View, dlg.edit)
} else {
ActivateControl(dlg.View, btn2)
}
CreateFrame(frm1, 1, 1, BorderNone, 1)
dlg.View.OnClose(func(ev Event) bool {
if dlg.result == DialogAlive {
dlg.result = DialogClosed
if ev.X != 1 {
WindowManager().DestroyWindow(dlg.View)
}
if dlg.onClose != nil {
dlg.onClose()
}
}
return true
})
return dlg
}
// OnClose sets the callback that is called when the
// dialog is closed
func (d *SelectDialog) OnClose(fn func()) {
WindowManager().BeginUpdate()
defer WindowManager().EndUpdate()
d.onClose = fn
}
// Result returns what button closed the dialog.
// See DialogButton constants. It can equal DialogAlive
// that means that the dialog is still visible and a
// user still does not click any button
func (d *SelectDialog) Result() int {
return d.result
}
// Value returns the number of the selected item or
// -1 if nothing is selected or the dialog is cancelled
func (d *SelectDialog) Value() int {
return d.value
}
// EditResult returns the text from editfield
func (d *SelectDialog) EditResult() string {
return d.edtResult
}