/
short.go
161 lines (146 loc) · 3.76 KB
/
short.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
// SPDX-License-Identifier: Unlicense OR MIT
package main
import (
"image"
"gioui.org/layout"
"gioui.org/op"
)
// Stack lays out child elements on top of each other,
// according to an alignment direction.
type Stack3 struct {
// Alignment is the direction to align children
// smaller than the available space.
Alignment layout.Direction
}
// StackChild represents a child for a Stack layout.
type StackChild3 struct {
expanded bool
widget layout.Widget
}
// Stacked returns a Stack child that is laid out with no minimum
// constraints and the maximum constraints passed to Stack.Layout.
func Stacked3(w layout.Widget) StackChild3 {
return StackChild3{
widget: w,
}
}
// Expanded returns a Stack child with the minimum constraints set
// to the largest Stacked child. The maximum constraints are set to
// the same as passed to Stack.Layout.
func Expanded3(w layout.Widget) StackChild3 {
return StackChild3{
expanded: true,
widget: w,
}
}
// Layout a stack of children. The position of the children are
// determined by the specified order, but Stacked children are laid out
// before Expanded children.
func (s Stack3) Layout3(gtx layout.Context, ax, bx, cx StackChild) layout.Dimensions {
// Scratch space.
var acall, bcall, ccall op.CallOp
var adims, bdims, cdims layout.Dimensions
var maxSZ image.Point
// First lay out Stacked children.
cgtx := gtx
cgtx.Constraints.Min = image.Point{}
if !ax.expanded {
macro := op.Record(gtx.Ops)
adims = ax.widget(cgtx)
acall = macro.Stop()
maxSZ.X = max(maxSZ.X, adims.Size.X)
maxSZ.Y = max(maxSZ.Y, adims.Size.Y)
}
if !bx.expanded {
macro := op.Record(gtx.Ops)
bdims = bx.widget(cgtx)
bcall = macro.Stop()
maxSZ.X = max(maxSZ.X, bdims.Size.X)
maxSZ.Y = max(maxSZ.Y, bdims.Size.Y)
}
if !cx.expanded {
macro := op.Record(gtx.Ops)
cdims = cx.widget(cgtx)
ccall = macro.Stop()
maxSZ.X = max(maxSZ.X, cdims.Size.X)
maxSZ.Y = max(maxSZ.Y, cdims.Size.Y)
}
if ax.expanded {
macro := op.Record(gtx.Ops)
cgtx.Constraints.Min = maxSZ
adims = ax.widget(cgtx)
bcall = macro.Stop()
maxSZ.X = max(maxSZ.X, adims.Size.X)
maxSZ.Y = max(maxSZ.Y, adims.Size.Y)
}
if bx.expanded {
macro := op.Record(gtx.Ops)
cgtx.Constraints.Min = maxSZ
bdims = bx.widget(cgtx)
ccall = macro.Stop()
maxSZ.X = max(maxSZ.X, bdims.Size.X)
maxSZ.Y = max(maxSZ.Y, bdims.Size.Y)
}
if cx.expanded {
macro := op.Record(gtx.Ops)
cgtx.Constraints.Min = maxSZ
cdims = cx.widget(cgtx)
ccall = macro.Stop()
maxSZ.X = max(maxSZ.X, cdims.Size.X)
maxSZ.Y = max(maxSZ.Y, cdims.Size.Y)
}
maxSZ = gtx.Constraints.Constrain(maxSZ)
var baseline int
if ax.expanded {
p := align(s.Alignment, adims.Size, maxSZ)
trans := op.Offset(p).Push(gtx.Ops)
acall.Add(gtx.Ops)
trans.Pop()
if baseline == 0 {
if b := adims.Baseline; b != 0 {
baseline = b + maxSZ.Y - adims.Size.Y - p.Y
}
}
}
if bx.expanded {
p := align(s.Alignment, bdims.Size, maxSZ)
trans := op.Offset(p).Push(gtx.Ops)
bcall.Add(gtx.Ops)
trans.Pop()
if baseline == 0 {
if b := bdims.Baseline; b != 0 {
baseline = b + maxSZ.Y - bdims.Size.Y - p.Y
}
}
}
if cx.expanded {
p := align(s.Alignment, cdims.Size, maxSZ)
trans := op.Offset(p).Push(gtx.Ops)
ccall.Add(gtx.Ops)
trans.Pop()
if baseline == 0 {
if b := cdims.Baseline; b != 0 {
baseline = b + maxSZ.Y - cdims.Size.Y - p.Y
}
}
}
return layout.Dimensions{
Size: maxSZ,
Baseline: baseline,
}
}
func align(a layout.Direction, s, max image.Point) (p image.Point) {
switch a {
case layout.N, layout.S, layout.Center:
p.X = (max.X - s.X) / 2
case layout.NE, layout.SE, layout.E:
p.X = max.X - s.X
}
switch a {
case layout.W, layout.Center, layout.E:
p.Y = (max.Y - s.Y) / 2
case layout.SW, layout.S, layout.SE:
p.Y = max.Y - s.Y
}
return p
}