-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombobox.go
More file actions
86 lines (72 loc) · 1.76 KB
/
combobox.go
File metadata and controls
86 lines (72 loc) · 1.76 KB
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
package gui
type ComboBoxChangeCallback func(index int)
type ComboBox struct {
Label
options []string
selectedIndex int
popup IPopup
open bool
onChangeCallback ComboBoxChangeCallback
}
func NewComboBox(options []string, selectedIndex int, changeCallback ...ComboBoxChangeCallback) *ComboBox {
c := &ComboBox{}
widgetInit(c)
c.style = CurrentGui().Theme().ComboBox
c.options = options
c.selectedIndex = selectedIndex
c.text = options[selectedIndex]
if len(changeCallback) == 1 {
c.onChangeCallback = changeCallback[0]
}
c.initPopup()
return c
}
func (c *ComboBox) initPopup() {
model := NewStringListModel(c.options)
c.popup = NewPopupMenu(model, func(source interface{}, index int) {
if source == c.popup {
c.SetSelectedIndex(index)
}
})
}
func (c *ComboBox) SelectedIndex() int {
return c.selectedIndex
}
func (c *ComboBox) SetSelectedIndex(index int) {
if index == c.selectedIndex {
return
}
c.selectedIndex = index
c.text = c.options[index]
c.fireChangeEvent(index)
}
func (c *ComboBox) SetOnChangeCallback(callback ComboBoxChangeCallback) {
c.onChangeCallback = callback
}
func (c *ComboBox) Open() bool {
return c.popup.Visible()
}
func (c *ComboBox) fireChangeEvent(selectedIndex int) {
if c.onChangeCallback != nil {
c.onChangeCallback(selectedIndex)
}
}
func (c *ComboBox) OnMouseEvent(event MouseEvent) IWidget {
if event.Type == MouseEventButton {
if event.Button != MouseButtonLeft {
return nil
}
if event.Action == EventActionPress {
if c.popup.Visible() {
CurrentGui().DismissPopup()
} else {
absPosition := c.AbsolutePosition()
c.popup.Widget().SetLeft(absPosition.X())
c.popup.Widget().SetTop(absPosition.Y())
CurrentGui().ShowPopup(c.popup)
}
return c
}
}
return nil
}