forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
88 lines (73 loc) · 2.29 KB
/
file.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
package gomobile
import (
"errors"
"fmt"
"io"
"github.com/fyne-io/mobile/app"
"fyne.io/fyne"
"fyne.io/fyne/storage"
)
type fileOpen struct {
io.ReadCloser
uri fyne.URI
done func()
}
func (f *fileOpen) Name() string {
return f.uri.Name()
}
func (f *fileOpen) URI() fyne.URI {
return f.uri
}
func (d *mobileDriver) FileReaderForURI(u fyne.URI) (fyne.URIReadCloser, error) {
file := &fileOpen{uri: u}
read, err := nativeFileOpen(file)
if read == nil {
return nil, err
}
file.ReadCloser = read
return file, err
}
func (d *mobileDriver) ListerForURI(uri fyne.URI) (fyne.ListableURI, error) {
return nil, fmt.Errorf("mobile driver does support creating listable URIs yet")
}
func (d *mobileDriver) FileWriterForURI(u fyne.URI) (fyne.URIWriteCloser, error) {
return nil, errors.New("file writing is not supported on mobile")
}
func mobileFilter(filter storage.FileFilter) *app.FileFilter {
mobile := &app.FileFilter{}
if f, ok := filter.(*storage.MimeTypeFileFilter); ok {
mobile.MimeTypes = f.MimeTypes
} else if f, ok := filter.(*storage.ExtensionFileFilter); ok {
mobile.Extensions = f.Extensions
} else {
fyne.LogError("Custom filter types not supported on mobile", nil)
}
return mobile
}
type hasPicker interface {
ShowFileOpenPicker(func(string, func()), *app.FileFilter)
}
// ShowFileOpenPicker loads the native file open dialog and returns the chosen file path via the callback func.
func ShowFileOpenPicker(callback func(fyne.URIReadCloser, error), filter storage.FileFilter) {
drv := fyne.CurrentApp().Driver().(*mobileDriver)
if a, ok := drv.app.(hasPicker); ok {
a.ShowFileOpenPicker(func(uri string, closer func()) {
f, err := drv.FileReaderForURI(storage.NewURI(uri))
if f != nil {
f.(*fileOpen).done = closer
}
callback(f, err)
}, mobileFilter(filter))
}
}
// ShowFolderOpenPicker loads the native folder open dialog and calls back the chosen directory path as a ListableURI.
func ShowFolderOpenPicker(callback func(fyne.ListableURI, error)) {
filter := storage.NewMimeTypeFileFilter([]string{"application/x-directory"})
drv := fyne.CurrentApp().Driver().(*mobileDriver)
if a, ok := drv.app.(hasPicker); ok {
a.ShowFileOpenPicker(func(uri string, _ func()) {
f, err := drv.ListerForURI(storage.NewURI(uri))
callback(f, err)
}, mobileFilter(filter))
}
}