diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-11-16 11:10:53 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-11-16 11:10:53 +0100 |
| commit | ac4d501945e9b63f6b5f11c4c1a2ec0738d0b058 (patch) | |
| tree | f5edb15b151ea22a3ad436950e2f637449f2fe21 /src/layout | |
| parent | 261ef9e33a8548d4b7aa53e69e71866648982ae8 (diff) | |
Move generalization/specialization methods 🚚
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/flex.rs | 11 | ||||
| -rw-r--r-- | src/layout/mod.rs | 24 | ||||
| -rw-r--r-- | src/layout/stacked.rs | 17 | ||||
| -rw-r--r-- | src/layout/tree.rs | 8 |
4 files changed, 43 insertions, 17 deletions
diff --git a/src/layout/flex.rs b/src/layout/flex.rs index c136ad0d..a65391da 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 2bafd792..442747b5 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 cc6caa5e..91bb09b5 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 ca2051f4..2957420d 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; } } |
