-
Notifications
You must be signed in to change notification settings - Fork 76
/
sample07_collections.nim
58 lines (43 loc) · 2.63 KB
/
sample07_collections.nim
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
import strutils
import sample_registry
import nimx / [ collection_view, popup_button, slider, text_field, timer, view ]
type CollectionsSampleView = ref object of View
method init(v: CollectionsSampleView, r: Rect) =
procCall v.View.init(r)
setTimeout 0.2, proc() =
let collection = @["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"]
let collectionView = newCollectionView(newRect(0, 0, 450, 250), newSize(50, 50), LayoutDirection.LeftToRight)
collectionView.numberOfItems = proc(): int =
return collection.len()
collectionView.viewForItem = proc(i: int): View =
result = newView(newRect(0, 0, 100, 100))
discard newLabel(result, newPoint(0, 0), newSize(50, 50), collection[i])
result.backgroundColor = newColor(1.0, 0.0, 0.0, 0.8)
collectionView.itemSize = newSize(50, 50)
collectionView.backgroundColor = newColor(1.0, 1.0, 1.0, 1.0)
collectionView.updateLayout()
v.addSubview(collectionView)
discard newLabel(v, newPoint(470, 5), newSize(100, 10), "Layout direction:")
let popupDirectionRule = newPopupButton(v, newPoint(470, 20), newSize(100, 20), ["LeftToRight", "TopDown"])
popupDirectionRule.onAction do():
collectionView.layoutDirection = popupDirectionRule.selectedIndex().LayoutDirection
collectionView.updateLayout()
discard newLabel(v, newPoint(470, 45), newSize(100, 10), "Layout width:")
let popupLayoutWidth = newPopupButton(v, newPoint(470, 60), newSize(100, 20), ["Auto", "1", "2", "3", "4"])
popupLayoutWidth.onAction do():
collectionView.layoutWidth = popupLayoutWidth.selectedIndex()
collectionView.updateLayout()
discard newLabel(v, newPoint(470, 85), newSize(100, 10), "Item size:")
let popupItemSize = newPopupButton(v, newPoint(470, 100), newSize(100, 20), ["50", "100", "150"])
popupItemSize.onAction do():
collectionView.itemSize = newSize((50 + 50 * popupItemSize.selectedIndex()).Coord, (50 + 50 * popupItemSize.selectedIndex()).Coord)
collectionView.updateLayout()
discard newLabel(v, newPoint(470, 125), newSize(100, 10), "Offset:")
let offsetField = newTextField(newRect(newPoint(470, 140), newSize(100, 20)))
offsetField.continuous = true
offsetField.onAction do():
var offset = try: parseFloat(offsetField.text) except: 0.0
collectionView.offset = offset
collectionView.updateLayout()
v.addSubview(offsetField)
registerSample(CollectionsSampleView, "Collections")