forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
421 lines (373 loc) · 10.6 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
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
package dialog
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/layout"
"fyne.io/fyne/storage"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
type textWidget interface {
fyne.Widget
SetText(string)
}
type fileDialog struct {
file *FileDialog
fileName textWidget
dismiss *widget.Button
open *widget.Button
breadcrumb *widget.Box
files *fyne.Container
fileScroll *widget.ScrollContainer
win *widget.PopUp
selected *fileDialogItem
dir string
}
// FileDialog is a dialog containing a file picker for use in opening or saving files.
type FileDialog struct {
save bool
callback interface{}
onClosedCallback func(bool)
filter storage.FileFilter
parent fyne.Window
dialog *fileDialog
dismissText string
}
// Declare conformity to Dialog interface
var _ Dialog = (*FileDialog)(nil)
func (f *fileDialog) makeUI() fyne.CanvasObject {
if f.file.save {
saveName := widget.NewEntry()
saveName.OnChanged = func(s string) {
if s == "" {
f.open.Disable()
} else {
f.open.Enable()
}
}
f.fileName = saveName
} else {
f.fileName = widget.NewLabel("")
}
label := "Open"
if f.file.save {
label = "Save"
}
f.open = widget.NewButton(label, func() {
if f.file.callback == nil {
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(false)
}
return
}
if f.file.save {
callback := f.file.callback.(func(fyne.URIWriteCloser, error))
name := f.fileName.(*widget.Entry).Text
path := filepath.Join(f.dir, name)
info, err := os.Stat(path)
if os.IsNotExist(err) {
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
callback(storage.SaveFileToURI(storage.NewURI("file://" + path)))
return
} else if info.IsDir() {
ShowInformation("Cannot overwrite",
"Files cannot replace a directory,\ncheck the file name and try again", f.file.parent)
return
}
ShowConfirm("Overwrite?", "Are you sure you want to overwrite the file\n"+name+"?",
func(ok bool) {
if !ok {
callback(nil, nil)
return
}
callback(storage.SaveFileToURI(storage.NewURI("file://" + path)))
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
}, f.file.parent)
} else if f.selected != nil {
callback := f.file.callback.(func(fyne.URIReadCloser, error))
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
callback(storage.OpenFileFromURI(storage.NewURI("file://" + f.selected.path)))
}
})
f.open.Style = widget.PrimaryButton
f.open.Disable()
dismissLabel := "Cancel"
if f.file.dismissText != "" {
dismissLabel = f.file.dismissText
}
f.dismiss = widget.NewButton(dismissLabel, func() {
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(false)
}
if f.file.callback != nil {
if f.file.save {
f.file.callback.(func(fyne.URIWriteCloser, error))(nil, nil)
} else {
f.file.callback.(func(fyne.URIReadCloser, error))(nil, nil)
}
}
})
buttons := widget.NewHBox(f.dismiss, f.open)
footer := fyne.NewContainerWithLayout(layout.NewBorderLayout(nil, nil, nil, buttons),
buttons, widget.NewHScrollContainer(f.fileName))
f.files = fyne.NewContainerWithLayout(layout.NewGridWrapLayout(fyne.NewSize(fileIconCellWidth,
fileIconSize+theme.Padding()+fileTextSize)),
)
f.fileScroll = widget.NewScrollContainer(f.files)
verticalExtra := int(float64(fileIconSize) * 0.25)
f.fileScroll.SetMinSize(fyne.NewSize(fileIconCellWidth*2+theme.Padding(),
(fileIconSize+fileTextSize)+theme.Padding()*2+verticalExtra))
f.breadcrumb = widget.NewHBox()
scrollBread := widget.NewScrollContainer(f.breadcrumb)
body := fyne.NewContainerWithLayout(layout.NewBorderLayout(scrollBread, nil, nil, nil),
scrollBread, f.fileScroll)
header := widget.NewLabelWithStyle(label+" File", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
favorites := widget.NewGroup("Favorites", f.loadFavorites()...)
return fyne.NewContainerWithLayout(layout.NewBorderLayout(header, footer, favorites, nil),
favorites, header, footer, body)
}
func (f *fileDialog) loadFavorites() []fyne.CanvasObject {
home, _ := os.UserHomeDir()
places := []fyne.CanvasObject{
widget.NewButton("Home", func() {
f.setDirectory(home)
}),
widget.NewButton("Documents", func() {
f.setDirectory(filepath.Join(home, "Documents"))
}),
widget.NewButton("Downloads", func() {
f.setDirectory(filepath.Join(home, "Downloads"))
}),
}
places = append(places, f.loadPlaces()...)
return places
}
func (f *fileDialog) refreshDir(dir string) {
f.files.Objects = nil
files, err := ioutil.ReadDir(dir)
if err != nil {
fyne.LogError("Unable to read path "+dir, err)
return
}
var icons []fyne.CanvasObject
parent := filepath.Dir(dir)
if parent != dir {
fi := &fileDialogItem{picker: f, icon: canvas.NewImageFromResource(theme.FolderOpenIcon()),
name: "(Parent)", path: filepath.Dir(dir), dir: true}
fi.ExtendBaseWidget(fi)
icons = append(icons, fi)
}
for _, file := range files {
if isHidden(file.Name(), dir) {
continue
}
itemPath := filepath.Join(dir, file.Name())
if file.IsDir() {
icons = append(icons, f.newFileItem(itemPath, true))
} else if f.file.filter == nil || f.file.filter.Matches(storage.NewURI("file://"+itemPath)) {
icons = append(icons, f.newFileItem(itemPath, false))
}
}
f.files.Objects = icons
f.files.Refresh()
f.fileScroll.Offset = fyne.NewPos(0, 0)
f.fileScroll.Refresh()
}
func (f *fileDialog) setDirectory(dir string) {
f.setSelected(nil)
f.dir = dir
f.breadcrumb.Children = nil
buildDir := filepath.VolumeName(dir)
for i, d := range strings.Split(dir, string(filepath.Separator)) {
if d == "" {
if i > 0 { // what we get if we split "/"
break
}
buildDir = "/"
d = "/"
} else if i > 0 {
buildDir = filepath.Join(buildDir, d)
} else {
d = buildDir
buildDir = d + string(os.PathSeparator)
}
newDir := buildDir
f.breadcrumb.Append(
widget.NewButton(d, func() {
f.setDirectory(newDir)
}),
)
}
f.refreshDir(dir)
}
func (f *fileDialog) setSelected(file *fileDialogItem) {
if f.selected != nil {
f.selected.isCurrent = false
f.selected.Refresh()
}
if file != nil && file.isDirectory() {
f.setDirectory(file.path)
return
}
f.selected = file
if file == nil || file.path == "" {
f.fileName.SetText("")
f.open.Disable()
} else {
file.isCurrent = true
f.fileName.SetText(filepath.Base(file.path))
f.open.Enable()
}
}
// effectiveStartingDir calculates the directory at which the file dialog
// should open, based on the values of CWD, home, and any error conditions
// which occur.
//
// Order of precedence is:
//
// * os.Getwd()
// * os.UserHomeDir()
// * "/" (should be filesystem root on all supported platforms)
func (f *FileDialog) effectiveStartingDir() string {
// Try to use CWD
var err error = nil
dir, err := os.Getwd()
if err == nil {
return dir
}
fyne.LogError("Could not load CWD", err)
// fail over to home dir
dir, err = os.UserHomeDir()
if err == nil {
return dir
}
fyne.LogError("Could not load user home dir", err)
return "/"
}
func showFile(file *FileDialog) *fileDialog {
d := &fileDialog{file: file}
ui := d.makeUI()
// by default, use ./ as the starting directory
dir, err := os.Getwd()
if err != nil {
// if that doesn't work, fail-over to ~/
fyne.LogError("Could not load CWD", err)
dir, err = os.UserHomeDir()
if err != nil {
// if that dosen't work, fail over to /
fyne.LogError("Could not load user home dir", err)
dir = "/"
}
}
d.setDirectory(dir)
size := ui.MinSize().Add(fyne.NewSize(fileIconCellWidth*2+theme.Padding()*4,
(fileIconSize+fileTextSize)+theme.Padding()*4))
d.win = widget.NewModalPopUp(ui, file.parent.Canvas())
d.win.Resize(size)
d.win.Show()
return d
}
// Show shows the file dialog.
func (f *FileDialog) Show() {
if f.save {
if fileSaveOSOverride(f) {
return
}
} else {
if fileOpenOSOverride(f) {
return
}
}
if f.dialog != nil {
f.dialog.win.Show()
return
}
f.dialog = showFile(f)
}
// Hide hides the file dialog.
func (f *FileDialog) Hide() {
if f.dialog == nil {
return
}
f.dialog.win.Hide()
if f.onClosedCallback != nil {
f.onClosedCallback(false)
}
}
// SetDismissText allows custom text to be set in the confirmation button
func (f *FileDialog) SetDismissText(label string) {
if f.dialog == nil {
return
}
f.dialog.dismiss.SetText(label)
widget.Refresh(f.dialog.win)
}
// SetOnClosed sets a callback function that is called when
// the dialog is closed.
func (f *FileDialog) SetOnClosed(closed func()) {
if f.dialog == nil {
return
}
// If there is already a callback set, remember it and call both.
originalCallback := f.onClosedCallback
f.onClosedCallback = func(response bool) {
closed()
if originalCallback != nil {
originalCallback(response)
}
}
}
// SetFilter sets a filter for limiting files that can be chosen in the file dialog.
func (f *FileDialog) SetFilter(filter storage.FileFilter) {
f.filter = filter
if f.dialog != nil {
f.dialog.refreshDir(f.dialog.dir)
}
}
// NewFileOpen creates a file dialog allowing the user to choose a file to open.
// The dialog will appear over the window specified when Show() is called.
func NewFileOpen(callback func(fyne.URIReadCloser, error), parent fyne.Window) *FileDialog {
dialog := &FileDialog{callback: callback, parent: parent}
return dialog
}
// NewFileSave creates a file dialog allowing the user to choose a file to save to (new or overwrite).
// If the user chooses an existing file they will be asked if they are sure.
// The dialog will appear over the window specified when Show() is called.
func NewFileSave(callback func(fyne.URIWriteCloser, error), parent fyne.Window) *FileDialog {
dialog := &FileDialog{callback: callback, parent: parent, save: true}
return dialog
}
// ShowFileOpen creates and shows a file dialog allowing the user to choose a file to open.
// The dialog will appear over the window specified.
func ShowFileOpen(callback func(fyne.URIReadCloser, error), parent fyne.Window) {
dialog := NewFileOpen(callback, parent)
if fileOpenOSOverride(dialog) {
return
}
dialog.Show()
}
// ShowFileSave creates and shows a file dialog allowing the user to choose a file to save to (new or overwrite).
// If the user chooses an existing file they will be asked if they are sure.
// The dialog will appear over the window specified.
func ShowFileSave(callback func(fyne.URIWriteCloser, error), parent fyne.Window) {
dialog := NewFileSave(callback, parent)
if fileSaveOSOverride(dialog) {
return
}
dialog.Show()
}