-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.go
164 lines (137 loc) · 3.13 KB
/
menu.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
package main
import (
"image"
"iter"
"slices"
"deedles.dev/kawa/draw"
"deedles.dev/wlr"
"deedles.dev/ximage/geom"
)
var (
menuItemInset = geom.Pt(WindowBorder, WindowBorder)
)
type Menu struct {
items []*MenuItem
bounds []geom.Rect[float64]
prev *MenuItem
}
func NewMenu(items ...*MenuItem) *Menu {
return NewMenuFromSeq(slices.Values(items), len(items))
}
func NewMenuFromSeq(items iter.Seq[*MenuItem], numitems int) *Menu {
m := Menu{
items: make([]*MenuItem, 0, numitems),
bounds: make([]geom.Rect[float64], 0, numitems),
}
for item := range items {
m.add(item)
}
m.updateBounds(false)
return &m
}
func (m *Menu) Items() iter.Seq2[*MenuItem, geom.Rect[float64]] {
return func(yield func(*MenuItem, geom.Rect[float64]) bool) {
for i, item := range m.items {
if !yield(item, m.bounds[i]) {
return
}
}
}
}
func (m *Menu) Len() int {
return len(m.items)
}
func (m *Menu) updateBounds(shrink bool) {
if shrink {
for i := range m.bounds {
m.bounds[i] = geom.Rect[float64]{
Max: geom.PConv[float64](m.items[i].Size().Add(menuItemInset)),
}
}
}
geom.ArrangeVerticalStack(m.bounds)
}
func (m *Menu) Item(i int) *MenuItem {
return m.items[i]
}
func (m *Menu) Bounds() (b geom.Rect[float64]) {
return geom.Rect[float64]{
Min: m.bounds[0].Min,
Max: m.bounds[len(m.bounds)-1].Max,
}
}
func (m *Menu) Select(item *MenuItem) {
i := slices.Index(m.items, item)
if i < 0 {
return
}
item.OnSelect()
m.prev = item
}
func (m *Menu) Prev() *MenuItem {
i := slices.Index(m.items, m.prev)
if i < 0 {
return nil
}
return m.prev
}
func (m *Menu) ItemAt(p geom.Point[float64]) *MenuItem {
for i, ib := range m.bounds {
if p.In(ib) {
return m.items[i]
}
}
return nil
}
func (m *Menu) ItemBounds(item *MenuItem) geom.Rect[float64] {
i := slices.Index(m.items, item)
if i < 0 {
return geom.Rect[float64]{}
}
return m.bounds[i]
}
func (m *Menu) add(item *MenuItem) {
m.items = append(m.items, item)
m.bounds = append(m.bounds, geom.Rect[float64]{
Max: geom.PConv[float64](item.Size().Add(menuItemInset)),
})
}
func (m *Menu) Add(item *MenuItem) {
m.add(item)
m.updateBounds(false)
}
func (m *Menu) Remove(item *MenuItem) {
i := slices.Index(m.items, item)
m.items = slices.Delete(m.items, i, i+1)
m.bounds = slices.Delete(m.bounds, i, i+1)
m.updateBounds(true)
}
type MenuItem struct {
OnSelect func()
active wlr.Texture
inactive wlr.Texture
}
func NewMenuItem(active, inactive wlr.Texture) *MenuItem {
if (active.Width() != inactive.Width()) || (active.Height() != inactive.Height()) {
panic("active and inactive sizes do no match")
}
return &MenuItem{
active: active,
inactive: inactive,
}
}
func NewTextMenuItem(renderer wlr.Renderer, text string) *MenuItem {
return NewMenuItem(
draw.CreateTextTexture(renderer, image.White, text),
draw.CreateTextTexture(renderer, image.Black, text),
)
}
func (item *MenuItem) Size() geom.Point[int] {
return geom.Rt(0, 0, item.active.Width(), item.active.Height()).
Union(geom.Rt(0, 0, item.inactive.Width(), item.inactive.Height())).
Size()
}
func (item *MenuItem) Release() {
item.active.Destroy()
item.inactive.Destroy()
}