~eliasnaur/gio

ref: 7aa7bb3be40858efe2a98f866fcc7db2026b02fd gio/ui/layout/layout.go -rw-r--r-- 4.1 KiB
7aa7bb3bElias Naur ui: rename ops to have Op suffixed, not prefixed 2 years 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
// SPDX-License-Identifier: Unlicense OR MIT

package layout

import (
	"image"
	"math"

	"gioui.org/ui"
)

type Constraints struct {
	Width  Constraint
	Height Constraint
}

type Constraint struct {
	Min, Max int
}

type Dimens struct {
	Size     image.Point
	Baseline int
}

type Axis uint8

const (
	Horizontal Axis = iota
	Vertical
)

func (c Constraint) Constrain(v int) int {
	if v < c.Min {
		return c.Min
	} else if v > c.Max {
		return c.Max
	}
	return v
}

func (c Constraints) Constrain(p image.Point) image.Point {
	return image.Point{X: c.Width.Constrain(p.X), Y: c.Height.Constrain(p.Y)}
}

func (c Constraints) Expand() Constraints {
	return Constraints{Width: c.Width.Expand(), Height: c.Height.Expand()}
}

func (c Constraint) Expand() Constraint {
	return Constraint{Min: c.Max, Max: c.Max}
}
func (c Constraints) Loose() Constraints {
	return Constraints{Width: c.Width.Loose(), Height: c.Height.Loose()}
}

func (c Constraint) Loose() Constraint {
	return Constraint{Max: c.Max}
}

// ExactConstraints returns the constraints that exactly represents the
// given dimensions.
func ExactConstraints(size image.Point) Constraints {
	return Constraints{
		Width:  Constraint{Min: size.X, Max: size.X},
		Height: Constraint{Min: size.Y, Max: size.Y},
	}
}

type Insets struct {
	Top, Right, Bottom, Left float32

	ops   *ui.Ops
	begun bool
	cs    Constraints
}

func (in *Insets) Begin(ops *ui.Ops, cs Constraints) Constraints {
	if in.begun {
		panic("must End before Begin")
	}
	in.begun = true
	in.ops = ops
	in.cs = cs
	mcs := cs
	t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left)))
	if mcs.Width.Max != ui.Inf {
		mcs.Width.Min -= l + r
		mcs.Width.Max -= l + r
		if mcs.Width.Min < 0 {
			mcs.Width.Min = 0
		}
		if mcs.Width.Max < mcs.Width.Min {
			mcs.Width.Max = mcs.Width.Min
		}
	}
	if mcs.Height.Max != ui.Inf {
		mcs.Height.Min -= t + b
		mcs.Height.Max -= t + b
		if mcs.Height.Min < 0 {
			mcs.Height.Min = 0
		}
		if mcs.Height.Max < mcs.Height.Min {
			mcs.Height.Max = mcs.Height.Min
		}
	}
	ui.PushOp{}.Add(ops)
	ui.TransformOp{Transform: ui.Offset(toPointF(image.Point{X: l, Y: t}))}.Add(ops)
	return mcs
}

func (in *Insets) End(dims Dimens) Dimens {
	if !in.begun {
		panic("must Begin before End")
	}
	in.begun = false
	ops := in.ops
	ui.PopOp{}.Add(ops)
	t, r, b, l := int(math.Round(float64(in.Top))), int(math.Round(float64(in.Right))), int(math.Round(float64(in.Bottom))), int(math.Round(float64(in.Left)))
	return Dimens{
		Size:     in.cs.Constrain(dims.Size.Add(image.Point{X: r + l, Y: t + b})),
		Baseline: dims.Baseline + t,
	}
}

func EqualInsets(v float32) Insets {
	return Insets{Top: v, Right: v, Bottom: v, Left: v}
}

func isInf(v ui.Value) bool {
	return math.IsInf(float64(v.V), 1)
}

type Sized struct {
	Width, Height float32
}

func (s Sized) Constrain(cs Constraints) Constraints {
	if h := int(s.Height + 0.5); h != 0 {
		if cs.Height.Min < h {
			cs.Height.Min = h
		}
		if h < cs.Height.Max {
			cs.Height.Max = h
		}
	}
	if w := int(s.Width + .5); w != 0 {
		if cs.Width.Min < w {
			cs.Width.Min = w
		}
		if w < cs.Width.Max {
			cs.Width.Max = w
		}
	}
	return cs
}

type Align struct {
	Alignment Direction

	ops   *ui.Ops
	begun bool
	cs    Constraints
}

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
	ops.Begin()
	return cs.Loose()
}

func (a *Align) End(dims Dimens) Dimens {
	if !a.begun {
		panic("must Begin before End")
	}
	a.begun = false
	ops := a.ops
	block := ops.End()
	sz := dims.Size
	if a.cs.Width.Max != ui.Inf {
		sz.X = a.cs.Width.Max
	}
	if a.cs.Height.Max != ui.Inf {
		sz.Y = a.cs.Height.Max
	}
	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
	}
	ui.PushOp{}.Add(ops)
	ui.TransformOp{Transform: ui.Offset(toPointF(p))}.Add(ops)
	block.Add(ops)
	ui.PopOp{}.Add(ops)
	return Dimens{
		Size:     sz,
		Baseline: dims.Baseline,
	}
}