Skip to main content

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    /// Invoked when this component, routed page, or application window becomes visually presented.
91    ///
92    /// # Arguments
93    ///
94    /// - `&Self` - Component or page instance reference.
95    fn on_appear(&self) {}
96
97    /// Invoked when this component, routed page, or application window ceases to be visually presented.
98    ///
99    /// # Arguments
100    ///
101    /// - `&Self` - Component or page instance reference.
102    fn on_disappear(&self) {}
103
104    /// Invoked when the application returns to active foreground execution.
105    ///
106    /// # Arguments
107    ///
108    /// - `&Self` - Component or page instance reference.
109    fn on_foreground(&self) {}
110
111    /// Invoked when the application enters suspended/hidden background execution.
112    ///
113    /// # Arguments
114    ///
115    /// - `&Self` - Component or page instance reference.
116    fn on_background(&self) {}
117
118    /// Invoked when the application enters inactive status (occluded, app switcher, or deactivated).
119    ///
120    /// # Arguments
121    ///
122    /// - `&Self` - Component or page instance reference.
123    fn on_inactive(&self) {}
124
125    /// Invoked when the application is terminating gracefully.
126    ///
127    /// # Arguments
128    ///
129    /// - `&Self` - Component or page instance reference.
130    fn on_terminate(&self) {}
131
132    /// Invoked for every normalized cross-platform `SystemEvent` delivered while this component is active.
133    ///
134    /// # Arguments
135    ///
136    /// - `&Self` - Component or page instance reference.
137    /// - `&SystemEvent` - Normalized cross-platform system event payload.
138    fn on_system_event(&self, _event: &SystemEvent) {}
139}
140
141/// Core declarative component trait unifying `Lifecycle` hooks with framework-managed `Context` rendering.
142pub trait Component: Lifecycle + Sized + 'static {
143    /// Compile-time opt-in flag indicating whether this component overrides lifecycle hooks or retention policy and requires `Rc<Self>` observer registration on mount.
144    const NEEDS_LIFECYCLE: bool = false;
145
146    /// Renders this component into the ambient framework-managed `Context` and returns its root `NodeId`.
147    ///
148    /// # Arguments
149    ///
150    /// - `&Self` - Immutable component instance reference.
151    ///
152    /// # Returns
153    ///
154    /// - `NodeId` - Root scene node handle produced by rendering.
155    fn render(&self) -> NodeId;
156
157    /// Mounts this component through the complete framework lifecycle (`on_create` -> `render` -> `on_mount`) and registers its `Lifecycle` observer on the mounted node.
158    ///
159    /// # Arguments
160    ///
161    /// - `Self` - Owned component instance.
162    ///
163    /// # Returns
164    ///
165    /// - `NodeId` - Mounted root scene node handle.
166    fn mount(self) -> NodeId {
167        if !Self::NEEDS_LIFECYCLE {
168            return self.render();
169        }
170        let instance: Rc<Self> = Rc::new(self);
171        instance.on_create();
172        let node: NodeId = instance.render();
173        let hooks: Rc<dyn Lifecycle> = Rc::clone(&instance) as Rc<dyn Lifecycle>;
174        with_current_context(|ctx: &Context| {
175            ctx.get_scene().with_write(
176                |tree: &mut SceneTree| {
177                    tree.attach_lifecycle(node, hooks);
178                },
179                (),
180            );
181        });
182        instance.on_mount(node);
183        node
184    }
185}
186
187/// Initialization contract enabling declarative named-attribute component construction inside `view! { MyComp { prop: value } }`.
188pub trait ComponentProps: Sized {
189    /// Constructs a component descriptor with default property values ready for fluent named-prop chaining.
190    ///
191    /// # Returns
192    ///
193    /// - `Self` - Component instance initialized with its declared prop defaults.
194    fn init_props() -> Self;
195}