Skip to main content

cranpose_ui/widgets/nodes/
layout_state.rs

1//! Where a layout node's parent placed it and what size it measured, and
2//! the placement bookkeeping of a layout pass.
3
4use std::cell::{Cell, RefCell};
5
6use cranpose_core::{NodeId, PackedU64, collections::growth_slack::GrowthSlack};
7
8use crate::modifier::{Point, Size};
9
10/// The running layout pass and how many placed nodes it has cleared and not
11/// placed again yet: the nodes it unplaces, unless their parents place them
12/// before it ends. Ids start past 0, the pass a node no pass cleared names.
13pub(crate) struct PlacementPass {
14    id: Cell<u64>,
15    unplaced: Cell<usize>,
16    // The placed nodes the pass cleared, placed again or not: its end looks
17    // for the unplaced among them, and not through the whole tree.
18    cleared: RefCell<Vec<NodeId>>,
19    // The nodes the passes since the scene phase last took them left
20    // unplaced.
21    left_unplaced: RefCell<Vec<NodeId>>,
22}
23
24impl PlacementPass {
25    pub(crate) const fn new() -> Self {
26        Self {
27            id: Cell::new(1),
28            unplaced: Cell::new(0),
29            cleared: RefCell::new(Vec::new()),
30            left_unplaced: RefCell::new(Vec::new()),
31        }
32    }
33
34    /// Starts the placement bookkeeping of a layout pass.
35    pub(crate) fn begin(&self) {
36        self.id.set(self.id.get() + 1);
37        self.unplaced.set(0);
38        self.cleared.borrow_mut().clear();
39    }
40
41    // Gives back the room a burst of cleared or unplaced nodes took.
42    pub(crate) fn trim(&self) {
43        self.cleared.borrow_mut().trim_growth_slack();
44        self.left_unplaced.borrow_mut().trim_growth_slack();
45    }
46
47    // The placed nodes the running pass cleared, for its end to read and
48    // give back with `keep_cleared`.
49    pub(crate) fn take_cleared(&self) -> Vec<NodeId> {
50        std::mem::take(&mut *self.cleared.borrow_mut())
51    }
52
53    pub(crate) fn keep_cleared(&self, cleared: Vec<NodeId>) {
54        *self.cleared.borrow_mut() = cleared;
55    }
56
57    // Keeps `nodes`, which the running pass left unplaced, for the scene
58    // phase.
59    pub(crate) fn keep_unplaced(&self, nodes: &[NodeId]) {
60        self.left_unplaced.borrow_mut().extend_from_slice(nodes);
61    }
62
63    // Moves the nodes the passes left unplaced since the last take into
64    // `output`, dropping what `output` held.
65    pub(crate) fn take_unplaced(&self, output: &mut Vec<NodeId>) {
66        output.clear();
67        std::mem::swap(&mut *self.left_unplaced.borrow_mut(), output);
68    }
69
70    /// The running pass, when it has left a node unplaced that it found placed.
71    pub(crate) fn with_unplaced_nodes(&self) -> Option<u64> {
72        (self.unplaced.get() > 0).then(|| self.id.get())
73    }
74
75    // Counts placed node `node_id`, which the running pass cleared, and
76    // returns the pass.
77    pub(super) fn clear(&self, node_id: Option<NodeId>) -> u64 {
78        self.unplaced.set(self.unplaced.get() + 1);
79        if let Some(node_id) = node_id {
80            self.cleared.borrow_mut().push(node_id);
81        }
82        self.id.get()
83    }
84
85    /// Whether a node cleared in pass `cleared_in` was cleared by the running
86    /// pass, which then counts it placed again.
87    pub(super) fn restore(&self, cleared_in: u64) -> bool {
88        let restored = cleared_in == self.id.get();
89        if restored {
90            self.unplaced.set(self.unplaced.get().saturating_sub(1));
91        }
92        restored
93    }
94}
95
96/// The measured size and placed position that a layout node kept from its latest layout pass.
97#[derive(Clone, Copy, Debug, Default)]
98pub struct LayoutState {
99    pub(super) size: Size,
100    pub(super) position: Point,
101    pub(super) is_placed: bool,
102    // The layout pass that cleared the placed flag while the node was placed.
103    pub(super) cleared_in_pass: PackedU64,
104    pub(super) content_offset: Point,
105    pub(super) z_index: f32,
106    // Whether the node's last placement gave a child a z index other than 0.
107    pub(super) places_children_with_z_index: bool,
108}
109
110// Where a parent placed a child: its position in the parent, and the z
111// index the placement gave it among its siblings.
112#[derive(Clone, Copy, Default)]
113pub(crate) struct ChildPlacement {
114    pub(super) position: Point,
115    pub(super) z_index: f32,
116}
117
118impl ChildPlacement {
119    pub(crate) fn new(position: Point, z_index: f32) -> Self {
120        Self { position, z_index }
121    }
122}
123
124impl LayoutState {
125    /// Returns the measured size, in logical pixels.
126    pub fn size(&self) -> Size {
127        self.size
128    }
129
130    /// Returns the placed position relative to the parent, in logical pixels.
131    pub fn position(&self) -> Point {
132        self.position
133    }
134
135    /// Offset of the content box relative to the node origin (e.g. due to
136    /// padding).
137    pub fn content_offset(&self) -> Point {
138        self.content_offset
139    }
140
141    /// The same state placed at the origin: what a window root's own scene
142    /// starts from, since the position its parent gave it belongs to the
143    /// parent's window.
144    pub fn at_origin(mut self) -> Self {
145        self.position = Point::default();
146        self
147    }
148
149    /// Returns whether layout has placed the node.
150    pub fn is_placed(&self) -> bool {
151        self.is_placed
152    }
153
154    /// The z index the node's parent last placed it with. It adds to the
155    /// node's `Modifier::z_index` to order the node among its siblings.
156    pub fn z_index(&self) -> f32 {
157        self.z_index
158    }
159
160    /// Whether the node's last placement gave one of its children a z index
161    /// other than 0: the scene then orders the children by z index.
162    pub fn places_children_with_z_index(&self) -> bool {
163        self.places_children_with_z_index
164    }
165
166    // Whether `pass` found this node placed and left it unplaced.
167    pub(crate) fn unplaced_in(&self, pass: u64) -> bool {
168        !self.is_placed && self.cleared_in_pass.get() == pass
169    }
170}
171
172// Counts a layout node whose last placement gave a child a z index among the
173// thread's reasons to order siblings, when `places`, or takes it out.
174pub(super) fn count_z_index_placer(places: bool) {
175    crate::thread_state::cells(|cells| {
176        let reasons = &cells.z_order_reasons;
177        reasons.set(if places {
178            reasons.get() + 1
179        } else {
180            reasons.get().saturating_sub(1)
181        });
182    });
183}