euv_example/pages/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 st: &ShowcaseState = self.get_state();
39 let _: bool = st.get_lifecycle_mounts().update(|c: &mut u32| *c += 1);
40 let _: bool = st
41 .get_lifecycle_last_event()
42 .set_if_changed(format!("MOUNT {route} (PageRetention::Freeze)"));
43 if route == ROUTE_GAME {
44 st.get_game_host().set_paused(false);
45 } else if route == ROUTE_GAME_3D {
46 st.get_game3d_host().set_paused(false);
47 }
48 }
49
50 /// Ensures the active page's embedded game engine runs exclusively while visible.
51 ///
52 /// # Arguments
53 ///
54 /// - `&Self` - Page component reference.
55 /// - `NodeId` - Mounted page root node handle.
56 fn on_activate(&self, _node: NodeId) {
57 let route: &str = self.get_route();
58 let st: &ShowcaseState = self.get_state();
59 if route == ROUTE_GAME {
60 st.get_game_host().set_paused(false);
61 } else if route == ROUTE_GAME_3D {
62 st.get_game3d_host().set_paused(false);
63 }
64 }
65
66 /// Freezes the departed page's reactive effects and pauses embedded 2D/3D engines at zero CPU cost.
67 ///
68 /// # Arguments
69 ///
70 /// - `&Self` - Page component reference.
71 /// - `NodeId` - Frozen page root node handle.
72 fn on_freeze(&self, _node: NodeId) {
73 let route: &str = self.get_route();
74 let st: &ShowcaseState = self.get_state();
75 let _: bool = st.get_lifecycle_freezes().update(|c: &mut u32| *c += 1);
76 let _: bool = st
77 .get_lifecycle_last_event()
78 .set_if_changed(format!("FREEZE {route} (effects & engines suspended)"));
79 if route == ROUTE_GAME {
80 st.get_game_host().set_paused(true);
81 } else if route == ROUTE_GAME_3D {
82 st.get_game3d_host().set_paused(true);
83 }
84 }
85
86 /// Resumes a previously frozen page and atomically flushes coalesced cross-page signal updates.
87 ///
88 /// # Arguments
89 ///
90 /// - `&Self` - Page component reference.
91 /// - `NodeId` - Resumed page root node handle.
92 fn on_resume(&self, _node: NodeId) {
93 let route: &str = self.get_route();
94 let st: &ShowcaseState = self.get_state();
95 let _: bool = st.get_lifecycle_resumes().update(|c: &mut u32| *c += 1);
96 let _: bool = st
97 .get_lifecycle_last_event()
98 .set_if_changed(format!("RESUME {route} (coalesced state synced)"));
99 if route == ROUTE_GAME {
100 st.get_game_host().set_paused(false);
101 } else if route == ROUTE_GAME_3D {
102 st.get_game3d_host().set_paused(false);
103 }
104 }
105
106 /// Records final disposal and ensures embedded game engines are paused.
107 ///
108 /// # Arguments
109 ///
110 /// - `&Self` - Page component reference.
111 fn on_destroy(&self) {
112 let route: &str = self.get_route();
113 let st: &ShowcaseState = self.get_state();
114 let _: bool = st.get_lifecycle_destroys().update(|c: &mut u32| *c += 1);
115 if route == ROUTE_GAME {
116 st.get_game_host().set_paused(true);
117 } else if route == ROUTE_GAME_3D {
118 st.get_game3d_host().set_paused(true);
119 }
120 }
121
122 /// Renders this showcase route lazily inside the ambient framework-managed `Context`.
123 ///
124 /// # Arguments
125 ///
126 /// - `&Self` - Page component reference.
127 ///
128 /// # Returns
129 ///
130 /// - `NodeId` - Mounted page node handle.
131 fn render(&self) -> NodeId {
132 mount_route_by_name(self.get_route(), self.get_state(), self.get_tokens())
133 }
134}