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/// The final result of a layout algorithm for a single [`Node`](crate::node::Node).
26#[derive(Copy, Debug, Clone)]
27pub struct Layout {
28 /// The relative ordering of the node
29 ///
30 /// Nodes with a higher order should be rendered on top of those with a lower order.
31 /// This is effectively a topological sort of each tree.
32 pub order: u32,
33 /// The width and height of the node
34 pub size: Size<f32>,
35 /// The bottom-left corner of the node
36 pub location: Point<f32>,
37}
38
39impl Layout {
40 /// Creates a new zero-[`Layout`].
41 ///
42 /// The Zero-layout has size and location set to ZERO.
43 /// The `order` value of this layout is set to the minimum value of 0.
44 /// This means it should be rendered below all other [`Layout`]s.
45 #[must_use]
46 pub const fn new() -> Self {
47 Self { order: 0, size: Size::zero(), location: Point::ZERO }
48 }
49
50 /// Creates a new zero-[`Layout`] with the supplied `order` value.
51 ///
52 /// Nodes with a higher order should be rendered on top of those with a lower order.
53 /// The Zero-layout has size and location set to ZERO.
54 #[must_use]
55 pub const fn with_order(order: u32) -> Self {
56 Self { order, size: Size::zero(), location: Point::ZERO }
57 }
58}
59
60/// Cached intermediate layout results
61#[derive(Debug, Clone, Copy)]
62pub struct Cache {
63 /// The initial cached size of the node itself
64 pub(crate) known_dimensions: Size<Option<f32>>,
65 /// The initial cached size of the parent's node
66 pub(crate) available_space: Size<AvailableSpace>,
67 /// Whether or not layout should be recomputed
68 pub(crate) run_mode: RunMode,
69
70 /// The cached size of the item
71 pub(crate) cached_size: Size<f32>,
72}