/
list.go
535 lines (453 loc) · 11.9 KB
/
list.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gui
import (
"github.com/g3n/engine/window"
)
// List represents a list GUI element
type List struct {
ItemScroller // Embedded scroller
styles *ListStyles // Pointer to styles
single bool // Single selection flag (default is true)
focus bool // has keyboard focus
dropdown bool // this is used as dropdown
keyNext window.Key // Code of key to select next item
keyPrev window.Key // Code of key to select previous item
}
// ListItem encapsulates each item inserted into the list
type ListItem struct {
Panel // Container panel
item IPanel // Original item
selected bool // Item selected flag
highlighted bool // Item highlighted flag
padLeft float32 // Additional left padding
list *List // Pointer to list
}
// ListStyles encapsulates a set of styles for the list and item.
type ListStyles struct {
Scroller *ItemScrollerStyles
Item *ListItemStyles
}
// ListItemStyles contains one ListItemStyle for each possible item state.
type ListItemStyles struct {
Normal ListItemStyle
Over ListItemStyle
Selected ListItemStyle
Highlighted ListItemStyle
SelHigh ListItemStyle
}
// ListItemStyle contains the styling of a list item.
type ListItemStyle BasicStyle
// OnListItemResize is the identifier of the event dispatched when a ListItem's child panel is resized
const OnListItemResize = "gui.OnListItemResize"
// NewVList creates and returns a pointer to a new vertical list panel
// with the specified dimensions
func NewVList(width, height float32) *List {
return newList(true, width, height)
}
// NewHList creates and returns a pointer to a new horizontal list panel
// with the specified dimensions
func NewHList(width, height float32) *List {
return newList(false, width, height)
}
// newList creates and returns a pointer to a new list panel
// with the specified orientation and dimensions
func newList(vert bool, width, height float32) *List {
li := new(List)
li.initialize(vert, width, height)
return li
}
func (li *List) initialize(vert bool, width, height float32) {
li.styles = &StyleDefault().List
li.single = true
li.ItemScroller.initialize(vert, width, height)
li.ItemScroller.SetStyles(li.styles.Scroller)
li.ItemScroller.adjustItem = true
li.ItemScroller.Subscribe(OnKeyDown, li.onKeyEvent)
li.ItemScroller.Subscribe(OnKeyRepeat, li.onKeyEvent)
if vert {
li.keyNext = window.KeyDown
li.keyPrev = window.KeyUp
} else {
li.keyNext = window.KeyRight
li.keyPrev = window.KeyLeft
}
li.update()
}
// SetSingle sets the single/multiple selection flag of the list
func (li *List) SetSingle(state bool) {
li.single = state
}
// Single returns the current state of the single/multiple selection flag
func (li *List) Single() bool {
return li.single
}
// SetStyles set the listr styles overriding the default style
func (li *List) SetStyles(s *ListStyles) {
li.styles = s
li.ItemScroller.SetStyles(li.styles.Scroller)
li.update()
}
// Add add a list item at the end of the list
func (li *List) Add(item IPanel) *ListItem {
return li.InsertAt(len(li.items), item)
}
// InsertAt inserts a list item at the specified position
// Returs true if the item was successfully inserted
func (li *List) InsertAt(pos int, item IPanel) *ListItem {
litem := newListItem(li, item)
li.ItemScroller.InsertAt(pos, litem)
litem.Panel.Subscribe(OnMouseDown, litem.onMouse)
litem.Panel.Subscribe(OnCursorEnter, litem.onCursor)
return litem
}
// RemoveAt removes the list item from the specified position
func (li *List) RemoveAt(pos int) IPanel {
// Remove the list item from the internal scroller
pan := li.ItemScroller.RemoveAt(pos)
litem := pan.(*ListItem)
// Remove item from the list item children and disposes of the list item panel
item := litem.item
litem.Remove(item)
litem.Dispose()
return item
}
// Remove removes the specified item from the list
func (li *List) Remove(item IPanel) {
for p, curr := range li.items {
if curr.(*ListItem).item == item {
li.RemoveAt(p)
return
}
}
}
// ItemAt returns the list item at the specified position
func (li *List) ItemAt(pos int) IPanel {
item := li.ItemScroller.ItemAt(pos)
if item == nil {
return nil
}
litem := item.(*ListItem)
return litem.item
}
// ItemPosition returns the position of the specified item in
// the list or -1 if not found
func (li *List) ItemPosition(item IPanel) int {
for pos := 0; pos < len(li.items); pos++ {
if li.items[pos].(*ListItem).item == item {
return pos
}
}
return -1
}
// Selected returns list with the currently selected items
func (li *List) Selected() []IPanel {
sel := []IPanel{}
for _, item := range li.items {
litem := item.(*ListItem)
if litem.selected {
sel = append(sel, litem.item)
}
}
return sel
}
// SetSelected selects or unselects the specified item
func (li *List) SetSelected(item IPanel, state bool) {
for _, curr := range li.items {
litem := curr.(*ListItem)
if litem.item == item {
litem.SetSelected(state)
li.update()
li.Dispatch(OnChange, nil)
return
}
}
}
// SelectPos selects or unselects the item at the specified position
func (li *List) SelectPos(pos int, state bool) {
if pos < 0 || pos >= len(li.items) {
return
}
litem := li.items[pos].(*ListItem)
if litem.selected == state {
return
}
litem.SetSelected(state)
li.update()
li.Dispatch(OnChange, nil)
}
// SetItemPadLeftAt sets the additional left padding for this item
// It is used mainly by the tree control
func (li *List) SetItemPadLeftAt(pos int, pad float32) {
if pos < 0 || pos >= len(li.items) {
return
}
litem := li.items[pos].(*ListItem)
litem.padLeft = pad
litem.update()
}
// selNext selects or highlights the next item, if possible
func (li *List) selNext(sel bool, update bool) *ListItem {
// Checks for empty list
if len(li.items) == 0 {
return nil
}
// Find currently selected item
var pos int
if sel {
pos = li.selected()
} else {
pos = li.highlighted()
}
var newItem *ListItem
newSel := true
// If no item found, returns first.
if pos < 0 {
newItem = li.items[0].(*ListItem)
if sel {
newItem.SetSelected(true)
} else {
newItem.SetHighlighted(true)
}
} else {
item := li.items[pos].(*ListItem)
// Item is not the last, get next
if pos < len(li.items)-1 {
newItem = li.items[pos+1].(*ListItem)
if sel {
item.SetSelected(false)
newItem.SetSelected(true)
} else {
item.SetHighlighted(false)
newItem.SetHighlighted(true)
}
if !li.ItemVisible(pos + 1) {
li.ScrollDown()
}
// Item is the last, don't change
} else {
newItem = item
newSel = false
}
}
if update {
li.update()
}
if sel && newSel {
li.Dispatch(OnChange, nil)
}
return newItem
}
// selPrev selects or highlights the next item, if possible
func (li *List) selPrev(sel bool, update bool) *ListItem {
// Check for empty list
if len(li.items) == 0 {
return nil
}
// Find first selected item
var pos int
if sel {
pos = li.selected()
} else {
pos = li.highlighted()
}
var newItem *ListItem
newSel := true
// If no item found, returns first.
if pos < 0 {
newItem = li.items[0].(*ListItem)
if sel {
newItem.SetSelected(true)
} else {
newItem.SetHighlighted(true)
}
} else {
item := li.items[pos].(*ListItem)
if pos == 0 {
newItem = item
newSel = false
} else {
newItem = li.items[pos-1].(*ListItem)
if sel {
item.SetSelected(false)
newItem.SetSelected(true)
} else {
item.SetHighlighted(false)
newItem.SetHighlighted(true)
}
if (pos - 1) < li.first {
li.ScrollUp()
}
}
}
if update {
li.update()
}
if sel && newSel {
li.Dispatch(OnChange, nil)
}
return newItem
}
// selected returns the position of first selected item
func (li *List) selected() (pos int) {
for pos, item := range li.items {
if item.(*ListItem).selected {
return pos
}
}
return -1
}
// highlighted returns the position of first highlighted item
func (li *List) highlighted() (pos int) {
for pos, item := range li.items {
if item.(*ListItem).highlighted {
return pos
}
}
return -1
}
// onKeyEvent receives subscribed key events for the list
func (li *List) onKeyEvent(evname string, ev interface{}) {
kev := ev.(*window.KeyEvent)
// Dropdown mode
if li.dropdown {
switch kev.Key {
case li.keyNext:
li.selNext(true, true)
case li.keyPrev:
li.selPrev(true, true)
case window.KeyEnter:
li.SetVisible(false)
default:
return
}
return
}
// Listbox mode single selection
if li.single {
switch kev.Key {
case li.keyNext:
li.selNext(true, true)
case li.keyPrev:
li.selPrev(true, true)
default:
return
}
return
}
// Listbox mode multiple selection
switch kev.Key {
case li.keyNext:
li.selNext(false, true)
case li.keyPrev:
li.selPrev(false, true)
case window.KeySpace:
pos := li.highlighted()
if pos >= 0 {
litem := li.items[pos].(*ListItem)
li.setSelection(litem, !litem.selected, true, true)
}
default:
return
}
}
// setSelection sets the selected state of the specified item
// updating the visual appearance of the list if necessary
func (li *List) setSelection(litem *ListItem, state bool, force bool, dispatch bool) {
Manager().SetKeyFocus(li)
// If already at this state, nothing to do
if litem.selected == state && !force {
return
}
litem.SetSelected(state)
// If single selection, deselects all other items
if li.single {
for _, curr := range li.items {
if curr.(*ListItem) != litem {
curr.(*ListItem).SetSelected(false)
curr.(*ListItem).SetHighlighted(false)
}
}
}
li.update()
if dispatch {
li.Dispatch(OnChange, nil)
}
}
// update updates the visual state the list and its items
func (li *List) update() {
// Update the list items styles
for _, item := range li.items {
item.(*ListItem).update()
}
}
//
// ListItem methods
//
func newListItem(list *List, item IPanel) *ListItem {
litem := new(ListItem)
litem.Panel.Initialize(litem, 0, 0)
litem.item = item
litem.list = list
litem.Panel.Add(item)
litem.SetContentWidth(item.GetPanel().Width())
litem.SetContentHeight(item.GetPanel().Height())
// If this list item is resized, sends event to its child panel
litem.Subscribe(OnResize, func(evname string, ev interface{}) {
item.GetPanel().Dispatch(OnListItemResize, nil)
})
litem.update()
return litem
}
// onMouse receives mouse button events over the list item
func (litem *ListItem) onMouse(evname string, ev interface{}) {
if litem.list.single {
litem.list.setSelection(litem, true, true, true)
} else {
litem.list.setSelection(litem, !litem.selected, true, true)
}
if litem.list.dropdown {
litem.list.SetVisible(false)
}
}
// onCursor receives subscribed cursor events over the list item
func (litem *ListItem) onCursor(evname string, ev interface{}) {
if litem.list.dropdown {
litem.list.setSelection(litem, true, true, false)
return
}
}
// SetSelected sets this item selected state
func (litem *ListItem) SetSelected(state bool) {
litem.selected = state
//litem.item.SetSelected2(state)
}
// SetHighlighted sets this item selected state
func (litem *ListItem) SetHighlighted(state bool) {
litem.highlighted = state
//litem.item.SetHighlighted2(state)
}
// updates the list item visual style accordingly to its current state
func (litem *ListItem) update() {
list := litem.list
if litem.selected && !litem.highlighted {
litem.applyStyle(&list.styles.Item.Selected)
return
}
if !litem.selected && litem.highlighted {
litem.applyStyle(&list.styles.Item.Highlighted)
return
}
if litem.selected && litem.highlighted {
litem.applyStyle(&list.styles.Item.SelHigh)
return
}
litem.applyStyle(&list.styles.Item.Normal)
}
// applyStyle applies the specified style to this ListItem
func (litem *ListItem) applyStyle(s *ListItemStyle) {
styleCopy := s.PanelStyle
styleCopy.Padding.Left += litem.padLeft
litem.Panel.ApplyStyle(&styleCopy)
}