-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.go
70 lines (55 loc) · 2.01 KB
/
plugin.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
package file_picker
import (
"github.com/go-flutter-desktop/go-flutter"
"github.com/go-flutter-desktop/go-flutter/plugin"
"github.com/pkg/errors"
)
const channelName = "file_picker"
type FilePickerPlugin struct{}
var _ flutter.Plugin = &FilePickerPlugin{} // compile-time type check
func (p *FilePickerPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
channel := plugin.NewMethodChannel(messenger, channelName, plugin.StandardMethodCodec{})
channel.CatchAllHandleFunc(p.handleFilePicker)
return nil
}
func (p *FilePickerPlugin) handleFilePicker(methodCall interface{}) (reply interface{}, err error) {
method := methodCall.(plugin.MethodCall)
multipleSelection := method.Arguments.(bool)
filter, err := fileFilter(method.Method)
if err != nil {
return nil, errors.Wrap(err, "failed to get filter")
}
dialogProvider := dialogProvider{}
fileDescriptor, err := p.filePicker(dialogProvider, false, filter, multipleSelection)
if err != nil {
return "", errors.Wrap(err, "user cancel select file")
}
return fileDescriptor, nil
}
func (p *FilePickerPlugin) filePicker(dialog dialog, isDirectory bool, filter string, multipleSelection bool) (reply interface{}, err error) {
switch multipleSelection {
case false:
fileDescriptor, _, err := dialog.File("select file", filter, isDirectory)
if err != nil {
return nil, errors.Wrap(err, "failed to open dialog picker")
}
return fileDescriptor, nil
case true:
fileDescriptors, _, err := dialog.FileMulti("select files", filter)
if err != nil {
return nil, errors.Wrap(err, "failed to open dialog picker")
}
//type []string is not supported by StandardMessageCodec
sliceFileDescriptors := make([]interface{}, len(fileDescriptors))
for i, file := range fileDescriptors {
sliceFileDescriptors[i] = file
}
return sliceFileDescriptors, nil
default:
fileDescriptor, _, err := dialog.File("select file", filter, isDirectory)
if err != nil {
return nil, errors.Wrap(err, "failed to open dialog picker")
}
return fileDescriptor, nil
}
}