From ac4d501945e9b63f6b5f11c4c1a2ec0738d0b058 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 16 Nov 2019 11:10:53 +0100 Subject: [PATCH] =?UTF-8?q?Move=20generalization/specialization=20methods?= =?UTF-8?q?=20=F0=9F=9A=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/flex.rs | 11 ++++++++--- src/layout/mod.rs | 24 ++++++++++++++++++++++-- src/layout/stacked.rs | 17 +++++++++++------ src/layout/tree.rs | 8 ++------ src/library/structure.rs | 2 +- src/size.rs | 22 ---------------------- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/layout/flex.rs b/src/layout/flex.rs index c136ad0d1..a65391da5 100644 --- a/src/layout/flex.rs +++ b/src/layout/flex.rs @@ -129,7 +129,7 @@ impl FlexLayouter { /// Layout a content box into the current flex run or start a new run if /// it does not fit. fn layout_box(&mut self, boxed: Layout) -> LayoutResult<()> { - let size = boxed.dimensions.generalized(self.ctx.axes); + let size = self.ctx.axes.generalize(boxed.dimensions); let space = self.space.unwrap_or(Size::zero()); let new_run_size = self.run.size.x + space + size.x; @@ -166,14 +166,14 @@ impl FlexLayouter { fn finish_run(&mut self) -> LayoutResult<()> { let mut actions = LayoutActionList::new(); for (x, layout) in self.run.content.drain(..) { - let position = Size2D::with_x(x).specialized(self.ctx.axes); + 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.run.size.specialized(self.ctx.axes), + dimensions: self.ctx.axes.specialize(self.run.size), actions: actions.into_vec(), debug_render: false, })?; @@ -183,6 +183,11 @@ impl FlexLayouter { Ok(()) } + /// Update the axes in use by this flex layouter. + pub fn set_axes(&self, axes: LayoutAxes) { + + } + /// This layouter's context. pub fn ctx(&self) -> FlexContext { self.ctx diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 2bafd792e..442747b5f 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -199,10 +199,30 @@ pub struct LayoutAxes { } impl LayoutAxes { + /// Returns the generalized version of a `Size2D` dependent on + /// the layouting axes, that is: + /// - The x coordinate describes the primary axis instead of the horizontal one. + /// - The y coordinate describes the secondary axis instead of the vertical one. + pub fn generalize(&self, space: Size2D) -> Size2D { + if self.primary.axis.is_horizontal() { + space + } else { + Size2D { x: space.y, y: space.x } + } + } + + /// Returns the specialized version of this generalized Size2D. + /// (Inverse to `generalized`). + pub fn specialize(&self, space: Size2D) -> Size2D { + // In fact, generalized is its own inverse. For reasons of clarity + // at the call site, we still have this second function. + self.generalize(space) + } + /// The position of the anchor specified by the two aligned axes /// in the given generalized space. - pub fn anchor(&self, area: Size2D) -> Size2D { - Size2D::new(self.primary.anchor(area.x), self.secondary.anchor(area.y)) + pub fn anchor(&self, space: Size2D) -> Size2D { + Size2D::new(self.primary.anchor(space.x), self.secondary.anchor(space.y)) } } diff --git a/src/layout/stacked.rs b/src/layout/stacked.rs index cc6caa5e3..91bb09b57 100644 --- a/src/layout/stacked.rs +++ b/src/layout/stacked.rs @@ -29,7 +29,7 @@ pub struct StackContext { impl StackLayouter { /// Create a new stack layouter. pub fn new(ctx: StackContext) -> StackLayouter { - let usable = ctx.spaces[0].usable().generalized(ctx.axes); + let usable = ctx.axes.generalize(ctx.spaces[0].usable()); StackLayouter { ctx, layouts: MultiLayout::new(), @@ -44,7 +44,7 @@ impl StackLayouter { /// Add a sublayout. pub fn add(&mut self, layout: Layout) -> LayoutResult<()> { - let size = layout.dimensions.generalized(self.ctx.axes); + let size = self.ctx.axes.generalize(layout.dimensions); let mut new_dimensions = self.size_with(size); // Search for a suitable space to insert the box. @@ -114,7 +114,7 @@ impl StackLayouter { for (offset, layout_anchor, layout) in self.boxes.drain(..) { let general_position = anchor - layout_anchor + Size2D::with_y(offset * factor); - let position = start + general_position.specialized(self.ctx.axes); + let position = start + self.ctx.axes.specialize(general_position); actions.add_layout(position, layout); } @@ -137,11 +137,16 @@ impl StackLayouter { /// content is added to it. fn start_new_space(&mut self, include_empty: bool) { self.active_space = self.next_space(); - self.usable = self.ctx.spaces[self.active_space].usable().generalized(self.ctx.axes); + self.usable = self.ctx.axes.generalize(self.ctx.spaces[self.active_space].usable()); self.dimensions = start_dimensions(self.usable, self.ctx.axes); self.include_empty = include_empty; } + /// Update the axes in use by this stack layouter. + pub fn set_axes(&self, axes: LayoutAxes) { + + } + /// This layouter's context. pub fn ctx(&self) -> StackContext { self.ctx @@ -154,9 +159,9 @@ impl StackLayouter { /// The remaining spaces for new layouts in the current space. pub fn remaining(&self, shrink_to_fit: bool) -> LayoutSpaces { + let remains = Size2D::new(self.usable.x, self.usable.y - self.dimensions.y); let mut spaces = smallvec![LayoutSpace { - dimensions: Size2D::new(self.usable.x, self.usable.y - self.dimensions.y) - .specialized(self.ctx.axes), + dimensions: self.ctx.axes.specialize(remains), padding: SizeBox::zero(), shrink_to_fit, }]; diff --git a/src/layout/tree.rs b/src/layout/tree.rs index ca2051f4a..2957420dc 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -100,12 +100,8 @@ impl<'a, 'p> TreeLayouter<'a, 'p> { Command::SetStyle(style) => *self.style.to_mut() = style, Command::SetAxes(axes) => { - if axes.secondary != self.ctx.axes.secondary { - self.stack.set_axis(axes.secondary); - } else if axes.primary != self.ctx.axes.primary { - self.flex.set_axis(axes.primary); - } - + self.stack.set_axes(axes); + self.flex.set_axes(axes); self.ctx.axes = axes; } } diff --git a/src/library/structure.rs b/src/library/structure.rs index 2bcd27444..59436b9b8 100644 --- a/src/library/structure.rs +++ b/src/library/structure.rs @@ -149,7 +149,7 @@ macro_rules! spacefunc { layout(this, ctx) { let $var = match this.0 { Spacing::Absolute(s) => s, - Spacing::Relative(f) => Size::pt(f * ctx.style.font_size), + Spacing::Relative(f) => f * ctx.style.font_size, }; Ok(commands![$command]) diff --git a/src/size.rs b/src/size.rs index f5f82d291..5a690b5f7 100644 --- a/src/size.rs +++ b/src/size.rs @@ -142,28 +142,6 @@ impl Size2D { } } - /// Returns the generalized version of this Size2D dependent on - /// the given layouting axes, that is: - /// - The x coordinate describes the primary axis instead of the horizontal one. - /// - The y coordinate describes the secondary axis instead of the vertical one. - #[inline] - pub fn generalized(&self, axes: LayoutAxes) -> Size2D { - if axes.primary.axis.is_horizontal() { - *self - } else { - Size2D { x: self.y, y: self.x } - } - } - - /// Returns the specialized version of this generalized Size2D. - /// (Inverse to `generalized`). - #[inline] - pub fn specialized(&self, axes: LayoutAxes) -> Size2D { - // In fact, generalized is its own inverse. For reasons of clarity - // at the call site, we still have this second function. - self.generalized(axes) - } - /// Whether the given 2D-size fits into this one, that is, /// both coordinate values are smaller or equal. #[inline]