-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.go
130 lines (115 loc) · 2.87 KB
/
ui.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
xWidget "fyne.io/x/fyne/widget"
)
func (ui *fylesUI) setDirectory(u fyne.URI) {
ui.pwd = u
dirStr := u.String()
if dirStr[len(dirStr)-1] == '/' && dirStr != "file:///" {
dirStr = dirStr[:len(dirStr)-1]
}
ui.fileTree.Select(dirStr)
ui.items.SetDir(u)
ui.fileScroll.ScrollToTop()
ui.filePath.SetText(u.Path())
ui.win.SetTitle(winTitle + " : " + u.Name())
}
func (ui *fylesUI) itemTapped(u fyne.URI) {
if u == nil {
return
}
listable, err := storage.CanList(u)
if err == nil && listable {
go func() {
// show it is selected then change
time.Sleep(canvas.DurationShort)
ui.setDirectory(u)
}()
return
}
}
func (ui *fylesUI) makeFilesPanel(u fyne.URI) *xWidget.FileTree {
vol := filepath.VolumeName(u.Path())
if vol == "" {
vol = "/"
}
root := storage.NewFileURI(vol)
files := xWidget.NewFileTree(root)
files.Filter = filterDir(ui.filter)
files.Sorter = func(u1, u2 fyne.URI) bool {
return u1.String() < u2.String() // Sort alphabetically
}
files.OnSelected = func(uid widget.TreeNodeID) {
if uid == "file://" {
uid = "file:///"
}
u, _ := storage.ParseURI(uid)
ui.setDirectory(u)
}
openParent(files, u)
return files
}
func openParent(files *xWidget.FileTree, path fyne.URI) {
parent, err := storage.Parent(path)
if err != nil {
return
}
if !files.IsBranchOpen(parent.String()) {
openParent(files, parent)
id := parent.String()
if strings.LastIndexByte(id, filepath.Separator) == len(id)-1 {
id = id[:len(id)-1]
}
files.OpenBranch(id)
}
}
func (ui *fylesUI) makeToolbar() *fyne.Container {
l := widget.NewLabel("")
ui.filePath = l
newFolderButton := widget.NewButtonWithIcon("", theme.FolderNewIcon(), func() {
newFolderEntry := widget.NewEntry()
dialog.ShowForm("New Folder", "Create Folder", "Cancel", []*widget.FormItem{
{
Text: "Name",
Widget: newFolderEntry,
},
}, func(s bool) {
if !s || newFolderEntry.Text == "" {
return
}
newFolderPath := filepath.Join(ui.pwd.Path(), newFolderEntry.Text)
createFolderErr := os.MkdirAll(newFolderPath, 0750)
if createFolderErr != nil {
fyne.LogError(
fmt.Sprintf("Failed to create folder with path %s", newFolderPath),
createFolderErr,
)
dialog.ShowError(errors.New("folder cannot be created"), ui.win)
}
ui.items.SetDir(ui.pwd)
}, ui.win)
})
newFolderButton.Importance = widget.LowImportance
return container.NewBorder(nil, nil, widget.NewToolbar(
widget.NewToolbarAction(theme.HomeIcon(), func() {
home, err := os.UserHomeDir()
if err != nil {
return
}
ui.setDirectory(storage.NewFileURI(home))
})), newFolderButton,
container.NewHScroll(l))
}