Repository navigation
Expand file tree
/
Copy pathlayout.go
More file actions
592 lines (539 loc) · 16 KB
/
Copy pathlayout.go
File metadata and controls
592 lines (539 loc) · 16 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright 2026 Han Li and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kit
import (
"image"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
)
// The layout library: rows, columns, stacks, padding and alignment, with the
// semantics of Flutter's boxes: a parent gives a child constraints, the child
// picks a size within them and the parent places it. Every container tells
// the registry where it put each child (see Registry.Shift), so that the
// registry knows where every widget is when the frame is complete.
// inf is the constraint that means unbounded, as in Gio's layout.List.
const inf = 1e6
// unbounded reports whether a maximum is the unbounded one.
func unbounded(max int) bool { return max >= inf }
func dpUnit(v float32) unit.Dp { return unit.Dp(v) }
func spUnit(v float32) unit.Sp { return unit.Sp(v) }
// placed is a child that was laid out into a macro and waits for its position.
type placed struct {
call op.CallOp
dims layout.Dimensions
from, to int
}
// measure lays out w within cs without drawing it yet.
func (c Ctx) measure(cs layout.Constraints, w Widget) placed {
from := c.Env.Reg.Mark()
macro := op.Record(c.Ops)
child := c
child.Constraints = cs
dims := w(child)
dims.Size = cs.Constrain(dims.Size)
call := macro.Stop()
return placed{call: call, dims: dims, from: from, to: c.Env.Reg.Mark()}
}
// put draws the child at the position, and moves its nodes with it.
func (p placed) put(c Ctx, at image.Point) {
c.Env.Reg.ShiftRange(p.from, p.to, at)
defer op.Offset(at).Push(c.Ops).Pop()
p.call.Add(c.Ops)
}
// size is the size of the child.
func (p placed) size() image.Point { return p.dims.Size }
func loose(max image.Point) layout.Constraints { return layout.Constraints{Max: max} }
func tight(size image.Point) layout.Constraints { return layout.Constraints{Min: size, Max: size} }
func within(min, max image.Point) layout.Constraints {
return layout.Constraints{Min: min, Max: max}
}
func axisPoint(axis layout.Axis, main, cross int) image.Point {
if axis == layout.Horizontal {
return image.Pt(main, cross)
}
return image.Pt(cross, main)
}
func axisMain(axis layout.Axis, p image.Point) int {
if axis == layout.Horizontal {
return p.X
}
return p.Y
}
func axisCross(axis layout.Axis, p image.Point) int {
if axis == layout.Horizontal {
return p.Y
}
return p.X
}
// MainAlign says where the children of a flex go along its main axis.
type MainAlign int
// The alignments along the main axis.
const (
MainStart MainAlign = iota
MainCenter
MainEnd
MainSpaceBetween
)
// CrossAlign says where the children of a flex go across its main axis.
type CrossAlign int
// The alignments across the main axis.
const (
CrossStart CrossAlign = iota
CrossCenter
CrossEnd
// CrossStretch makes the children as wide (or high) as the flex.
CrossStretch
)
// FlexOpts are the options of a row or a column.
type FlexOpts struct {
// Main is where the children go along the main axis when there is
// space left.
Main MainAlign
// Cross is where the children go across it.
Cross CrossAlign
// Gap is the space between the children, in dp.
Gap float32
// Min makes the flex only as long as its children; by default it takes
// all the space along the main axis.
Min bool
}
// FlexChild is a child of a row or a column.
type FlexChild struct {
w Widget
flex float32
tight bool
}
// Rigid is a child that has its own size along the main axis.
func Rigid(w Widget) FlexChild { return FlexChild{w: w} }
// Expanded is a child that takes a share of the space that the rigid children
// leave, in proportion to flex.
func Expanded(flex float32, w Widget) FlexChild { return FlexChild{w: w, flex: flex, tight: true} }
// Flexible is a child that may take a share of the space, but can be smaller.
func Flexible(flex float32, w Widget) FlexChild { return FlexChild{w: w, flex: flex} }
// Row lays out its children from left to right.
func Row(o FlexOpts, children ...FlexChild) Widget { return flexLayout(layout.Horizontal, o, children) }
// Column lays out its children from top to bottom.
func Column(o FlexOpts, children ...FlexChild) Widget {
return flexLayout(layout.Vertical, o, children)
}
func flexLayout(axis layout.Axis, o FlexOpts, all []FlexChild) Widget {
inner := flexLayoutBounded(axis, o, all)
return func(c Ctx) layout.Dimensions {
if o.Cross != CrossStretch || !unbounded(axisCross(axis, c.Constraints.Max)) {
return inner(c)
}
// Children stretch to the tallest (or widest) of them, as in
// Flutter's IntrinsicHeight: lay them out once to find its size, then
// again, tightly.
mark := c.Env.Reg.Mark()
macro := op.Record(c.Ops)
probe := inner(c)
macro.Stop()
c.Env.Reg.Truncate(mark)
cs := c.Constraints
cross := axisCross(axis, probe.Size)
if axis == layout.Horizontal {
cs.Min.Y, cs.Max.Y = cross, cross
} else {
cs.Min.X, cs.Max.X = cross, cross
}
c.Constraints = cs
return inner(c)
}
}
func flexLayoutBounded(axis layout.Axis, o FlexOpts, all []FlexChild) Widget {
return func(c Ctx) layout.Dimensions {
var children []FlexChild
for _, child := range all {
if child.w != nil {
children = append(children, child)
}
}
cs := c.Constraints
mainMax := axisMain(axis, cs.Max)
crossMax := axisCross(axis, cs.Max)
crossMin := axisCross(axis, cs.Min)
gap := c.DP(o.Gap)
totalGap := 0
if len(children) > 1 {
totalGap = gap * (len(children) - 1)
}
stretch := o.Cross == CrossStretch && !unbounded(crossMax)
childCross := 0
if stretch {
childCross = crossMax
}
measured := make([]placed, len(children))
used := totalGap
var flexTotal float32
for i, child := range children {
if child.flex > 0 && !unbounded(mainMax) {
flexTotal += child.flex
continue
}
measured[i] = c.measure(layout.Constraints{
Min: axisPoint(axis, 0, childCross),
Max: axisPoint(axis, int(inf), crossMax),
}, child.w)
used += axisMain(axis, measured[i].size())
}
if flexTotal > 0 {
space := mainMax - used
if space < 0 {
space = 0
}
given := 0
remaining := flexTotal
for i, child := range children {
if child.flex <= 0 || unbounded(mainMax) {
continue
}
share := int(float32(space-given) * child.flex / remaining)
remaining -= child.flex
given += share
minMain := 0
if child.tight {
minMain = share
}
measured[i] = c.measure(layout.Constraints{
Min: axisPoint(axis, minMain, childCross),
Max: axisPoint(axis, share, crossMax),
}, child.w)
used += axisMain(axis, measured[i].size())
}
}
cross := crossMin
for _, p := range measured {
if v := axisCross(axis, p.size()); v > cross {
cross = v
}
}
if stretch {
cross = crossMax
}
mainSize := used
if !o.Min && !unbounded(mainMax) {
mainSize = mainMax
}
if mainMin := axisMain(axis, cs.Min); mainSize < mainMin {
mainSize = mainMin
}
leftover := mainSize - used
if leftover < 0 {
leftover = 0
}
pos := 0
between := 0
switch o.Main {
case MainCenter:
pos = leftover / 2
case MainEnd:
pos = leftover
case MainSpaceBetween:
if len(children) > 1 {
between = leftover / (len(children) - 1)
}
}
for _, p := range measured {
at := 0
switch o.Cross {
case CrossCenter:
at = (cross - axisCross(axis, p.size())) / 2
case CrossEnd:
at = cross - axisCross(axis, p.size())
}
p.put(c, axisPoint(axis, pos, at))
pos += axisMain(axis, p.size()) + gap + between
}
return layout.Dimensions{Size: cs.Constrain(axisPoint(axis, mainSize, cross))}
}
}
// StackChild is a child of a stack.
type StackChild struct {
w Widget
kind int
place func(stack, child image.Point) image.Point
}
// Stacked is a child that has its own size, which the stack grows to hold.
func Stacked(w Widget) StackChild { return StackChild{w: w} }
// Expand is a child that is as large as the stack.
func Expand(w Widget) StackChild { return StackChild{w: w, kind: 1} }
// PlacedAt is a child that has its own size, which does not count for the size
// of the stack, and that place puts where it wants: it gets the size of the
// stack and of the child.
func PlacedAt(place func(stack, child image.Point) image.Point, w Widget) StackChild {
return StackChild{w: w, kind: 2, place: place}
}
// Stack lays out its children on top of each other, the last one on top.
// Stacked children are positioned by align inside the stack.
func Stack(align layout.Direction, children ...StackChild) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
size := cs.Min
measured := make([]placed, len(children))
for i, child := range children {
if child.w == nil || child.kind != 0 {
continue
}
measured[i] = c.measure(loose(cs.Max), child.w)
size = image.Pt(max(size.X, measured[i].size().X), max(size.Y, measured[i].size().Y))
}
size = cs.Constrain(size)
for i, child := range children {
if child.w == nil {
continue
}
switch child.kind {
case 0:
measured[i].put(c, align.Position(measured[i].size(), size))
case 1:
p := c.measure(tight(size), child.w)
p.put(c, image.Point{})
case 2:
p := c.measure(loose(cs.Max), child.w)
p.put(c, child.place(size, p.size()))
}
}
return layout.Dimensions{Size: size}
}
}
// Inset is padding, in dp.
type Inset struct{ Left, Top, Right, Bottom float32 }
// All is the same padding on every side.
func All(v float32) Inset { return Inset{v, v, v, v} }
// Sym is horizontal and vertical padding.
func Sym(horizontal, vertical float32) Inset {
return Inset{horizontal, vertical, horizontal, vertical}
}
// Pad pads its child.
func Pad(in Inset, w Widget) Widget {
return func(c Ctx) layout.Dimensions {
left, top, right, bottom := c.DP(in.Left), c.DP(in.Top), c.DP(in.Right), c.DP(in.Bottom)
cs := c.Constraints
inner := layout.Constraints{
Min: image.Pt(max(0, cs.Min.X-left-right), max(0, cs.Min.Y-top-bottom)),
Max: image.Pt(subtractUnbounded(cs.Max.X, left+right), subtractUnbounded(cs.Max.Y, top+bottom)),
}
p := c.measure(inner, w)
p.put(c, image.Pt(left, top))
size := cs.Constrain(p.size().Add(image.Pt(left+right, top+bottom)))
return layout.Dimensions{Size: size, Baseline: p.dims.Baseline + top}
}
}
func subtractUnbounded(max, v int) int {
if unbounded(max) {
return max
}
if max-v < 0 {
return 0
}
return max - v
}
// Align puts its child in the space it is given, as the direction says. The
// child may be smaller than the space.
func Align(d layout.Direction, w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
p := c.measure(loose(cs.Max), w)
size := cs.Max
if unbounded(size.X) {
size.X = p.size().X
}
if unbounded(size.Y) {
size.Y = p.size().Y
}
size = cs.Constrain(size)
p.put(c, d.Position(p.size(), size))
return layout.Dimensions{Size: size}
}
}
// Center puts its child in the middle of the space it is given.
func Center(w Widget) Widget { return Align(layout.Center, w) }
// Sized gives its child a fixed size, in dp. A zero width or height leaves that
// dimension to the child.
func Sized(width, height float32, w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
min, max := cs.Min, cs.Max
if width > 0 {
px := cs.Constrain(image.Pt(c.DP(width), 0)).X
min.X, max.X = px, px
}
if height > 0 {
px := cs.Constrain(image.Pt(0, c.DP(height))).Y
min.Y, max.Y = px, px
}
p := c.measure(within(min, max), w)
p.put(c, image.Point{})
return layout.Dimensions{Size: p.size(), Baseline: p.dims.Baseline}
}
}
// Limit constrains its child to at most maxWidth and maxHeight, in dp (zero means
// no limit) and at least minWidth and minHeight.
func Limit(minWidth, minHeight, maxWidth, maxHeight float32, w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
min, max := cs.Min, cs.Max
if maxWidth > 0 {
max.X = minInt(max.X, c.DP(maxWidth))
}
if maxHeight > 0 {
max.Y = minInt(max.Y, c.DP(maxHeight))
}
if minWidth > 0 {
min.X = minInt(max.X, maxInt(min.X, c.DP(minWidth)))
}
if minHeight > 0 {
min.Y = minInt(max.Y, maxInt(min.Y, c.DP(minHeight)))
}
min.X, min.Y = minInt(min.X, max.X), minInt(min.Y, max.Y)
p := c.measure(layout.Constraints{Min: min, Max: max}, w)
p.put(c, image.Point{})
return layout.Dimensions{Size: p.size(), Baseline: p.dims.Baseline}
}
}
// MaxWidth is Limit with only a maximum width, in dp.
func MaxWidth(width float32, w Widget) Widget { return Limit(0, 0, width, 0, w) }
// Loose lets its child be smaller than the space it is given.
func Loose(w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
cs.Min = image.Point{}
c.Constraints = cs
return w(c)
}
}
// Fill makes its child as large as the space it is given, where that is
// bounded.
func Fill(w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
if !unbounded(cs.Max.X) {
cs.Min.X = cs.Max.X
}
if !unbounded(cs.Max.Y) {
cs.Min.Y = cs.Max.Y
}
c.Constraints = cs
return w(c)
}
}
// FillWidth makes its child as wide as the space it is given, where that is
// bounded.
func FillWidth(w Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
if !unbounded(cs.Max.X) {
cs.Min.X = cs.Max.X
}
c.Constraints = cs
return w(c)
}
}
// Space is empty space of the given size, in dp.
func Space(width, height float32) Widget {
return func(c Ctx) layout.Dimensions {
return layout.Dimensions{Size: c.Constraints.Constrain(image.Pt(c.DP(width), c.DP(height)))}
}
}
// Wrap lays out its children in rows, starting a new row where one is full.
type Wrap struct {
// Spacing is the space between the children of a row, in dp.
Spacing float32
// RunSpacing is the space between the rows, in dp.
RunSpacing float32
// Align puts the rows horizontally: MainStart, MainCenter or MainEnd.
Align MainAlign
// CrossCenter centres the children of a row vertically, instead of
// putting them at its top.
CrossCenter bool
}
// Layout lays out the children.
func (wr Wrap) Layout(children ...Widget) Widget {
return func(c Ctx) layout.Dimensions {
cs := c.Constraints
maxWidth := cs.Max.X
spacing, runSpacing := c.DP(wr.Spacing), c.DP(wr.RunSpacing)
type run struct {
children []placed
width int
height int
}
var runs []run
current := run{}
for _, w := range children {
if w == nil {
continue
}
p := c.measure(loose(image.Pt(maxWidth, cs.Max.Y)), w)
next := current.width + p.size().X
if len(current.children) > 0 {
next += spacing
}
if len(current.children) > 0 && !unbounded(maxWidth) && next > maxWidth {
runs = append(runs, current)
current = run{}
next = p.size().X
}
current.children = append(current.children, p)
current.width = next
current.height = max(current.height, p.size().Y)
}
if len(current.children) > 0 {
runs = append(runs, current)
}
width, y := 0, 0
for _, r := range runs {
width = max(width, r.width)
}
if !unbounded(maxWidth) && wr.Align != MainStart {
width = max(width, cs.Min.X)
}
for i, r := range runs {
x := 0
switch wr.Align {
case MainCenter:
x = (width - r.width) / 2
case MainEnd:
x = width - r.width
}
for _, p := range r.children {
at := 0
if wr.CrossCenter {
at = (r.height - p.size().Y) / 2
}
p.put(c, image.Pt(x, y+at))
x += p.size().X + spacing
}
y += r.height
if i < len(runs)-1 {
y += runSpacing
}
}
return layout.Dimensions{Size: cs.Constrain(image.Pt(width, y))}
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}