Skip to main content

euv_core/scene/node/
impl.rs

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