From e2d17aa9d9491b339e6200c97b52f7ade51fa1d8 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 13 Oct 2019 12:36:45 +0200 Subject: =?UTF-8?q?Move=20functions=20to=20command-based=20architecture=20?= =?UTF-8?q?=E2=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/mod.rs | 31 ++++++++++++++++++++++--------- src/layout/stacked.rs | 12 ++++++++++-- 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'src/layout') diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 19543389..1bd11e14 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -81,6 +81,15 @@ impl MultiLayout { } } +impl IntoIterator for MultiLayout { + type Item = Layout; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.layouts.into_iter() + } +} + /// The context for layouting. #[derive(Copy, Clone)] pub struct LayoutContext<'a, 'p> { @@ -122,13 +131,14 @@ pub enum Alignment { } pub fn layout_tree(tree: &SyntaxTree, ctx: LayoutContext) -> LayoutResult { - Layouter::new(tree, ctx).layout() + let mut layouter = Layouter::new(ctx); + layouter.layout(tree)?; + layouter.finish() } /// Transforms a syntax tree into a box layout. struct Layouter<'a, 'p> { ctx: LayoutContext<'a, 'p>, - tree: &'a SyntaxTree, stack_layouter: StackLayouter, flex_layouter: FlexLayouter, style: Cow<'a, TextStyle>, @@ -136,10 +146,9 @@ struct Layouter<'a, 'p> { impl<'a, 'p> Layouter<'a, 'p> { /// Create a new layouter. - fn new(tree: &'a SyntaxTree, ctx: LayoutContext<'a, 'p>) -> Layouter<'a, 'p> { + fn new(ctx: LayoutContext<'a, 'p>) -> Layouter<'a, 'p> { Layouter { ctx, - tree, stack_layouter: StackLayouter::new(StackContext { space: ctx.space }), flex_layouter: FlexLayouter::new(FlexContext { space: LayoutSpace { @@ -155,9 +164,9 @@ impl<'a, 'p> Layouter<'a, 'p> { } /// Layout the tree into a box. - fn layout(mut self) -> LayoutResult { + fn layout(&mut self, tree: &SyntaxTree) -> LayoutResult<()> { // Walk all nodes and layout them. - for node in &self.tree.nodes { + for node in &tree.nodes { match node { // Layout a single piece of text. Node::Text(text) => self.layout_text(text, false)?, @@ -190,6 +199,10 @@ impl<'a, 'p> Layouter<'a, 'p> { } } + Ok(()) + } + + fn finish(mut self) -> LayoutResult { // If there are remainings, add them to the layout. if !self.flex_layouter.is_empty() { self.layout_flex()?; @@ -254,9 +267,9 @@ impl<'a, 'p> Layouter<'a, 'p> { for command in commands { match command { - Command::Layout(tree) => unimplemented!(), - Command::Add(layout) => unimplemented!(), - Command::AddMany(layouts) => unimplemented!(), + Command::Layout(tree) => self.layout(tree)?, + Command::Add(layout) => self.stack_layouter.add_box(layout)?, + Command::AddMany(layouts) => self.stack_layouter.add_many(layouts)?, Command::ToggleStyleClass(class) => self.style.to_mut().toggle_class(class), } } diff --git a/src/layout/stacked.rs b/src/layout/stacked.rs index 78eb0058..312681ac 100644 --- a/src/layout/stacked.rs +++ b/src/layout/stacked.rs @@ -72,9 +72,17 @@ impl StackLayouter { Ok(()) } + /// Add multiple sublayouts. + pub fn add_many(&mut self, layouts: MultiLayout) -> LayoutResult<()> { + for layout in layouts { + self.add_box(layout)?; + } + Ok(()) + } + /// Add a sublayout at an absolute position. - pub fn add_box_absolute(&mut self, position: Size2D, layout: Layout) -> LayoutResult<()> { - Ok(self.actions.add_box(position, layout)) + pub fn add_box_absolute(&mut self, position: Size2D, layout: Layout) { + self.actions.add_box(position, layout); } /// Add space in between two boxes. -- cgit v1.2.3