euv_core/scene/node/impl.rs
1use super::*;
2
3/// Detached default for `NodeTopology` with all link indices set to `NODE_INDEX_NONE`.
4impl Default for NodeTopology {
5 /// Creates an unattached leaf `NodeTopology` entry.
6 ///
7 /// # Returns
8 ///
9 /// - `Self` - Detached topology slot.
10 #[inline(always)]
11 fn default() -> Self {
12 Self {
13 parent: NODE_INDEX_NONE,
14 first_child: NODE_INDEX_NONE,
15 last_child: NODE_INDEX_NONE,
16 next_sibling: NODE_INDEX_NONE,
17 prev_sibling: NODE_INDEX_NONE,
18 child_count: 0,
19 }
20 }
21}
22
23/// Safe optional accessors for `SceneNode`.
24impl SceneNode {
25 /// Creates an initialized live `SceneNode` with `generation` and `kind`.
26 ///
27 /// # Arguments
28 ///
29 /// - `u32` - Slot generation tag.
30 /// - `NodeKind` - Semantic node kind.
31 /// - `Style` - Initial compiled style.
32 ///
33 /// # Returns
34 ///
35 /// - `Self` - Constructed scene node marked dirty for initial layout and paint.
36 #[must_use]
37 pub fn new(generation: u32, kind: NodeKind, style: Style) -> Self {
38 let focusable: bool = matches!(kind, NodeKind::Input(_));
39 Self {
40 generation,
41 alive: true,
42 node_flags: NODE_FLAG_ALIVE,
43 kind,
44 style,
45 layout_box: LayoutBox::default(),
46 last_constraint: None,
47 dirty: DirtyFlags::all(),
48 parent: None,
49 interaction: InteractionState::new(focusable, false, false, false),
50 extras: None,
51 }
52 }
53
54 /// Returns whether this node has lazily allocated cold optional metadata (`NodeExtras`), gated by `NODE_FLAG_HAS_EXTRAS`.
55 ///
56 /// # Arguments
57 ///
58 /// - `&Self` - Scene node reference.
59 ///
60 /// # Returns
61 ///
62 /// - `bool` - `true` when cold metadata has been allocated.
63 #[must_use]
64 #[inline(always)]
65 pub(crate) fn has_extras(&self) -> bool {
66 (self.get_node_flags() & NODE_FLAG_HAS_EXTRAS) != 0
67 }
68
69 /// Lazily allocates and returns a mutable reference to `NodeExtras`, raising `NODE_FLAG_HAS_EXTRAS`.
70 ///
71 /// # Arguments
72 ///
73 /// - `&Self` - Mutable scene node.
74 ///
75 /// # Returns
76 ///
77 /// - `&mut NodeExtras` - Mutable reference to boxed cold metadata.
78 #[inline]
79 fn extras_mut(&mut self) -> &mut NodeExtras {
80 let next_flags: u16 = self.get_node_flags() | NODE_FLAG_HAS_EXTRAS;
81 let _: &mut Self = self.set_node_flags(next_flags);
82 let Self { extras, .. } = self;
83 extras.get_or_insert_with(Box::default)
84 }
85
86 /// Drops any lazily allocated cold metadata on node disposal and clears extras flags.
87 ///
88 /// # Arguments
89 ///
90 /// - `&Self` - Mutable scene node.
91 #[inline(always)]
92 pub(crate) fn clear_extras(&mut self) {
93 let extras_bits: u16 = NODE_FLAG_HAS_EXTRAS + NODE_FLAG_HAS_LIFECYCLE + NODE_FLAG_HAS_SCOPE;
94 let cleared: u16 = self.get_node_flags() & !extras_bits;
95 let _: &mut Self = self.set_node_flags(cleared);
96 let Self { extras, .. } = self;
97 *extras = None;
98 }
99
100 /// Returns a shared reference to the compiled `Style` without copying by value.
101 ///
102 /// # Arguments
103 ///
104 /// - `&Self` - Scene node reference.
105 ///
106 /// # Returns
107 ///
108 /// - `&Style` - Borrowed compiled style reference.
109 #[must_use]
110 #[inline(always)]
111 pub fn style_ref(&self) -> &Style {
112 let Self { style, .. } = self;
113 style
114 }
115
116 /// Returns a shared reference to the compiled `LayoutStyle` without copying by value.
117 ///
118 /// # Arguments
119 ///
120 /// - `&Self` - Scene node reference.
121 ///
122 /// # Returns
123 ///
124 /// - `&LayoutStyle` - Borrowed layout style reference.
125 #[must_use]
126 #[inline(always)]
127 pub fn layout_ref(&self) -> &LayoutStyle {
128 self.style_ref().layout_ref()
129 }
130
131 /// Returns a shared reference to the compiled `PaintStyle` without copying by value.
132 ///
133 /// # Arguments
134 ///
135 /// - `&Self` - Scene node reference.
136 ///
137 /// # Returns
138 ///
139 /// - `&PaintStyle` - Borrowed paint style reference.
140 #[must_use]
141 #[inline(always)]
142 pub fn paint_ref(&self) -> &PaintStyle {
143 self.style_ref().paint_ref()
144 }
145
146 /// Returns a shared reference to the compiled `TextStyle` without copying by value.
147 ///
148 /// # Arguments
149 ///
150 /// - `&Self` - Scene node reference.
151 ///
152 /// # Returns
153 ///
154 /// - `&TextStyle` - Borrowed typography style reference.
155 #[must_use]
156 #[inline(always)]
157 pub fn text_ref(&self) -> &TextStyle {
158 self.style_ref().text_ref()
159 }
160
161 /// Returns a shared reference to the computed `LayoutBox` without copying by value.
162 ///
163 /// # Arguments
164 ///
165 /// - `&Self` - Scene node reference.
166 ///
167 /// # Returns
168 ///
169 /// - `&LayoutBox` - Borrowed layout geometry reference.
170 #[must_use]
171 #[inline(always)]
172 pub fn layout_box_ref(&self) -> &LayoutBox {
173 let Self { layout_box, .. } = self;
174 layout_box
175 }
176
177 /// Returns the optional parent `NodeId`.
178 ///
179 /// # Arguments
180 ///
181 /// - `&Self` - Scene node reference.
182 ///
183 /// # Returns
184 ///
185 /// - `Option<NodeId>` - Parent node handle if attached.
186 #[must_use]
187 #[inline(always)]
188 pub fn parent_id(&self) -> Option<NodeId> {
189 let Self { parent, .. } = self;
190 *parent
191 }
192
193 /// Returns a shared reference to the node's `EventHandlers` without allocating `NodeExtras`.
194 ///
195 /// # Arguments
196 ///
197 /// - `&Self` - Scene node reference.
198 ///
199 /// # Returns
200 ///
201 /// - `&EventHandlers` - Borrowed event handler table (shared empty table when unallocated).
202 #[must_use]
203 #[inline(always)]
204 pub fn get_handlers(&self) -> &EventHandlers {
205 let Self { extras, .. } = self;
206 match extras {
207 Some(ext) => ext.get_handlers(),
208 None => EMPTY_EVENT_HANDLERS_REF,
209 }
210 }
211
212 /// Returns a mutable reference to the node's `EventHandlers`, allocating `NodeExtras` lazily.
213 ///
214 /// # Arguments
215 ///
216 /// - `&Self` - Mutable scene node.
217 ///
218 /// # Returns
219 ///
220 /// - `&mut EventHandlers` - Mutable event handler table.
221 #[inline]
222 pub fn get_mut_handlers(&mut self) -> &mut EventHandlers {
223 self.extras_mut().get_mut_handlers()
224 }
225
226 /// Replaces the node's `EventHandlers`, avoiding allocation when setting an empty table on a plain node.
227 ///
228 /// # Arguments
229 ///
230 /// - `&Self` - Mutable scene node.
231 /// - `EventHandlers` - Replacement event handler collection.
232 ///
233 /// # Returns
234 ///
235 /// - `&mut Self` - Mutable scene node for chaining.
236 #[inline]
237 pub fn set_handlers(&mut self, value: EventHandlers) -> &mut Self {
238 if !self.has_extras() && value.is_empty() {
239 return self;
240 }
241 let _: &mut NodeExtras = self.extras_mut().set_handlers(value);
242 self
243 }
244
245 /// Returns the optional hover paint style override.
246 ///
247 /// # Arguments
248 ///
249 /// - `&Self` - Scene node reference.
250 ///
251 /// # Returns
252 ///
253 /// - `Option<PaintStyle>` - Hover paint style if registered.
254 #[must_use]
255 #[inline(always)]
256 pub fn hover_paint(&self) -> Option<PaintStyle> {
257 let Self { extras, .. } = self;
258 extras
259 .as_deref()
260 .and_then(|ext: &NodeExtras| ext.hover_style)
261 }
262
263 /// Sets the optional hover paint style override.
264 ///
265 /// # Arguments
266 ///
267 /// - `&Self` - Mutable scene node.
268 /// - `Option<PaintStyle>` - Hover paint override or `None` to clear.
269 ///
270 /// # Returns
271 ///
272 /// - `&mut Self` - Mutable scene node for chaining.
273 #[inline]
274 pub fn set_hover_style(&mut self, value: Option<PaintStyle>) -> &mut Self {
275 if !self.has_extras() && value.is_none() {
276 return self;
277 }
278 let _: &mut NodeExtras = self.extras_mut().set_hover_style(value);
279 self
280 }
281
282 /// Returns a mutable reference to the optional hover paint style slot.
283 ///
284 /// # Arguments
285 ///
286 /// - `&Self` - Mutable scene node.
287 ///
288 /// # Returns
289 ///
290 /// - `&mut Option<PaintStyle>` - Mutable hover paint override slot.
291 #[inline]
292 pub fn get_mut_hover_style(&mut self) -> &mut Option<PaintStyle> {
293 self.extras_mut().get_mut_hover_style()
294 }
295
296 /// Sets the optional reconciliation key for keyed list items.
297 ///
298 /// # Arguments
299 ///
300 /// - `&Self` - Mutable scene node.
301 /// - `Option<String>` - Reconciliation key or `None` to clear.
302 ///
303 /// # Returns
304 ///
305 /// - `&mut Self` - Mutable scene node for chaining.
306 #[inline]
307 pub fn set_key(&mut self, value: Option<String>) -> &mut Self {
308 if !self.has_extras() && value.is_none() {
309 return self;
310 }
311 let _: &mut NodeExtras = self.extras_mut().set_key(value);
312 self
313 }
314
315 /// Returns a mutable reference to the optional reconciliation key slot.
316 ///
317 /// # Arguments
318 ///
319 /// - `&Self` - Mutable scene node.
320 ///
321 /// # Returns
322 ///
323 /// - `&mut Option<String>` - Mutable reconciliation key slot.
324 #[inline]
325 pub fn get_mut_key(&mut self) -> &mut Option<String> {
326 self.extras_mut().get_mut_key()
327 }
328
329 /// Returns the optional reactive scope owned by this node.
330 ///
331 /// # Arguments
332 ///
333 /// - `&Self` - Scene node reference.
334 ///
335 /// # Returns
336 ///
337 /// - `Option<Scope>` - Attached reactive scope if present.
338 #[must_use]
339 #[inline(always)]
340 pub fn owned_scope(&self) -> Option<Scope> {
341 let Self { extras, .. } = self;
342 extras.as_deref().and_then(|ext: &NodeExtras| ext.scope)
343 }
344
345 /// Sets the optional reactive ownership scope on this node.
346 ///
347 /// # Arguments
348 ///
349 /// - `&Self` - Mutable scene node.
350 /// - `Option<Scope>` - Scope handle or `None` to clear.
351 ///
352 /// # Returns
353 ///
354 /// - `&mut Self` - Mutable scene node for chaining.
355 #[inline]
356 pub fn set_scope(&mut self, value: Option<Scope>) -> &mut Self {
357 if !self.has_extras() && value.is_none() {
358 return self;
359 }
360 let has_scope: bool = value.is_some();
361 let _: &mut NodeExtras = self.extras_mut().set_scope(value);
362 let next_flags: u16 = if has_scope {
363 self.get_node_flags() | NODE_FLAG_HAS_SCOPE
364 } else {
365 self.get_node_flags() & !NODE_FLAG_HAS_SCOPE
366 };
367 let _: &mut Self = self.set_node_flags(next_flags);
368 self
369 }
370
371 /// Returns a mutable reference to the optional reactive ownership scope slot.
372 ///
373 /// # Arguments
374 ///
375 /// - `&Self` - Mutable scene node.
376 ///
377 /// # Returns
378 ///
379 /// - `&mut Option<Scope>` - Mutable scope slot.
380 #[inline]
381 pub fn get_mut_scope(&mut self) -> &mut Option<Scope> {
382 self.extras_mut().get_mut_scope()
383 }
384
385 /// Returns a cloned handle to the custom canvas painter if present.
386 ///
387 /// # Arguments
388 ///
389 /// - `&Self` - Scene node reference.
390 ///
391 /// # Returns
392 ///
393 /// - `Option<CanvasDrawCallback>` - Custom canvas callback if set.
394 #[must_use]
395 #[inline(always)]
396 pub fn canvas_callback(&self) -> Option<CanvasDrawCallback> {
397 let Self { extras, .. } = self;
398 extras
399 .as_deref()
400 .and_then(|ext: &NodeExtras| ext.canvas_painter.clone())
401 }
402
403 /// Sets the optional custom 2D canvas draw callback on this node.
404 ///
405 /// # Arguments
406 ///
407 /// - `&Self` - Mutable scene node.
408 /// - `Option<CanvasDrawCallback>` - Custom canvas painter or `None` to clear.
409 ///
410 /// # Returns
411 ///
412 /// - `&mut Self` - Mutable scene node for chaining.
413 #[inline]
414 pub fn set_canvas_painter(&mut self, value: Option<CanvasDrawCallback>) -> &mut Self {
415 if !self.has_extras() && value.is_none() {
416 return self;
417 }
418 let _: &mut NodeExtras = self.extras_mut().set_canvas_painter(value);
419 self
420 }
421
422 /// Returns a mutable reference to the optional custom canvas painter slot.
423 ///
424 /// # Arguments
425 ///
426 /// - `&Self` - Mutable scene node.
427 ///
428 /// # Returns
429 ///
430 /// - `&mut Option<CanvasDrawCallback>` - Mutable canvas painter slot.
431 #[inline]
432 pub fn get_mut_canvas_painter(&mut self) -> &mut Option<CanvasDrawCallback> {
433 self.extras_mut().get_mut_canvas_painter()
434 }
435
436 /// Borrows the optional explicit semantic accessibility metadata attached to this node without cloning.
437 ///
438 /// # Arguments
439 ///
440 /// - `&Self` - Scene node reference.
441 ///
442 /// # Returns
443 ///
444 /// - `Option<&A11yProps>` - Borrowed accessibility metadata if explicitly configured (`None` in O(1) when `extras` is absent).
445 #[must_use]
446 #[inline(always)]
447 pub fn a11y_props_ref(&self) -> Option<&A11yProps> {
448 let Self { extras, .. } = self;
449 extras
450 .as_deref()
451 .and_then(|ext: &NodeExtras| ext.a11y.as_ref())
452 }
453
454 /// Returns the optional explicit semantic accessibility metadata attached to this node.
455 ///
456 /// # Arguments
457 ///
458 /// - `&Self` - Scene node reference.
459 ///
460 /// # Returns
461 ///
462 /// - `Option<A11yProps>` - Cloned accessibility metadata if explicitly configured.
463 #[must_use]
464 #[inline(always)]
465 pub fn a11y_props(&self) -> Option<A11yProps> {
466 self.a11y_props_ref().cloned()
467 }
468
469 /// Sets the optional semantic accessibility metadata on this node.
470 ///
471 /// # Arguments
472 ///
473 /// - `&Self` - Mutable scene node.
474 /// - `Option<A11yProps>` - Accessibility metadata or `None` to clear.
475 ///
476 /// # Returns
477 ///
478 /// - `&mut Self` - Mutable scene node for chaining.
479 #[inline]
480 pub fn set_a11y(&mut self, value: Option<A11yProps>) -> &mut Self {
481 if !self.has_extras() && value.is_none() {
482 return self;
483 }
484 let _: &mut NodeExtras = self.extras_mut().set_a11y(value);
485 self
486 }
487
488 /// Returns a mutable reference to the optional accessibility metadata slot.
489 ///
490 /// # Arguments
491 ///
492 /// - `&Self` - Mutable scene node.
493 ///
494 /// # Returns
495 ///
496 /// - `&mut Option<A11yProps>` - Mutable accessibility metadata slot.
497 #[inline]
498 pub fn get_mut_a11y(&mut self) -> &mut Option<A11yProps> {
499 self.extras_mut().get_mut_a11y()
500 }
501
502 /// Returns the optional virtualized scroll metrics `(scroll_y, total_content_height)`.
503 ///
504 /// # Arguments
505 ///
506 /// - `&Self` - Scene node reference.
507 ///
508 /// # Returns
509 ///
510 /// - `Option<(f32, f32)>` - Virtual scroll position and full content height when configured.
511 #[must_use]
512 #[inline(always)]
513 pub fn virtual_scroll_metrics(&self) -> Option<(f32, f32)> {
514 let Self { extras, .. } = self;
515 extras
516 .as_deref()
517 .and_then(|ext: &NodeExtras| ext.virtual_scroll)
518 }
519
520 /// Sets the optional virtualized scroll metrics `(scroll_y, total_content_height)`.
521 ///
522 /// # Arguments
523 ///
524 /// - `&Self` - Mutable scene node.
525 /// - `Option<(f32, f32)>` - Virtual scroll metrics or `None` to clear.
526 ///
527 /// # Returns
528 ///
529 /// - `&mut Self` - Mutable scene node for chaining.
530 #[inline]
531 pub fn set_virtual_scroll(&mut self, value: Option<(f32, f32)>) -> &mut Self {
532 if !self.has_extras() && value.is_none() {
533 return self;
534 }
535 let _: &mut NodeExtras = self.extras_mut().set_virtual_scroll(value);
536 self
537 }
538
539 /// Returns a mutable reference to the optional virtualized scroll metrics slot.
540 ///
541 /// # Arguments
542 ///
543 /// - `&Self` - Mutable scene node.
544 ///
545 /// # Returns
546 ///
547 /// - `&mut Option<(f32, f32)>` - Mutable virtual scroll metrics slot.
548 #[inline]
549 pub fn get_mut_virtual_scroll(&mut self) -> &mut Option<(f32, f32)> {
550 self.extras_mut().get_mut_virtual_scroll()
551 }
552
553 /// Returns the cached layout constraint if previously laid out.
554 ///
555 /// # Arguments
556 ///
557 /// - `&Self` - Scene node reference.
558 ///
559 /// # Returns
560 ///
561 /// - `Option<AvailableSpace>` - Last layout constraint.
562 #[must_use]
563 #[inline(always)]
564 pub(crate) fn cached_constraint(&self) -> Option<AvailableSpace> {
565 let Self {
566 last_constraint, ..
567 } = self;
568 *last_constraint
569 }
570
571 /// Registers a `Lifecycle` observer on this scene node.
572 ///
573 /// # Arguments
574 ///
575 /// - `&Self` - Mutable scene node.
576 /// - `Rc<dyn Lifecycle>` - Shared lifecycle hook observer.
577 #[inline]
578 pub fn add_lifecycle(&mut self, hook: Rc<dyn Lifecycle>) {
579 self.extras_mut().get_mut_lifecycles().push(hook);
580 let next_flags: u16 = self.get_node_flags() | NODE_FLAG_HAS_LIFECYCLE;
581 let _: &mut Self = self.set_node_flags(next_flags);
582 }
583
584 /// Returns a mutable reference to the lifecycle observer vector.
585 ///
586 /// # Arguments
587 ///
588 /// - `&Self` - Mutable scene node.
589 ///
590 /// # Returns
591 ///
592 /// - `&mut Vec<Rc<dyn Lifecycle>>` - Mutable lifecycle observer list.
593 #[inline]
594 pub fn get_mut_lifecycles(&mut self) -> &mut Vec<Rc<dyn Lifecycle>> {
595 self.extras_mut().get_mut_lifecycles()
596 }
597
598 /// Replaces the lifecycle observer vector on this node.
599 ///
600 /// # Arguments
601 ///
602 /// - `&Self` - Mutable scene node.
603 /// - `Vec<Rc<dyn Lifecycle>>` - Replacement lifecycle observers.
604 ///
605 /// # Returns
606 ///
607 /// - `&mut Self` - Mutable scene node for chaining.
608 #[inline]
609 pub fn set_lifecycles(&mut self, value: Vec<Rc<dyn Lifecycle>>) -> &mut Self {
610 if !self.has_extras() && value.is_empty() {
611 return self;
612 }
613 let _: &mut NodeExtras = self.extras_mut().set_lifecycles(value);
614 self
615 }
616
617 /// Returns cloned handles to all `Lifecycle` observers attached to this node.
618 ///
619 /// # Arguments
620 ///
621 /// - `&Self` - Scene node reference.
622 ///
623 /// # Returns
624 ///
625 /// - `Vec<Rc<dyn Lifecycle>>` - Attached lifecycle observers.
626 #[must_use]
627 #[inline]
628 pub fn lifecycle_observers(&self) -> Vec<Rc<dyn Lifecycle>> {
629 let Self { extras, .. } = self;
630 match extras.as_deref() {
631 Some(ext) if !ext.lifecycles.is_empty() => ext.lifecycles.clone(),
632 _ => Vec::new(),
633 }
634 }
635
636 /// Removes and returns all `Lifecycle` observers attached to this node during disposal.
637 ///
638 /// # Arguments
639 ///
640 /// - `&Self` - Mutable scene node.
641 ///
642 /// # Returns
643 ///
644 /// - `Vec<Rc<dyn Lifecycle>>` - Drained lifecycle observers.
645 #[inline]
646 pub(crate) fn take_lifecycles(&mut self) -> Vec<Rc<dyn Lifecycle>> {
647 let cleared: u16 = self.get_node_flags() & !NODE_FLAG_HAS_LIFECYCLE;
648 let _: &mut Self = self.set_node_flags(cleared);
649 let Self { extras, .. } = self;
650 match extras.as_deref_mut() {
651 Some(ext) if !ext.lifecycles.is_empty() => take(ext.get_mut_lifecycles()),
652 _ => Vec::new(),
653 }
654 }
655}