-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.go
132 lines (109 loc) · 2.91 KB
/
page.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
package dotui
import (
"image/color"
"gioui.org/f32"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"github.com/kreativka/dot-ui/desktop"
"github.com/sahilm/fuzzy"
)
type mainPage struct {
env *Env
}
func (m *mainPage) Layout(gtx *layout.Context) {
th := m.env.theme
entries := m.env.currList
dims := f32.Point{
X: float32(gtx.Constraints.Width.Max),
Y: th.TextSize.V + 5,
}
m.filterEntries(m.env.editor.Text())
entries.handleResize(gtx.Constraints.Height.Max, int(dims.Y))
head := layout.Rigid(func() {
layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func() {
th.Body("Run: ").Layout(gtx)
}),
layout.Rigid(func() {
th.Editor("start typing...").Layout(gtx, m.env.editor)
}),
)
})
list := layout.Rigid(func() {
entries.Reset()
for entries.Next() {
layout.Stack{}.Layout(gtx, layout.Stacked(func() {
name := entries.Value()
switch {
case entries.IsCurrentHighlighted():
fillBg(gtx, dims, th.Color.BgCurr)
th.BodyOdd(name).Layout(gtx)
case entries.IsCurrentEven():
fillBg(gtx, dims, th.Color.BgEven)
th.Body(name).Layout(gtx)
default:
th.Body(name).Layout(gtx)
}
}))
op.TransformOp{}.Offset(f32.Point{Y: dims.Y}).Add(gtx.Ops)
}
})
layout.Flex{Axis: layout.Vertical}.Layout(gtx,
head, list,
)
}
func (m *mainPage) filterEntries(pattern string) {
entries := m.env.currList
// No filter
if entries.filter == pattern {
return
}
// Filter was cleared, reset to initial state
if pattern == "" && entries.Len() != len(m.env.entries) {
entries.filter = pattern
entries.names = m.env.entries
entries.list = desktop.Flatten(entries.names)
return
}
var list []*desktop.Entry
if len(pattern) > len(entries.filter) {
list = entries.names
} else {
list = m.env.entries
entries.list = desktop.Flatten(list)
}
res := filterMatches(list, entries.list, pattern)
if len(res) >= len(m.env.currList.names) ||
(len(res) < len(m.env.currList.names) && entries.start > len(res)) ||
entries.start+entries.limit >= len(res) {
entries.start = 0
}
if entries.curr >= len(res) && len(res) > 0 {
entries.curr = len(res) - 1
}
if entries.curr > entries.start+entries.limit {
entries.curr = entries.start + entries.limit - 1
}
entries.filter = pattern
entries.list = desktop.Flatten(res)
entries.names = res
}
func filterMatches(entries []*desktop.Entry, lists [][]string, pattern string) []*desktop.Entry {
rv := make([]*desktop.Entry, 0, len(entries))
seen := make(map[int]bool, len(entries))
for _, elems := range lists {
matches := fuzzy.Find(pattern, elems)
for _, match := range matches {
if !seen[match.Index] {
rv = append(rv, entries[match.Index])
seen[match.Index] = true
}
}
}
return rv
}
func fillBg(gtx *layout.Context, maxDims f32.Point, color color.RGBA) {
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{Rect: f32.Rectangle{Max: maxDims}}.Add(gtx.Ops)
}