-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistview.go
More file actions
107 lines (94 loc) · 3.01 KB
/
listview.go
File metadata and controls
107 lines (94 loc) · 3.01 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gui
import (
"github.com/maxfish/GoNativeUI-Core/utils"
)
const (
DefaultRowWidth = 150
DefaultRowHeight = 20
DefaultVisibleRows = 10
)
type ItemSelectedCallback func(source interface{}, index int)
type ListView struct {
Widget
dataModel ListModel
offset float32
visibleRows int
hoveredIndex int
selectedIndex int
itemSelectedCallback ItemSelectedCallback
}
func NewListView(dataModel ListModel, itemSelectedCallback ...ItemSelectedCallback) *ListView {
l := &ListView{}
widgetInit(l)
l.style = CurrentGui().Theme().ListView
l.visibleRows = DefaultVisibleRows
l.hoveredIndex = -1
l.selectedIndex = -1
l.dataModel = dataModel
if len(itemSelectedCallback) == 1 {
l.itemSelectedCallback = itemSelectedCallback[0]
}
return l
}
func (l *ListView) Offset() int { return int(l.offset) }
func (l *ListView) DataModel() ListModel { return l.dataModel }
func (l *ListView) SetDataModel(dataModel ListModel) { l.dataModel = dataModel }
func (l *ListView) SelectedIndex() int { return l.selectedIndex }
func (l *ListView) HoveredIndex() int { return l.hoveredIndex }
func (l *ListView) SetSelectedIndex(index int) { l.selectedIndex = index }
func (l *ListView) SetOnItemSelectedCallback(f ItemSelectedCallback) {
l.itemSelectedCallback = f
}
func (l *ListView) fireSelectionChangedEvent(index int) {
if l.itemSelectedCallback != nil {
l.itemSelectedCallback(l, index)
}
}
func (l *ListView) Measure() {
l.computeContentSize()
l.measuredWidth = l.contentWidth + l.style.Padding.Left + l.style.Padding.Right
l.measuredHeight = utils.MinI(l.visibleRows*l.dataModel.ItemHeight(l), l.contentHeight) + l.style.Padding.Top + l.style.Padding.Bottom
l.measuredFlex = l.flex
}
func (l *ListView) computeContentSize() {
if l.dataModel != nil {
l.contentWidth = l.dataModel.ItemsMaxWidth(l)
l.contentHeight = l.dataModel.NumItems(l) * l.dataModel.ItemHeight(l)
} else {
l.contentWidth = DefaultRowWidth
l.contentHeight = l.visibleRows * DefaultRowHeight
}
}
func (l *ListView) hoveredIndexFromCoords(x, y float32) {
l.hoveredIndex = utils.ClampI((int(y+l.offset))/l.dataModel.ItemHeight(l), 0, l.dataModel.NumItems(l)-1)
}
func (l *ListView) OnMouseEvent(event MouseEvent) IWidget {
if event.Type == MouseEventPosition {
// Updates the hovered item index
l.hoveredIndexFromCoords(event.X, event.Y)
return l
}
if event.Type == MouseEventWheel {
l.offset = utils.Clamp(l.offset-event.ScrollY, 0, float32(l.contentHeight-l.InnerBounds().H))
l.hoveredIndexFromCoords(event.X, event.Y)
return l
}
if event.Type == MouseEventButton {
if event.Button != MouseButtonLeft {
return nil
}
if event.Action == EventActionPress {
if l.dataModel != nil {
newIndex := l.hoveredIndex
if newIndex != l.selectedIndex {
l.selectedIndex = newIndex
l.fireSelectionChangedEvent(l.selectedIndex)
}
}
return l
} else if event.Action == EventActionRelease {
return l
}
}
return nil
}