/
votebar_widget.go
303 lines (263 loc) · 8.33 KB
/
votebar_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package components
import (
"fmt"
"image"
"image/color"
"strconv"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/values"
)
// VoteBar widget implements voting stat for proposals.
// VoteBar shows the range/percentage of the yes votes and no votes against the total required.
type VoteBar struct {
*load.Load
yesVotes float32
noVotes float32
eligibleVotes float32
totalVotes int
requiredPercentage float32
passPercentage float32
token string
publishedAt int64
numComment int32
isDisableInfoTitle bool
yesColor color.NRGBA
noColor color.NRGBA
passTooltip *cryptomaterial.Tooltip
quorumTooltip *cryptomaterial.Tooltip
legendIcon *cryptomaterial.Icon
infoButton cryptomaterial.IconButton
BottomExtra layout.Widget
}
var voteBarThumbWidth = 2
func NewVoteBar(l *load.Load) *VoteBar {
vb := &VoteBar{
Load: l,
yesColor: l.Theme.Color.Success,
noColor: l.Theme.Color.Danger,
passTooltip: l.Theme.Tooltip(),
quorumTooltip: l.Theme.Tooltip(),
legendIcon: cryptomaterial.NewIcon(l.Theme.Icons.DotIcon),
}
_, vb.infoButton = SubpageHeaderButtons(l)
vb.infoButton.Inset = layout.Inset{}
vb.infoButton.Size = values.MarginPadding20
return vb
}
func (v *VoteBar) SetYesNoVoteParams(yesVotes, noVotes float32) *VoteBar {
v.yesVotes = yesVotes
v.noVotes = noVotes
v.totalVotes = int(yesVotes + noVotes)
return v
}
func (v *VoteBar) SetDisableInfoTitle(isDisable bool) *VoteBar {
v.isDisableInfoTitle = isDisable
return v
}
func (v *VoteBar) SetVoteValidityParams(eligibleVotes, requiredPercentage, passPercentage float32) *VoteBar {
v.eligibleVotes = eligibleVotes
v.passPercentage = passPercentage
v.requiredPercentage = requiredPercentage
return v
}
func (v *VoteBar) SetProposalDetails(numComment int32, publishedAt int64, token string) *VoteBar {
v.numComment = numComment
v.publishedAt = publishedAt
v.token = token
return v
}
func (v *VoteBar) SetBottomLayout(lay layout.Widget) *VoteBar {
v.BottomExtra = lay
return v
}
func (v *VoteBar) votebarLayout(gtx C) D {
var rW, rE int
r := gtx.Dp(values.MarginPadding4)
progressBarWidth := gtx.Constraints.Max.X
quorumRequirement := (v.requiredPercentage / 100) * v.eligibleVotes
yesVotes := 0
noVotes := 0
if quorumRequirement > 0 {
yesVotes = int((v.yesVotes / quorumRequirement) * 100)
noVotes = int((v.noVotes / quorumRequirement) * 100)
}
yesWidth := (progressBarWidth / 100) * yesVotes
noWidth := (progressBarWidth / 100) * noVotes
// progressScale represent the different progress bar layers
progressScale := func(width int, color color.NRGBA, layer int) layout.Dimensions {
maxHeight := values.MarginPadding17
rW, rE = 0, 0
if layer == 2 {
if width >= progressBarWidth {
rE = r
}
rW = r
} else if layer == 3 {
if v.yesVotes == 0 {
rW = r
}
rE = r
} else {
rE, rW = r, r
}
d := image.Point{X: width, Y: gtx.Dp(maxHeight)}
defer clip.RRect{
Rect: image.Rectangle{Max: image.Point{X: width, Y: gtx.Dp(maxHeight)}},
NE: rE, NW: rW, SE: rE, SW: rW,
}.Push(gtx.Ops).Pop()
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
return layout.Dimensions{
Size: d,
}
}
if yesWidth > progressBarWidth || noWidth > progressBarWidth || (yesWidth+noWidth) > progressBarWidth {
totalVotes := float32(v.totalVotes)
yes := (v.yesVotes / totalVotes) * 100
no := (v.noVotes / totalVotes) * 100
noWidth = int((float32(progressBarWidth) / 100) * no)
yesWidth = int((float32(progressBarWidth) / 100) * yes)
rE = r
} else if yesWidth < 0 {
yesWidth, noWidth = 0, 0
}
return layout.Stack{Alignment: layout.W}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return progressScale(progressBarWidth, v.Theme.Color.Gray2, 1)
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if yesWidth == 0 {
return D{}
}
return progressScale(yesWidth, v.yesColor, 2)
}),
layout.Rigid(func(gtx C) D {
if noWidth == 0 {
return D{}
}
return progressScale(noWidth, v.noColor, 3)
}),
)
}),
layout.Stacked(v.requiredYesVotesIndicator),
)
}
func (v *VoteBar) votesIndicatorTooltip(gtx C, r image.Rectangle, tipPos float32) {
insetLeft := tipPos - float32(voteBarThumbWidth/2) - 205
inset := layout.Inset{Left: unit.Dp(insetLeft), Top: values.MarginPadding25}
v.passTooltip.Layout(gtx, r, inset, func(gtx C) D {
txt := values.StringF(values.StrVoteTooltip, int(v.passPercentage))
return v.Theme.Caption(txt).Layout(gtx)
})
}
func (v *VoteBar) requiredYesVotesIndicator(gtx C) D {
thumbLeftPos := (v.passPercentage / 100) * float32(gtx.Constraints.Max.X)
rect := image.Rectangle{
Min: image.Point{
X: int(thumbLeftPos - float32(voteBarThumbWidth/2)),
Y: -1,
},
Max: image.Point{
X: int(thumbLeftPos) + voteBarThumbWidth,
Y: gtx.Dp(24),
},
}
defer clip.Rect(rect).Push(gtx.Ops).Pop()
paint.Fill(gtx.Ops, v.Theme.Color.Gray3)
v.votesIndicatorTooltip(gtx, rect, thumbLeftPos)
return D{
Size: rect.Max,
}
}
func (v *VoteBar) Layout(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding5, Bottom: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
yesLabel := v.Theme.Body1(values.String(values.StrYes) + values.String(values.StrColon))
yesLabel.TextSize = v.ConvertTextSize(values.TextSize14)
return v.layoutIconAndText(gtx, yesLabel, int(v.yesVotes), v.yesColor)
}),
layout.Rigid(func(gtx C) D {
noLabel := v.Theme.Body1(values.String(values.StrNo) + values.String(values.StrColon))
noLabel.TextSize = v.ConvertTextSize(values.TextSize14)
return v.layoutIconAndText(gtx, noLabel, int(v.noVotes), v.noColor)
}),
layout.Flexed(1, func(gtx C) D {
if v.isDisableInfoTitle || v.IsMobileView() {
return D{}
}
return layout.E.Layout(gtx, func(gtx C) D {
lb := v.Theme.Body1(values.StringF(values.StrVotes, v.totalVotes))
lb.TextSize = v.ConvertTextSize(values.TextSize14)
lb.Font.Weight = font.SemiBold
return lb.Layout(gtx)
})
}),
)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding5}.Layout(gtx, v.votebarLayout)
}),
layout.Rigid(func(gtx C) D {
if !v.IsMobileView() {
return D{}
}
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.E.Layout(gtx, func(gtx C) D {
lb := v.Theme.Body1(values.StringF(values.StrVotes, v.totalVotes))
lb.TextSize = v.ConvertTextSize(values.TextSize14)
lb.Font.Weight = font.SemiBold
return lb.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
if v.BottomExtra == nil {
return D{}
}
return layout.Inset{Top: values.MarginPadding5}.Layout(gtx, v.BottomExtra)
}),
)
})
}),
)
}
func (v *VoteBar) layoutIconAndText(gtx C, lbl cryptomaterial.Label, count int, col color.NRGBA) D {
return layout.Inset{Right: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Right: values.MarginPadding5, Top: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
v.legendIcon.Color = col
return v.legendIcon.Layout(gtx, values.MarginPadding10)
})
}),
layout.Rigid(func(gtx C) D {
lbl.Font.Weight = font.SemiBold
return lbl.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
count := float64(count)
percentage := 0.0
if v.totalVotes != 0 && count != 0 {
percentage = (count / float64(v.totalVotes)) * 100
}
percentageStr := strconv.FormatFloat(percentage, 'f', 1, 64) + "%"
countStr := strconv.FormatFloat(count, 'f', 0, 64)
lb := v.Theme.Body1(fmt.Sprintf("%s (%s)", countStr, percentageStr))
lb.TextSize = v.ConvertTextSize(values.TextSize14)
return lb.Layout(gtx)
}),
)
})
}