From 9ea3928574aff6d316a7708f6585acc08684c284 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 29 Jun 2024 23:47:58 +1200 Subject: [PATCH] Always stretch-fit the root node --- src/compute/mod.rs | 71 ++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/src/compute/mod.rs b/src/compute/mod.rs index b2279ddad..da5cf460c 100644 --- a/src/compute/mod.rs +++ b/src/compute/mod.rs @@ -52,53 +52,36 @@ use crate::tree::{ use crate::util::debug::{debug_log, debug_log_node, debug_pop_node, debug_push_node}; use crate::util::sys::round; use crate::util::ResolveOrZero; -use crate::{Display, MaybeMath, MaybeResolve}; +use crate::{MaybeMath, MaybeResolve}; /// Compute layout for the root node in the tree pub fn compute_root_layout(tree: &mut impl LayoutPartialTree, root: NodeId, available_space: Size) { - let mut known_dimensions = Size::NONE; - - #[cfg(feature = "block_layout")] - { - let parent_size = available_space.into_options(); - let style = tree.get_style(root); - - if style.display == Display::Block { - // Pull these out earlier to avoid borrowing issues - let aspect_ratio = style.aspect_ratio; - let margin = style.margin.resolve_or_zero(parent_size.width); - let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio); - let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio); - let padding = style.padding.resolve_or_zero(parent_size.width); - let border = style.border.resolve_or_zero(parent_size.width); - let padding_border_size = (padding + border).sum_axes(); - let clamped_style_size = style - .size - .maybe_resolve(parent_size) - .maybe_apply_aspect_ratio(aspect_ratio) - .maybe_clamp(min_size, max_size); - - // If both min and max in a given axis are set and max <= min then this determines the size in that axis - let min_max_definite_size = min_size.zip_map(max_size, |min, max| match (min, max) { - (Some(min), Some(max)) if max <= min => Some(min), - _ => None, - }); - - // Block nodes automatically stretch fit their width to fit available space if available space is definite - let available_space_based_size = Size { - width: available_space.width.into_option().maybe_sub(margin.horizontal_axis_sum()), - height: None, - }; - - let styled_based_known_dimensions = known_dimensions - .or(min_max_definite_size) - .or(clamped_style_size) - .or(available_space_based_size) - .maybe_max(padding_border_size); - - known_dimensions = styled_based_known_dimensions; - } - } + let parent_size = available_space.into_options(); + let style = tree.get_style(root); + + // Pull these out earlier to avoid borrowing issues + let aspect_ratio = style.aspect_ratio; + let margin = style.margin.resolve_or_zero(parent_size.width); + let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio); + let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio); + let padding = style.padding.resolve_or_zero(parent_size.width); + let border = style.border.resolve_or_zero(parent_size.width); + let padding_border_size = (padding + border).sum_axes(); + let clamped_style_size = + style.size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size); + + // If both min and max in a given axis are set and max <= min then this determines the size in that axis + let min_max_definite_size = min_size.zip_map(max_size, |min, max| match (min, max) { + (Some(min), Some(max)) if max <= min => Some(min), + _ => None, + }); + + // The root node should automatically stretch fit it's width to fit available space if available space is definite + let available_space_based_size = + Size { width: available_space.width.into_option().maybe_sub(margin.horizontal_axis_sum()), height: None }; + + let known_dimensions = + min_max_definite_size.or(clamped_style_size).or(available_space_based_size).maybe_max(padding_border_size); // Recursively compute node layout let output = tree.perform_child_layout(