/
governance_page.go
213 lines (181 loc) · 5.88 KB
/
governance_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
package governance
import (
"image"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/load"
"github.com/planetdecred/godcr/ui/page/components"
"github.com/planetdecred/godcr/ui/values"
)
const GovernancePageID = "Governance"
type Page struct {
*load.Load
multiWallet *dcrlibwallet.MultiWallet
tabCategoryList *decredmaterial.ClickableList
splashScreenInfoButton decredmaterial.IconButton
enableGovernanceBtn decredmaterial.Button
proposalsPage *ProposalsPage
consensusPage *ConsensusPage
selectedCategoryIndex int
changed bool
}
var governanceTabTitles = []string{"Proposals", "Consensus Changes"}
func NewGovernancePage(l *load.Load) *Page {
pg := &Page{
Load: l,
multiWallet: l.WL.MultiWallet,
selectedCategoryIndex: -1,
proposalsPage: NewProposalsPage(l),
consensusPage: NewConsensusPage(l),
tabCategoryList: l.Theme.NewClickableList(layout.Horizontal),
}
pg.tabCategoryList.IsHoverable = false
pg.initSplashScreenWidgets()
return pg
}
// OnNavigatedTo is called when the page is about to be displayed and
// may be used to initialize page features that are only relevant when
// the page is displayed.
// Part of the load.Page interface.
func (pg *Page) OnNavigatedTo() {
selectedCategory := pg.selectedCategoryIndex
if selectedCategory == -1 {
pg.selectedCategoryIndex = 0
}
if pg.selectedCategoryIndex == 1 {
pg.consensusPage.OnNavigatedTo()
} else {
pg.proposalsPage.OnNavigatedTo()
}
}
// OnNavigatedFrom is called when the page is about to be removed from
// the displayed window. This method should ideally be used to disable
// features that are irrelevant when the page is NOT displayed.
// NOTE: The page may be re-displayed on the app's window, in which case
// OnNavigatedTo() will be called again. This method should not destroy UI
// components unless they'll be recreated in the OnNavigatedTo() method.
// Part of the load.Page interface.
func (pg *Page) OnNavigatedFrom() {
pg.consensusPage.OnNavigatedFrom()
pg.proposalsPage.OnNavigatedFrom()
}
func (pg *Page) ID() string {
return GovernancePageID
}
func (pg *Page) HandleUserInteractions() {
for pg.enableGovernanceBtn.Clicked() {
go pg.consensusPage.FetchAgendas()
go pg.WL.MultiWallet.Politeia.Sync()
pg.proposalsPage.isSyncing = pg.multiWallet.Politeia.IsSyncing()
pg.WL.MultiWallet.SaveUserConfigValue(load.FetchProposalConfigKey, true)
}
if clicked, selectedItem := pg.tabCategoryList.ItemClicked(); clicked {
if pg.selectedCategoryIndex != selectedItem {
pg.selectedCategoryIndex = selectedItem
pg.changed = true
}
// call selected page OnNavigatedTo() only once
if pg.changed && pg.selectedCategoryIndex == 0 {
pg.proposalsPage.OnNavigatedTo()
} else if pg.changed && pg.selectedCategoryIndex == 1 {
pg.consensusPage.OnNavigatedTo()
}
pg.changed = false
}
// handle individual page user interactions
if pg.selectedCategoryIndex == 0 {
pg.proposalsPage.HandleUserInteractions()
} else {
pg.consensusPage.HandleUserInteractions()
}
}
func (pg *Page) Layout(gtx C) D {
if pg.WL.MultiWallet.ReadBoolConfigValueForKey(load.FetchProposalConfigKey, false) {
return components.UniformPadding(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(pg.layoutPageTopNav),
layout.Rigid(pg.layoutTabs),
layout.Rigid(pg.Theme.Separator().Layout),
layout.Flexed(1, func(gtx C) D {
return layout.Inset{Top: values.MarginPadding16}.Layout(gtx, func(gtx C) D {
return pg.switchTab(gtx, pg.selectedCategoryIndex)
})
}),
)
})
}
return components.UniformPadding(gtx, pg.splashScreenLayout)
}
func (pg *Page) switchTab(gtx C, selectedCategoryIndex int) D {
if selectedCategoryIndex == 0 {
return pg.proposalsPage.Layout(gtx)
}
return pg.consensusPage.Layout(gtx)
}
func (pg *Page) layoutTabs(gtx C) D {
var dims layout.Dimensions
return layout.Inset{
Top: values.MarginPadding20,
}.Layout(gtx, func(gtx C) D {
return pg.tabCategoryList.Layout(gtx, len(governanceTabTitles), func(gtx C, i int) D {
return layout.Stack{Alignment: layout.S}.Layout(gtx,
layout.Stacked(func(gtx C) D {
return layout.Inset{
Right: values.MarginPadding24,
Bottom: values.MarginPadding8,
}.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
lbl := pg.Theme.Label(values.TextSize16, governanceTabTitles[i])
lbl.Color = pg.Theme.Color.GrayText1
if pg.selectedCategoryIndex == i {
lbl.Color = pg.Theme.Color.Primary
dims = lbl.Layout(gtx)
}
return lbl.Layout(gtx)
})
})
}),
layout.Stacked(func(gtx C) D {
if pg.selectedCategoryIndex != i {
return D{}
}
tabHeight := gtx.Px(values.MarginPadding2)
tabRect := image.Rect(0, 0, dims.Size.X, tabHeight)
return layout.Inset{
Left: values.MarginPaddingMinus22,
}.Layout(gtx, func(gtx C) D {
paint.FillShape(gtx.Ops, pg.Theme.Color.Primary, clip.Rect(tabRect).Op())
return layout.Dimensions{
Size: image.Point{X: dims.Size.X, Y: tabHeight},
}
})
}),
)
})
})
}
func (pg *Page) layoutPageTopNav(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(pg.Theme.Icons.GovernanceActiveIcon.Layout24dp),
layout.Rigid(func(gtx C) D {
return layout.Inset{
Left: values.MarginPadding20,
}.Layout(gtx, func(gtx C) D {
txt := pg.Theme.Label(values.TextSize20, "Governance")
txt.Font.Weight = text.SemiBold
return txt.Layout(gtx)
})
}),
layout.Flexed(1, func(gtx C) D {
return layout.E.Layout(gtx, func(gtx C) D {
return D{}
//TODO: governance syncing functionality.
//TODO: Split wallet sync from governance
})
}),
)
}