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