-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbox.v
323 lines (288 loc) · 7.04 KB
/
listbox.v
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
module ui
import gx
import eventbus
type SelectionChangedFn = fn (voidptr, voidptr) // The second will be ListBox
const (
_item_height = 20
_col_list_bkgrnd = gx.white
_col_item_select = gx.light_blue
_col_border = gx.gray
_text_offset_y = 3
_text_offset_x = 5
)
pub struct ListBoxConfig {
mut:
x int
y int
width int
height int
callback SelectionChangedFn = SelectionChangedFn(0)
draw_lines bool // Draw a rectangle around every item?
col_border gx.Color = _col_border // Item and list border color
col_bkgrnd gx.Color = _col_list_bkgrnd // ListBox background color
col_selected gx.Color = _col_item_select // Selected item background color
item_height int = _item_height
text_offset_y int = _text_offset_y
id string // To use one callback for multiple ListBoxes
}
// Keys of the items map are IDs of the elements, values are text
pub fn listbox(c ListBoxConfig, items map[string]string) &ListBox {
mut list := &ListBox{
x: if c.draw_lines { c.x } else { c.x - 1 }
y: if c.draw_lines { c.y } else { c.y - 1 }
width: c.width
height: c.height
selection: -1
clbk: c.callback
draw_lines: c.draw_lines
col_bkgrnd: c.col_bkgrnd
col_selected: c.col_selected
col_border: c.col_border
item_height: c.item_height
text_offset_y: c.text_offset_y
id: c.id
ui: 0
}
for id, text in items {
list.append_item(id, text, 0)
}
return list
}
pub fn (mut list ListBox) add_item(id string, text string) {
list.append_item(id, text, list.get_draw_to(text))
}
pub struct ListBox {
pub mut:
height int
width int
x int
y int
parent Layout
ui &UI
items []ListItem
selection int = -1
draw_count int
clbk SelectionChangedFn = SelectionChangedFn(0)
focused bool
draw_lines bool
col_bkgrnd gx.Color = _col_list_bkgrnd
col_selected gx.Color = _col_item_select
col_border gx.Color = _col_border
item_height int = _item_height
text_offset_y int = _text_offset_y
id string
}
struct ListItem {
x int
id string
list &ListBox
mut:
y int
text string
draw_text string
}
fn (mut lb ListBox) get_draw_to(text string) int {
width := lb.ui.gg.text_width(text)
real_w := lb.width - _text_offset_x * 2
mut draw_to := text.len
if width >= real_w {
draw_to = int(f32(text.len) * (f32(real_w) / f32(width)))
for draw_to > 1 && lb.ui.gg.text_width(text[0..draw_to]) > real_w {
draw_to--
}
}
return draw_to
}
fn (mut lb ListBox) append_item(id string, text string, draw_to int) {
lb.items << ListItem{
x: lb.x
y: lb.y + lb.item_height * lb.items.len
id: id
text: text
list: lb
draw_text: text[0..draw_to]
}
}
pub fn (lb &ListBox) is_selected() bool {
if lb.selection < 0 || lb.selection >= lb.items.len {
return false
}
return true
}
// Returns the ID and the text of the selected item
pub fn (lb &ListBox) selected() ?(string, string) {
if !lb.is_selected() {
return error('Nothing is selected')
}
return lb.items[lb.selection].id, lb.items[lb.selection].text
}
// Returns the index of the selected item
pub fn (lb &ListBox) selected_inx() ?int {
if !lb.is_selected() {
return error('Nothing is selected')
}
return lb.selection
}
pub fn (mut lb ListBox) set_text(id string, text string) {
for i in 0 .. lb.items.len {
if lb.items[i].id == id {
lb.items[i].text = text
lb.items[i].draw_text = text[0..lb.get_draw_to(text)]
break
}
}
}
pub fn (mut lb ListBox) remove_item(id string) {
for i in 0 .. lb.items.len {
if lb.items[i].id == id {
lb.remove_inx(i)
break
}
}
}
pub fn (mut lb ListBox) remove_inx(i int) {
if i < 0 || i >= lb.items.len {
return
}
for j in (i + 1) .. lb.items.len {
lb.items[j].y -= lb.item_height
}
lb.items.delete(i)
}
pub fn (mut lb ListBox) clear() {
lb.items.clear()
lb.selection = -1
}
fn (mut lb ListBox) draw_item(li ListItem, selected bool) {
col := if selected { lb.col_selected } else { lb.col_bkgrnd }
lb.ui.gg.draw_rect(li.x, li.y, lb.width, lb.item_height, col)
lb.ui.gg.draw_text_def(li.x + _text_offset_x, li.y + lb.text_offset_y, li.draw_text)
if lb.draw_lines {
lb.ui.gg.draw_empty_rect(li.x, li.y, lb.width, lb.item_height, lb.col_border)
}
}
fn (mut lb ListBox) init(parent Layout) {
lb.parent = parent
lb.ui = parent.get_ui()
lb.draw_count = lb.height / lb.item_height
lb.text_offset_y = (lb.item_height - lb.ui.gg.text_height('W')) / 2
if lb.text_offset_y < 0 {
lb.text_offset_y = 0
}
for i, item in lb.items {
lb.items[i].draw_text = item.text[0..lb.get_draw_to(item.text)]
}
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, on_click, lb)
subscriber.subscribe_method(events.on_key_up, on_key_up, lb)
}
fn (mut lb ListBox) draw() {
lb.ui.gg.draw_rect(lb.x, lb.y, lb.width, lb.height, lb.col_bkgrnd)
if !lb.draw_lines {
lb.ui.gg.draw_empty_rect(lb.x, lb.y, lb.width + 1, lb.height + 1, lb.col_border)
}
for inx, item in lb.items {
if inx >= lb.draw_count {
break
}
lb.draw_item(item, inx == lb.selection)
}
}
fn (lb &ListBox) point_inside(x f64, y f64) bool {
return x >= lb.x && x <= lb.x + lb.width && y >= lb.y && y <= lb.y + lb.height
}
fn (li &ListItem) point_inside(x f64, y f64) bool {
return x >= li.x && x <= li.x + li.list.width && y >= li.y && y <= li.y + li.list.item_height
}
fn on_click(mut lb ListBox, e &MouseEvent, window &Window) {
if int(e.action) != 1 {
return
}
if !lb.point_inside(e.x, e.y) {
lb.unfocus()
return
}
lb.focus()
for inx, item in lb.items {
if inx >= lb.draw_count {
break
}
if item.point_inside(e.x, e.y) {
if lb.selection != inx {
lb.selection = inx
if lb.clbk != voidptr(0) {
lb.clbk(window.state, lb)
}
}
break
}
}
}
// Up and Down keys work on the list when it's focused
fn on_key_up(mut lb ListBox, e &KeyEvent, window &Window) {
if !lb.focused {
return
}
match e.key {
.down {
if lb.selection >= lb.draw_count - 1 {
return
}
if lb.selection >= lb.items.len - 1 {
return
}
lb.selection++
}
.up {
if lb.selection <= 0 {
return
}
lb.selection--
}
else {
return
}
}
if lb.clbk != voidptr(0) {
lb.clbk(window.state, lb)
}
}
fn (mut lb ListBox) set_pos(x int, y int) {
lb.x = x
lb.y = y
}
fn (mut lb ListBox) focus() {
lb.focused = true
}
fn (mut lb ListBox) unfocus() {
lb.focused = false
}
fn (lb &ListBox) is_focused() bool {
return lb.focused
}
fn (lb &ListBox) get_ui() &UI {
return lb.ui
}
fn (mut lb ListBox) unfocus_all() {
lb.focused = false
}
fn (mut lb ListBox) resize(width int, height int) {
lb.width = width
lb.height = height
lb.draw_count = lb.height / lb.item_height
}
fn (lb &ListBox) get_state() voidptr {
parent := lb.parent
return parent.get_state()
}
fn (lb &ListBox) get_subscriber() &eventbus.Subscriber {
parent := lb.parent
return parent.get_subscriber()
}
fn (lb &ListBox) size() (int, int) {
return lb.width, lb.height
}
fn (mut lb ListBox) propose_size(w int, h int) (int, int) {
lb.resize(w, h)
return lb.width, lb.height
}