-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_style.go
370 lines (303 loc) · 8.48 KB
/
tree_style.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
package filetree
import (
"errors"
"fmt"
"image/color"
"log"
"slices"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/oligo/gioview/menu"
"github.com/oligo/gioview/misc"
"github.com/oligo/gioview/navi"
"github.com/oligo/gioview/theme"
"github.com/oligo/gioview/view"
"golang.org/x/exp/shiny/materialdesign/icons"
)
type (
C = layout.Context
D = layout.Dimensions
)
var (
folderIcon, _ = widget.NewIcon(icons.FileFolder)
folderOpenIcon, _ = widget.NewIcon(icons.FileFolderOpen)
fileIcon, _ = widget.NewIcon(icons.ActionDescription)
)
var _ navi.NavSection = (*FileTreeNav)(nil)
var _ navi.NavItem = (*EntryNavItem)(nil)
type FileTreeNav struct {
title string
root *navi.NavItemStyle
}
type EntryNavItem struct {
state *EntryNode
menuOptionFunc MenuOptionFunc
onSelectFunc OnSelectFunc
parent navi.NavItem
children []navi.NavItem
nameEditor *widget.Editor
expaned bool
needSync bool
isEditing bool
// FolderIcon *widget.Icon
// FolderOpenIcon *widget.Icon
// FileIcon *widget.Icon
}
type MenuOptionFunc func(gtx C, item *EntryNavItem) [][]menu.MenuOption
type OnSelectFunc func(gtx C, item *EntryNode) view.Intent
// Construct a FileTreeNav object that loads files and folders from rootDir. The skipFolders
// parameter allows you to specify folder name prefixes to exclude from the navigation.
func NewFileTreeNav(drawer *navi.NavDrawer, title string, navRoot *EntryNavItem) *FileTreeNav {
return &FileTreeNav{
title: title,
root: navi.NewNavItem(navRoot, drawer),
}
}
func (tn *FileTreeNav) Attach(drawer *navi.NavDrawer) {
// NOOP
}
func (tn *FileTreeNav) Title() string {
return tn.title
}
func (tn *FileTreeNav) Layout(gtx C, th *theme.Theme) D {
return tn.root.Layout(gtx, th)
}
// Construct a file tree object that loads files and folders from rootDir.
// `skipFolders` allows you to specify folder name prefixes to exclude from the navigation.
// `menuOptionFunc` is used to define the operations allowed by context menu(use right click to active it).
// `onSelectFunc` defines what action to take when a navigable item is clicked (files or folders).
func NewEntryNavItem(rootDir string, skipFolders []string, menuOptionFunc MenuOptionFunc, onSelectFunc OnSelectFunc) *EntryNavItem {
tree, err := NewFileTree(rootDir, skipFolders, true)
if err != nil {
log.Fatal(err)
}
//tree.Print()
if err != nil {
log.Println("load file tree failed", err)
return nil
}
return &EntryNavItem{
parent: nil,
state: tree,
menuOptionFunc: menuOptionFunc,
onSelectFunc: onSelectFunc,
expaned: true,
}
}
func (eitem *EntryNavItem) Icon() *widget.Icon {
if eitem.state.Kind == FolderNode {
if eitem.expaned {
return folderOpenIcon
}
return folderIcon
}
return fileIcon
}
func (eitem *EntryNavItem) OnSelect(gtx C) view.Intent {
eitem.expaned = !eitem.expaned
if eitem.expaned {
eitem.needSync = true
}
// move focus to the nav item clicked.
if !gtx.Focused(eitem.nameEditor) {
gtx.Execute(key.FocusCmd{Tag: eitem})
}
if eitem.state.Kind == FileNode && eitem.onSelectFunc != nil {
return eitem.onSelectFunc(gtx, eitem.state)
}
return view.Intent{}
}
func (eitem *EntryNavItem) Layout(gtx layout.Context, th *theme.Theme, textColor color.NRGBA) D {
eitem.update(gtx)
return layout.Flex{
Axis: layout.Horizontal,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Flexed(1, func(gtx C) D {
if eitem.isEditing {
return eitem.layoutEditArea(gtx, th)
}
lb := material.Label(th.Theme, th.TextSize, eitem.state.Name())
lb.Color = textColor
return lb.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
if eitem.state.Kind == FileNode {
return D{}
}
return layout.Inset{Left: unit.Dp(4)}.Layout(gtx, func(gtx C) D {
label := material.Label(th.Theme, th.TextSize, fmt.Sprintf("%d", len(eitem.state.Children)))
label.Color = misc.WithAlpha(textColor, 0xb6)
label.TextSize = th.TextSize * 0.7
return label.Layout(gtx)
})
}),
)
}
func (eitem *EntryNavItem) layoutEditArea(gtx C, th *theme.Theme) D {
macro := op.Record(gtx.Ops)
dims := material.Editor(th.Theme, eitem.nameEditor, "").Layout(gtx)
call := macro.Stop()
rect := clip.Rect{Max: dims.Size}
paint.FillShape(gtx.Ops, misc.WithAlpha(th.Fg, 0x60),
clip.Stroke{
Path: rect.Path(),
Width: float32(gtx.Dp(1)),
}.Op(),
)
call.Add(gtx.Ops)
return dims
}
func (eitem *EntryNavItem) IsDir() bool {
return eitem.state.Kind == FolderNode
}
func (eitem *EntryNavItem) SetMenuOptions(menuOptionFunc MenuOptionFunc) {
eitem.menuOptionFunc = menuOptionFunc
}
func (eitem *EntryNavItem) ContextMenuOptions(gtx C) ([][]menu.MenuOption, bool) {
if eitem.menuOptionFunc != nil {
return eitem.menuOptionFunc(gtx, eitem), false
}
return nil, false
}
func (eitem *EntryNavItem) Children() []navi.NavItem {
if eitem.state.Kind == FileNode {
return nil
}
if !eitem.expaned {
return nil
}
if eitem.children == nil {
eitem.buildChildren()
}
if eitem.needSync {
eitem.state.Refresh()
eitem.buildChildren()
eitem.needSync = false
}
return eitem.children
}
func (eitem *EntryNavItem) buildChildren() {
eitem.children = eitem.children[:0]
for _, c := range eitem.state.Children {
eitem.children = append(eitem.children, &EntryNavItem{
parent: eitem,
state: c,
menuOptionFunc: eitem.menuOptionFunc,
onSelectFunc: eitem.onSelectFunc,
expaned: false,
needSync: false,
})
}
}
// StartEditing inits and focused on the editor to accept user input.
func (eitem *EntryNavItem) StartEditing(gtx C) {
if eitem.nameEditor == nil {
eitem.nameEditor = &widget.Editor{
SingleLine: true,
MaxLen: 256, // windows has max filename length 256
Submit: true,
}
}
eitem.nameEditor.SetText(eitem.state.Name())
gtx.Execute(key.FocusCmd{Tag: eitem.nameEditor})
eitem.nameEditor.SetCaret(0, len(eitem.nameEditor.Text()))
eitem.isEditing = true
}
// IsEditing is used to indicate the current node is being edited.
func (eitem *EntryNavItem) IsEditing(gtx C) bool {
return eitem.nameEditor != nil && eitem.isEditing
}
// update process eitem edit events. name is updated when one of the following situation
// occurred:
// 1. user pressed Enter key after input
// 2. current node's editor lost focus but text changed.
func (eitem *EntryNavItem) update(gtx C) {
if eitem.nameEditor == nil {
return
}
saveName := false
//check if user pressed Enter key after input
for {
event, ok := eitem.nameEditor.Update(gtx)
if !ok {
break
}
switch event.(type) {
case widget.SubmitEvent:
// enter pressed
saveName = true
}
}
// or editor lost focus but name is changed
if eitem.IsEditing(gtx) && !gtx.Focused(eitem.nameEditor) && eitem.state.Name() != eitem.nameEditor.Text() {
saveName = true
}
if saveName {
err := eitem.state.UpdateName(eitem.nameEditor.Text())
if err != nil {
log.Println("err: ", err)
}
eitem.isEditing = false
}
}
// Create file or subfolder under the current folder.
// File or subfolder is inserted at the beginning of the children.
func (eitem *EntryNavItem) CreateChild(gtx C, kind NodeKind) error {
if eitem.state.Kind == FileNode {
return nil
}
var err error
if kind == FileNode {
err = eitem.state.AddChild("new file", FileNode)
} else {
err = eitem.state.AddChild("new folder", FolderNode)
}
if err != nil {
// TODO: use modal to show the error if user provided one.
log.Println(err)
return err
}
//eitem.StartEditing(gtx)
child := &EntryNavItem{
parent: eitem,
state: eitem.state.Children[0],
menuOptionFunc: eitem.menuOptionFunc,
onSelectFunc: eitem.onSelectFunc,
expaned: false,
needSync: false,
}
eitem.children = slices.Insert[[]navi.NavItem, navi.NavItem](eitem.children, 0, child)
// focus the child input
child.StartEditing(gtx)
return nil
}
func (eitem *EntryNavItem) Remove() error {
if eitem.parent == nil {
return errors.New("cannot remove root dir/file")
}
err := eitem.state.Delete(true)
if err != nil {
return err
}
(eitem.parent).(*EntryNavItem).needSync = true
return nil
}
// File or folder name of this node
func (eitem *EntryNavItem) Name() string {
return eitem.state.Name()
}
// File or folder path of this node
func (eitem *EntryNavItem) Path() string {
return eitem.state.Path
}
// EntryNode kind of this node
func (eitem *EntryNavItem) Kind() NodeKind {
return eitem.state.Kind
}