-
Notifications
You must be signed in to change notification settings - Fork 3
/
stack.go
153 lines (137 loc) · 3.36 KB
/
stack.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
// SPDX-License-Identifier: Unlicense OR MIT
package layout
import (
"image"
"github.com/kanryu/mado/op"
)
// Stack lays out child elements on top of each other,
// according to an alignment direction.
type Stack struct {
// Alignment is the direction to align children
// smaller than the available space.
Alignment Direction
}
// StackChild represents a child for a Stack layout.
type StackChild struct {
expanded bool
widget Widget
// Scratch space.
call op.CallOp
dims Dimensions
}
// Stacked returns a Stack child that is laid out with no minimum
// constraints and the maximum constraints passed to Stack.Layout.
func Stacked(w Widget) StackChild {
return StackChild{
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 Expanded(w Widget) StackChild {
return StackChild{
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 Stack) Layout(gtx Context, children ...StackChild) Dimensions {
var maxSZ image.Point
// First lay out Stacked children.
cgtx := gtx
cgtx.Constraints.Min = image.Point{}
for i, w := range children {
if w.expanded {
continue
}
macro := op.Record(gtx.Ops)
dims := w.widget(cgtx)
call := macro.Stop()
if w := dims.Size.X; w > maxSZ.X {
maxSZ.X = w
}
if h := dims.Size.Y; h > maxSZ.Y {
maxSZ.Y = h
}
children[i].call = call
children[i].dims = dims
}
// Then lay out Expanded children.
for i, w := range children {
if !w.expanded {
continue
}
macro := op.Record(gtx.Ops)
cgtx.Constraints.Min = maxSZ
dims := w.widget(cgtx)
call := macro.Stop()
if w := dims.Size.X; w > maxSZ.X {
maxSZ.X = w
}
if h := dims.Size.Y; h > maxSZ.Y {
maxSZ.Y = h
}
children[i].call = call
children[i].dims = dims
}
maxSZ = gtx.Constraints.Constrain(maxSZ)
var baseline int
for _, ch := range children {
sz := ch.dims.Size
var p image.Point
switch s.Alignment {
case N, S, Center:
p.X = (maxSZ.X - sz.X) / 2
case NE, SE, E:
p.X = maxSZ.X - sz.X
}
switch s.Alignment {
case W, Center, E:
p.Y = (maxSZ.Y - sz.Y) / 2
case SW, S, SE:
p.Y = maxSZ.Y - sz.Y
}
trans := op.Offset(p).Push(gtx.Ops)
ch.call.Add(gtx.Ops)
trans.Pop()
if baseline == 0 {
if b := ch.dims.Baseline; b != 0 {
baseline = b + maxSZ.Y - sz.Y - p.Y
}
}
}
return Dimensions{
Size: maxSZ,
Baseline: baseline,
}
}
// Background lays out single child widget on top of a background,
// centering, if necessary.
type Background struct{}
// Layout a widget and then add a background to it.
func (Background) Layout(gtx Context, background, widget Widget) Dimensions {
macro := op.Record(gtx.Ops)
wdims := widget(gtx)
baseline := wdims.Baseline
call := macro.Stop()
cgtx := gtx
cgtx.Constraints.Min = gtx.Constraints.Constrain(wdims.Size)
bdims := background(cgtx)
if bdims.Size != wdims.Size {
p := image.Point{
X: (bdims.Size.X - wdims.Size.X) / 2,
Y: (bdims.Size.Y - wdims.Size.Y) / 2,
}
baseline += (bdims.Size.Y - wdims.Size.Y) / 2
trans := op.Offset(p).Push(gtx.Ops)
defer trans.Pop()
}
call.Add(gtx.Ops)
return Dimensions{
Size: bdims.Size,
Baseline: baseline,
}
}