From 6b2872dbf5c016ba2871b5794801e6a3973185dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 8 Oct 2018 16:44:32 +0200 Subject: [PATCH] Added initial flex-basis preparation --- src/css_parser.rs | 4 +-- src/lib.rs | 1 + src/ui_solver.rs | 84 ++++++++++++++++++++++++++++++++++------------- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/css_parser.rs b/src/css_parser.rs index 8ab75f613..6e5a6087c 100644 --- a/src/css_parser.rs +++ b/src/css_parser.rs @@ -875,7 +875,7 @@ fn parse_color_no_hash<'a>(input: &'a str) } /// Represents a parsed CSS `padding` attribute -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct LayoutPadding { pub top: Option, pub bottom: Option, @@ -947,7 +947,7 @@ fn parse_layout_padding<'a>(input: &'a str) } /// Represents a parsed CSS `padding` attribute -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct LayoutMargin { pub top: Option, pub bottom: Option, diff --git a/src/lib.rs b/src/lib.rs index ed5ad66e5..0ce1b57a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,7 @@ )] #![deny(unused_must_use)] +#![deny(unreachable_patterns)] #![deny(missing_copy_implementations)] #![allow(dead_code)] diff --git a/src/ui_solver.rs b/src/ui_solver.rs index c4a8c2f5e..293af101d 100644 --- a/src/ui_solver.rs +++ b/src/ui_solver.rs @@ -448,6 +448,12 @@ enum WhConstraint { Unconstrained, } +impl Default for WhConstraint { + fn default() -> Self { + WhConstraint::Unconstrained + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq)] enum WhPrefer { Max, @@ -538,8 +544,7 @@ macro_rules! determine_preferred { }) } -/* -use css_parser::{LayoutAxis, LayoutMargin, LayoutPadding}; +use css_parser::{LayoutMargin, LayoutPadding}; #[derive(Debug, Copy, Clone)] struct WidthCalculatedRect { @@ -550,35 +555,70 @@ struct WidthCalculatedRect { } impl WidthCalculatedRect { - /// Get the flex basis in the main direction - pub fn get_flex_basis(&self, parent_axis: &LayoutAxis) -> f32 { - match parent_axis { - Horizontal => { - self.preferred_width.unwrap_or(0.0) - + self.margin.left.unwrap_or(0.0) - + self.margin.right.unwrap_or(0.0) - + self.padding.right.unwrap_or(0.0) - + self.padding.left.unwrap_or(0.0) - }, - Vertical => { - self.preferred_height.unwrap_or(0.0) - + self.margin.top.unwrap_or(0.0) - + self.margin.bottom.unwrap_or(0.0) - + self.padding.top.unwrap_or(0.0) - + self.padding.bottom.unwrap_or(0.0) - }, + /// Get the flex basis in the horizontal direction - vertical axis has to be calculated differently + pub fn get_flex_basis(&self) -> FlexBasisHorizontal { + FlexBasisHorizontal { + min_width: self.preferred_width.min_needed_space(), + self_margin_left: self.margin.left.and_then(|px| Some(px.to_pixels())).unwrap_or(0.0), + self_margin_right: self.margin.right.and_then(|px| Some(px.to_pixels())).unwrap_or(0.0), + self_padding_left: self.padding.left.and_then(|px| Some(px.to_pixels())).unwrap_or(0.0), + self_padding_right: self.padding.right.and_then(|px| Some(px.to_pixels())).unwrap_or(0.0), } } } -/// Fill out the width of all nodes that don't have a `first_child` -fn fill_out_width_of_innermost_nodes<'a>(arena: &Arena>, fill_out: &mut Arena<>) { - for (node_id, node) in arena.linear_iter().filter(|(node_id, node)| node.first_child.is_some()) { +/// Fill out the width of all nodes that don't have a `first_child` and are therefore leaf nodes +fn fill_out_width_of_leaf_nodes<'a>(arena: &Arena>) -> Arena> { + arena.transform(|node, node_id| { + if arena[node_id].first_child.is_some() { + None + } else { + Some(WidthCalculatedRect { + preferred_width: determine_preferred_width(&node.layout), + preferred_height: determine_preferred_height(&node.layout), + margin: node.layout.margin.unwrap_or_default(), + padding: node.layout.padding.unwrap_or_default(), + }) + } + }) +} +struct FlexBasisHorizontal { + pub min_width: f32, + pub self_margin_left: f32, + pub self_margin_right: f32, + pub self_padding_right: f32, + pub self_padding_left: f32, +} + +impl FlexBasisHorizontal { + /// Total flex basis in the horizontal direction (sum of the components) + pub fn total(&self) -> f32 { + self.min_width + + self.self_margin_left + + self.self_margin_right + + self.self_padding_left + + self.self_padding_right } } + +/* +/// NOTE: Assumed that `arena` and `display_arena` have the same IDs +fn caclulate_flex_basis_width<'a>( + node_id: NodeId, + arena: &Arena>, + display_arena: &Arena>, + current_width: &mut FlexBasisHorizontal) +{ + for node_id in +} + +fn caclulate_flex_basis(leaf_nodes_populated: &Arena>) -> Arena { + +} */ + /// Returns the preferred width, given [width, min_width, max_width] inside a RectLayout /// or `None` if the height can't be determined from the node alone. ///