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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
use super::{AlignElem, Spacing};
use crate::prelude::*;
/// Arranges content and spacing horizontally or vertically.
///
/// The stack places a list of items along an axis, with optional spacing
/// between each item.
///
/// ## Example { #example }
/// ```example
/// #stack(
/// dir: ttb,
/// rect(width: 40pt),
/// rect(width: 120pt),
/// rect(width: 90pt),
/// )
/// ```
///
/// Display: Stack
/// Category: layout
#[element(Layout)]
pub struct StackElem {
/// The direction along which the items are stacked. Possible values are:
///
/// - `{ltr}`: Left to right.
/// - `{rtl}`: Right to left.
/// - `{ttb}`: Top to bottom.
/// - `{btt}`: Bottom to top.
///
/// You cab use the `start` and `end` methods to obtain the initial and
/// final points (respectively) of a direction, as `alignment`. You can also
/// use the `axis` method to determine whether a direction is
/// `{"horizontal"}` or `{"vertical"}`. The `inv` method returns a
/// direction's inverse direction.
///
/// For example, `{ttb.start()}` is `top`, `{ttb.end()}` is `bottom`,
/// `{ttb.axis()}` is `{"vertical"}` and `{ttb.inv()}` is equal to `btt`.
#[default(Dir::TTB)]
pub dir: Dir,
/// Spacing to insert between items where no explicit spacing was provided.
pub spacing: Option<Spacing>,
/// The children to stack along the axis.
#[variadic]
pub children: Vec<StackChild>,
}
impl Layout for StackElem {
#[tracing::instrument(name = "StackElem::layout", skip_all)]
fn layout(
&self,
vt: &mut Vt,
styles: StyleChain,
regions: Regions,
) -> SourceResult<Fragment> {
let mut layouter = StackLayouter::new(self.dir(styles), regions, styles);
// Spacing to insert before the next block.
let spacing = self.spacing(styles);
let mut deferred = None;
for child in self.children() {
match child {
StackChild::Spacing(kind) => {
layouter.layout_spacing(kind);
deferred = None;
}
StackChild::Block(block) => {
if let Some(kind) = deferred {
layouter.layout_spacing(kind);
}
layouter.layout_block(vt, &block, styles)?;
deferred = spacing;
}
}
}
Ok(layouter.finish())
}
}
/// A child of a stack element.
#[derive(Hash)]
pub enum StackChild {
/// Spacing between other children.
Spacing(Spacing),
/// Arbitrary block-level content.
Block(Content),
}
impl Debug for StackChild {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Spacing(kind) => kind.fmt(f),
Self::Block(block) => block.fmt(f),
}
}
}
cast! {
StackChild,
self => match self {
Self::Spacing(spacing) => spacing.into_value(),
Self::Block(content) => content.into_value(),
},
v: Spacing => Self::Spacing(v),
v: Content => Self::Block(v),
}
/// Performs stack layout.
struct StackLayouter<'a> {
/// The stacking direction.
dir: Dir,
/// The axis of the stacking direction.
axis: Axis,
/// The regions to layout children into.
regions: Regions<'a>,
/// The inherited styles.
styles: StyleChain<'a>,
/// Whether the stack itself should expand to fill the region.
expand: Axes<bool>,
/// The initial size of the current region before we started subtracting.
initial: Size,
/// The generic size used by the frames for the current region.
used: Gen<Abs>,
/// The sum of fractions in the current region.
fr: Fr,
/// Already layouted items whose exact positions are not yet known due to
/// fractional spacing.
items: Vec<StackItem>,
/// Finished frames for previous regions.
finished: Vec<Frame>,
}
/// A prepared item in a stack layout.
enum StackItem {
/// Absolute spacing between other items.
Absolute(Abs),
/// Fractional spacing between other items.
Fractional(Fr),
/// A frame for a layouted block.
Frame(Frame, Axes<Align>),
}
impl<'a> StackLayouter<'a> {
/// Create a new stack layouter.
fn new(dir: Dir, mut regions: Regions<'a>, styles: StyleChain<'a>) -> Self {
let axis = dir.axis();
let expand = regions.expand;
// Disable expansion along the block axis for children.
regions.expand.set(axis, false);
Self {
dir,
axis,
regions,
styles,
expand,
initial: regions.size,
used: Gen::zero(),
fr: Fr::zero(),
items: vec![],
finished: vec![],
}
}
/// Add spacing along the spacing direction.
#[tracing::instrument(name = "StackLayouter::layout_spacing", skip_all)]
fn layout_spacing(&mut self, spacing: Spacing) {
match spacing {
Spacing::Rel(v) => {
// Resolve the spacing and limit it to the remaining space.
let resolved = v
.resolve(self.styles)
.relative_to(self.regions.base().get(self.axis));
let remaining = self.regions.size.get_mut(self.axis);
let limited = resolved.min(*remaining);
if self.dir.axis() == Axis::Y {
*remaining -= limited;
}
self.used.main += limited;
self.items.push(StackItem::Absolute(resolved));
}
Spacing::Fr(v) => {
self.fr += v;
self.items.push(StackItem::Fractional(v));
}
}
}
/// Layout an arbitrary block.
#[tracing::instrument(name = "StackLayouter::layout_block", skip_all)]
fn layout_block(
&mut self,
vt: &mut Vt,
block: &Content,
styles: StyleChain,
) -> SourceResult<()> {
if self.regions.is_full() {
self.finish_region();
}
// Block-axis alignment of the `AlignElement` is respected by stacks.
let aligns = if let Some(align) = block.to::<AlignElem>() {
align.alignment(styles)
} else if let Some((_, local)) = block.to_styled() {
AlignElem::alignment_in(styles.chain(local))
} else {
AlignElem::alignment_in(styles)
}
.resolve(styles);
let fragment = block.layout(vt, styles, self.regions)?;
let len = fragment.len();
for (i, frame) in fragment.into_iter().enumerate() {
// Grow our size, shrink the region and save the frame for later.
let size = frame.size();
if self.dir.axis() == Axis::Y {
self.regions.size.y -= size.y;
}
let gen = match self.axis {
Axis::X => Gen::new(size.y, size.x),
Axis::Y => Gen::new(size.x, size.y),
};
self.used.main += gen.main;
self.used.cross.set_max(gen.cross);
self.items.push(StackItem::Frame(frame, aligns));
if i + 1 < len {
self.finish_region();
}
}
Ok(())
}
/// Advance to the next region.
fn finish_region(&mut self) {
// Determine the size of the stack in this region depending on whether
// the region expands.
let mut size = self
.expand
.select(self.initial, self.used.into_axes(self.axis))
.min(self.initial);
// Expand fully if there are fr spacings.
let full = self.initial.get(self.axis);
let remaining = full - self.used.main;
if self.fr.get() > 0.0 && full.is_finite() {
self.used.main = full;
size.set(self.axis, full);
}
let mut output = Frame::new(size);
let mut cursor = Abs::zero();
let mut ruler: Align = self.dir.start().into();
// Place all frames.
for item in self.items.drain(..) {
match item {
StackItem::Absolute(v) => cursor += v,
StackItem::Fractional(v) => cursor += v.share(self.fr, remaining),
StackItem::Frame(frame, aligns) => {
if self.dir.is_positive() {
ruler = ruler.max(aligns.get(self.axis));
} else {
ruler = ruler.min(aligns.get(self.axis));
}
// Align along the main axis.
let parent = size.get(self.axis);
let child = frame.size().get(self.axis);
let main = ruler.position(parent - self.used.main)
+ if self.dir.is_positive() {
cursor
} else {
self.used.main - child - cursor
};
// Align along the cross axis.
let other = self.axis.other();
let cross = aligns
.get(other)
.position(size.get(other) - frame.size().get(other));
let pos = Gen::new(cross, main).to_point(self.axis);
cursor += child;
output.push_frame(pos, frame);
}
}
}
// Advance to the next region.
self.regions.next();
self.initial = self.regions.size;
self.used = Gen::zero();
self.fr = Fr::zero();
self.finished.push(output);
}
/// Finish layouting and return the resulting frames.
fn finish(mut self) -> Fragment {
self.finish_region();
Fragment::frames(self.finished)
}
}
/// A container with a main and cross component.
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
struct Gen<T> {
/// The main component.
pub cross: T,
/// The cross component.
pub main: T,
}
impl<T> Gen<T> {
/// Create a new instance from the two components.
const fn new(cross: T, main: T) -> Self {
Self { cross, main }
}
/// Convert to the specific representation, given the current main axis.
fn into_axes(self, main: Axis) -> Axes<T> {
match main {
Axis::X => Axes::new(self.main, self.cross),
Axis::Y => Axes::new(self.cross, self.main),
}
}
}
impl Gen<Abs> {
/// The zero value.
fn zero() -> Self {
Self { cross: Abs::zero(), main: Abs::zero() }
}
/// Convert to a point.
fn to_point(self, main: Axis) -> Point {
self.into_axes(main).to_point()
}
}
|