diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-10-05 16:34:20 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-10-05 16:44:35 +0200 |
| commit | 5a7a32a9bafbcc69077e7766451310ab5ece62bf (patch) | |
| tree | 29d5fc9ec380c2bd1b63bff1fd605408e8f53eea /src/layout | |
| parent | 93eaafb236bba73cd72c8cd142279366b8afa0be (diff) | |
Streamline generic layouting primitives 🛫
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/line.rs | 10 | ||||
| -rw-r--r-- | src/layout/mod.rs | 2 | ||||
| -rw-r--r-- | src/layout/primitive.rs | 143 | ||||
| -rw-r--r-- | src/layout/stack.rs | 21 |
4 files changed, 111 insertions, 65 deletions
diff --git a/src/layout/line.rs b/src/layout/line.rs index 19ec053d..745a0d14 100644 --- a/src/layout/line.rs +++ b/src/layout/line.rs @@ -67,7 +67,7 @@ impl LineLayouter { } else if align.primary > prev.primary { let mut rest_run = LineRun::new(); - let usable = self.stack.usable().primary(sys); + let usable = self.stack.usable().get(sys.primary.axis()); rest_run.usable = Some(match align.primary { GenAlign::Start => unreachable!("start > x"), GenAlign::Center => usable - 2.0 * self.run.size.width, @@ -181,7 +181,7 @@ impl LineLayouter { /// it will fit into this layouter's underlying stack. pub fn remaining(&self) -> Vec<LayoutSpace> { let mut spaces = self.stack.remaining(); - *spaces[0].size.secondary_mut(self.ctx.sys) -= self.run.size.height; + *spaces[0].size.get_mut(self.ctx.sys.secondary.axis()) -= self.run.size.height; spaces } @@ -213,7 +213,11 @@ impl LineLayouter { for (offset, child) in layouts { let x = match self.ctx.sys.primary.is_positive() { true => offset, - false => self.run.size.width - offset - child.size.primary(self.ctx.sys), + false => { + self.run.size.width + - offset + - child.size.get(self.ctx.sys.primary.axis()) + } }; let pos = Point::new(x, 0.0); diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 5e4e020e..2ba1b0de 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -11,7 +11,7 @@ pub use primitive::*; pub use stack::*; pub use tree::*; -use crate::geom::{Insets, Point, Rect, RectExt, Sides, Size, SizeExt}; +use crate::geom::{Insets, Point, Rect, RectExt, Size, SizeExt}; use crate::diag::Diag; use crate::eval::{PageState, State, TextState}; diff --git a/src/layout/primitive.rs b/src/layout/primitive.rs index d13110e3..64cd1dff 100644 --- a/src/layout/primitive.rs +++ b/src/layout/primitive.rs @@ -27,7 +27,7 @@ impl Default for LayoutAlign { pub type LayoutExpansion = Spec2<bool>; /// The four directions into which content can be laid out. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Dir { /// Left to right. LTR, @@ -40,26 +40,6 @@ pub enum Dir { } impl Dir { - /// The side this direction starts at. - pub fn start(self) -> Side { - match self { - Self::LTR => Side::Left, - Self::RTL => Side::Right, - Self::TTB => Side::Top, - Self::BTT => Side::Bottom, - } - } - - /// The side this direction ends at. - pub fn end(self) -> Side { - match self { - Self::LTR => Side::Right, - Self::RTL => Side::Left, - Self::TTB => Side::Bottom, - Self::BTT => Side::Top, - } - } - /// The specific axis this direction belongs to. pub fn axis(self) -> SpecAxis { match self { @@ -95,6 +75,18 @@ impl Dir { Self::BTT => Self::TTB, } } + + /// The side of this direction the alignment identifies. + /// + /// `Center` alignment is treated the same as `Start` alignment. + pub fn side(self, align: GenAlign) -> Side { + match if align == GenAlign::End { self.inv() } else { self } { + Self::LTR => Side::Left, + Self::RTL => Side::Right, + Self::TTB => Side::Top, + Self::BTT => Side::Bottom, + } + } } impl Display for Dir { @@ -109,7 +101,7 @@ impl Display for Dir { } /// The two generic layouting axes. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum GenAxis { /// The primary layouting direction into which text and lines flow. Primary, @@ -134,7 +126,7 @@ impl Display for GenAxis { } /// The two specific layouting axes. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum SpecAxis { /// The horizontal layouting axis. Horizontal, @@ -162,17 +154,8 @@ impl Display for SpecAxis { } } -/// A side of a container. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum Side { - Left, - Top, - Right, - Bottom, -} - /// Where to align content along an axis in a generic context. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] pub enum GenAlign { Start, Center, @@ -201,7 +184,7 @@ impl Display for GenAlign { } /// Where to align content along an axis in a specific context. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] pub enum SpecAlign { Left, Right, @@ -256,8 +239,29 @@ impl Display for SpecAlign { } } +/// A side of a container. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum Side { + Left, + Top, + Right, + Bottom, +} + +impl Side { + /// The opposite side. + pub fn inv(self) -> Self { + match self { + Self::Left => Self::Right, + Self::Top => Self::Bottom, + Self::Right => Self::Left, + Self::Bottom => Self::Top, + } + } +} + /// A generic container with two components for the two generic axes. -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] pub struct Gen2<T> { /// The primary component. pub primary: T, @@ -279,14 +283,6 @@ impl<T> Gen2<T> { } } - /// Borrow the component for the specified generic axis. - pub fn get_ref(&mut self, axis: GenAxis) -> &T { - match axis { - GenAxis::Primary => &mut self.primary, - GenAxis::Secondary => &mut self.secondary, - } - } - /// Borrow the component for the specified generic axis mutably. pub fn get_mut(&mut self, axis: GenAxis) -> &mut T { match axis { @@ -297,7 +293,7 @@ impl<T> Gen2<T> { } /// A generic container with two components for the two specific axes. -#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] pub struct Spec2<T> { /// The horizontal component. pub horizontal: T, @@ -319,19 +315,64 @@ impl<T> Spec2<T> { } } - /// Borrow the component for the given specific axis. - pub fn get_ref(&mut self, axis: SpecAxis) -> &T { + /// Borrow the component for the given specific axis mutably. + pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T { match axis { SpecAxis::Horizontal => &mut self.horizontal, SpecAxis::Vertical => &mut self.vertical, } } +} - /// Borrow the component for the given specific axis mutably. - pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T { - match axis { - SpecAxis::Horizontal => &mut self.horizontal, - SpecAxis::Vertical => &mut self.vertical, +/// A generic container with left, top, right and bottom components. +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] +pub struct Sides<T> { + /// The value for the left side. + pub left: T, + /// The value for the top side. + pub top: T, + /// The value for the right side. + pub right: T, + /// The value for the bottom side. + pub bottom: T, +} + +impl<T> Sides<T> { + /// Create a new box from four sizes. + pub fn new(left: T, top: T, right: T, bottom: T) -> Self { + Self { left, top, right, bottom } + } + + /// Create an instance with all four components set to the same `value`. + pub fn uniform(value: T) -> Self + where + T: Clone, + { + Self { + left: value.clone(), + top: value.clone(), + right: value.clone(), + bottom: value, + } + } + + /// Return the component for the given side. + pub fn get(self, side: Side) -> T { + match side { + Side::Left => self.left, + Side::Right => self.right, + Side::Top => self.top, + Side::Bottom => self.bottom, + } + } + + /// Borrow the component for the given side mutably. + pub fn get_mut(&mut self, side: Side) -> &mut T { + match side { + Side::Left => &mut self.left, + Side::Right => &mut self.right, + Side::Top => &mut self.top, + Side::Bottom => &mut self.bottom, } } } diff --git a/src/layout/stack.rs b/src/layout/stack.rs index 04c78da5..de933fbf 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -90,9 +90,10 @@ impl StackLayouter { // A hard space is simply an empty box. SpacingKind::Hard => { // Reduce the spacing such that it definitely fits. - spacing = spacing.min(self.space.usable.secondary(self.ctx.sys)); - let size = Size::new(0.0, spacing); + let axis = self.ctx.sys.secondary.axis(); + spacing = spacing.min(self.space.usable.get(axis)); + let size = Size::new(0.0, spacing); self.update_metrics(size); self.space.layouts.push(( self.ctx.sys, @@ -133,29 +134,29 @@ impl StackLayouter { self.space.size = size.specialized(sys); self.space.extra = extra.specialized(sys); - *self.space.usable.secondary_mut(sys) -= added.height; + *self.space.usable.get_mut(sys.secondary.axis()) -= added.height; } /// Returns true if a space break is necessary. fn update_rulers(&mut self, align: LayoutAlign) -> bool { let allowed = self.is_fitting_alignment(align); if allowed { - *self.space.rulers.get_mut(self.ctx.sys.secondary, GenAlign::Start) = - align.secondary; + let side = self.ctx.sys.secondary.side(GenAlign::Start); + *self.space.rulers.get_mut(side) = align.secondary; } allowed } /// Whether a layout with the given alignment can still be layouted into the /// active space or a space break is necessary. - pub(crate) fn is_fitting_alignment(&mut self, align: LayoutAlign) -> bool { + pub(crate) fn is_fitting_alignment(&self, align: LayoutAlign) -> bool { self.is_fitting_axis(self.ctx.sys.primary, align.primary) && self.is_fitting_axis(self.ctx.sys.secondary, align.secondary) } - fn is_fitting_axis(&mut self, dir: Dir, align: GenAlign) -> bool { - align >= *self.space.rulers.get_mut(dir, GenAlign::Start) - && align <= self.space.rulers.get_mut(dir, GenAlign::End).inv() + fn is_fitting_axis(&self, dir: Dir, align: GenAlign) -> bool { + align >= self.space.rulers.get(dir.side(GenAlign::Start)) + && align <= self.space.rulers.get(dir.side(GenAlign::End)).inv() } /// Update the layouting system. @@ -284,7 +285,7 @@ impl StackLayouter { // the usable space for following layouts at its origin by its // extent along the secondary axis. *bound.get_mut(sys.secondary, GenAlign::Start) += - sys.secondary.factor() * layout.size.secondary(*sys); + sys.secondary.factor() * layout.size.get(sys.secondary.axis()); } // ------------------------------------------------------------------ // |
