forked from fyne-io/fyne
/
file.go
852 lines (750 loc) · 21.5 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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
package dialog
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/gorustyt/fyne/v2"
"github.com/gorustyt/fyne/v2/container"
"github.com/gorustyt/fyne/v2/storage"
"github.com/gorustyt/fyne/v2/storage/repository"
"github.com/gorustyt/fyne/v2/theme"
"github.com/gorustyt/fyne/v2/widget"
)
type viewLayout int
const (
gridView viewLayout = iota
listView
)
type textWidget interface {
fyne.Widget
SetText(string)
}
type favoriteItem struct {
locName string
locIcon fyne.Resource
loc fyne.URI
}
type fileDialogPanel interface {
fyne.Widget
Unselect(int)
}
type fileDialog struct {
file *FileDialog
fileName textWidget
dismiss *widget.Button
open *widget.Button
breadcrumb *fyne.Container
breadcrumbScroll *container.Scroll
files fileDialogPanel
filesScroll *container.Scroll
favorites []favoriteItem
favoritesList *widget.List
showHidden bool
view viewLayout
data []fyne.URI
dataLock sync.RWMutex
win *widget.PopUp
selected fyne.URI
selectedID int
dir fyne.ListableURI
// this will be the initial filename in a FileDialog in save mode
initialFileName string
}
// FileDialog is a dialog containing a file picker for use in opening or saving files.
type FileDialog struct {
callback interface{}
onClosedCallback func(bool)
parent fyne.Window
dialog *fileDialog
confirmText, dismissText string
desiredSize fyne.Size
filter storage.FileFilter
save bool
// this will be applied to dialog.dir when it's loaded
startingLocation fyne.ListableURI
// this will be the initial filename in a FileDialog in save mode
initialFileName 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()
}
}
saveName.SetPlaceHolder("Enter filename")
f.fileName = saveName
} else {
f.fileName = widget.NewLabel("")
}
label := "Open"
if f.file.save {
label = "Save"
}
if f.file.confirmText != "" {
label = f.file.confirmText
}
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
location, _ := storage.Child(f.dir, name)
exists, _ := storage.Exists(location)
// check if a directory is selected
listable, err := storage.CanList(location)
if !exists {
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
callback(storage.Writer(location))
return
} else if err == nil && listable {
// a directory has been selected
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 {
return
}
f.win.Hide()
callback(storage.Writer(location))
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.Reader(f.selected))
} else if f.file.isDirectory() {
callback := f.file.callback.(func(fyne.ListableURI, error))
f.win.Hide()
if f.file.onClosedCallback != nil {
f.file.onClosedCallback(true)
}
callback(f.dir, nil)
}
})
f.open.Importance = widget.HighImportance
f.open.Disable()
if f.file.save {
f.fileName.SetText(f.initialFileName)
}
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 if f.file.isDirectory() {
f.file.callback.(func(fyne.ListableURI, error))(nil, nil)
} else {
f.file.callback.(func(fyne.URIReadCloser, error))(nil, nil)
}
}
})
buttons := container.NewGridWithRows(1, f.dismiss, f.open)
f.filesScroll = container.NewScroll(nil) // filesScroll's content will be set by setView function.
verticalExtra := float32(float64(fileIconSize) * 0.25)
itemMin := f.newFileItem(storage.NewFileURI("filename.txt"), false, false).MinSize()
f.filesScroll.SetMinSize(itemMin.AddWidthHeight(itemMin.Width+theme.Padding()*3, verticalExtra))
f.breadcrumb = container.NewHBox()
f.breadcrumbScroll = container.NewHScroll(container.NewPadded(f.breadcrumb))
title := label + " File"
if f.file.isDirectory() {
title = label + " Folder"
}
f.setView(gridView)
f.loadFavorites()
f.favoritesList = widget.NewList(
func() int {
return len(f.favorites)
},
func() fyne.CanvasObject {
return container.NewHBox(container.New(&iconPaddingLayout{}, widget.NewIcon(theme.DocumentIcon())), widget.NewLabel("Template Object"))
},
func(id widget.ListItemID, item fyne.CanvasObject) {
item.(*fyne.Container).Objects[0].(*fyne.Container).Objects[0].(*widget.Icon).SetResource(f.favorites[id].locIcon)
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(f.favorites[id].locName)
},
)
f.favoritesList.OnSelected = func(id widget.ListItemID) {
f.setLocation(f.favorites[id].loc)
}
var optionsButton *widget.Button
optionsButton = widget.NewButtonWithIcon("", theme.SettingsIcon(), func() {
f.optionsMenu(fyne.CurrentApp().Driver().AbsolutePositionForObject(optionsButton), optionsButton.Size())
})
var toggleViewButton *widget.Button
toggleViewButton = widget.NewButtonWithIcon("", theme.ListIcon(), func() {
if f.view == gridView {
f.setView(listView)
toggleViewButton.SetIcon(theme.GridIcon())
} else {
f.setView(gridView)
toggleViewButton.SetIcon(theme.ListIcon())
}
})
newFolderButton := widget.NewButtonWithIcon("", theme.FolderNewIcon(), func() {
newFolderEntry := widget.NewEntry()
ShowForm("New Folder", "Create Folder", "Cancel", []*widget.FormItem{
{
Text: "Name",
Widget: newFolderEntry,
},
}, func(s bool) {
if !s || newFolderEntry.Text == "" {
return
}
newFolderPath := filepath.Join(f.dir.Path(), newFolderEntry.Text)
createFolderErr := os.MkdirAll(newFolderPath, 0750)
if createFolderErr != nil {
fyne.LogError(
fmt.Sprintf("Failed to create folder with path %s", newFolderPath),
createFolderErr,
)
ShowError(errors.New("folder cannot be created"), f.file.parent)
}
f.refreshDir(f.dir)
}, f.file.parent)
})
optionsbuttons := container.NewHBox(
newFolderButton,
toggleViewButton,
optionsButton,
)
header := container.NewBorder(nil, nil, nil, optionsbuttons,
optionsbuttons, widget.NewLabelWithStyle(title, fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
)
footer := container.NewBorder(nil, nil, nil, buttons,
buttons, container.NewHScroll(f.fileName),
)
body := container.NewHSplit(
f.favoritesList,
container.NewBorder(f.breadcrumbScroll, nil, nil, nil,
f.breadcrumbScroll, f.filesScroll,
),
)
body.SetOffset(0) // Set the minimum offset so that the favoritesList takes only it's minimal width
return container.NewBorder(header, footer, nil, nil, body)
}
func (f *fileDialog) optionsMenu(position fyne.Position, buttonSize fyne.Size) {
hiddenFiles := widget.NewCheck("Show Hidden Files", func(changed bool) {
f.showHidden = changed
f.refreshDir(f.dir)
})
hiddenFiles.Checked = f.showHidden
hiddenFiles.Refresh()
content := container.NewVBox(hiddenFiles)
p := position.Add(buttonSize)
pos := fyne.NewPos(p.X-content.MinSize().Width-theme.Padding()*2, p.Y+theme.Padding()*2)
widget.ShowPopUpAtPosition(content, f.win.Canvas, pos)
}
func (f *fileDialog) loadFavorites() {
favoriteLocations, err := getFavoriteLocations()
if err != nil {
fyne.LogError("Getting favorite locations", err)
}
favoriteIcons := getFavoriteIcons()
favoriteOrder := getFavoriteOrder()
f.favorites = []favoriteItem{
{locName: "Home", locIcon: theme.HomeIcon(), loc: favoriteLocations["Home"]}}
app := fyne.CurrentApp()
if hasAppFiles(app) {
f.favorites = append(f.favorites,
favoriteItem{locName: "App Files", locIcon: theme.FileIcon(), loc: storageURI(app)})
}
f.favorites = append(f.favorites, f.getPlaces()...)
for _, locName := range favoriteOrder {
loc, ok := favoriteLocations[locName]
if !ok {
continue
}
locIcon := favoriteIcons[locName]
f.favorites = append(f.favorites,
favoriteItem{locName: locName, locIcon: locIcon, loc: loc})
}
}
func (f *fileDialog) refreshDir(dir fyne.ListableURI) {
f.dataLock.Lock()
f.data = nil
f.dataLock.Unlock()
files, err := dir.List()
if err != nil {
fyne.LogError("Unable to read ListableURI "+dir.String(), err)
return
}
var icons []fyne.URI
parent, err := storage.Parent(dir)
if err != nil && err != repository.ErrURIRoot {
fyne.LogError("Unable to get parent of "+dir.String(), err)
return
}
if parent != nil && parent.String() != dir.String() {
icons = append(icons, parent)
}
for _, file := range files {
if !f.showHidden && isHidden(file) {
continue
}
listable, err := storage.ListerForURI(file)
if f.file.isDirectory() && err != nil {
continue
} else if err == nil { // URI points to a directory
icons = append(icons, listable)
} else if f.file.filter == nil || f.file.filter.Matches(file) {
icons = append(icons, file)
}
}
f.dataLock.Lock()
f.data = icons
f.dataLock.Unlock()
f.files.Refresh()
f.filesScroll.Offset = fyne.NewPos(0, 0)
f.filesScroll.Refresh()
}
func (f *fileDialog) setLocation(dir fyne.URI) error {
if f.selectedID > -1 {
f.files.Unselect(f.selectedID)
}
if dir == nil {
return fmt.Errorf("failed to open nil directory")
}
list, err := storage.ListerForURI(dir)
if err != nil {
return err
}
isFav := false
for i, fav := range f.favorites {
if fav.loc == nil {
continue
}
if fav.loc.Path() == dir.Path() {
f.favoritesList.Select(i)
isFav = true
break
}
}
if !isFav {
f.favoritesList.UnselectAll()
}
f.setSelected(nil, -1)
f.dir = list
f.breadcrumb.Objects = nil
localdir := dir.String()[len(dir.Scheme())+3:]
buildDir := filepath.VolumeName(localdir)
for i, d := range strings.Split(localdir, "/") {
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 := storage.NewFileURI(buildDir)
isDir, err := storage.CanList(newDir)
if err != nil {
return err
}
if !isDir {
return errors.New("location was not a listable URI")
}
f.breadcrumb.Add(
widget.NewButton(d, func() {
err := f.setLocation(newDir)
if err != nil {
fyne.LogError("Failed to set directory", err)
}
}),
)
}
f.breadcrumbScroll.Refresh()
f.breadcrumbScroll.Offset.X = f.breadcrumbScroll.Content.Size().Width - f.breadcrumbScroll.Size().Width
f.breadcrumbScroll.Refresh()
if f.file.isDirectory() {
f.fileName.SetText(dir.Name())
f.open.Enable()
}
f.refreshDir(list)
return nil
}
func (f *fileDialog) setSelected(file fyne.URI, id int) {
if file != nil {
if listable, err := storage.CanList(file); err == nil && listable {
f.setLocation(file)
return
}
}
f.selected = file
f.selectedID = id
if file == nil || file.String()[len(file.Scheme())+3:] == "" {
// keep user input while navigating
// in a FileSave dialog
if !f.file.save {
f.fileName.SetText("")
f.open.Disable()
}
} else {
f.fileName.SetText(file.Name())
f.open.Enable()
}
}
func (f *fileDialog) setView(view viewLayout) {
f.view = view
count := func() int {
f.dataLock.RLock()
defer f.dataLock.RUnlock()
return len(f.data)
}
template := func() fyne.CanvasObject {
return f.newFileItem(storage.NewFileURI("./tempfile"), true, false)
}
update := func(id widget.GridWrapItemID, o fyne.CanvasObject) {
if dir, ok := f.getDataItem(id); ok {
parent := id == 0 && len(dir.Path()) < len(f.dir.Path())
_, isDir := dir.(fyne.ListableURI)
o.(*fileDialogItem).setLocation(dir, isDir || parent, parent)
}
}
choose := func(id int) {
if file, ok := f.getDataItem(id); ok {
f.selectedID = id
f.setSelected(file, id)
}
}
if f.view == gridView {
grid := widget.NewGridWrap(count, template, update)
grid.OnSelected = choose
f.files = grid
} else {
list := widget.NewList(count, template, update)
list.OnSelected = choose
f.files = list
}
if f.dir != nil {
f.refreshDir(f.dir)
}
f.filesScroll.Content = container.NewPadded(f.files)
f.filesScroll.Refresh()
}
func (f *fileDialog) getDataItem(id int) (fyne.URI, bool) {
f.dataLock.RLock()
defer f.dataLock.RUnlock()
if id >= len(f.data) {
return nil, false
}
return f.data[id], true
}
// effectiveStartingDir calculates the directory at which the file dialog should
// open, based on the values of startingDirectory, CWD, home, and any error
// conditions which occur.
//
// Order of precedence is:
//
// - file.startingDirectory if non-empty, os.Stat()-able, and uses the file://
// URI scheme
// - os.UserHomeDir()
// - os.Getwd()
// - "/" (should be filesystem root on all supported platforms)
func (f *FileDialog) effectiveStartingDir() fyne.ListableURI {
if f.startingLocation != nil {
if f.startingLocation.Scheme() == "file" {
path := f.startingLocation.Path()
// the starting directory is set explicitly
if _, err := os.Stat(path); err != nil {
fyne.LogError("Error with StartingLocation", err)
} else {
return f.startingLocation
}
}
}
// Try app storage
app := fyne.CurrentApp()
if hasAppFiles(app) {
list, _ := storage.ListerForURI(storageURI(app))
return list
}
// Try home dir
dir, err := os.UserHomeDir()
if err == nil {
lister, err := storage.ListerForURI(storage.NewFileURI(dir))
if err == nil {
return lister
}
fyne.LogError("Could not create lister for user home dir", err)
}
fyne.LogError("Could not load user home dir", err)
// Try to get ./
wd, err := os.Getwd()
if err == nil {
lister, err := storage.ListerForURI(storage.NewFileURI(wd))
if err == nil {
return lister
}
fyne.LogError("Could not create lister for working dir", err)
}
lister, err := storage.ListerForURI(storage.NewFileURI("/"))
if err != nil {
fyne.LogError("could not create lister for /", err)
return nil
}
return lister
}
func showFile(file *FileDialog) *fileDialog {
d := &fileDialog{file: file, initialFileName: file.initialFileName}
ui := d.makeUI()
pad := theme.Padding()
itemMin := d.newFileItem(storage.NewFileURI("filename.txt"), false, false).MinSize()
size := ui.MinSize().Add(itemMin.AddWidthHeight(itemMin.Width+pad*4, pad*2))
d.win = widget.NewModalPopUp(ui, file.parent.Canvas())
d.win.Resize(size)
d.setLocation(file.effectiveStartingDir())
d.win.Show()
return d
}
// MinSize returns the size that this dialog should not shrink below
//
// Since: 2.1
func (f *FileDialog) MinSize() fyne.Size {
return f.dialog.win.MinSize()
}
// 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)
if !f.desiredSize.IsZero() {
f.Resize(f.desiredSize)
}
}
// Refresh causes this dialog to be updated
func (f *FileDialog) Refresh() {
f.dialog.win.Refresh()
}
// Resize dialog to the requested size, if there is sufficient space.
// If the parent window is not large enough then the size will be reduced to fit.
func (f *FileDialog) Resize(size fyne.Size) {
f.desiredSize = size
if f.dialog == nil {
return
}
f.dialog.win.Resize(size)
}
// 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)
}
}
// SetConfirmText allows custom text to be set in the confirmation button
//
// Since: 2.2
func (f *FileDialog) SetConfirmText(label string) {
f.confirmText = label
if f.dialog == nil {
return
}
f.dialog.open.SetText(label)
f.dialog.win.Refresh()
}
// SetDismissText allows custom text to be set in the dismiss button
func (f *FileDialog) SetDismissText(label string) {
f.dismissText = label
if f.dialog == nil {
return
}
f.dialog.dismiss.SetText(label)
f.dialog.win.Refresh()
}
// SetLocation tells this FileDirectory which location to display.
// This is normally called before the dialog is shown.
//
// Since: 1.4
func (f *FileDialog) SetLocation(u fyne.ListableURI) {
f.startingLocation = u
if f.dialog != nil {
f.dialog.setLocation(u)
}
}
// 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) {
if f.isDirectory() {
fyne.LogError("Cannot set a filter for a folder dialog", nil)
return
}
f.filter = filter
if f.dialog != nil {
f.dialog.refreshDir(f.dialog.dir)
}
}
// SetFileName sets the filename in a FileDialog in save mode.
// This is normally called before the dialog is shown.
func (f *FileDialog) SetFileName(fileName string) {
if f.save {
f.initialFileName = fileName
//Update entry if fileDialog has already been created
if f.dialog != nil {
f.dialog.fileName.SetText(fileName)
}
}
}
// NewFileOpen creates a file dialog allowing the user to choose a file to open.
// The callback function will run when the dialog closes. The URI will be nil
// when the user cancels or when nothing is selected.
//
// 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 callback function will run when the dialog
// closes. The URI will be nil when the user cancels or when nothing is
// selected.
//
// 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 callback function will run when the dialog closes. The URI
// will be nil when the user cancels or when nothing is selected.
//
// 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 callback function will run when the
// dialog closes. The URI will be nil when the user cancels or when nothing is
// selected.
//
// 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()
}
func getFavoriteIcons() map[string]fyne.Resource {
if runtime.GOOS == "darwin" {
return map[string]fyne.Resource{
"Documents": theme.DocumentIcon(),
"Downloads": theme.DownloadIcon(),
"Music": theme.MediaMusicIcon(),
"Pictures": theme.MediaPhotoIcon(),
"Movies": theme.MediaVideoIcon(),
}
}
return map[string]fyne.Resource{
"Documents": theme.DocumentIcon(),
"Downloads": theme.DownloadIcon(),
"Music": theme.MediaMusicIcon(),
"Pictures": theme.MediaPhotoIcon(),
"Videos": theme.MediaVideoIcon(),
}
}
func getFavoriteOrder() []string {
order := []string{
"Documents",
"Downloads",
"Music",
"Pictures",
"Videos",
}
if runtime.GOOS == "darwin" {
order[4] = "Movies"
}
return order
}
func hasAppFiles(a fyne.App) bool {
return len(a.Storage().List()) > 0
}
func storageURI(a fyne.App) fyne.URI {
dir, _ := storage.Child(a.Storage().RootURI(), "Documents")
return dir
}
// iconPaddingLayout adds padding to the left of a widget.Icon().
// NOTE: It assumes that the slice only contains one item.
type iconPaddingLayout struct {
}
func (i *iconPaddingLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
padding := theme.Padding() * 2
objects[0].Move(fyne.NewPos(padding, 0))
objects[0].Resize(size.SubtractWidthHeight(padding, 0))
}
func (i *iconPaddingLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
return objects[0].MinSize().AddWidthHeight(theme.Padding()*2, 0)
}