diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-12-04 19:34:29 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-12-04 19:35:28 +0100 |
| commit | 9fb31defd037a90bf8f9e38fa33acae23a70b269 (patch) | |
| tree | e0fd887792a59cbb3262a5d3157d0c786df56d60 /src/layout | |
| parent | ace57c34206a13b4bc3885b944cc51e274f30b0f (diff) | |
Expand functionality of function! macro 🛰
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/flex.rs | 18 | ||||
| -rw-r--r-- | src/layout/mod.rs | 116 | ||||
| -rw-r--r-- | src/layout/stack.rs | 2 | ||||
| -rw-r--r-- | src/layout/text.rs | 2 | ||||
| -rw-r--r-- | src/layout/tree.rs | 6 |
5 files changed, 119 insertions, 25 deletions
diff --git a/src/layout/flex.rs b/src/layout/flex.rs index afca23ec..3e8a64e1 100644 --- a/src/layout/flex.rs +++ b/src/layout/flex.rs @@ -14,7 +14,7 @@ pub struct FlexLayouter { #[derive(Debug, Clone)] enum FlexUnit { Boxed(Layout), - Space(Size, SpaceKind), + Space(Size, SpacingKind), SetAxes(LayoutAxes), Break, } @@ -102,11 +102,11 @@ impl FlexLayouter { self.units.push(FlexUnit::Break); } - pub fn add_primary_space(&mut self, space: Size, kind: SpaceKind) { + pub fn add_primary_space(&mut self, space: Size, kind: SpacingKind) { self.units.push(FlexUnit::Space(space, kind)) } - pub fn add_secondary_space(&mut self, space: Size, kind: SpaceKind) -> LayoutResult<()> { + pub fn add_secondary_space(&mut self, space: Size, kind: SpacingKind) -> LayoutResult<()> { if !self.run_is_empty() { self.finish_run()?; } @@ -179,7 +179,7 @@ impl FlexLayouter { debug_render: false, })?; - self.stack.add_spacing(self.flex_spacing, SpaceKind::Independent); + self.stack.add_spacing(self.flex_spacing, SpacingKind::Independent); let remaining = self.axes.specialize(Size2D { x: self.part.usable @@ -230,7 +230,7 @@ impl FlexLayouter { while size.x > self.line.usable { if self.stack.space_is_last() { - lerr!("box does not fit into line"); + lr!("box does not fit into line"); } self.stack.finish_space(true); @@ -238,7 +238,7 @@ impl FlexLayouter { } if let LastSpacing::Soft(space) = self.part.space { - self.layout_space(space, SpaceKind::Hard); + self.layout_space(space, SpacingKind::Hard); } let offset = self.part.dimensions.x; @@ -251,8 +251,8 @@ impl FlexLayouter { Ok(()) } - fn layout_space(&mut self, space: Size, kind: SpaceKind) { - if kind == SpaceKind::Soft { + fn layout_space(&mut self, space: Size, kind: SpacingKind) { + if kind == SpacingKind::Soft { if self.part.space != LastSpacing::Forbidden { self.part.space = LastSpacing::Soft(space); } @@ -263,7 +263,7 @@ impl FlexLayouter { self.part.dimensions.x += space; } - if kind == SpaceKind::Hard { + if kind == SpacingKind::Hard { self.part.space = LastSpacing::Forbidden; } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index f45ed10a..4304e46e 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -102,7 +102,7 @@ impl LayoutSpace { } /// The axes along which the content is laid out. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct LayoutAxes { pub primary: Axis, pub secondary: Axis, @@ -136,17 +136,66 @@ impl LayoutAxes { // at the call site, we still have this second function. self.generalize(size) } -} -/// The two kinds of axes. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub enum AxisKind { - Primary, - Secondary, + /// Returns the generic axis kind which is the horizontal axis. + pub fn horizontal(&self) -> GenericAxisKind { + match self.primary.is_horizontal() { + true => GenericAxisKind::Primary, + false => GenericAxisKind::Secondary, + } + } + + /// Returns the generic axis kind which is the vertical axis. + pub fn vertical(&self) -> GenericAxisKind { + self.horizontal().inv() + } + + /// Returns the specific axis kind which is the primary axis. + pub fn primary(&self) -> SpecificAxisKind { + match self.primary.is_horizontal() { + true => SpecificAxisKind::Horizontal, + false => SpecificAxisKind::Vertical, + } + } + + /// Returns the specific axis kind which is the secondary axis. + pub fn secondary(&self) -> SpecificAxisKind { + self.primary().inv() + } + + /// Returns the generic alignment corresponding to left-alignment. + pub fn left(&self) -> Alignment { + let positive = match self.primary.is_horizontal() { + true => self.primary.is_positive(), + false => self.secondary.is_positive(), + }; + + if positive { Alignment::Origin } else { Alignment::End } + } + + /// Returns the generic alignment corresponding to right-alignment. + pub fn right(&self) -> Alignment { + self.left().inv() + } + + /// Returns the generic alignment corresponding to top-alignment. + pub fn top(&self) -> Alignment { + let positive = match self.primary.is_horizontal() { + true => self.secondary.is_positive(), + false => self.primary.is_positive(), + }; + + if positive { Alignment::Origin } else { Alignment::End } + } + + /// Returns the generic alignment corresponding to bottom-alignment. + pub fn bottom(&self) -> Alignment { + self.top().inv() + } } /// Directions along which content is laid out. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Axis { LeftToRight, RightToLeft, @@ -180,8 +229,42 @@ impl Axis { } } +/// The two generic kinds of layouting axes. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum GenericAxisKind { + Primary, + Secondary, +} + +impl GenericAxisKind { + /// The other axis. + pub fn inv(&self) -> GenericAxisKind { + match self { + GenericAxisKind::Primary => GenericAxisKind::Secondary, + GenericAxisKind::Secondary => GenericAxisKind::Primary, + } + } +} + +/// The two specific kinds of layouting axes. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum SpecificAxisKind { + Horizontal, + Vertical, +} + +impl SpecificAxisKind { + /// The other axis. + pub fn inv(&self) -> SpecificAxisKind { + match self { + SpecificAxisKind::Horizontal => SpecificAxisKind::Vertical, + SpecificAxisKind::Vertical => SpecificAxisKind::Horizontal, + } + } +} + /// The place to put a layout in a container. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct LayoutAlignment { pub primary: Alignment, pub secondary: Alignment, @@ -194,13 +277,24 @@ impl LayoutAlignment { } /// Where to align content. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum Alignment { Origin, Center, End, } +impl Alignment { + /// The inverse alignment. + pub fn inv(&self) -> Alignment { + match self { + Alignment::Origin => Alignment::End, + Alignment::Center => Alignment::Center, + Alignment::End => Alignment::Origin, + } + } +} + /// The specialized anchor position for an item with the given alignment in a /// container with a given size along the given axis. pub fn anchor(axis: Axis, size: Size, alignment: Alignment) -> Size { @@ -213,7 +307,7 @@ pub fn anchor(axis: Axis, size: Size, alignment: Alignment) -> Size { } /// Whitespace between boxes with different interaction properties. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum SpacingKind { /// A hard space consumes surrounding soft spaces and is always layouted. Hard, diff --git a/src/layout/stack.rs b/src/layout/stack.rs index 793c2044..64823b67 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -108,7 +108,7 @@ impl StackLayouter { // Find the first (sub-)space that fits the layout. while !self.sub.usable.fits(new_size) { if self.space_is_last() && self.space_is_empty() { - lerr!("box does not fit into stack"); + lr!("box does not fit into stack"); } self.finish_space(true); diff --git a/src/layout/text.rs b/src/layout/text.rs index 343127e3..514bfe92 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -116,6 +116,6 @@ impl<'a, 'p> TextLayouter<'a, 'p> { self.classes.pop(); } - lerr!("no suitable font for character `{}`", c); + lr!("no suitable font for character `{}`", c); } } diff --git a/src/layout/tree.rs b/src/layout/tree.rs index efa0c7b7..5fe3b6fd 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -99,8 +99,8 @@ impl<'a, 'p> TreeLayouter<'a, 'p> { Add(layout) => self.flex.add(layout), AddMultiple(layouts) => self.flex.add_multiple(layouts), AddSpacing(space, kind, axis) => match axis { - AxisKind::Primary => self.flex.add_primary_space(space, kind), - AxisKind::Secondary => self.flex.add_secondary_space(space, kind)?, + GenericAxisKind::Primary => self.flex.add_primary_space(space, kind), + GenericAxisKind::Secondary => self.flex.add_secondary_space(space, kind)?, } FinishLine => self.flex.add_break(), @@ -111,7 +111,7 @@ impl<'a, 'p> TreeLayouter<'a, 'p> { SetTextStyle(style) => self.style.text = style, SetPageStyle(style) => { if !self.ctx.top_level { - lerr!("page style cannot only be altered in the top-level context"); + lr!("page style cannot only be altered in the top-level context"); } self.style.page = style; |
