Skip to main content

euv_example/pages/shell/
impl.rs

1use super::*;
2
3/// Construction helper for `ShowcasePage`.
4impl ShowcasePage {
5    /// Creates a typed `ShowcasePage` component bound to `route` with `PageRetention::Freeze` lifecycle semantics.
6    ///
7    /// # Arguments
8    ///
9    /// - `&'static str` - Normalized route path.
10    /// - `ShowcaseState` - Shared showcase reactive state.
11    /// - `ThemeTokens` - Active design tokens.
12    ///
13    /// # Returns
14    ///
15    /// - `Self` - Configured lazy page component.
16    #[must_use]
17    pub fn new(route: &'static str, state: ShowcaseState, tokens: ThemeTokens) -> Self {
18        Self {
19            route,
20            state,
21            tokens,
22        }
23    }
24}
25
26/// Declarative `#[component(retention = Freeze)]` generating both `Lifecycle` (Freeze-on-leave + game pause/resume) and `Component` for every showcase route.
27#[component(retention = Freeze)]
28/// Inherent implementation of [`ShowcasePage`].
29impl ShowcasePage {
30    /// Records lazy page creation and activates embedded game engines only when their page is displayed.
31    ///
32    /// # Arguments
33    ///
34    /// - `&Self` - Page component reference.
35    /// - `NodeId` - Mounted page root node handle.
36    fn on_mount(&self, _node: NodeId) {
37        let route: &str = self.get_route();
38        let node_style: &ShowcaseState = self.get_state();
39        let _: bool = {
40            if let Some(mut updated_value) = (node_style.get_lifecycle_mounts()).try_get_untracked()
41            {
42                {
43                    let char_item: &mut u32 = &mut updated_value;
44                    *char_item += 1;
45                }
46                (node_style.get_lifecycle_mounts()).set(updated_value)
47            } else {
48                false
49            }
50        };
51        let _: bool = node_style
52            .get_lifecycle_last_event()
53            .set_if_changed(format!("MOUNT {route} (PageRetention::Freeze)"));
54        if !node_style.get_bg_frozen().get_untracked() {
55            if route == ROUTE_GAME {
56                ShowcaseState::resume_game_host(node_style.get_game_host());
57            } else if route == ROUTE_GAME_3D {
58                ShowcaseState::resume_game_host(node_style.get_game3d_host());
59            } else if route == ROUTE_WATER {
60                ShowcaseState::resume_game_host(node_style.get_water_host());
61            }
62        }
63    }
64
65    /// Ensures the active page's embedded game engine runs exclusively while visible.
66    ///
67    /// # Arguments
68    ///
69    /// - `&Self` - Page component reference.
70    /// - `NodeId` - Mounted page root node handle.
71    fn on_activate(&self, _node: NodeId) {
72        let route: &str = self.get_route();
73        let node_style: &ShowcaseState = self.get_state();
74        if !node_style.get_bg_frozen().get_untracked() {
75            if route == ROUTE_GAME {
76                ShowcaseState::resume_game_host(node_style.get_game_host());
77            } else if route == ROUTE_GAME_3D {
78                ShowcaseState::resume_game_host(node_style.get_game3d_host());
79            } else if route == ROUTE_WATER {
80                ShowcaseState::resume_game_host(node_style.get_water_host());
81            }
82        }
83    }
84
85    /// Freezes the departed page's reactive effects and pauses embedded 2D/3D engines at zero CPU cost.
86    ///
87    /// # Arguments
88    ///
89    /// - `&Self` - Page component reference.
90    /// - `NodeId` - Frozen page root node handle.
91    fn on_freeze(&self, _node: NodeId) {
92        let route: &str = self.get_route();
93        let node_style: &ShowcaseState = self.get_state();
94        let _: bool = {
95            if let Some(mut updated_value) =
96                (node_style.get_lifecycle_freezes()).try_get_untracked()
97            {
98                {
99                    let char_item: &mut u32 = &mut updated_value;
100                    *char_item += 1;
101                }
102                (node_style.get_lifecycle_freezes()).set(updated_value)
103            } else {
104                false
105            }
106        };
107        let _: bool = node_style
108            .get_lifecycle_last_event()
109            .set_if_changed(format!("FREEZE {route} (effects & engines suspended)"));
110        if route == ROUTE_GAME {
111            node_style.get_game_host().set_paused(true);
112        } else if route == ROUTE_GAME_3D {
113            node_style.get_game3d_host().set_paused(true);
114        } else if route == ROUTE_WATER {
115            node_style.get_water_host().set_paused(true);
116        }
117    }
118
119    /// Resumes a previously frozen page and atomically flushes coalesced cross-page signal updates.
120    ///
121    /// # Arguments
122    ///
123    /// - `&Self` - Page component reference.
124    /// - `NodeId` - Resumed page root node handle.
125    fn on_resume(&self, _node: NodeId) {
126        let route: &str = self.get_route();
127        let node_style: &ShowcaseState = self.get_state();
128        let _: bool = {
129            if let Some(mut updated_value) =
130                (node_style.get_lifecycle_resumes()).try_get_untracked()
131            {
132                {
133                    let char_item: &mut u32 = &mut updated_value;
134                    *char_item += 1;
135                }
136                (node_style.get_lifecycle_resumes()).set(updated_value)
137            } else {
138                false
139            }
140        };
141        let _: bool = node_style
142            .get_lifecycle_last_event()
143            .set_if_changed(format!("RESUME {route} (coalesced state synced)"));
144        if !node_style.get_bg_frozen().get_untracked() {
145            if route == ROUTE_GAME {
146                ShowcaseState::resume_game_host(node_style.get_game_host());
147            } else if route == ROUTE_GAME_3D {
148                ShowcaseState::resume_game_host(node_style.get_game3d_host());
149            } else if route == ROUTE_WATER {
150                ShowcaseState::resume_game_host(node_style.get_water_host());
151            }
152        }
153    }
154
155    /// Records final disposal and ensures embedded game engines are paused.
156    ///
157    /// # Arguments
158    ///
159    /// - `&Self` - Page component reference.
160    fn on_destroy(&self) {
161        let route: &str = self.get_route();
162        let node_style: &ShowcaseState = self.get_state();
163        let _: bool = {
164            if let Some(mut updated_value) =
165                (node_style.get_lifecycle_destroys()).try_get_untracked()
166            {
167                {
168                    let char_item: &mut u32 = &mut updated_value;
169                    *char_item += 1;
170                }
171                (node_style.get_lifecycle_destroys()).set(updated_value)
172            } else {
173                false
174            }
175        };
176        if route == ROUTE_GAME {
177            node_style.get_game_host().set_paused(true);
178        } else if route == ROUTE_GAME_3D {
179            node_style.get_game3d_host().set_paused(true);
180        } else if route == ROUTE_WATER {
181            node_style.get_water_host().set_paused(true);
182        }
183    }
184
185    /// Renders this showcase route lazily inside the ambient framework-managed `Context`.
186    ///
187    /// # Arguments
188    ///
189    /// - `&Self` - Page component reference.
190    ///
191    /// # Returns
192    ///
193    /// - `NodeId` - Mounted page node handle.
194    fn render(&self) -> NodeId {
195        mount_route_by_name(self.get_route(), self.get_state(), self.get_tokens())
196    }
197}