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
|
use super::*;
/// A node that stacks and align its children.
#[derive(Debug, Clone, PartialEq)]
pub struct Stack {
/// The `main` and `cross` directions of this stack.
///
/// The children are stacked along the `main` direction. The `cross`
/// direction is required for aligning the children.
pub flow: Flow,
/// How to align this stack in _its_ parent.
pub align: BoxAlign,
/// Whether to expand the axes to fill the area or to fit the content.
pub expansion: Gen<Expansion>,
/// The nodes to be stacked.
pub children: Vec<LayoutNode>,
}
impl Layout for Stack {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
let mut layouter = StackLayouter::new(self, areas.clone());
for child in &self.children {
match child.layout(ctx, &layouter.areas) {
Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
Layouted::Layout(layout, align) => layouter.push_layout(layout, align),
Layouted::Layouts(layouts, align) => {
for layout in layouts {
layouter.push_layout(layout, align);
}
}
}
}
Layouted::Layouts(layouter.finish(), self.align)
}
}
impl From<Stack> for LayoutNode {
fn from(stack: Stack) -> Self {
Self::dynamic(stack)
}
}
struct StackLayouter<'a> {
stack: &'a Stack,
main: SpecAxis,
flow: Flow,
areas: Areas,
finished: Vec<BoxLayout>,
layouts: Vec<(Length, BoxLayout, BoxAlign)>,
used: Gen<Length>,
ruler: Align,
}
impl<'a> StackLayouter<'a> {
fn new(stack: &'a Stack, areas: Areas) -> Self {
Self {
stack,
main: stack.flow.main.axis(),
flow: stack.flow,
areas,
finished: vec![],
layouts: vec![],
used: Gen::ZERO,
ruler: Align::Start,
}
}
fn push_spacing(&mut self, amount: Length) {
let main_rest = self.areas.current.rem.get_mut(self.main);
let capped = amount.min(*main_rest);
*main_rest -= capped;
self.used.main += capped;
}
fn push_layout(&mut self, layout: BoxLayout, align: BoxAlign) {
if self.ruler > align.main {
self.finish_area();
}
while !self.areas.current.rem.fits(layout.size) {
if self.areas.in_full_last() {
// TODO: Diagnose once the necessary spans exist.
let _ = warning!("cannot fit box into any area");
break;
} else {
self.finish_area();
}
}
let size = layout.size.switch(self.flow);
self.layouts.push((self.used.main, layout, align));
*self.areas.current.rem.get_mut(self.main) -= size.main;
self.used.main += size.main;
self.used.cross = self.used.cross.max(size.cross);
self.ruler = align.main;
}
fn finish_area(&mut self) {
let full_size = {
let full = self.areas.current.full.switch(self.flow);
Gen::new(
match self.stack.expansion.main {
Expansion::Fill => full.main,
Expansion::Fit => self.used.main.min(full.main),
},
match self.stack.expansion.cross {
Expansion::Fill => full.cross,
Expansion::Fit => self.used.cross.min(full.cross),
},
)
};
let mut output = BoxLayout::new(full_size.switch(self.flow).to_size());
for (before, layout, align) in std::mem::take(&mut self.layouts) {
let child_size = layout.size.switch(self.flow);
// Align along the main axis.
let main = align.main.resolve(if self.flow.main.is_positive() {
let after_with_self = self.used.main - before;
before .. full_size.main - after_with_self
} else {
let before_with_self = before + child_size.main;
let after = self.used.main - (before + child_size.main);
full_size.main - before_with_self .. after
});
// Align along the cross axis.
let cross = align.cross.resolve(if self.flow.cross.is_positive() {
Length::ZERO .. full_size.cross - child_size.cross
} else {
full_size.cross - child_size.cross .. Length::ZERO
});
let pos = Gen::new(main, cross).switch(self.flow).to_point();
output.push_layout(pos, layout);
}
self.finished.push(output);
self.areas.next();
self.used = Gen::ZERO;
self.ruler = Align::Start;
}
fn finish(mut self) -> Vec<BoxLayout> {
self.finish_area();
self.finished
}
}
|