-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.go
687 lines (611 loc) · 16.4 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package webkitgtk
import (
"strings"
"sync"
"sync/atomic"
"unsafe"
)
const (
GSourceRemove int = 0
// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/gdkwindow.h#L121
GdkHintMinSize = 1 << 1
GdkHintMaxSize = 1 << 2
// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/gdkevents.h#L512
GdkWindowStateIconified = 1 << 1
GdkWindowStateMaximized = 1 << 2
GdkWindowStateFullscreen = 1 << 4
// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gtk/gtkmessagedialog.h#L87
GtkButtonsNone int = 0
GtkButtonsOk = 1
GtkButtonsClose = 2
GtkButtonsCancel = 3
GtkButtonsYesNo = 4
GtkButtonsOkCancel = 5
// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gtk/gtkdialog.h#L36
GtkDialogModal = 1 << 0
GtkDialogDestroyWithParent = 1 << 1
GtkDialogUseHeaderBar = 1 << 2 // actions in header bar instead of action area
GtkOrientationVertical = 1
)
var dialogMapID = make(map[uint64]struct{})
var dialogIDLock sync.RWMutex
func getDialogID() uint64 {
dialogIDLock.Lock()
defer dialogIDLock.Unlock()
dialogID := uint64(1)
for {
if _, ok := dialogMapID[dialogID]; !ok {
dialogMapID[dialogID] = struct{}{}
break
}
dialogID++
if dialogID == 0 {
panic("no more dialog IDs")
}
}
return dialogID
}
func freeDialogID(id uint64) {
dialogIDLock.Lock()
defer dialogIDLock.Unlock()
delete(dialogMapID, id)
}
type Dialog struct {
app *App
window *Window
}
func (a *App) Dialog() *Dialog {
return &Dialog{
app: a,
window: a.CurrentWindow(),
}
}
func (w *Window) Dialog() *Dialog {
return &Dialog{
app: w.app,
window: w,
}
}
func (d *Dialog) Open(title string) *OpenFileDialog {
return d.open(title)
}
func (d *Dialog) Save(title string) *SaveFileDialog {
return d.save(title)
}
func (d *Dialog) Info(title string, message string, actions ...string) *MessageDialog {
if len(actions) == 0 {
actions = []string{"OK"}
}
return d.message(0, title, message, actions)
}
func (d *Dialog) Warn(title string, message string, actions ...string) *MessageDialog {
if len(actions) == 0 {
actions = []string{"OK"}
}
return d.message(1, title, message, actions)
}
func (d *Dialog) Ask(title string, message string, actions ...string) *MessageDialog {
if len(actions) == 0 {
actions = []string{"Yes", "No"}
}
return d.message(2, title, message, actions)
}
func (d *Dialog) Fail(title string, message string, actions ...string) *MessageDialog {
if len(actions) == 0 {
actions = []string{"OK"}
}
return d.message(3, title, message, actions)
}
func (d *Dialog) open(title string) *OpenFileDialog {
return &OpenFileDialog{
app: d.app,
window: d.window,
log: newLogFunc("open-dialog"),
title: title,
canChooseDirectories: false,
canChooseFiles: true,
canCreateDirectories: true,
resolvesAliases: false,
}
}
func (d *Dialog) save(title string) *SaveFileDialog {
return &SaveFileDialog{
app: d.app,
window: d.window,
log: newLogFunc("save-dialog"),
title: title,
canCreateDirectories: true,
}
}
// enum GtkMessageType: GtkMessageInfo = 0 GtkMessageWarning = 1 GtkMessageQuestion = 2 GtkMessageError = 3
func (d *Dialog) message(dtype int, title string, message string, actions []string) *MessageDialog {
var buttons []*dialogMessageButton
for _, action := range actions {
buttons = append(buttons, &dialogMessageButton{
Action: action,
Label: action,
})
}
var firstButton *dialogMessageButton
if len(buttons) > 0 {
firstButton = buttons[0]
}
if firstButton != nil {
firstButton.IsDefault = true
}
var lastButton *dialogMessageButton
if len(buttons) > 0 {
lastButton = buttons[len(buttons)-1]
}
if lastButton != nil {
lastButton.IsCancel = true
}
return &MessageDialog{
app: d.app,
window: d.window,
log: newLogFunc("msg-dialog"),
dtype: dtype,
title: title,
message: message,
buttons: buttons,
}
}
type dialogMessageButton struct {
Action string
Label string
IsCancel bool
IsDefault bool
}
type MessageDialog struct {
app *App
log logFunc
id atomic.Uint64
result chan int
dtype int
title string
message string
buttons []*dialogMessageButton
icon []byte
window *Window
}
func (d *MessageDialog) run() {
if d.id.Load() == 0 {
id := getDialogID()
d.id.Store(id)
defer func() {
freeDialogID(id)
d.id.Store(0)
d.log("free", "id", id)
}()
d.log("open", "id", id, "title", d.title, "message", d.message)
action := d.app.thread.InvokeSyncWithResult(func() any {
return runMessageDialog(d)
})
if d.result != nil {
result, ok := action.(int)
if !ok {
d.result <- -1
} else {
d.result <- result
}
close(d.result)
}
}
}
func (d *MessageDialog) SetIcon(icon []byte) *MessageDialog {
d.icon = icon
return d
}
func (d *MessageDialog) AddDefault(action string, label string) *MessageDialog {
d.buttons = append(d.buttons, &dialogMessageButton{
Action: action,
Label: label,
IsDefault: true,
})
return d
}
func (d *MessageDialog) AddCancel(action string, label string) *MessageDialog {
d.buttons = append(d.buttons, &dialogMessageButton{
Action: action,
Label: label,
IsCancel: true,
})
return d
}
func (d *MessageDialog) AddButton(action string, label string) *MessageDialog {
d.buttons = append(d.buttons, &dialogMessageButton{
Action: action,
Label: label,
})
return d
}
func (d *MessageDialog) Show(callbacks ...func(int)) chan int {
d.result = make(chan int, 1)
d.app.started.run(d)
if len(callbacks) > 0 {
go func() {
result := <-d.result
for _, callback := range callbacks {
callback(result)
}
}()
return nil
}
return d.result
}
type dialogFileFilter struct {
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
Pattern string // semicolon separated list of extensions, EG: "*.jpg;*.png"
}
type OpenFileDialog struct {
app *App
window *Window
result chan []string
id atomic.Uint64
log logFunc
title string
message string
buttonText string
directory string
filters []dialogFileFilter
canChooseDirectories bool
canChooseFiles bool
canCreateDirectories bool
showHiddenFiles bool
resolvesAliases bool
allowsMultipleSelection bool
hideExtension bool
canSelectHiddenExtension bool
treatsFilePackagesAsDirectories bool
allowsOtherFileTypes bool
}
func (d *OpenFileDialog) run() {
if d.id.Load() == 0 {
id := getDialogID()
d.id.Store(id)
defer func() {
freeDialogID(id)
d.id.Store(0)
d.log("free", "id", id)
}()
d.log("open", "id", id, "title", d.title, "message", d.message, "directory", d.directory, "buttonText", d.buttonText, "filters", d.filters)
selections, err := d.app.thread.InvokeSyncWithResultAndError(func() (any, error) {
return runOpenFileDialog(d)
})
if d.result != nil {
result, ok := selections.([]string)
if err != nil || !ok {
d.result <- []string{}
} else {
d.result <- result
}
close(d.result)
}
}
}
func (d *OpenFileDialog) CanChooseFiles(canChooseFiles bool) *OpenFileDialog {
d.canChooseFiles = canChooseFiles
return d
}
func (d *OpenFileDialog) CanChooseDirectories(canChooseDirectories bool) *OpenFileDialog {
d.canChooseDirectories = canChooseDirectories
return d
}
func (d *OpenFileDialog) CanCreateDirectories(canCreateDirectories bool) *OpenFileDialog {
d.canCreateDirectories = canCreateDirectories
return d
}
func (d *OpenFileDialog) AllowsOtherFileTypes(allowsOtherFileTypes bool) *OpenFileDialog {
d.allowsOtherFileTypes = allowsOtherFileTypes
return d
}
func (d *OpenFileDialog) AllowsMultipleSelection(allowsMultipleSelection bool) *OpenFileDialog {
d.allowsMultipleSelection = allowsMultipleSelection
return d
}
func (d *OpenFileDialog) ShowHiddenFiles(showHiddenFiles bool) *OpenFileDialog {
d.showHiddenFiles = showHiddenFiles
return d
}
func (d *OpenFileDialog) HideExtension(hideExtension bool) *OpenFileDialog {
d.hideExtension = hideExtension
return d
}
func (d *OpenFileDialog) TreatsFilePackagesAsDirectories(treatsFilePackagesAsDirectories bool) *OpenFileDialog {
d.treatsFilePackagesAsDirectories = treatsFilePackagesAsDirectories
return d
}
func (d *OpenFileDialog) ResolvesAliases(resolvesAliases bool) *OpenFileDialog {
d.resolvesAliases = resolvesAliases
return d
}
// AddFilter adds a filter to the dialog. The filter is a display name and a semicolon separated list of extensions.
// EG: AddFilter("Image Files", "*.jpg;*.png")
func (d *OpenFileDialog) AddFilter(displayName, pattern string) *OpenFileDialog {
d.filters = append(d.filters, dialogFileFilter{
DisplayName: strings.TrimSpace(displayName),
Pattern: strings.TrimSpace(pattern),
})
return d
}
func (d *OpenFileDialog) SetButtonText(text string) *OpenFileDialog {
d.buttonText = text
return d
}
func (d *OpenFileDialog) SetDirectory(directory string) *OpenFileDialog {
d.directory = directory
return d
}
func (d *OpenFileDialog) CanSelectHiddenExtension(canSelectHiddenExtension bool) *OpenFileDialog {
d.canSelectHiddenExtension = canSelectHiddenExtension
return d
}
func (d *OpenFileDialog) Show(callbacks ...func([]string)) chan []string {
d.result = make(chan []string, 1)
d.app.started.run(d)
if len(callbacks) > 0 {
go func() {
result := <-d.result
for _, callback := range callbacks {
callback(result)
}
}()
return nil
}
return d.result
}
type SaveFileDialog struct {
id atomic.Uint64
log logFunc
app *App
window *Window
result chan string
canCreateDirectories bool
showHiddenFiles bool
canSelectHiddenExtension bool
allowOtherFileTypes bool
hideExtension bool
treatsFilePackagesAsDirectories bool
title string
message string
directory string
filename string
buttonText string
filters []dialogFileFilter
}
func (d *SaveFileDialog) run() {
if d.id.Load() == 0 {
id := getDialogID()
d.id.Store(id)
defer func() {
freeDialogID(id)
d.id.Store(0)
d.log("free", "id", id)
}()
d.log("open", "id", id, "title", d.title, "message", d.message, "directory", d.directory, "filename", d.filename, "buttonText", d.buttonText, "filters", d.filters)
selections, err := d.app.thread.InvokeSyncWithResultAndError(func() (any, error) {
return runSaveFileDialog(d)
})
if d.result != nil {
result, ok := selections.(string)
if err != nil || !ok {
d.result <- ""
} else {
d.result <- result
}
close(d.result)
}
}
}
// AddFilter adds a filter to the dialog. The filter is a display name and a semicolon separated list of extensions.
// EG: AddFilter("Image Files", "*.jpg;*.png")
func (d *SaveFileDialog) AddFilter(displayName, pattern string) *SaveFileDialog {
d.filters = append(d.filters, dialogFileFilter{
DisplayName: strings.TrimSpace(displayName),
Pattern: strings.TrimSpace(pattern),
})
return d
}
func (d *SaveFileDialog) CanCreateDirectories(canCreateDirectories bool) *SaveFileDialog {
d.canCreateDirectories = canCreateDirectories
return d
}
func (d *SaveFileDialog) CanSelectHiddenExtension(canSelectHiddenExtension bool) *SaveFileDialog {
d.canSelectHiddenExtension = canSelectHiddenExtension
return d
}
func (d *SaveFileDialog) ShowHiddenFiles(showHiddenFiles bool) *SaveFileDialog {
d.showHiddenFiles = showHiddenFiles
return d
}
func (d *SaveFileDialog) SetDirectory(directory string) *SaveFileDialog {
d.directory = directory
return d
}
func (d *SaveFileDialog) SetButtonText(text string) *SaveFileDialog {
d.buttonText = text
return d
}
func (d *SaveFileDialog) SetFilename(filename string) *SaveFileDialog {
d.filename = filename
return d
}
func (d *SaveFileDialog) AllowsOtherFileTypes(allowOtherFileTypes bool) *SaveFileDialog {
d.allowOtherFileTypes = allowOtherFileTypes
return d
}
func (d *SaveFileDialog) HideExtension(hideExtension bool) *SaveFileDialog {
d.hideExtension = hideExtension
return d
}
func (d *SaveFileDialog) TreatsFilePackagesAsDirectories(treatsFilePackagesAsDirectories bool) *SaveFileDialog {
d.treatsFilePackagesAsDirectories = treatsFilePackagesAsDirectories
return d
}
func (d *SaveFileDialog) Show(callbacks ...func(string)) chan string {
d.result = make(chan string, 1)
d.app.started.run(d)
if len(callbacks) > 0 {
go func() {
result := <-d.result
for _, callback := range callbacks {
callback(result)
}
}()
return nil
}
return d.result
}
func runChooserDialog(window windowPtr, allowMultiple, createFolders, showHidden bool, currentFolder, title string, action int, acceptLabel string, filters []dialogFileFilter) ([]string, error) {
GtkResponseCancel := 0
GtkResponseAccept := 1
fc := lib.gtk.FileChooserDialogNew(
title,
window,
action,
"_Cancel",
GtkResponseCancel,
acceptLabel,
GtkResponseAccept,
0)
lib.gtk.FileChooserSetAction(fc, action)
var gtkFilters []ptr
for _, filter := range filters {
f := lib.gtk.FileFilterNew()
lib.gtk.FileFilterSetName(f, filter.DisplayName)
lib.gtk.FileFilterAddPattern(f, filter.Pattern)
lib.gtk.FileChooserAddFilter(fc, f)
gtkFilters = append(gtkFilters, f)
}
lib.gtk.FileChooserSetSelectMultiple(fc, allowMultiple)
lib.gtk.FileChooserSetCreateFolders(fc, createFolders)
lib.gtk.FileChooserSetShowHidden(fc, showHidden)
if currentFolder != "" {
lib.gtk.FileChooserSetCurrentFolder(fc, currentFolder)
}
buildStringAndFree := func(s ptr) string {
bytes := []byte{}
p := unsafe.Pointer(s)
for {
val := *(*byte)(p)
if val == 0 { // this is the null terminator
break
}
bytes = append(bytes, val)
p = unsafe.Add(p, 1)
}
lib.g.Free(s) // so we don't have to iterate a second time
return string(bytes)
}
response := lib.gtk.DialogRun(fc)
var selections []string
if response == GtkResponseAccept {
filenames := lib.gtk.FileChooserGetFilenames(fc)
iter := filenames
count := 0
for {
selections = append(selections, buildStringAndFree(iter.data))
iter = iter.next
if iter == nil || count == 1024 {
break
}
count++
}
}
defer lib.gtk.WidgetDestroy(windowPtr(fc))
return selections, nil
}
// dialog related
func runOpenFileDialog(d *OpenFileDialog) ([]string, error) {
window := windowPtr(0)
if d.window != nil {
window = d.window.pointer
}
buttonText := d.buttonText
if buttonText == "" {
buttonText = "_Open"
}
return runChooserDialog(
window,
d.allowsMultipleSelection,
d.canCreateDirectories,
d.showHiddenFiles,
d.directory,
d.title,
0, // GtkFileChooserActionOpen
buttonText,
d.filters)
}
func runMessageDialog(d *MessageDialog) int {
window := windowPtr(0)
if d.window != nil {
window = d.window.pointer
}
buttonMask := GtkButtonsOk
if len(d.buttons) > 0 {
buttonMask = GtkButtonsNone
}
dialog := lib.gtk.MessageDialogNew(
window,
GtkDialogModal|GtkDialogDestroyWithParent,
d.dtype,
buttonMask,
d.message)
if d.title != "" {
lib.gtk.WindowSetTitle(dialog, d.title)
}
GdkColorspaceRGB := 0
if img, err := pngToImage(d.icon); err == nil {
gbytes := lib.g.BytesNewStatic(uintptr(unsafe.Pointer(&img.Pix[0])), len(img.Pix))
defer lib.g.BytesUnref(gbytes)
pixBuf := lib.gdk.PixbufNewFromBytes(
gbytes,
GdkColorspaceRGB,
1, // has_alpha
8,
img.Bounds().Dx(),
img.Bounds().Dy(),
img.Stride,
)
image := lib.gtk.ImageNewFromPixbuf(pixBuf)
widgetSetVisible(image, false)
contentArea := lib.gtk.DialogGetContentArea(dialog)
lib.gtk.ContainerAdd(contentArea, image)
}
for i, button := range d.buttons {
lib.gtk.DialogAddButton(
dialog,
button.Label,
i,
)
if button.IsDefault {
lib.gtk.DialogSetDefaultResponse(dialog, i)
}
}
defer lib.gtk.WidgetDestroy(dialog)
return lib.gtk.DialogRun(dialog)
}
func runSaveFileDialog(d *SaveFileDialog) (string, error) {
window := windowPtr(0)
if d.window != nil {
window = d.window.pointer
}
buttonText := d.buttonText
if buttonText == "" {
buttonText = "_Save"
}
results, err := runChooserDialog(
window,
false, // multiple selection
d.canCreateDirectories,
d.showHiddenFiles,
d.directory,
d.title,
1, // GtkFileChooserActionSave
buttonText,
d.filters)
if err != nil || len(results) == 0 {
return "", err
}
return results[0], nil
}