~eliasnaur/gio

ref: 5c3dcc6f9a07dc22a26319616adc2c63e33da966 gio/ui/layout/layout.go -rw-r--r-- 5.1 KiB
5c3dcc6fElias Naur ui/gesture: typo 1 year, 1 month ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package layout

import (
	"image"

	"gioui.org/ui"
)

// Constraints represent a set of acceptable ranges for
// a widget's width and height.
type Constraints struct {
	Width  Constraint
	Height Constraint
}

// Constraint is a range of acceptable sizes in a single
// dimension.
type Constraint struct {
	Min, Max int
}

// Dimens are the resolved size and baseline for a user
// interface element.
type Dimens struct {
	Size     image.Point
	Baseline int
}

// Axis is the the Horizontal or Vertical direction.
type Axis uint8

// Alignment is the relative alignment of a list of
// interface elements.
type Alignment uint8

// Direction is the alignment of a set of interface elements
// relative to a containing space.
type Direction uint8

const (
	Start Alignment = iota
	End
	Middle
	Baseline
)

const (
	NW Direction = iota
	N
	NE
	E
	SE
	S
	SW
	W
	Center
)

const (
	Horizontal Axis = iota
	Vertical
)

// Constrain a value to the range [Min; Max].
func (c Constraint) Constrain(v int) int {
	if v < c.Min {
		return c.Min
	} else if v > c.Max {
		return c.Max
	}
	return v
}

// Constrain a size to the Width and Height ranges.
func (c Constraints) Constrain(size image.Point) image.Point {
	return image.Point{X: c.Width.Constrain(size.X), Y: c.Height.Constrain(size.Y)}
}

// RigidConstraints returns the constraints that can only be
// satisfied by the given dimensions.
func RigidConstraints(size image.Point) Constraints {
	return Constraints{
		Width:  Constraint{Min: size.X, Max: size.X},
		Height: Constraint{Min: size.Y, Max: size.Y},
	}
}

// Inset adds space around an interface element.
type Inset struct {
	Top, Right, Bottom, Left ui.Value

	stack                    ui.StackOp
	top, right, bottom, left int
	begun                    bool
	cs                       Constraints
}

// Align aligns an interface element in the available space.
type Align struct {
	Alignment Direction

	macro ui.MacroOp
	ops   *ui.Ops
	begun bool
	cs    Constraints
}

// Begin the inset operation and modify the input constraints to
// account for the insets.
func (in *Inset) Begin(c ui.Config, ops *ui.Ops, cs Constraints) Constraints {
	if in.begun {
		panic("must End before Begin")
	}
	in.top = c.Px(in.Top)
	in.right = c.Px(in.Right)
	in.bottom = c.Px(in.Bottom)
	in.left = c.Px(in.Left)
	in.begun = true
	in.cs = cs
	mcs := cs
	mcs.Width.Min -= in.left + in.right
	mcs.Width.Max -= in.left + in.right
	if mcs.Width.Min < 0 {
		mcs.Width.Min = 0
	}
	if mcs.Width.Max < mcs.Width.Min {
		mcs.Width.Max = mcs.Width.Min
	}
	mcs.Height.Min -= in.top + in.bottom
	mcs.Height.Max -= in.top + in.bottom
	if mcs.Height.Min < 0 {
		mcs.Height.Min = 0
	}
	if mcs.Height.Max < mcs.Height.Min {
		mcs.Height.Max = mcs.Height.Min
	}
	in.stack.Push(ops)
	ui.TransformOp{}.Offset(toPointF(image.Point{X: in.left, Y: in.top})).Add(ops)
	return mcs
}

// End the inset operation and return the dimensions for the
// inset child.
func (in *Inset) End(dims Dimens) Dimens {
	if !in.begun {
		panic("must Begin before End")
	}
	in.begun = false
	in.stack.Pop()
	return Dimens{
		Size:     in.cs.Constrain(dims.Size.Add(image.Point{X: in.right + in.left, Y: in.top + in.bottom})),
		Baseline: dims.Baseline + in.top,
	}
}

// UniformInset returns an Inset with a single inset applied to all
// edges.
func UniformInset(v ui.Value) Inset {
	return Inset{Top: v, Right: v, Bottom: v, Left: v}
}

// Begin aligning and return the constraints with no minimum size.
func (a *Align) Begin(ops *ui.Ops, cs Constraints) Constraints {
	if a.begun {
		panic("must End before Begin")
	}
	a.begun = true
	a.ops = ops
	a.cs = cs
	a.macro.Record(ops)
	cs.Width.Min = 0
	cs.Height.Min = 0
	return cs
}

// End the align operation and return the dimensions for the
// aligned child.
func (a *Align) End(dims Dimens) Dimens {
	if !a.begun {
		panic("must Begin before End")
	}
	a.begun = false
	ops := a.ops
	a.macro.Stop()
	sz := dims.Size
	if sz.X < a.cs.Width.Min {
		sz.X = a.cs.Width.Min
	}
	if sz.Y < a.cs.Height.Min {
		sz.Y = a.cs.Height.Min
	}
	var p image.Point
	switch a.Alignment {
	case N, S, Center:
		p.X = (sz.X - dims.Size.X) / 2
	case NE, SE, E:
		p.X = sz.X - dims.Size.X
	}
	switch a.Alignment {
	case W, Center, E:
		p.Y = (sz.Y - dims.Size.Y) / 2
	case SW, S, SE:
		p.Y = sz.Y - dims.Size.Y
	}
	var stack ui.StackOp
	stack.Push(ops)
	ui.TransformOp{}.Offset(toPointF(p)).Add(ops)
	a.macro.Add(ops)
	stack.Pop()
	return Dimens{
		Size:     sz,
		Baseline: dims.Baseline,
	}
}

func (a Alignment) String() string {
	switch a {
	case Start:
		return "Start"
	case End:
		return "End"
	case Middle:
		return "Middle"
	case Baseline:
		return "Baseline"
	default:
		panic("unreachable")
	}
}

func (a Axis) String() string {
	switch a {
	case Horizontal:
		return "Horizontal"
	case Vertical:
		return "Vertical"
	default:
		panic("unreachable")
	}
}

func (d Direction) String() string {
	switch d {
	case NW:
		return "NW"
	case N:
		return "N"
	case NE:
		return "NE"
	case E:
		return "E"
	case SE:
		return "SE"
	case S:
		return "S"
	case SW:
		return "SW"
	case W:
		return "W"
	case Center:
		return "Center"
	default:
		panic("unreachable")
	}
}