forked from faiface/gui
/
browser.go
177 lines (159 loc) · 4.21 KB
/
browser.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
package main
import (
"image"
"image/color"
"image/draw"
"os"
"path/filepath"
"github.com/faiface/gui"
"github.com/faiface/gui/win"
"golang.org/x/image/math/fixed"
)
func Browser(env gui.Env, theme *Theme, dir string, cd <-chan string, view chan<- string) {
reload := func(dir string) (names []string, lineHeight int, namesImage *image.RGBA) {
names = nil
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if path == dir {
return nil
}
rel, err := filepath.Rel(dir, path)
if err != nil {
return nil
}
if info.IsDir() {
names = append(names, rel+string(filepath.Separator))
return filepath.SkipDir
}
names = append(names, rel)
return nil
})
var images []image.Image
for _, name := range names {
images = append(images, MakeTextImage(name, theme.Face, theme.Text))
}
const inset = 4
var width int
for _, img := range images {
if img.Bounds().Dx() > width {
width = img.Bounds().Inset(-inset).Dx()
}
}
metrics := theme.Face.Metrics()
lineHeight = (metrics.Height + 2*fixed.I(inset)).Ceil()
height := lineHeight * len(names)
namesImage = image.NewRGBA(image.Rect(0, 0, width+2*inset, height+2*inset))
for i := range images {
r := image.Rect(
0, lineHeight*i,
width, lineHeight*(i+1),
)
DrawLeftCentered(namesImage, r.Inset(inset), images[i], draw.Over)
}
return names, lineHeight, namesImage
}
redraw := func(r image.Rectangle, selected int, position image.Point, lineHeight int, namesImage image.Image) func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
draw.Draw(drw, r, &image.Uniform{theme.Background}, image.ZP, draw.Src)
draw.Draw(drw, r, namesImage, position, draw.Over)
if selected >= 0 {
highlightR := image.Rect(
namesImage.Bounds().Min.X,
namesImage.Bounds().Min.Y+lineHeight*selected,
namesImage.Bounds().Max.X,
namesImage.Bounds().Min.Y+lineHeight*(selected+1),
)
highlightR = highlightR.Sub(position).Add(r.Min)
draw.DrawMask(
drw, highlightR.Intersect(r),
&image.Uniform{theme.Highlight}, image.ZP,
&image.Uniform{color.Alpha{64}}, image.ZP,
draw.Over,
)
}
return r
}
}
names, lineHeight, namesImage := reload(dir)
var (
r image.Rectangle
position = image.ZP
selected = -1
)
for {
select {
case path := <-cd:
if filepath.IsAbs(path) {
dir = path
} else {
dir = filepath.Join(dir, path)
}
names, lineHeight, namesImage = reload(dir)
position = image.ZP
selected = -1
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
case e, ok := <-env.Events():
if !ok {
close(env.Draw())
return
}
switch e := e.(type) {
case gui.Resize:
r = e.Rectangle
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
case win.MoDown:
if !e.Point.In(r) {
continue
}
click := e.Point.Sub(r.Min).Add(position)
i := click.Y / lineHeight
if i < 0 || i >= len(names) {
continue
}
if selected == i {
func() {
path := filepath.Join(dir, names[selected])
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return
}
if info.IsDir() {
dir = path
names, lineHeight, namesImage = reload(dir)
position = image.ZP
selected = -1
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
} else {
view <- path
}
}()
} else {
selected = i
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
}
case win.MoScroll:
newP := position.Sub(e.Point.Mul(16))
if newP.X > namesImage.Bounds().Max.X-r.Dx() {
newP.X = namesImage.Bounds().Max.X - r.Dx()
}
if newP.Y > namesImage.Bounds().Max.Y-r.Dy() {
newP.Y = namesImage.Bounds().Max.Y - r.Dy()
}
if newP.X < namesImage.Bounds().Min.X {
newP.X = namesImage.Bounds().Min.X
}
if newP.Y < namesImage.Bounds().Min.Y {
newP.Y = namesImage.Bounds().Min.Y
}
if newP != position {
position = newP
env.Draw() <- redraw(r, selected, position, lineHeight, namesImage)
}
}
}
}
}