/
util.go
215 lines (191 loc) · 5.43 KB
/
util.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
package cryptomaterial
import (
"fmt"
"image"
"image/color"
"math"
"math/rand"
"strings"
"time"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"github.com/crypto-power/cryptopower/app"
"github.com/crypto-power/cryptopower/ui/values"
)
func drawInk(gtx layout.Context, c widget.Press, highlightColor color.NRGBA) {
// duration is the number of seconds for the
// completed animation: expand while fading in, then
// out.
const (
expandDuration = float32(0.5)
fadeDuration = float32(0.9)
)
now := gtx.Now
t := float32(now.Sub(c.Start).Seconds())
end := c.End
if end.IsZero() {
// If the press hasn't ended, don't fade-out.
end = now
}
endt := float32(end.Sub(c.Start).Seconds())
// Compute the fade-in/out position in [0;1].
var alphat float32
{
var haste float32
if c.Cancelled {
// If the press was cancelled before the inkwell
// was fully faded in, fast forward the animation
// to match the fade-out.
if h := 0.5 - endt/fadeDuration; h > 0 {
haste = h
}
}
// Fade in.
half1 := t/fadeDuration + haste
if half1 > 0.5 {
half1 = 0.5
}
// Fade out.
half2 := float32(now.Sub(end).Seconds())
half2 /= fadeDuration
half2 += haste
if half2 > 0.5 {
// Too old.
return
}
alphat = half1 + half2
}
// Compute the expand position in [0;1].
sizet := t
if c.Cancelled {
// Freeze expansion of cancelled presses.
sizet = endt
}
sizet /= expandDuration
// Animate only ended presses, and presses that are fading in.
if !c.End.IsZero() || sizet <= 1.0 {
op.InvalidateOp{}.Add(gtx.Ops)
}
if sizet > 1.0 {
sizet = 1.0
}
if alphat > .5 {
// Start fadeout after half the animation.
alphat = 1.0 - alphat
}
// Twice the speed to attain fully faded in at 0.5.
t2 := alphat * 2
// Beziér ease-in curve.
alphaBezier := t2 * t2 * (3.0 - 2.0*t2)
sizeBezier := sizet * sizet * (3.0 - 2.0*sizet)
size := gtx.Constraints.Min.X
if h := gtx.Constraints.Min.Y; h > size {
size = h
}
// Cover the entire constraints min rectangle.
size *= 2 * int(math.Sqrt(2))
// Apply curve values to size and color.
size *= int(sizeBezier)
alpha := 0.7 * alphaBezier
const col = 0.8
ba, _ := byte(alpha*0xff), byte(col*0xff)
rgba := mulAlpha(highlightColor, ba)
ink := paint.ColorOp{Color: rgba}
ink.Add(gtx.Ops)
rr := size / 2
defer op.Offset(c.Position.Add(image.Point{
X: -rr,
Y: -rr,
})).Push(gtx.Ops).Pop()
defer clip.UniformRRect(image.Rectangle{Max: image.Pt(size, size)}, rr).Push(gtx.Ops).Pop()
paint.PaintOp{}.Add(gtx.Ops)
}
func GenerateRandomNumber() int {
return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
}
// AnyKey returns a key.Set that will match any of the provided keys.
// See the doc on key.Set for more.
func AnyKey(keys ...string) key.Set {
keysCombined := strings.Join(keys, "|")
return key.Set(keysCombined)
}
// AnyKeyWithOptionalModifier returns a key.Set that will match any of the
// provided keys whether or not they are pressed with provided modifier.
// See the doc on key.Set for more.
func AnyKeyWithOptionalModifier(modifier key.Modifiers, keys ...string) key.Set {
keysCombined := strings.Join(keys, ",")
keysWithModifier := fmt.Sprintf("(%s)-[%s]", modifier, keysCombined)
return key.Set(keysWithModifier)
}
func UniformPaddingWithTopInset(topInset unit.Dp, gtx layout.Context, body layout.Widget, isMobileView ...bool) D {
_isMobileView := len(isMobileView) > 0 && isMobileView[0]
width := gtx.Constraints.Max.X
paddingHorizontal := values.MarginPadding24
bottomPadding := values.MarginPadding24
if (width - 2*gtx.Dp(paddingHorizontal)) > gtx.Dp(values.AppWidth) {
paddingValue := float32(width-gtx.Dp(values.AppWidth)) / 4
paddingHorizontal = unit.Dp(paddingValue)
}
if _isMobileView {
paddingHorizontal = values.MarginPadding16
bottomPadding = values.MarginPadding0
}
return layout.Inset{
Top: topInset,
Right: paddingHorizontal,
Bottom: bottomPadding,
Left: paddingHorizontal,
}.Layout(gtx, body)
}
func UniformPadding(gtx layout.Context, body layout.Widget, isMobileView ...bool) D {
return UniformPaddingWithTopInset(values.MarginPadding0, gtx, body, isMobileView...)
}
func DisableLayout(currentPage app.Page, gtx C, titleLayout, subtitleLayout func(gtx C) D, transparency uint8, color color.NRGBA, actionButton *Button) D {
return layout.Stack{Alignment: layout.N}.Layout(gtx,
layout.Expanded(func(gtx C) D {
if currentPage == nil {
return D{}
}
mgtx := gtx.Disabled()
return currentPage.Layout(mgtx)
}),
layout.Stacked(func(gtx C) D {
overlayColor := color
overlayColor.A = transparency
gtx.Constraints.Min.X = gtx.Constraints.Max.X
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
FillMax(gtx, overlayColor, 10)
if titleLayout == nil && subtitleLayout == nil && actionButton == nil {
return D{}
}
return layout.Center.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if titleLayout == nil {
return D{}
}
return titleLayout(gtx)
}),
layout.Rigid(func(gtx C) D {
if subtitleLayout == nil {
return D{}
}
return subtitleLayout(gtx)
}),
layout.Rigid(func(gtx C) D {
if actionButton == nil {
return D{}
}
actionButton.TextSize = values.TextSize14
return actionButton.Layout(gtx)
}),
)
})
}),
)
}