-
Notifications
You must be signed in to change notification settings - Fork 47
/
proposal_details_page.go
321 lines (290 loc) · 9.3 KB
/
proposal_details_page.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package ui
import (
"encoding/base64"
"fmt"
"os/exec"
"runtime"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/utils"
"github.com/planetdecred/godcr/ui/values"
)
const (
PageProposalDetails = "ProposalDetails"
)
type proposalItemWidgets struct {
widgets []layout.Widget
clickables map[string]*widget.Clickable
}
type proposalDetails struct {
theme *decredmaterial.Theme
line *decredmaterial.Line
backButton decredmaterial.IconButton
descriptionCard decredmaterial.Card
proposalItems map[string]proposalItemWidgets
descriptionList *layout.List
selectedProposal **dcrlibwallet.Proposal
redirectIcon *widget.Image
redirectButton *widget.Clickable
redirectLabel decredmaterial.Label
voteBar decredmaterial.VoteBar
rejectedIcon *widget.Icon
successIcon *widget.Icon
}
func (win *Window) ProposalDetailsPage(common pageCommon) layout.Widget {
pg := &proposalDetails{
theme: common.theme,
line: common.theme.Line(),
backButton: common.theme.PlainIconButton(new(widget.Clickable), common.icons.navigationArrowBack),
descriptionCard: common.theme.Card(),
descriptionList: &layout.List{Axis: layout.Vertical},
selectedProposal: &win.selectedProposal,
redirectButton: new(widget.Clickable),
redirectIcon: common.icons.redirectIcon,
redirectLabel: common.theme.Body2("View on Politeia"),
voteBar: common.theme.VoteBar(common.icons.actionInfo, common.icons.imageBrightness1),
proposalItems: make(map[string]proposalItemWidgets),
rejectedIcon: common.icons.navigationCancel,
successIcon: common.icons.navigationCheck,
}
pg.backButton.Color = common.theme.Color.Hint
pg.backButton.Inset = layout.UniformInset(values.MarginPadding0)
pg.backButton.Size = values.MarginPadding30
pg.line.Color = pg.theme.Color.Background
return func(gtx C) D {
pg.handle(common)
return pg.Layout(gtx, common)
}
}
func (pg *proposalDetails) handle(common pageCommon) {
for pg.backButton.Button.Clicked() {
common.ChangePage(PageProposals)
}
for token := range pg.proposalItems {
for location, clickable := range pg.proposalItems[token].clickables {
if clickable.Clicked() {
pg.goToURL(location)
}
}
}
for pg.redirectButton.Clicked() {
proposal := *pg.selectedProposal
pg.goToURL("https://proposals.decred.org/proposals/" + proposal.Token)
}
}
func (pg *proposalDetails) goToURL(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Error(err)
}
}
func (pg *proposalDetails) layoutLinkButtons(gtx C) D {
proposal := *pg.selectedProposal
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return pg.backButton.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
return pg.theme.H6(truncateString(proposal.Name, 40)).Layout(gtx)
})
}),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
return material.Clickable(gtx, pg.redirectButton, func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding5}.Layout(gtx, func(gtx C) D {
return pg.redirectLabel.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return pg.redirectIcon.Layout(gtx)
}),
)
})
})
}),
)
}
func (pg *proposalDetails) getProposalText() []byte {
proposal := *pg.selectedProposal
desc, _ := base64.StdEncoding.DecodeString(proposal.IndexFile)
return desc
}
func (pg *proposalDetails) layoutProposalVoteBar(gtx C) D {
proposal := *pg.selectedProposal
yes := float32(proposal.YesVotes)
no := float32(proposal.NoVotes)
quorumPercent := float32(proposal.QuorumPercentage)
passPercentage := float32(proposal.PassPercentage)
eligibleTickets := float32(proposal.EligibleTickets)
return pg.voteBar.SetParams(yes, no, eligibleTickets, quorumPercent, passPercentage).LayoutWithLegend(gtx)
}
func (pg *proposalDetails) layoutInDiscussionTitle(gtx C, proposal *dcrlibwallet.Proposal) D {
return D{}
}
func (pg *proposalDetails) layoutVotingTitle(gtx C, proposal *dcrlibwallet.Proposal) D {
return D{}
}
func (pg *proposalDetails) layoutNormalTitle(gtx C, proposal *dcrlibwallet.Proposal) D {
var label decredmaterial.Label
var icon *widget.Icon
switch proposal.Category {
case dcrlibwallet.ProposalCategoryApproved:
label = pg.theme.Body2("Approved")
icon = pg.successIcon
icon.Color = pg.theme.Color.Success
case dcrlibwallet.ProposalCategoryRejected:
label = pg.theme.Body2("Rejected")
icon = pg.rejectedIcon
icon.Color = pg.theme.Color.Danger
case dcrlibwallet.ProposalCategoryAbandoned:
label = pg.theme.Body2("Abandoned")
}
timeagoLabel := pg.theme.Body2(timeAgo(proposal.Timestamp))
return layout.Flex{}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if icon == nil {
return D{}
}
return icon.Layout(gtx, unit.Dp(20))
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: values.MarginPadding5}.Layout(gtx, label.Layout)
}),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, timeagoLabel.Layout)
}),
)
}
func (pg *proposalDetails) layoutTitle(gtx C) D {
proposal := *pg.selectedProposal
return pg.descriptionCard.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding15).Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if proposal.Category == dcrlibwallet.ProposalCategoryPre {
return pg.layoutInDiscussionTitle(gtx, proposal)
} else if proposal.Category == dcrlibwallet.ProposalCategoryActive {
return pg.layoutVotingTitle(gtx, proposal)
}
return pg.layoutNormalTitle(gtx, proposal)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: values.MarginPadding10, Bottom: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return pg.line.Layout(gtx)
})
}),
layout.Rigid(func(gtx C) D {
if proposal.Category == dcrlibwallet.ProposalCategoryActive ||
proposal.Category == dcrlibwallet.ProposalCategoryApproved ||
proposal.Category == dcrlibwallet.ProposalCategoryRejected {
return pg.layoutProposalVoteBar(gtx)
}
return D{}
}),
)
})
})
}
func (pg *proposalDetails) layoutDescription(gtx C) D {
proposal := *pg.selectedProposal
dotLabel := pg.theme.H4(" . ")
userLabel := pg.theme.Body2(proposal.Username)
versionLabel := pg.theme.Body2("Version " + proposal.Version)
publishedLabel := pg.theme.Body2("Published " + timeAgo(proposal.PublishedAt))
updatedLabel := pg.theme.Body2("Updated " + timeAgo(proposal.Timestamp))
w := []layout.Widget{
func(gtx C) D {
return layout.Inset{
Top: values.MarginPadding5,
Bottom: values.MarginPadding10,
}.Layout(gtx, func(gtx C) D {
return pg.theme.H5(proposal.Name).Layout(gtx)
})
},
func(gtx C) D {
pg.line.Width = gtx.Constraints.Max.X
return pg.line.Layout(gtx)
},
func(gtx C) D {
return layout.Inset{
Top: values.MarginPadding15,
Bottom: values.MarginPadding15,
}.Layout(gtx, func(gtx C) D {
return layout.Flex{}.Layout(gtx,
layout.Rigid(userLabel.Layout),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: unit.Dp(-23)}.Layout(gtx, dotLabel.Layout)
}),
layout.Rigid(publishedLabel.Layout),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: unit.Dp(-23)}.Layout(gtx, dotLabel.Layout)
}),
layout.Rigid(versionLabel.Layout),
layout.Rigid(func(gtx C) D {
return layout.Inset{Top: unit.Dp(-23)}.Layout(gtx, dotLabel.Layout)
}),
layout.Rigid(updatedLabel.Layout),
)
})
},
func(gtx C) D {
pg.line.Width = gtx.Constraints.Max.X
return pg.line.Layout(gtx)
},
}
if item, ok := pg.proposalItems[proposal.Token]; ok {
w = append(w, item.widgets...)
} else {
r := utils.RenderMarkdown(gtx, pg.theme, pg.getProposalText())
proposalWidgets, proposalClickables := r.Layout()
pg.proposalItems[proposal.Token] = proposalItemWidgets{
widgets: proposalWidgets,
clickables: proposalClickables,
}
w = append(w, proposalWidgets...)
}
return pg.descriptionCard.Layout(gtx, func(gtx C) D {
return layout.UniformInset(values.MarginPadding15).Layout(gtx, func(gtx C) D {
return pg.descriptionList.Layout(gtx, len(w), func(gtx C, i int) D {
return layout.UniformInset(unit.Dp(0)).Layout(gtx, w[i])
})
})
})
}
func (pg *proposalDetails) Layout(gtx C, common pageCommon) D {
return common.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return pg.layoutLinkButtons(gtx)
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Top: unit.Dp(-30),
Bottom: values.MarginPadding10,
}.Layout(gtx, func(gtx C) D {
return pg.layoutTitle(gtx)
})
}),
layout.Rigid(func(gtx C) D {
return pg.layoutDescription(gtx)
}),
)
})
}