forked from marcetin/parallelcoin
/
helpers.go
216 lines (193 loc) · 4.71 KB
/
helpers.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
package p9
import (
"image"
"image/color"
"time"
"gioui.org/f32"
"gioui.org/io/system"
l "gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
)
type (
// Defining these as types gives flexibility later to create methods that modify them
Fonts map[string]text.Typeface
Icons map[string]*Icon
Collection []text.FontFace
)
const Inf = 1e6
// Fill is a general fill function that covers the background of the current context space
func Fill(gtx l.Context, col color.NRGBA) l.Dimensions {
cs := gtx.Constraints
d := cs.Min
// dr := f32.Rectangle{
// Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
// }
// paint.ColorOp{Color: col}.Add(gtx.Ops)
// paint.PaintOp{Rect: dr}.Add(gtx.Ops)
fill(gtx, col, d)
return l.Dimensions{Size: d}
}
func (th *Theme) GetFont(font string) *text.Font {
for i := range th.collection {
if th.collection[i].Font.Typeface == text.Typeface(font) {
return &th.collection[i].Font
}
}
return nil
}
func rgb(c uint32) color.RGBA {
return argb(0xff000000 | c)
}
func argb(c uint32) color.RGBA {
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
}
func toPointF(p image.Point) f32.Point {
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}
func axisPoint(a l.Axis, main, cross int) image.Point {
if a == l.Horizontal {
return image.Point{X: main, Y: cross}
} else {
return image.Point{X: cross, Y: main}
}
}
func axisMain(a l.Axis, sz image.Point) int {
if a == l.Horizontal {
return sz.X
} else {
return sz.Y
}
}
func axisCross(a l.Axis, sz image.Point) int {
if a == l.Horizontal {
return sz.Y
} else {
return sz.X
}
}
func axisMainConstraint(a l.Axis, cs l.Constraints) (int, int) {
if a == l.Horizontal {
return cs.Min.X, cs.Max.X
} else {
return cs.Min.Y, cs.Max.Y
}
}
func axisCrossConstraint(a l.Axis, cs l.Constraints) (int, int) {
if a == l.Horizontal {
return cs.Min.Y, cs.Max.Y
} else {
return cs.Min.X, cs.Max.X
}
}
func axisConstraints(a l.Axis, mainMin, mainMax, crossMin, crossMax int) l.Constraints {
if a == l.Horizontal {
return l.Constraints{Min: image.Pt(mainMin, crossMin), Max: image.Pt(mainMax, crossMax)}
} else {
return l.Constraints{Min: image.Pt(crossMin, mainMin), Max: image.Pt(crossMax, mainMax)}
}
}
func EmptySpace(x, y int) func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{
Size: image.Point{
X: x,
Y: y,
},
}
}
}
func EmptyFromSize(size image.Point) func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{
Size: size,
}
}
}
func EmptyMaxWidth() func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{
Size: image.Point{X: gtx.Constraints.Max.X, Y: gtx.Constraints.Min.Y},
Baseline: 0,
}
}
}
func EmptyMaxHeight() func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{Size: image.Point{X: gtx.Constraints.Min.X, Y: gtx.Constraints.Min.Y}}
}
}
func EmptyMinWidth() func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{
Size: image.Point{X: gtx.Constraints.Min.X, Y: gtx.Constraints.Min.Y},
Baseline: 0,
}
}
}
func EmptyMinHeight() func(gtx l.Context) l.Dimensions {
return func(gtx l.Context) l.Dimensions {
return l.Dimensions{Size: image.Point{Y: gtx.Constraints.Min.Y}}
}
}
// CopyContextDimensionsWithMaxAxis copies the dimensions out with the max set by an image.Point along the axis
func CopyContextDimensionsWithMaxAxis(gtx l.Context, size image.Point, axis l.Axis) l.Context {
ip := image.Point{}
if axis == l.Horizontal {
ip.Y = size.Y
ip.X = gtx.Constraints.Max.X
} else {
ip.Y = gtx.Constraints.Max.Y
ip.X = size.X
}
var ops op.Ops
gtx1 := l.NewContext(
&ops, system.FrameEvent{
Now: time.Now(),
Metric: gtx.Metric,
Size: ip,
},
)
if axis == l.Horizontal {
gtx1.Constraints.Min.X = 0
gtx1.Constraints.Min.Y = gtx.Constraints.Min.Y
} else {
gtx1.Constraints.Min.X = gtx.Constraints.Min.X
gtx1.Constraints.Min.Y = 0
}
return gtx1
}
// GetInfContext creates a context with infinite max constraints
func GetInfContext(gtx l.Context) l.Context {
ip := image.Point{}
ip.Y = Inf
ip.X = Inf
var ops op.Ops
gtx1 := l.NewContext(
&ops, system.FrameEvent{
Now: time.Now(),
Metric: gtx.Metric,
Size: ip,
},
)
return gtx1
}
func If(value bool, t, f l.Widget) l.Widget {
if value {
return t
} else {
return f
}
}
func (th *Theme) SliceToWidget(w []l.Widget, axis l.Axis) l.Widget {
var out *Flex
if axis == l.Horizontal {
out = th.Flex().AlignStart()
} else {
out = th.VFlex().AlignStart()
}
for i := range w {
out.Rigid(w[i])
}
return out.Fn
}