summaryrefslogtreecommitdiff
path: root/src/layout/flex.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-11-17 11:53:59 +0100
committerLaurenz <laurmaedje@gmail.com>2019-11-17 11:53:59 +0100
commit467d7203ee67ce29ce9ba62c9406f5586cb5214a (patch)
treef2097653bdb6ad1b9ab6ed1bbe0c2e504b35c269 /src/layout/flex.rs
parentbd66ebd68344f0d02f5dc6163f7d2aa0b4ded79d (diff)
Remove top-level stack layouter from tree layouter 🗑
Diffstat (limited to 'src/layout/flex.rs')
-rw-r--r--src/layout/flex.rs87
1 files changed, 58 insertions, 29 deletions
diff --git a/src/layout/flex.rs b/src/layout/flex.rs
index 01086c32..dfe38bba 100644
--- a/src/layout/flex.rs
+++ b/src/layout/flex.rs
@@ -20,8 +20,8 @@ use super::*;
#[derive(Debug, Clone)]
pub struct FlexLayouter {
ctx: FlexContext,
- units: Vec<FlexUnit>,
stack: StackLayouter,
+ units: Vec<FlexUnit>,
usable: Size,
run: FlexRun,
@@ -35,6 +35,7 @@ pub struct FlexLayouter {
pub struct FlexContext {
pub spaces: LayoutSpaces,
pub axes: LayoutAxes,
+ pub shrink_to_fit: bool,
/// The spacing between two lines of boxes.
pub flex_spacing: Size,
}
@@ -63,6 +64,7 @@ impl FlexLayouter {
let stack = StackLayouter::new(StackContext {
spaces: ctx.spaces,
axes: ctx.axes,
+ shrink_to_fit: ctx.shrink_to_fit,
});
FlexLayouter {
@@ -88,14 +90,20 @@ impl FlexLayouter {
}
}
+ /// Add a forced run break.
+ pub fn add_run_break(&mut self) {
+ self.units.push(FlexUnit::Break);
+ }
+
/// Add a space box which can be replaced by a run break.
- pub fn add_space(&mut self, space: Size) {
+ pub fn add_primary_space(&mut self, space: Size) {
self.units.push(FlexUnit::Space(space));
}
- /// Add a forced run break.
- pub fn add_break(&mut self) {
- self.units.push(FlexUnit::Break);
+ pub fn add_secondary_space(&mut self, space: Size) -> LayoutResult<()> {
+ self.finish_box()?;
+ self.stack.add_space(space);
+ Ok(())
}
/// Update the axes in use by this flex layouter.
@@ -109,6 +117,21 @@ impl FlexLayouter {
/// with borrowed layouters. The state of the layouter is not reset.
/// Therefore, it should not be further used after calling `finish`.
pub fn finish(&mut self) -> LayoutResult<MultiLayout> {
+ self.finish_box()?;
+ Ok(self.stack.finish())
+ }
+
+ pub fn finish_layout(&mut self, hard: bool) -> LayoutResult<()> {
+ self.finish_box()?;
+ self.stack.finish_layout(hard);
+ Ok(())
+ }
+
+ pub fn finish_box(&mut self) -> LayoutResult<()> {
+ if self.box_is_empty() {
+ return Ok(());
+ }
+
// Move the units out of the layout because otherwise, we run into
// ownership problems.
let units = std::mem::replace(&mut self.units, vec![]);
@@ -131,7 +154,28 @@ impl FlexLayouter {
// Finish the last flex run.
self.finish_run()?;
- Ok(self.stack.finish())
+ Ok(())
+ }
+
+ /// Finish the current flex run.
+ fn finish_run(&mut self) -> LayoutResult<()> {
+ let mut actions = LayoutActionList::new();
+ for (x, layout) in self.run.content.drain(..) {
+ let position = self.ctx.axes.specialize(Size2D::with_x(x));
+ actions.add_layout(position, layout);
+ }
+
+ self.run.size.y += self.ctx.flex_spacing;
+
+ self.stack.add(Layout {
+ dimensions: self.ctx.axes.specialize(self.run.size),
+ actions: actions.into_vec(),
+ debug_render: false,
+ })?;
+
+ self.run.size = Size2D::zero();
+
+ Ok(())
}
/// Layout a content box into the current flex run or start a new run if
@@ -150,7 +194,7 @@ impl FlexLayouter {
Err(LayoutError::NotEnoughSpace("cannot fix box into flex run"))?;
}
- self.stack.add_break(true);
+ self.stack.finish_layout(true);
self.usable = self.stack.usable().x;
}
@@ -174,34 +218,19 @@ impl FlexLayouter {
// TODO
}
- /// Finish the current flex run.
- fn finish_run(&mut self) -> LayoutResult<()> {
- let mut actions = LayoutActionList::new();
- for (x, layout) in self.run.content.drain(..) {
- let position = self.ctx.axes.specialize(Size2D::with_x(x));
- actions.add_layout(position, layout);
- }
-
- self.run.size.y += self.ctx.flex_spacing;
-
- self.stack.add(Layout {
- dimensions: self.ctx.axes.specialize(self.run.size),
- actions: actions.into_vec(),
- debug_render: false,
- })?;
-
- self.run.size = Size2D::zero();
-
- Ok(())
- }
-
/// This layouter's context.
pub fn ctx(&self) -> FlexContext {
self.ctx
}
+ pub fn remaining(&self) -> LayoutResult<LayoutSpaces> {
+ let mut future = self.clone();
+ future.finish_box()?;
+ Ok(future.stack.remaining())
+ }
+
/// Whether this layouter contains any items.
- pub fn is_empty(&self) -> bool {
+ pub fn box_is_empty(&self) -> bool {
self.units.is_empty()
}
}