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
272
273
274
275
276
277
278
279
280
|
use smallvec::smallvec;
use super::*;
/// The stack layouter arranges boxes stacked onto each other.
///
/// The boxes are laid out in the direction of the secondary layouting axis and
/// are aligned along both axes.
#[derive(Debug, Clone)]
pub struct StackLayouter {
/// The context for layouter.
ctx: StackContext,
/// The output layouts.
layouts: MultiLayout,
/// The full layout space.
space: Space,
/// The currently active subspace.
sub: Subspace,
}
#[derive(Debug, Clone)]
struct Space {
/// The index of this space in the list of spaces.
index: usize,
/// Whether to add the layout for this space even if it would be empty.
hard: bool,
/// The layouting actions accumulated from the subspaces.
actions: LayoutActionList,
/// The used size of this space from the top-left corner to
/// the bottomright-most point of used space (specialized).
combined_dimensions: Size2D,
}
#[derive(Debug, Clone)]
struct Subspace {
/// The axes along which contents in this subspace are laid out.
axes: LayoutAxes,
/// The beginning of this subspace in the parent space (specialized).
origin: Size2D,
/// The total usable space of this subspace (generalized).
usable: Size2D,
/// The used size of this subspace (generalized), with
/// - `x` being the maximum of the primary size of all boxes.
/// - `y` being the total extent of all boxes and space in the secondary
/// direction.
size: Size2D,
/// The so-far accumulated (offset, anchor, box) triples.
boxes: Vec<(Size, Size, Layout)>,
/// The last added spacing if the last was spacing.
last_spacing: LastSpacing,
}
impl Space {
fn new(index: usize, hard: bool) -> Space {
Space {
index,
hard,
actions: LayoutActionList::new(),
combined_dimensions: Size2D::zero(),
}
}
}
impl Subspace {
fn new(origin: Size2D, usable: Size2D, axes: LayoutAxes) -> Subspace {
Subspace {
origin,
anchor: axes.anchor(usable),
factor: axes.secondary.axis.factor(),
boxes: vec![],
usable: axes.generalize(usable),
dimensions: Size2D::zero(),
space: LastSpacing::Forbidden,
}
}
}
/// The context for stack layouting.
///
/// See [`LayoutContext`] for details about the fields.
#[derive(Debug, Clone)]
pub struct StackContext {
pub spaces: LayoutSpaces,
pub axes: LayoutAxes,
pub expand: bool,
}
impl StackLayouter {
/// Create a new stack layouter.
pub fn new(ctx: StackContext) -> StackLayouter {
let axes = ctx.axes;
let space = ctx.spaces[0];
StackLayouter {
ctx,
layouts: MultiLayout::new(),
space: Space::new(0, true),
sub: Subspace::new(space.start(), space.usable(), axes),
}
}
pub fn add(&mut self, layout: Layout) -> LayoutResult<()> {
if let LastSpacing::Soft(space) = self.sub.space {
self.add_space(space, SpaceKind::Hard);
}
let size = self.ctx.axes.generalize(layout.dimensions);
let mut new_dimensions = Size2D {
x: crate::size::max(self.sub.dimensions.x, size.x),
y: self.sub.dimensions.y + size.y
};
while !self.sub.usable.fits(new_dimensions) {
if self.space_is_last() && self.space_is_empty() {
lerr!("box does not fit into stack");
}
self.finish_space(true);
new_dimensions = size;
}
let offset = self.sub.dimensions.y;
let anchor = self.ctx.axes.primary.anchor(size.x);
self.sub.boxes.push((offset, anchor, layout));
self.sub.dimensions = new_dimensions;
self.sub.space = LastSpacing::Allowed;
Ok(())
}
pub fn add_multiple(&mut self, layouts: MultiLayout) -> LayoutResult<()> {
for layout in layouts {
self.add(layout)?;
}
Ok(())
}
pub fn add_space(&mut self, space: Size, kind: SpaceKind) {
if kind == SpaceKind::Soft {
if self.sub.space != LastSpacing::Forbidden {
self.sub.space = LastSpacing::Soft(space);
}
} else {
if self.sub.dimensions.y + space > self.sub.usable.y {
self.sub.dimensions.y = self.sub.usable.y;
} else {
self.sub.dimensions.y += space;
}
if kind == SpaceKind::Hard {
self.sub.space = LastSpacing::Forbidden;
}
}
}
pub fn set_axes(&mut self, axes: LayoutAxes) {
if axes != self.ctx.axes {
self.finish_subspace();
let (origin, usable) = self.remaining_subspace();
self.ctx.axes = axes;
self.sub = Subspace::new(origin, usable, axes);
}
}
pub fn set_spaces(&mut self, spaces: LayoutSpaces, replace_empty: bool) {
if replace_empty && self.space_is_empty() {
self.ctx.spaces = spaces;
self.start_space(0, self.space.hard);
} else {
self.ctx.spaces.truncate(self.space.index + 1);
self.ctx.spaces.extend(spaces);
}
}
pub fn remaining(&self) -> LayoutSpaces {
let mut spaces = smallvec![LayoutSpace {
dimensions: self.remaining_subspace().1,
padding: SizeBox::zero(),
}];
for space in &self.ctx.spaces[self.next_space()..] {
spaces.push(space.usable_space());
}
spaces
}
pub fn primary_usable(&self) -> Size {
self.sub.usable.x
}
pub fn space_is_empty(&self) -> bool {
self.space.combined_dimensions == Size2D::zero()
&& self.space.actions.is_empty()
&& self.sub.dimensions == Size2D::zero()
}
pub fn space_is_last(&self) -> bool {
self.space.index == self.ctx.spaces.len() - 1
}
pub fn finish(mut self) -> MultiLayout {
if self.space.hard || !self.space_is_empty() {
self.finish_space(false);
}
self.layouts
}
pub fn finish_space(&mut self, hard: bool) {
self.finish_subspace();
let space = self.ctx.spaces[self.space.index];
self.layouts.add(Layout {
dimensions: match self.ctx.expand {
true => space.dimensions,
false => self.space.combined_dimensions.padded(space.padding),
},
actions: self.space.actions.to_vec(),
debug_render: true,
});
self.start_space(self.next_space(), hard);
}
fn start_space(&mut self, space: usize, hard: bool) {
self.space = Space::new(space, hard);
let space = self.ctx.spaces[space];
self.sub = Subspace::new(space.start(), space.usable(), self.ctx.axes);
}
fn next_space(&self) -> usize {
(self.space.index + 1).min(self.ctx.spaces.len() - 1)
}
fn finish_subspace(&mut self) {
let factor = self.ctx.axes.secondary.axis.factor();
let anchor =
self.ctx.axes.anchor(self.sub.usable)
- self.ctx.axes.anchor(Size2D::with_y(self.sub.dimensions.y));
for (offset, layout_anchor, layout) in self.sub.boxes.drain(..) {
let pos = self.sub.origin
+ self.ctx.axes.specialize(
anchor + Size2D::new(-layout_anchor, factor * offset)
);
self.space.actions.add_layout(pos, layout);
}
if self.ctx.axes.primary.needs_expansion() {
self.sub.dimensions.x = self.sub.usable.x;
}
if self.ctx.axes.secondary.needs_expansion() {
self.sub.dimensions.y = self.sub.usable.y;
}
let space = self.ctx.spaces[self.space.index];
let origin = self.sub.origin;
let dimensions = self.ctx.axes.specialize(self.sub.dimensions);
self.space.combined_dimensions.max_eq(origin - space.start() + dimensions);
}
fn remaining_subspace(&self) -> (Size2D, Size2D) {
let new_origin = self.sub.origin + match self.ctx.axes.secondary.axis.is_positive() {
true => self.ctx.axes.specialize(Size2D::with_y(self.sub.dimensions.y)),
false => Size2D::zero(),
};
let new_usable = self.ctx.axes.specialize(Size2D {
x: self.sub.usable.x,
y: self.sub.usable.y - self.sub.dimensions.y - self.sub.space.soft_or_zero(),
});
(new_origin, new_usable)
}
}
|