This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_grid.go
105 lines (88 loc) · 2.35 KB
/
file_grid.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
package components
import (
"os"
"path/filepath"
"time"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type FileGrid struct {
app.Compo
Index []os.FileInfo
SelectedPath string
SetSelectedPath func(string)
CurrentPath string
SetCurrentPath func(string)
Standalone bool
hasInitiatedClick bool
}
func (c *FileGrid) Render() app.UI {
return app.Div().
Class(func() string {
classes := "pf-l-grid pf-m-gutter"
if c.Standalone {
classes += " pf-m-all-4-col-on-md pf-m-all-3-col-on-xl pf-u-py-md"
} else {
classes += " pf-m-all-4-col-on-sm pf-m-all-4-col-on-md pf-m-all-3-col-on-lg pf-m-all-3-col-on-xl"
}
return classes
}()).
Body(
app.Range(c.Index).Slice(func(i int) app.UI {
return app.Div().
Class("pf-l-grid__item pf-u-text-align-center").
Body(
app.Div().
Class(
func() string {
classes := "pf-c-card pf-m-plain pf-m-hoverable pf-m-selectable"
if c.SelectedPath == filepath.Join(c.CurrentPath, c.Index[i].Name()) {
classes += " pf-m-selected"
}
return classes
}()).
OnClick(func(ctx app.Context, e app.Event) {
newSelectedPath := filepath.Join(c.CurrentPath, c.Index[i].Name())
if c.SelectedPath == newSelectedPath {
// Handle double click
if c.hasInitiatedClick && c.Index[i].IsDir() {
c.SetCurrentPath(filepath.Join(c.CurrentPath, c.Index[i].Name()))
c.SetSelectedPath("")
return
}
newSelectedPath = ""
}
c.SetSelectedPath(newSelectedPath)
// Prepare for double click
c.hasInitiatedClick = true
time.AfterFunc(time.Second, func() {
c.hasInitiatedClick = false
})
}).
Aria("role", "button").
TabIndex(0).
Body(
app.Div().
Class("pf-c-card__body").
Body(
app.I().
Class(func() string {
classes := "fas pf-u-font-size-3xl"
if c.Index[i].IsDir() {
classes += " fa-folder"
} else {
classes += " fa-file-alt"
}
return classes
}()).
Aria("hidden", true),
),
app.Div().
Class("pf-c-card__footer").
Body(
app.Text(c.Index[i].Name()),
),
),
)
}),
)
}