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
|
use super::*;
/// Layouts boxes stack-like.
///
/// The boxes are arranged vertically, each layout gettings it's own "line".
#[derive(Debug, Clone)]
pub struct StackLayouter {
ctx: StackContext,
layouts: MultiLayout,
actions: LayoutActionList,
space: LayoutSpace,
usable: Size2D,
dimensions: Size2D,
cursor: Size2D,
in_extra_space: bool,
started: bool,
}
/// The context for stack layouting.
///
/// See [`LayoutContext`] for details about the fields.
#[derive(Debug, Copy, Clone)]
pub struct StackContext {
pub alignment: Alignment,
pub space: LayoutSpace,
pub followup_spaces: Option<LayoutSpace>,
pub shrink_to_fit: bool,
pub flow: Flow,
}
macro_rules! reuse {
($ctx:expr, $flow:expr) => {
StackContext {
alignment: $ctx.alignment,
space: $ctx.space,
followup_spaces: $ctx.followup_spaces,
shrink_to_fit: $ctx.shrink_to_fit,
flow: $flow
}
};
}
impl StackContext {
/// Create a stack context from a generic layout context.
pub fn from_layout_ctx(ctx: LayoutContext) -> StackContext {
reuse!(ctx, ctx.flow)
}
/// Create a stack context from a flex context.
pub fn from_flex_ctx(ctx: FlexContext, flow: Flow) -> StackContext {
reuse!(ctx, flow)
}
}
impl StackLayouter {
/// Create a new stack layouter.
pub fn new(ctx: StackContext) -> StackLayouter {
StackLayouter {
ctx,
layouts: MultiLayout::new(),
actions: LayoutActionList::new(),
space: ctx.space,
usable: ctx.space.usable(),
dimensions: start_dimensions(ctx.alignment, ctx.space),
cursor: start_cursor(ctx.alignment, ctx.space),
in_extra_space: false,
started: true,
}
}
/// This layouter's context.
pub fn ctx(&self) -> StackContext {
self.ctx
}
/// Add a sublayout to the bottom.
pub fn add(&mut self, layout: Layout) -> LayoutResult<()> {
if !self.started {
self.start_new_space()?;
}
let new_dimensions = match self.ctx.flow {
Flow::Vertical => Size2D {
x: crate::size::max(self.dimensions.x, layout.dimensions.x),
y: self.dimensions.y + layout.dimensions.y,
},
Flow::Horizontal => Size2D {
x: self.dimensions.x + layout.dimensions.x,
y: crate::size::max(self.dimensions.y, layout.dimensions.y),
}
};
if self.overflows(new_dimensions) {
if self.ctx.followup_spaces.is_some() &&
!(self.in_extra_space && self.overflows(layout.dimensions))
{
self.finish_layout(true)?;
return self.add(layout);
} else {
return Err(LayoutError::NotEnoughSpace("cannot fit box into stack"));
}
}
// Determine where to put the box. When we right-align it, we want the
// cursor to point to the top-right corner of the box. Therefore, the
// position has to be moved to the left by the width of the box.
let position = match self.ctx.alignment {
Alignment::Left => self.cursor,
Alignment::Right => self.cursor - Size2D::with_x(layout.dimensions.x),
Alignment::Center => self.cursor - Size2D::with_x(layout.dimensions.x / 2),
};
self.dimensions = new_dimensions;
match self.ctx.flow {
Flow::Vertical => self.cursor.y += layout.dimensions.y,
Flow::Horizontal => self.cursor.x += layout.dimensions.x,
}
self.actions.add_layout(position, layout);
Ok(())
}
/// Add multiple sublayouts from a multi-layout.
pub fn add_many(&mut self, layouts: MultiLayout) -> LayoutResult<()> {
for layout in layouts {
self.add(layout)?;
}
Ok(())
}
/// Add space after the last layout.
pub fn add_space(&mut self, space: Size) -> LayoutResult<()> {
if !self.started {
self.start_new_space()?;
}
let new_space = match self.ctx.flow {
Flow::Vertical => Size2D::with_y(space),
Flow::Horizontal => Size2D::with_x(space),
};
if self.overflows(self.dimensions + new_space) {
if self.ctx.followup_spaces.is_some() {
self.finish_layout(false)?;
} else {
return Err(LayoutError::NotEnoughSpace("cannot fit space into stack"));
}
} else {
self.cursor += new_space;
self.dimensions += new_space;
}
Ok(())
}
/// Finish the layouting.
///
/// The layouter is not consumed by this to prevent ownership problems.
/// It should not be used further.
pub fn finish(&mut self) -> LayoutResult<MultiLayout> {
if self.started {
self.finish_layout(false)?;
}
Ok(std::mem::replace(&mut self.layouts, MultiLayout::new()))
}
/// Finish the current layout and start a new one in an extra space
/// (if there is an extra space).
///
/// If `start_new_empty` is true, a new empty layout will be started. Otherwise,
/// the new layout only emerges when new content is added.
pub fn finish_layout(&mut self, start_new_empty: bool) -> LayoutResult<()> {
let actions = std::mem::replace(&mut self.actions, LayoutActionList::new());
self.layouts.add(Layout {
dimensions: if self.ctx.shrink_to_fit {
self.dimensions.padded(self.space.padding)
} else {
self.space.dimensions
},
actions: actions.into_vec(),
debug_render: true,
});
self.started = false;
if start_new_empty {
self.start_new_space()?;
}
Ok(())
}
pub fn start_new_space(&mut self) -> LayoutResult<()> {
if let Some(space) = self.ctx.followup_spaces {
self.started = true;
self.space = space;
self.usable = space.usable();
self.dimensions = start_dimensions(self.ctx.alignment, space);
self.cursor = start_cursor(self.ctx.alignment, space);
self.in_extra_space = true;
Ok(())
} else {
Err(LayoutError::NotEnoughSpace("no extra space to start"))
}
}
/// The remaining space for new layouts.
pub fn remaining(&self) -> Size2D {
match self.ctx.flow {
Flow::Vertical => Size2D {
x: self.usable.x,
y: self.usable.y - self.dimensions.y,
},
Flow::Horizontal => Size2D {
x: self.usable.x - self.dimensions.x,
y: self.usable.y,
},
}
}
/// Whether the active space of this layouter contains no content.
pub fn current_space_is_empty(&self) -> bool {
!self.started || self.actions.is_empty()
}
fn overflows(&self, dimensions: Size2D) -> bool {
!self.usable.fits(dimensions)
}
}
fn start_dimensions(alignment: Alignment, space: LayoutSpace) -> Size2D {
match alignment {
Alignment::Left => Size2D::zero(),
Alignment::Right | Alignment::Center => Size2D::with_x(space.usable().x),
}
}
fn start_cursor(alignment: Alignment, space: LayoutSpace) -> Size2D {
Size2D {
// If left-align, the cursor points to the top-left corner of
// each box. If we right-align, it points to the top-right
// corner.
x: match alignment {
Alignment::Left => space.padding.left,
Alignment::Right => space.dimensions.x - space.padding.right,
Alignment::Center => space.padding.left + (space.usable().x / 2),
},
y: space.padding.top,
}
}
|