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
//! Container axes, alignment, justification, and style builders.
use crate::dock::sanitize_gap;
/// Main layout axis of a container.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
/// Children are placed left to right.
Horizontal,
/// Children are placed top to bottom.
Vertical,
}
impl Axis {
/// Returns `true` for [`Axis::Horizontal`].
pub const fn is_horizontal(self) -> bool {
matches!(self, Self::Horizontal)
}
/// Returns `true` for [`Axis::Vertical`].
pub const fn is_vertical(self) -> bool {
matches!(self, Self::Vertical)
}
/// Splits `width`/`height` into main and cross sizes for this axis.
pub fn split_main_cross(self, width: f32, height: f32) -> (f32, f32) {
match self {
Self::Horizontal => (width, height),
Self::Vertical => (height, width),
}
}
/// Combines main and cross sizes back into `width`/`height`.
pub fn merge_main_cross(self, main: f32, cross: f32) -> (f32, f32) {
match self {
Self::Horizontal => (main, cross),
Self::Vertical => (cross, main),
}
}
}
/// Distribution of free space along a container's main axis.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Justify {
/// Pack items at the start of the line.
#[default]
Start,
/// Center items as a group.
Center,
/// Pack items at the end of the line.
End,
/// Spread items so the first and last touch the edges.
SpaceBetween,
/// Give every item equal space on both sides.
SpaceAround,
/// Give every item equal space, including the outer edges.
SpaceEvenly,
}
impl Justify {
/// Computes the leading offset and the gap added between items.
///
/// `extra` is the free space to distribute and is clamped to zero for
/// invalid values.
pub fn distribute(self, count: usize, _base_gap: f32, extra: f32) -> (f32, f32) {
let extra = if extra.is_finite() {
extra.max(0.0)
} else {
0.0
};
match self {
Self::Start => (0.0, 0.0),
Self::Center => (extra * 0.5, 0.0),
Self::End => (extra, 0.0),
Self::SpaceBetween => {
if count > 1 {
(0.0, extra / (count - 1) as f32)
} else if count == 1 {
(extra * 0.5, 0.0)
} else {
(0.0, 0.0)
}
}
Self::SpaceAround => {
if count > 0 {
let step = extra / count as f32;
(step * 0.5, step)
} else {
(0.0, 0.0)
}
}
Self::SpaceEvenly => {
if count > 0 {
let step = extra / (count + 1) as f32;
(step, step)
} else {
(0.0, 0.0)
}
}
}
}
/// Returns only the leading offset produced by [`Justify::distribute`].
pub fn start_offset(self, extra: f32) -> f32 {
let (start, _) = self.distribute(0, 0.0, extra);
match self {
Self::Start | Self::Center | Self::End => start,
_ => start,
}
}
}
/// Alignment of children on a container's cross axis.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Align {
/// Align children at the start of the cross axis.
#[default]
Start,
/// Center children on the cross axis.
Center,
/// Align children at the end of the cross axis.
End,
/// Stretch children to fill the cross axis.
Stretch,
}
impl Align {
/// Returns `true` for [`Align::Stretch`].
pub const fn is_stretch(self) -> bool {
matches!(self, Self::Stretch)
}
/// Returns the cross-axis offset of a `child` inside `avail`.
///
/// Stretch and start both produce zero; non-finite space produces zero.
pub fn offset(self, avail: f32, child: f32) -> f32 {
if !avail.is_finite() {
return 0.0;
}
let slack = (avail - child).max(0.0);
match self {
Self::Start | Self::Stretch => 0.0,
Self::Center => slack * 0.5,
Self::End => slack,
}
}
/// Uses the child alignment when set, otherwise the container alignment.
pub fn resolve(child: Option<Align>, container: Align) -> Align {
child.unwrap_or(container)
}
}
/// Layout style of a container that arranges its children on an axis.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContainerStyle {
/// Main axis along which children are placed.
pub axis: Axis,
/// Gap between children along the main axis.
pub gap: f32,
/// Gap between wrapped lines or cross-axis tracks.
pub cross_gap: f32,
/// Default cross-axis alignment for children.
pub align: Align,
/// Main-axis distribution of free space.
pub justify: Justify,
/// Whether children wrap onto multiple lines.
pub wrap: bool,
/// Cross-axis alignment of wrapped lines within the container.
pub align_content: Align,
}
impl Default for ContainerStyle {
fn default() -> Self {
Self {
axis: Axis::Horizontal,
gap: 0.0,
cross_gap: 0.0,
align: Align::Start,
justify: Justify::Start,
wrap: false,
align_content: Align::Start,
}
}
}
impl ContainerStyle {
/// Creates a style for `axis` with equal main and cross gaps.
pub fn new(axis: Axis, gap: f32) -> Self {
let gap = sanitize_gap(gap);
Self {
axis,
gap,
cross_gap: gap,
..Self::default()
}
}
/// Sets both the main and cross gaps.
pub fn gap(mut self, gap: f32) -> Self {
let gap = sanitize_gap(gap);
self.gap = gap;
self.cross_gap = gap;
self
}
/// Sets the main-axis gap.
pub fn main_gap(mut self, gap: f32) -> Self {
self.gap = sanitize_gap(gap);
self
}
/// Sets the cross-axis gap.
pub fn cross_gap(mut self, gap: f32) -> Self {
self.cross_gap = sanitize_gap(gap);
self
}
/// Sets the main and cross gaps independently.
pub fn gaps(mut self, main: f32, cross: f32) -> Self {
self.gap = sanitize_gap(main);
self.cross_gap = sanitize_gap(cross);
self
}
/// Sets the default cross-axis alignment of children.
pub fn align_items(mut self, align: Align) -> Self {
self.align = align;
self
}
/// Sets the main-axis distribution of free space.
pub fn justify_content(mut self, justify: Justify) -> Self {
self.justify = justify;
self
}
/// Enables or disables wrapping.
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap = wrap;
self
}
/// Enables wrapping.
pub fn wrapped(mut self) -> Self {
self.wrap = true;
self
}
/// Sets the cross-axis alignment of wrapped lines.
pub fn align_content(mut self, align: Align) -> Self {
self.align_content = align;
self
}
/// Sets the main axis.
pub fn axis(mut self, axis: Axis) -> Self {
self.axis = axis;
self
}
/// Returns the total main-axis gap for `count` children.
pub fn main_gaps_total(self, count: usize) -> f32 {
if count > 1 {
self.gap * (count - 1) as f32
} else {
0.0
}
}
}