//! Final data structures that represent the high-level UI layout use crate::geometry::{Line, Point, Size}; use crate::style::AvailableSpace; use crate::util::sys::{f32_max, f32_min}; /// Whether we are performing a full layout, or we merely need to size the node #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum RunMode { /// A full layout for this node and all children should be computed PerformLayout, /// The layout algorithm should be executed such that an accurate container size for the node can be determined. /// Layout steps that aren't necessary for determining the container size of the current node can be skipped. ComputeSize, } /// Whether styles should be taken into account when computing size #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum SizingMode { /// Only content contributions should be taken into account ContentSize, /// Inherent size styles should be taken into account in addition to content contributions InherentSize, } /// A set of margins that are available for collapsing with for block layout's margin collapsing #[derive(Copy, Clone, Debug, PartialEq)] pub struct CollapsibleMarginSet { /// The largest positive margin positive: f32, /// The smallest negative margin (with largest absolute value) negative: f32, } impl CollapsibleMarginSet { /// A default margin set with no collapsible margins pub const ZERO: Self = Self { positive: 0.0, negative: 0.0 }; /// Create a set from a single margin pub fn from_margin(margin: f32) -> Self { if margin >= 0.0 { Self { positive: margin, negative: 0.0 } } else { Self { positive: 0.0, negative: margin } } } /// Collapse a single margin with this set pub fn collapse_with_margin(mut self, margin: f32) -> Self { if margin >= 0.0 { self.positive = f32_max(self.positive, margin); } else { self.negative = f32_min(self.negative, margin); } self } /// Collapse another margin set with this set pub fn collapse_with_set(mut self, other: CollapsibleMarginSet) -> Self { self.positive = f32_max(self.positive, other.positive); self.negative = f32_min(self.negative, other.negative); self } /// Resolve the resultant margin from this set once all collapsible margins /// have been collapsed into it pub fn resolve(&self) -> f32 { self.positive + self.negative } } /// A struct containing the inputs constraints/hints for laying out a node, which are passed in by the parent #[derive(Debug, Copy, Clone)] pub struct LayoutInput { /// Known dimensions represent dimensions (width/height) which should be taken as fixed when performing layout. /// For example, if known_dimensions.width is set to Some(WIDTH) then this means something like: /// /// "What would the height of this node be, assuming the width is WIDTH" /// /// Layout functions will be called with both known_dimensions set for final layout. Where the meaning is: /// /// "The exact size of this node is WIDTHxHEIGHT. Please lay out your children" /// pub known_dimensions: Size>, /// Parent size dimensions are intended to be used for percentage resolution. pub parent_size: Size>, /// Available space represents an amount of space to layout into, and is used as a soft constraint /// for the purpose of wrapping. pub available_space: Size, /// Whether a Node's style sizes should be taken into account or ignored pub sizing_mode: SizingMode, /// Whether we only need to know the Node's size, or whe pub run_mode: RunMode, /// Specific to CSS Block layout. Used for correctly computing margin collapsing. You probably want to set this to `Line::FALSE`. pub vertical_margins_are_collapsible: Line, } /// A struct containing the result of laying a single node, which is returned up to the parent node /// /// A baseline is the line on which text sits. Your node likely has a baseline if it is a text node, or contains /// children that may be text nodes. See for details. /// If your node does not have a baseline (or you are unsure how to compute it), then simply return `Point::NONE` /// for the first_baselines field #[derive(Debug, Copy, Clone)] pub struct LayoutOutput { /// The size of the node pub size: Size, /// The first baseline of the node in each dimension, if any pub first_baselines: Point>, /// Top margin that can be collapsed with. This is used for CSS block layout and can be set to /// `CollapsibleMarginSet::ZERO` for other layout modes that don't support margin collapsing pub top_margin: CollapsibleMarginSet, /// Bottom margin that can be collapsed with. This is used for CSS block layout and can be set to /// `CollapsibleMarginSet::ZERO` for other layout modes that don't support margin collapsing pub bottom_margin: CollapsibleMarginSet, /// Whether margins can be collapsed through this node. This is used for CSS block layout and can /// be set to `false` for other layout modes that don't support margin collapsing pub margins_can_collapse_through: bool, } impl LayoutOutput { /// An all-zero `LayoutOutput` for hidden nodes pub const HIDDEN: Self = Self { size: Size::ZERO, first_baselines: Point::NONE, top_margin: CollapsibleMarginSet::ZERO, bottom_margin: CollapsibleMarginSet::ZERO, margins_can_collapse_through: false, }; /// Constructor to create a `LayoutOutput` from just the size and baselines pub fn from_size_and_baselines(size: Size, first_baselines: Point>) -> Self { Self { size, first_baselines, top_margin: CollapsibleMarginSet::ZERO, bottom_margin: CollapsibleMarginSet::ZERO, margins_can_collapse_through: false, } } } impl From> for LayoutOutput { fn from(size: Size) -> Self { Self { size, first_baselines: Point::NONE, top_margin: CollapsibleMarginSet::ZERO, bottom_margin: CollapsibleMarginSet::ZERO, margins_can_collapse_through: false, } } } /// The final result of a layout algorithm for a single node. #[derive(Debug, Copy, Clone)] pub struct Layout { /// The relative ordering of the node /// /// Nodes with a higher order should be rendered on top of those with a lower order. /// This is effectively a topological sort of each tree. pub order: u32, /// The width and height of the node pub size: Size, /// The top-left corner of the node pub location: Point, } impl Layout { /// Creates a new zero-[`Layout`]. /// /// The Zero-layout has size and location set to ZERO. /// The `order` value of this layout is set to the minimum value of 0. /// This means it should be rendered below all other [`Layout`]s. #[must_use] pub const fn new() -> Self { Self { order: 0, size: Size::zero(), location: Point::ZERO } } /// Creates a new zero-[`Layout`] with the supplied `order` value. /// /// Nodes with a higher order should be rendered on top of those with a lower order. /// The Zero-layout has size and location set to ZERO. #[must_use] pub const fn with_order(order: u32) -> Self { Self { order, size: Size::zero(), location: Point::ZERO } } }