-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
dialog.go
89 lines (77 loc) · 2.58 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
//go:build linux
// +build linux
package linux
import (
"github.com/wailsapp/wails/v2/internal/frontend"
"unsafe"
)
/*
#include <stdlib.h>
#include "gtk/gtk.h"
*/
import "C"
const (
GTK_FILE_CHOOSER_ACTION_OPEN C.GtkFileChooserAction = C.GTK_FILE_CHOOSER_ACTION_OPEN
GTK_FILE_CHOOSER_ACTION_SAVE C.GtkFileChooserAction = C.GTK_FILE_CHOOSER_ACTION_SAVE
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER C.GtkFileChooserAction = C.GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
)
var openFileResults = make(chan []string)
var messageDialogResult = make(chan string)
func (f *Frontend) OpenFileDialog(dialogOptions frontend.OpenDialogOptions) (result string, err error) {
f.mainWindow.OpenFileDialog(dialogOptions, 0, GTK_FILE_CHOOSER_ACTION_OPEN)
results := <-openFileResults
if len(results) == 1 {
return results[0], nil
}
return "", nil
}
func (f *Frontend) OpenMultipleFilesDialog(dialogOptions frontend.OpenDialogOptions) ([]string, error) {
f.mainWindow.OpenFileDialog(dialogOptions, 1, GTK_FILE_CHOOSER_ACTION_OPEN)
result := <-openFileResults
return result, nil
}
func (f *Frontend) OpenDirectoryDialog(dialogOptions frontend.OpenDialogOptions) (string, error) {
f.mainWindow.OpenFileDialog(dialogOptions, 0, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
result := <-openFileResults
if len(result) == 1 {
return result[0], nil
}
return "", nil
}
func (f *Frontend) SaveFileDialog(dialogOptions frontend.SaveDialogOptions) (string, error) {
options := frontend.OpenDialogOptions{
DefaultDirectory: dialogOptions.DefaultDirectory,
DefaultFilename: dialogOptions.DefaultFilename,
Title: dialogOptions.Title,
Filters: dialogOptions.Filters,
ShowHiddenFiles: dialogOptions.ShowHiddenFiles,
CanCreateDirectories: dialogOptions.CanCreateDirectories,
}
f.mainWindow.OpenFileDialog(options, 0, GTK_FILE_CHOOSER_ACTION_SAVE)
results := <-openFileResults
if len(results) == 1 {
return results[0], nil
}
return "", nil
}
func (f *Frontend) MessageDialog(dialogOptions frontend.MessageDialogOptions) (string, error) {
f.mainWindow.MessageDialog(dialogOptions)
return <-messageDialogResult, nil
}
//export processOpenFileResult
func processOpenFileResult(carray **C.char) {
// Create a Go slice from the C array
var result []string
goArray := (*[1024]*C.char)(unsafe.Pointer(carray))[:1024:1024]
for _, s := range goArray {
if s == nil {
break
}
result = append(result, C.GoString(s))
}
openFileResults <- result
}
//export processMessageDialogResult
func processMessageDialogResult(result *C.char) {
messageDialogResult <- C.GoString(result)
}