summaryrefslogtreecommitdiff
path: root/src/layout/mod.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-13 12:36:45 +0200
committerLaurenz <laurmaedje@gmail.com>2019-10-13 12:36:45 +0200
commite2d17aa9d9491b339e6200c97b52f7ade51fa1d8 (patch)
tree95dfa729cc6cbe44ecbf5135f87a3dd8bb70200a /src/layout/mod.rs
parent463e4ebd8234da5e28700e9b22b6ef5f0dfef56f (diff)
Move functions to command-based architecture ✈
Diffstat (limited to 'src/layout/mod.rs')
-rw-r--r--src/layout/mod.rs31
1 files changed, 22 insertions, 9 deletions
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<Layout>;
+
+ 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<MultiLayout> {
- 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<MultiLayout> {
+ 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<MultiLayout> {
// 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),
}
}