summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-17 19:21:47 +0200
committerLaurenz <laurmaedje@gmail.com>2019-10-17 19:21:47 +0200
commit991e879e1d2ed53125dbff4edba80804ff28f2a9 (patch)
tree0917f83108feca10ca4207dd9089fe57cf8098d5 /src/layout
parent1987e5861cf2c033e3a540a5ef7c0f7106016929 (diff)
Extend stack layouts from vertical to horizontal flows ➡
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/flex.rs2
-rw-r--r--src/layout/mod.rs9
-rw-r--r--src/layout/stacked.rs56
-rw-r--r--src/layout/tree.rs1
4 files changed, 50 insertions, 18 deletions
diff --git a/src/layout/flex.rs b/src/layout/flex.rs
index a6f2e091..b97b4239 100644
--- a/src/layout/flex.rs
+++ b/src/layout/flex.rs
@@ -90,7 +90,7 @@ impl FlexLayouter {
ctx,
units: vec![],
- stack: StackLayouter::new(StackContext::from_flex_ctx(ctx)),
+ stack: StackLayouter::new(StackContext::from_flex_ctx(ctx, Flow::Vertical)),
usable_width: ctx.space.usable().x,
run: FlexRun {
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index fccbe8c8..470ac3ba 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -134,6 +134,8 @@ pub struct LayoutContext<'a, 'p> {
pub style: &'a TextStyle,
/// The alignment to use for the content.
pub alignment: Alignment,
+ /// How to stack the context.
+ pub flow: Flow,
/// The primary space to layout in.
pub space: LayoutSpace,
/// The additional spaces which are used when the primary space
@@ -176,6 +178,13 @@ pub enum Alignment {
Center,
}
+/// The flow of content.
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum Flow {
+ Vertical,
+ Horizontal,
+}
+
/// The error type for layouting.
pub enum LayoutError {
/// There is not enough space to add an item.
diff --git a/src/layout/stacked.rs b/src/layout/stacked.rs
index d87e5394..0097ce3e 100644
--- a/src/layout/stacked.rs
+++ b/src/layout/stacked.rs
@@ -26,15 +26,17 @@ pub struct StackContext {
pub space: LayoutSpace,
pub followup_spaces: Option<LayoutSpace>,
pub shrink_to_fit: bool,
+ pub flow: Flow,
}
macro_rules! reuse {
- ($ctx:expr) => {
+ ($ctx:expr, $flow:expr) => {
StackContext {
alignment: $ctx.alignment,
space: $ctx.space,
followup_spaces: $ctx.followup_spaces,
- shrink_to_fit: $ctx.shrink_to_fit
+ shrink_to_fit: $ctx.shrink_to_fit,
+ flow: $flow
}
};
}
@@ -42,12 +44,12 @@ macro_rules! reuse {
impl StackContext {
/// Create a stack context from a generic layout context.
pub fn from_layout_ctx(ctx: LayoutContext) -> StackContext {
- reuse!(ctx)
+ reuse!(ctx, ctx.flow)
}
/// Create a stack context from a flex context.
- pub fn from_flex_ctx(ctx: FlexContext) -> StackContext {
- reuse!(ctx)
+ pub fn from_flex_ctx(ctx: FlexContext, flow: Flow) -> StackContext {
+ reuse!(ctx, flow)
}
}
@@ -79,9 +81,15 @@ impl StackLayouter {
self.start_new_space()?;
}
- let new_dimensions = Size2D {
- x: crate::size::max(self.dimensions.x, layout.dimensions.x),
- y: self.dimensions.y + layout.dimensions.y,
+ 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) {
@@ -104,9 +112,13 @@ impl StackLayouter {
Alignment::Center => self.cursor - Size2D::with_x(layout.dimensions.x / 2),
};
- self.cursor.y += layout.dimensions.y;
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(())
@@ -120,23 +132,26 @@ impl StackLayouter {
Ok(())
}
- /// Add vertical space after the last layout.
+ /// Add space after the last layout.
pub fn add_space(&mut self, space: Size) -> LayoutResult<()> {
if !self.started {
self.start_new_space()?;
}
- let new_dimensions = self.dimensions + Size2D::with_y(space);
+ let new_space = match self.ctx.flow {
+ Flow::Vertical => Size2D::with_y(space),
+ Flow::Horizontal => Size2D::with_x(space),
+ };
- if self.overflows(new_dimensions) {
+ 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.y += space;
- self.dimensions.y += space;
+ self.cursor += new_space;
+ self.dimensions += new_space;
}
Ok(())
@@ -195,10 +210,17 @@ impl StackLayouter {
/// The remaining space for new layouts.
pub fn remaining(&self) -> Size2D {
- Size2D {
- x: self.usable.x,
- y: self.usable.y - self.dimensions.y,
+ 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.
diff --git a/src/layout/tree.rs b/src/layout/tree.rs
index 2c904369..3a58115f 100644
--- a/src/layout/tree.rs
+++ b/src/layout/tree.rs
@@ -96,6 +96,7 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
let mut ctx = self.ctx;
ctx.style = &self.style;
+ ctx.flow = Flow::Vertical;
ctx.shrink_to_fit = true;
ctx.space.dimensions = remaining;
ctx.space.padding = SizeBox::zero();