cranpose_ui/widgets/nodes/
layout_state.rs1use std::cell::{Cell, RefCell};
5
6use cranpose_core::{NodeId, PackedU64, collections::growth_slack::GrowthSlack};
7
8use crate::modifier::{Point, Size};
9
10pub(crate) struct PlacementPass {
14 id: Cell<u64>,
15 unplaced: Cell<usize>,
16 cleared: RefCell<Vec<NodeId>>,
19 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 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 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 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 pub(crate) fn keep_unplaced(&self, nodes: &[NodeId]) {
60 self.left_unplaced.borrow_mut().extend_from_slice(nodes);
61 }
62
63 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 pub(crate) fn with_unplaced_nodes(&self) -> Option<u64> {
72 (self.unplaced.get() > 0).then(|| self.id.get())
73 }
74
75 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 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#[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 pub(super) cleared_in_pass: PackedU64,
104 pub(super) content_offset: Point,
105 pub(super) z_index: f32,
106 pub(super) places_children_with_z_index: bool,
108}
109
110#[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 pub fn size(&self) -> Size {
127 self.size
128 }
129
130 pub fn position(&self) -> Point {
132 self.position
133 }
134
135 pub fn content_offset(&self) -> Point {
138 self.content_offset
139 }
140
141 pub fn at_origin(mut self) -> Self {
145 self.position = Point::default();
146 self
147 }
148
149 pub fn is_placed(&self) -> bool {
151 self.is_placed
152 }
153
154 pub fn z_index(&self) -> f32 {
157 self.z_index
158 }
159
160 pub fn places_children_with_z_index(&self) -> bool {
163 self.places_children_with_z_index
164 }
165
166 pub(crate) fn unplaced_in(&self, pass: u64) -> bool {
168 !self.is_placed && self.cleared_in_pass.get() == pass
169 }
170}
171
172pub(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}