-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown.go
121 lines (93 loc) · 2.29 KB
/
dropdown.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
package glui
import (
)
// dropdown is a flat button, with a menu showing below it when clicked,
// the button stays depressed while the menu is showing
func NewDropdown(align Align, items []MenuItemConfig) *Button {
e := NewStickyFlatButton()
menuItemMaker := func() []*MenuItem {
items_ := make([]*MenuItem, len(items))
for i, item := range items {
items_[i] = newMenuItemFromConfig(item)
}
return items_
}
m := e.Root.Menu
e.OnClick(func() {
if m.IsOwnedBy(e) {
m.Hide()
e.Unstick()
} else {
bh := 30
m.ClearChildren()
items := menuItemMaker()
menuW := 0
for _, item := range items {
item.On("click", func(evt *Event) {
e.Unstick()
})
m.AddItem(item.H(bh), true, false)
itemW, _ := item.GetSize()
if itemW > menuW {
menuW = itemW
}
}
switch align {
case START:
m.ShowAt(
e, 0.0, 1.0, menuW,
)
case CENTER:
m.ShowAt(
e, 0.5, 1.0, menuW,
)
case END:
m.ShowAt(
e, 1.0, 1.0, menuW,
)
default:
panic("unhandled align for Dropdown")
}
}
})
e.On("mousebuttonoutsidemenu", func(evt *Event) {
m.Hide()
e.Unstick()
if e.IsHit(evt.X, evt.Y) {
evt.stopPropagation = true
}
})
// keyup event is triggered before keypress, stop propagation so events are actually handled by keypress instead
e.On("keyup", func(evt *Event) {
if m.IsOwnedBy(e) {
evt.stopPropagation = true
}
})
e.On("keypress", func(evt *Event) {
if m.IsOwnedBy(e) {
evt.stopPropagation = true
if evt.IsEscape() {
m.Hide()
e.Unstick()
} else if evt.IsReturnOrSpace() {
if m.SelectedIndex() == -1 {
m.Hide()
e.Unstick()
} else {
m.ClickSelected()
e.Unstick()
}
} else if evt.Key == "down" {
m.SelectNext()
} else if evt.Key == "up" {
m.SelectPrev()
}
}
})
return e
}
func NewIconDropdown(iconName string, iconSize int, align Align, items []MenuItemConfig) *Button {
e := NewDropdown(align, items)
e.A(NewHor(CENTER, CENTER, 0).H(-1).A(NewIcon(iconName, iconSize)))
return e
}