Skip to content

Commit

Permalink
Always stretch-fit the root node
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jun 29, 2024
1 parent 458d78b commit 9ea3928
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvailableSpace>) {
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(
Expand Down

0 comments on commit 9ea3928

Please sign in to comment.