/
filebrowser.go
268 lines (205 loc) · 5.42 KB
/
filebrowser.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
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"path"
"time"
)
import (
"github.com/lxn/walk"
)
type FileInfo struct {
Name string
Size int64
Modified time.Time
}
type FileInfoModel struct {
items []*FileInfo
rowsResetPublisher walk.EventPublisher
rowChangedPublisher walk.IntEventPublisher
}
func (m *FileInfoModel) Columns() []walk.TableColumn {
return []walk.TableColumn{
{Title: "Name", Width: 200},
{Title: "Size", Format: "%d", Alignment: walk.AlignFar, Width: 80},
{Title: "Modified", Format: "2006-01-02 15:04:05", Width: 120},
}
}
func (m *FileInfoModel) RowCount() int {
return len(m.items)
}
func (m *FileInfoModel) Value(row, col int) interface{} {
item := m.items[row]
switch col {
case 0:
return item.Name
case 1:
return item.Size
case 2:
return item.Modified
}
panic("unexpected col")
}
func (m *FileInfoModel) RowsReset() *walk.Event {
return m.rowsResetPublisher.Event()
}
func (m *FileInfoModel) RowChanged() *walk.IntEvent {
return m.rowChangedPublisher.Event()
}
func (m *FileInfoModel) ResetRows(dirPath string) error {
dir, err := os.Open(dirPath)
if err != nil {
return err
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return err
}
m.items = make([]*FileInfo, 0, len(names))
for _, name := range names {
if !excludePath(name) {
fullPath := path.Join(dirPath, name)
fi, err := os.Stat(fullPath)
if err != nil {
continue
}
item := &FileInfo{
Name: name,
Size: fi.Size(),
Modified: fi.ModTime(),
}
m.items = append(m.items, item)
}
}
m.rowsResetPublisher.Publish()
return nil
}
type MainWindow struct {
*walk.MainWindow
fileInfoModel *FileInfoModel
treeView *walk.TreeView
selTvwItem *walk.TreeViewItem
tableView *walk.TableView
preview *walk.WebView
}
func (mw *MainWindow) showError(err error) {
if err == nil {
return
}
walk.MsgBox(mw, "Error", err.Error(), walk.MsgBoxOK|walk.MsgBoxIconError)
}
func (mw *MainWindow) populateTreeViewItem(parent *walk.TreeViewItem) {
mw.treeView.SetSuspended(true)
defer mw.treeView.SetSuspended(false)
// Remove dummy child
parent.Children().Clear()
dirPath := pathForTreeViewItem(parent)
dir, err := os.Open(dirPath)
if err != nil {
mw.showError(err)
return
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
panicIfErr(err)
for _, name := range names {
fi, err := os.Stat(path.Join(dirPath, name))
panicIfErr(err)
if !excludePath(name) && fi.IsDir() {
child := newTreeViewItem(name)
parent.Children().Add(child)
}
}
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
func pathForTreeViewItem(item *walk.TreeViewItem) string {
var parts []string
for item != nil {
parts = append([]string{item.Text()}, parts...)
item = item.Parent()
}
return path.Join(parts...)
}
func excludePath(path string) bool {
if path == "System Volume Information" {
return true
}
return false
}
func newTreeViewItem(text string) *walk.TreeViewItem {
item := walk.NewTreeViewItem()
item.SetText(text)
// For now, we add a dummy child to make the item expandable.
item.Children().Add(walk.NewTreeViewItem())
return item
}
func main() {
walk.Initialize(walk.InitParams{PanicOnError: true})
defer walk.Shutdown()
mainWnd, _ := walk.NewMainWindow()
mw := &MainWindow{
MainWindow: mainWnd,
fileInfoModel: &FileInfoModel{},
}
mw.SetTitle("Walk File Browser Example")
mw.SetLayout(walk.NewHBoxLayout())
fileMenu, _ := walk.NewMenu()
fileMenuAction, _ := mw.Menu().Actions().AddMenu(fileMenu)
fileMenuAction.SetText("&File")
exitAction := walk.NewAction()
exitAction.SetText("E&xit")
exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
fileMenu.Actions().Add(exitAction)
helpMenu, _ := walk.NewMenu()
helpMenuAction, _ := mw.Menu().Actions().AddMenu(helpMenu)
helpMenuAction.SetText("&Help")
aboutAction := walk.NewAction()
aboutAction.SetText("&About")
aboutAction.Triggered().Attach(func() {
walk.MsgBox(mw, "About", "Walk File Browser Example", walk.MsgBoxOK|walk.MsgBoxIconInformation)
})
helpMenu.Actions().Add(aboutAction)
splitter, _ := walk.NewSplitter(mw)
mw.treeView, _ = walk.NewTreeView(splitter)
mw.treeView.ItemExpanded().Attach(func(item *walk.TreeViewItem) {
children := item.Children()
if children.Len() == 1 && children.At(0).Text() == "" {
mw.populateTreeViewItem(item)
}
})
mw.treeView.SelectionChanged().Attach(func(old, new *walk.TreeViewItem) {
mw.selTvwItem = new
mw.showError(mw.fileInfoModel.ResetRows(pathForTreeViewItem(new)))
})
drives, _ := walk.DriveNames()
mw.treeView.SetSuspended(true)
for _, drive := range drives {
driveItem := newTreeViewItem(drive[:2])
mw.treeView.Items().Add(driveItem)
}
mw.treeView.SetSuspended(false)
mw.tableView, _ = walk.NewTableView(splitter)
mw.tableView.SetModel(mw.fileInfoModel)
mw.tableView.SetSingleItemSelection(true)
mw.tableView.CurrentIndexChanged().Attach(func() {
var url string
index := mw.tableView.CurrentIndex()
if index > -1 {
name := mw.fileInfoModel.items[index].Name
url = path.Join(pathForTreeViewItem(mw.selTvwItem), name)
}
mw.preview.SetURL(url)
})
mw.preview, _ = walk.NewWebView(splitter)
mw.SetMinMaxSize(walk.Size{600, 400}, walk.Size{})
mw.SetSize(walk.Size{800, 600})
mw.Show()
mw.Run()
}