1//! Final and cached data structures that represent the high-level UI layout
2
3use crate::geometry::{Point, Size};
4use crate::style::AvailableSpace;
5
6/// Whether we are performing a full layout, or we merely need to size the node
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub enum RunMode {
9 /// A full layout for this node and all children should be computed
10 PeformLayout,
11 /// The layout algorithm should be executed such that an accurate container size for the node can be determined.
12 /// Layout steps that aren't necessary for determining the container size of the current node can be skipped.
13 ComputeSize,
14}
15
16/// Whether styles should be taken into account when computing size
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
18pub enum SizingMode {
19 /// Only content contributions should be taken into account
20 ContentSize,
21 /// Inherent size styles should be taken into account in addition to content contributions
22 InherentSize,
23}
24
25/// A struct containing both the size of a node and it's first baseline in each dimension (if it has any)
26///
27/// A baseline is the line on which text sits. Your node likely has a baseline if it is a text node, or contains
28/// children that may be text nodes. See https://www.w3.org/TR/css-writing-modes-3/#intro-baselines for details.
29/// If your node does not have a baseline (or you are unsure how to compute it), then simply return `Point::NONE`
30/// for the first_baselines field
31#[derive(Debug, Copy, Clone)]
32pub struct SizeAndBaselines {
33 /// The size of the node
34 pub size: Size<f32>,
35 /// The first baseline of the node in each dimension, if any
36 pub first_baselines: Point<Option<f32>>,
37}
38
39impl From<Size<f32>> for SizeAndBaselines {
40 fn from(size: Size<f32>) -> Self {
41 Self { size, first_baselines: Point::NONE }
42 }
43}
44
45/// The final result of a layout algorithm for a single [`Node`](crate::node::Node).
46#[derive(Debug, Copy, Clone)]
47pub struct Layout {
48 /// The relative ordering of the node
49 ///
50 /// Nodes with a higher order should be rendered on top of those with a lower order.
51 /// This is effectively a topological sort of each tree.
52 pub order: u32,
53 /// The width and height of the node
54 pub size: Size<f32>,
55 /// The bottom-left corner of the node
56 pub location: Point<f32>,
57}
58
59impl Layout {
60 /// Creates a new zero-[`Layout`].
61 ///
62 /// The Zero-layout has size and location set to ZERO.
63 /// The `order` value of this layout is set to the minimum value of 0.
64 /// This means it should be rendered below all other [`Layout`]s.
65 #[must_use]
66 pub const fn new() -> Self {
67 Self { order: 0, size: Size::zero(), location: Point::ZERO }
68 }
69
70 /// Creates a new zero-[`Layout`] with the supplied `order` value.
71 ///
72 /// Nodes with a higher order should be rendered on top of those with a lower order.
73 /// The Zero-layout has size and location set to ZERO.
74 #[must_use]
75 pub const fn with_order(order: u32) -> Self {
76 Self { order, size: Size::zero(), location: Point::ZERO }
77 }
78}
79
80/// Cached intermediate layout results
81#[derive(Debug, Clone, Copy)]
82pub struct Cache {
83 /// The initial cached size of the node itself
84 pub(crate) known_dimensions: Size<Option<f32>>,
85 /// The initial cached size of the parent's node
86 pub(crate) available_space: Size<AvailableSpace>,
87 /// Whether or not layout should be recomputed
88 pub(crate) run_mode: RunMode,
89
90 /// The cached size and baselines of the item
91 pub(crate) cached_size_and_baselines: SizeAndBaselines,
92}