-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard_widget.go
135 lines (115 loc) · 2.86 KB
/
card_widget.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
package main
import (
"strconv"
"time"
"cogentcore.org/core/colors"
"cogentcore.org/core/core"
"cogentcore.org/core/cursors"
"cogentcore.org/core/events"
"cogentcore.org/core/math32"
"cogentcore.org/core/styles"
"cogentcore.org/core/styles/abilities"
"cogentcore.org/core/styles/states"
"cogentcore.org/core/styles/units"
"cogentcore.org/core/tree"
)
// this required to go generate to work
// https://github.com/cogentcore/core/discussions/1243
var _ tree.Node = nil
type Card struct {
core.Frame
Heading string
SubHeading string
Data interface{}
}
func (cd *Card) Init() {
cd.Frame.Init()
cd.Frame.OnClick(cd.onClickEvent)
tree.AddChild(cd, func(w *core.Text) {
w.SetType(core.TextHeadlineSmall)
w.Updater(func() {
w.SetText(cd.Heading)
})
w.Styler(func (s *styles.Style) {
s.SetNonSelectable()
s.Font.Weight = styles.WeightBold
})
})
tree.AddChild(cd, func(w *core.Text) {
w.Updater(func() {
w.SetText(cd.SubHeading)
})
w.Styler(func (s *styles.Style) {
s.SetNonSelectable()
})
})
cd.Styler(func (s *styles.Style) {
s.Direction = styles.Column
s.Grow.Set(1, 0)
s.Border.Width.Set(units.Dp(4))
s.Border.Color.Set(colors.Scheme.Outline)
s.SetAbilities(true, abilities.Selectable)
s.Cursor = cursors.Pointer
})
}
func (cd *Card) showSelection() {
vTimer := time.NewTimer(100 * time.Millisecond)
<-vTimer.C
cd.AsyncLock()
cd.SetState(false, states.Focused)
cd.Update()
cd.AsyncUnlock()
}
func (cd *Card) onClickEvent(e events.Event) {
cd.SetState(true, states.Focused)
cd.Update()
go cd.showSelection()
}
// ----------------------------------------------------------------------
type CardData struct {
Data any
Heading string
SubHeading string
}
type CardList struct {
core.Frame
OnClick func(v CardData)
cardDataSlice []CardData
}
func (this *CardList) Init() {
this.Frame.Init()
this.Frame.Styler(func (s *styles.Style) {
s.Direction = styles.Column
s.Grow.Set(1, 1)
s.Overflow.Set(styles.OverflowAuto)
})
this.Frame.Updater(func() {
this.Frame.ScrollDimToContentStart(math32.Y)
})
this.Frame.Maker(func(p *tree.Plan) {
for i := range this.cardDataSlice {
tree.AddAt(p, strconv.Itoa(i), func(w *Card) {
w.Updater(func () {
vData := this.cardDataSlice[i]
w.SetHeading(vData.Heading)
w.SetSubHeading(vData.SubHeading)
w.SetData(vData.Data)
})
w.OnClick(func(e events.Event) {
this.OnClick(this.cardDataSlice[i])
})
})
}
})
}
func (this *CardList) Add(pId any, pHeading string, pSubHeading string) {
this.cardDataSlice = append(this.cardDataSlice, CardData{Data: pId, Heading: pHeading, SubHeading: pSubHeading})
}
func (this *CardList) Length() int {
return len(this.cardDataSlice)
}
func (this *CardList) Clear() {
this.cardDataSlice = nil
this.Update()
}
// ----------------------------------------------------------------------