euv_core/scene/lifecycle/trait.rs
1use super::*;
2
3/// Standalone lifecycle hook contract implemented by components and routed pages.
4///
5/// Business code overrides only the hooks it needs; every method provides a zero-cost no-op default.
6pub trait Lifecycle {
7 /// Declares how the framework retains this page or component when navigating away.
8 ///
9 /// # Arguments
10 ///
11 /// - `&Self` - Component or page instance reference.
12 ///
13 /// # Returns
14 ///
15 /// - `PageRetention` - Retention strategy (`Destroy`, `Freeze`, or `Alive`).
16 fn retention_policy(&self) -> PageRetention {
17 PageRetention::Freeze
18 }
19
20 /// Invoked once immediately before the component's initial `render` pass executes.
21 ///
22 /// # Arguments
23 ///
24 /// - `&Self` - Component or page instance reference.
25 fn on_create(&self) {}
26
27 /// Invoked once immediately after `render` attaches the root `NodeId` to the scene tree.
28 ///
29 /// # Arguments
30 ///
31 /// - `&Self` - Component or page instance reference.
32 /// - `NodeId` - Mounted root scene node handle.
33 fn on_mount(&self, _node: NodeId) {}
34
35 /// Invoked when external props, theme tokens, or cross-page state trigger a component update.
36 ///
37 /// # Arguments
38 ///
39 /// - `&Self` - Component or page instance reference.
40 /// - `NodeId` - Active root scene node handle.
41 fn on_update(&self, _node: NodeId) {}
42
43 /// Invoked whenever this component or routed page becomes the active visible view.
44 ///
45 /// # Arguments
46 ///
47 /// - `&Self` - Component or page instance reference.
48 /// - `NodeId` - Active root scene node handle.
49 fn on_activate(&self, _node: NodeId) {}
50
51 /// Invoked when the router or parent suspends this subtree under `PageRetention::Freeze`.
52 ///
53 /// # Arguments
54 ///
55 /// - `&Self` - Component or page instance reference.
56 /// - `NodeId` - Frozen root scene node handle.
57 fn on_freeze(&self, _node: NodeId) {}
58
59 /// Invoked when a previously frozen subtree resumes and coalesced cross-page updates flush.
60 ///
61 /// # Arguments
62 ///
63 /// - `&Self` - Component or page instance reference.
64 /// - `NodeId` - Resumed root scene node handle.
65 fn on_resume(&self, _node: NodeId) {}
66
67 /// Invoked immediately before the subtree is detached and its reactive scope is disposed.
68 ///
69 /// # Arguments
70 ///
71 /// - `&Self` - Component or page instance reference.
72 /// - `NodeId` - Unmounting root scene node handle.
73 fn on_unmount(&self, _node: NodeId) {}
74
75 /// Invoked after the component's reactive scope and child nodes have been fully destroyed.
76 ///
77 /// # Arguments
78 ///
79 /// - `&Self` - Component or page instance reference.
80 fn on_destroy(&self) {}
81
82 /// Invoked when an error boundary or runtime fault surfaces inside this component subtree.
83 ///
84 /// # Arguments
85 ///
86 /// - `&Self` - Component or page instance reference.
87 /// - `&str` - Diagnostic error message.
88 fn on_error(&self, _error: &str) {}
89}
90
91/// Core declarative component trait unifying `Lifecycle` hooks with framework-managed `Context` rendering.
92pub trait Component: Lifecycle + Sized + 'static {
93 /// Renders this component into the ambient framework-managed `Context` and returns its root `NodeId`.
94 ///
95 /// # Arguments
96 ///
97 /// - `&Self` - Immutable component instance reference.
98 ///
99 /// # Returns
100 ///
101 /// - `NodeId` - Root scene node handle produced by rendering.
102 fn render(&self) -> NodeId;
103
104 /// Mounts this component through the complete framework lifecycle (`on_create` -> `render` -> `on_mount`) and registers its `Lifecycle` observer on the mounted node.
105 ///
106 /// # Arguments
107 ///
108 /// - `Self` - Owned component instance.
109 ///
110 /// # Returns
111 ///
112 /// - `NodeId` - Mounted root scene node handle.
113 fn mount(self) -> NodeId {
114 let instance: Rc<Self> = Rc::new(self);
115 instance.on_create();
116 let node: NodeId = instance.render();
117 let hooks: Rc<dyn Lifecycle> = Rc::clone(&instance) as Rc<dyn Lifecycle>;
118 let ctx: Context = current_context();
119 ctx.get_scene().with_write(
120 |tree: &mut SceneTree| {
121 tree.attach_lifecycle(node, hooks);
122 },
123 (),
124 );
125 instance.on_mount(node);
126 node
127 }
128}