/
info_modal.go
454 lines (383 loc) · 11.8 KB
/
info_modal.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package modal
import (
"image/color"
"gioui.org/font"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget/material"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
"github.com/crypto-power/cryptopower/ui/load"
"github.com/crypto-power/cryptopower/ui/values"
)
type InfoModal struct {
*load.Load
*cryptomaterial.Modal
dialogIcon *cryptomaterial.Image
dialogTitle string
subtitle string
customTemplate []layout.Widget
customWidget layout.Widget
// positiveButtonText string
positiveButtonClicked ClickFunc
btnPositive cryptomaterial.Button
btnPositiveWidth unit.Dp
// negativeButtonText string
negativeButtonClicked func()
btnNegative cryptomaterial.Button
checkbox cryptomaterial.CheckBoxStyle
mustBeChecked bool
titleAlignment, subtitleAlignment, btnAlignment layout.Direction
materialLoader material.LoaderStyle
titleTextAlignment, subTileTextAlignment text.Alignment
isCancelable bool
isLoading bool
}
// ButtonType is the type of button in modal.
type ButtonType uint8
// ClickFunc defines the positive button click method signature.
// Adding the InfoModal parameter allow reference of the parent info modal
// qualities inside the positive button function call.
type ClickFunc func(isChecked bool, im *InfoModal) bool
const (
// CustomBtn defines the bare metal custom modal button type.
CustomBtn ButtonType = iota
// DangerBtn defines the default danger modal button type
DangerBtn
// InfoBtn defines the default info modal button type
InfoBtn
)
// NewCustomModal returns a modal that can be customized.
func NewCustomModal(l *load.Load) *InfoModal {
return newInfoModalWithKey(l, "info_modal", InfoBtn)
}
// NewSuccessModal returns the default success modal UI component.
func NewSuccessModal(l *load.Load, title string, clicked ClickFunc) *InfoModal {
icon := l.Theme.Icons.SuccessIcon
md := newModal(l, title, icon, clicked)
md.SetContentAlignment(layout.Center, layout.Center, layout.Center)
return md
}
// NewErrorModal returns the default error modal UI component.
func NewErrorModal(l *load.Load, title string, clicked ClickFunc) *InfoModal {
icon := l.Theme.Icons.FailedIcon
md := newModal(l, title, icon, clicked)
md.SetContentAlignment(layout.Center, layout.Center, layout.Center)
return md
}
// DefaultClickFunc returns the default click function satisfying the positive
// btn click function.
func DefaultClickFunc() ClickFunc {
return func(isChecked bool, in *InfoModal) bool {
return true
}
}
func newModal(l *load.Load, title string, icon *cryptomaterial.Image, clicked ClickFunc) *InfoModal {
info := newInfoModalWithKey(l, "info_modal", InfoBtn)
info.positiveButtonClicked = clicked
info.btnPositiveWidth = values.MarginPadding100
info.dialogIcon = icon
info.dialogTitle = title
info.titleAlignment = layout.Center
info.btnAlignment = layout.Center
return info
}
func newInfoModalWithKey(l *load.Load, key string, btnPositiveType ButtonType) *InfoModal {
in := &InfoModal{
Load: l,
Modal: l.Theme.ModalFloatTitle(key, l.IsMobileView()),
btnNegative: l.Theme.OutlineButton(""),
isCancelable: true,
isLoading: false,
btnAlignment: layout.E,
btnPositiveWidth: 0,
}
in.btnPositive = getPositiveButtonType(l, btnPositiveType)
in.btnPositive.Font.Weight = font.Medium
in.btnNegative.Font.Weight = font.Medium
// Set the default click functions
in.positiveButtonClicked = DefaultClickFunc()
in.negativeButtonClicked = func() {}
in.materialLoader = material.Loader(l.Theme.Base)
return in
}
func getPositiveButtonType(l *load.Load, btnType ButtonType) cryptomaterial.Button {
switch btnType {
case InfoBtn:
return l.Theme.Button(values.String(values.StrOk))
case DangerBtn:
return l.Theme.DangerButton(values.String(values.StrOk))
default:
return l.Theme.OutlineButton(values.String(values.StrOk))
}
}
func (in *InfoModal) OnResume() {}
func (in *InfoModal) OnDismiss() {}
func (in *InfoModal) SetCancelable(min bool) *InfoModal {
in.isCancelable = min
return in
}
func (in *InfoModal) SetContentAlignment(title, subTitle, btn layout.Direction) *InfoModal {
in.titleAlignment = title
in.subtitleAlignment = subTitle
switch title {
case layout.Center:
in.titleTextAlignment = text.Middle
case layout.E:
in.titleTextAlignment = text.End
default:
in.titleTextAlignment = text.Start
}
switch subTitle {
case layout.Center:
in.subTileTextAlignment = text.Middle
case layout.E:
in.subTileTextAlignment = text.End
default:
in.subTileTextAlignment = text.Start
}
in.btnAlignment = btn
return in
}
func (in *InfoModal) Icon(icon *cryptomaterial.Image) *InfoModal {
in.dialogIcon = icon
return in
}
func (in *InfoModal) CheckBox(checkbox cryptomaterial.CheckBoxStyle, mustBeChecked bool) *InfoModal {
in.checkbox = checkbox
in.mustBeChecked = mustBeChecked // determine if the checkbox must be selected to proceed
return in
}
func (in *InfoModal) setLoading(loading bool) {
in.isLoading = loading
in.Modal.SetDisabled(loading)
}
func (in *InfoModal) Title(title string) *InfoModal {
in.dialogTitle = title
return in
}
func (in *InfoModal) Body(subtitle string) *InfoModal {
in.subtitle = subtitle
return in
}
func (in *InfoModal) SetPositiveButtonText(text string) *InfoModal {
in.btnPositive.Text = text
return in
}
func (in *InfoModal) SetPositiveButtonCallback(clicked ClickFunc) *InfoModal {
in.positiveButtonClicked = clicked
return in
}
func (in *InfoModal) PositiveButtonStyle(background, text color.NRGBA) *InfoModal {
in.btnPositive.Background, in.btnPositive.Color = background, text
return in
}
func (in *InfoModal) PositiveButtonWidth(width unit.Dp) *InfoModal {
in.btnPositiveWidth = width
return in
}
func (in *InfoModal) SetNegativeButtonText(text string) *InfoModal {
in.btnNegative.Text = text
return in
}
func (in *InfoModal) SetNegativeButtonCallback(clicked func()) *InfoModal {
in.negativeButtonClicked = clicked
return in
}
func (in *InfoModal) NegativeButtonStyle(background, text color.NRGBA) *InfoModal {
in.btnNegative.Background, in.btnNegative.Color = background, text
return in
}
// for backwards compatibility.
func (in *InfoModal) SetupWithTemplate(template string) *InfoModal {
title := in.dialogTitle
subtitle := in.subtitle
var customTemplate []layout.Widget
switch template {
case TransactionDetailsInfoTemplate:
title = values.String(values.StrHowToCopy)
customTemplate = transactionDetailsInfo(in.Theme)
case SignMessageInfoTemplate:
customTemplate = signMessageInfo(in.Theme)
case VerifyMessageInfoTemplate:
customTemplate = verifyMessageInfo(in.Theme)
case PrivacyInfoTemplate:
title = values.String(values.StrUseMixer)
customTemplate = privacyInfo(in.Load)
case SetupMixerInfoTemplate:
customTemplate = setupMixerInfo(in.Theme)
case WalletBackupInfoTemplate:
customTemplate = backupInfo(in.Theme)
case SecurityToolsInfoTemplate:
customTemplate = securityToolsInfo(in.Theme)
case SourceModalInfoTemplate:
customTemplate = sourceModalInfo(in.Theme)
case TotalValueInfoTemplate:
customTemplate = totalValueInfo(in.Theme)
case BondStrengthInfoTemplate:
customTemplate = bondStrengthInfo(in.Theme)
}
in.dialogTitle = title
in.subtitle = subtitle
in.customTemplate = customTemplate
return in
}
func (in *InfoModal) UseCustomWidget(layout layout.Widget) *InfoModal {
in.customWidget = layout
return in
}
// KeysToHandle returns an expression that describes a set of key combinations
// that this modal wishes to capture. The HandleKeyPress() method will only be
// called when any of these key combinations is pressed.
// Satisfies the load.KeyEventHandler interface for receiving key events.
func (in *InfoModal) KeysToHandle() key.Set {
return cryptomaterial.AnyKey(key.NameReturn, key.NameEnter, key.NameEscape)
}
// HandleKeyPress is called when one or more keys are pressed on the current
// window that match any of the key combinations returned by KeysToHandle().
// Satisfies the load.KeyEventHandler interface for receiving key events.
func (in *InfoModal) HandleKeyPress(_ *key.Event) {
in.btnPositive.Click()
in.ParentWindow().Reload()
}
func (in *InfoModal) Handle() {
for in.btnPositive.Clicked() {
if in.isLoading {
return
}
isChecked := false
if in.checkbox.CheckBox != nil {
isChecked = in.checkbox.CheckBox.Value
}
in.setLoading(true)
go func() {
if in.positiveButtonClicked(isChecked, in) {
in.Dismiss()
return
}
in.setLoading(false)
}()
}
for in.btnNegative.Clicked() {
if !in.isLoading {
in.Dismiss()
in.negativeButtonClicked()
}
}
if in.Modal.BackdropClicked(in.isCancelable) {
if !in.isLoading {
in.Dismiss()
in.negativeButtonClicked()
}
}
if in.checkbox.CheckBox != nil {
if in.mustBeChecked {
in.btnNegative.SetEnabled(in.checkbox.CheckBox.Value)
}
}
}
func (in *InfoModal) Layout(gtx layout.Context) D {
icon := func(gtx C) D {
if in.dialogIcon == nil {
return D{}
}
return layout.Inset{Top: values.MarginPadding10}.Layout(gtx, func(gtx C) D {
return layout.Center.Layout(gtx, func(gtx C) D {
return in.dialogIcon.LayoutSize(gtx, values.MarginPadding50)
})
})
}
checkbox := func(gtx C) D {
if in.checkbox.CheckBox == nil {
return D{}
}
return layout.Inset{Top: values.MarginPaddingMinus5, Left: values.MarginPaddingMinus5}.Layout(gtx, func(gtx C) D {
in.checkbox.TextSize = values.TextSize14
if in.IsMobileView() {
in.checkbox.TextSize = values.TextSize12
}
in.checkbox.Color = in.Theme.Color.GrayText1
in.checkbox.IconColor = in.Theme.Color.Gray2
if in.checkbox.CheckBox.Value {
in.checkbox.IconColor = in.Theme.Color.Primary
}
return in.checkbox.Layout(gtx)
})
}
subtitle := func(gtx C) D {
text := in.Theme.Body1(in.subtitle)
text.Alignment = in.subTileTextAlignment
text.Color = in.Theme.Color.GrayText2
return layout.Inset{Bottom: values.MarginPadding8}.Layout(gtx, text.Layout)
}
var w []layout.Widget
// Every section of the dialog is optional
if in.dialogIcon != nil {
w = append(w, icon)
}
if in.dialogTitle != "" {
w = append(w, in.titleLayout())
}
if in.subtitle != "" {
w = append(w, subtitle)
}
if in.customTemplate != nil {
w = append(w, in.customTemplate...)
}
if in.checkbox.CheckBox != nil {
w = append(w, checkbox)
}
if in.customWidget != nil {
w = append(w, in.customWidget)
}
if in.btnNegative.Text != "" || in.btnPositive.Text != "" {
w = append(w, in.actionButtonsLayout())
}
return in.Modal.Layout(gtx, w)
}
func (in *InfoModal) titleLayout() layout.Widget {
return func(gtx C) D {
t := in.Theme.H6(in.dialogTitle)
if in.IsMobileView() {
t.TextSize = values.TextSize16
}
t.Alignment = in.titleTextAlignment
t.Font.Weight = font.SemiBold
return in.titleAlignment.Layout(gtx, t.Layout)
}
}
func (in *InfoModal) actionButtonsLayout() layout.Widget {
btnTextSize := values.TextSize16
if in.IsMobileView() {
btnTextSize = values.TextSize14
}
in.btnNegative.TextSize = btnTextSize
in.btnPositive.TextSize = btnTextSize
return func(gtx C) D {
return in.btnAlignment.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if in.btnNegative.Text == "" || in.isLoading {
return D{}
}
gtx.Constraints.Max.X = gtx.Dp(values.MarginPadding250)
return layout.Inset{Right: values.MarginPadding5}.Layout(gtx, in.btnNegative.Layout)
}),
layout.Rigid(func(gtx C) D {
if in.isLoading {
return in.materialLoader.Layout(gtx)
}
if in.btnPositive.Text == "" {
return D{}
}
gtx.Constraints.Max.X = gtx.Dp(values.MarginPadding250)
if in.btnPositiveWidth > 0 {
gtx.Constraints.Min.X = gtx.Dp(in.btnPositiveWidth)
}
return in.btnPositive.Layout(gtx)
}),
)
})
}
}