Skip to main content

euv_core/dynamic/hub/
struct.rs

1use super::*;
2
3/// Serializable style overrides attached to a `DynamicNodeSpec`.
4#[derive(Clone, Copy, Data, Debug, Default, Deserialize, PartialEq, Serialize)]
5#[serde(default)]
6pub struct DynamicStyleSpec {
7    /// Optional explicit pixel width (when `Some`, overrides default auto width).
8    #[get(skip)]
9    pub(crate) width: Option<f32>,
10    /// Optional explicit pixel height (when `Some`, overrides default auto height).
11    #[get(skip)]
12    pub(crate) height: Option<f32>,
13    /// Whether this node should stretch to `100%` of its parent width.
14    #[get(type(copy))]
15    pub(crate) full_width: bool,
16    /// Optional uniform inner padding in logical pixels.
17    #[get(skip)]
18    pub(crate) padding: Option<f32>,
19    /// Optional inter-child gap in logical pixels.
20    #[get(skip)]
21    pub(crate) gap: Option<f32>,
22    /// Optional uniform corner radius in logical pixels.
23    #[get(skip)]
24    pub(crate) radius: Option<f32>,
25    /// Optional text font size in logical pixels.
26    #[get(skip)]
27    pub(crate) font_size: Option<f32>,
28    /// Whether typography renders in bold weight.
29    #[get(type(copy))]
30    pub(crate) bold: bool,
31    /// Optional 24-bit RGB hex background fill color (e.g. `0x2563EB`).
32    #[get(skip)]
33    pub(crate) background_hex: Option<u32>,
34    /// Optional 24-bit RGB hex foreground text color (e.g. `0xFFFFFF`).
35    #[get(skip)]
36    pub(crate) color_hex: Option<u32>,
37    /// Optional border stroke width in logical pixels.
38    #[get(skip)]
39    pub(crate) border_width: Option<f32>,
40    /// Optional 24-bit RGB hex border stroke color.
41    #[get(skip)]
42    pub(crate) border_hex: Option<u32>,
43    /// Grid column count when `kind == DynamicNodeKind::Grid`.
44    #[get(type(copy))]
45    pub(crate) columns: u16,
46    /// Flex grow factor inside parent flex containers.
47    #[get(type(copy))]
48    pub(crate) flex_grow: f32,
49}
50
51/// Serializable declarative UI node tree compiled directly into native `SceneTree` nodes at runtime.
52#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
53#[serde(default)]
54pub struct DynamicNodeSpec {
55    /// Semantic native primitive kind.
56    #[get(type(copy))]
57    pub(crate) kind: DynamicNodeKind,
58    /// Optional UTF-8 text content for `Text`, `Button`, `Badge`, or `Card` title.
59    #[new(skip)]
60    pub(crate) text: String,
61    /// Optional action identifier emitted as `DynamicActionEvent` when clicked.
62    #[new(skip)]
63    #[get(skip)]
64    pub(crate) action: Option<String>,
65    /// Optional opaque string payload carried by `DynamicActionEvent`.
66    #[new(skip)]
67    pub(crate) payload: String,
68    /// Optional declarative visual and layout style overrides.
69    #[new(skip)]
70    #[get(type(copy))]
71    pub(crate) style: DynamicStyleSpec,
72    /// Ordered nested child specifications.
73    #[new(skip)]
74    pub(crate) children: Vec<DynamicNodeSpec>,
75}
76
77/// Versioned, checksum-verified component-level dynamic update payload.
78#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
79pub struct DynamicComponentUpdate {
80    /// Unique slot identifier targeted by this component update (e.g. `"promo_banner"`).
81    pub(crate) id: String,
82    /// Monotonic component revision number (`>= 1`).
83    #[get(type(copy))]
84    pub(crate) version: u64,
85    /// Declarative UI subtree replacing the slot contents.
86    pub(crate) spec: DynamicNodeSpec,
87    /// Optional expected deterministic FNV-1a checksum (`0` skips checksum verification).
88    #[new(skip)]
89    #[serde(default)]
90    #[get(type(copy))]
91    pub(crate) checksum: u64,
92    /// Whether this update may apply a version lower than or equal to the currently active version.
93    #[new(skip)]
94    #[serde(default)]
95    #[get(type(copy))]
96    pub(crate) allow_downgrade: bool,
97}
98
99/// Versioned, checksum-verified page-level dynamic update payload (overrides or injects a route).
100#[derive(Clone, Data, Debug, Deserialize, New, PartialEq, Serialize)]
101pub struct DynamicPageUpdate {
102    /// Normalized route path targeted by this page update (e.g. `"/promo-live"`).
103    pub(crate) route: String,
104    /// Human-readable page title exposed for navigation menus and headers.
105    pub(crate) title: String,
106    /// Monotonic page revision number (`>= 1`).
107    #[get(type(copy))]
108    pub(crate) version: u64,
109    /// Declarative UI page tree rendered when this route is active.
110    pub(crate) spec: DynamicNodeSpec,
111    /// Optional expected deterministic FNV-1a checksum (`0` skips checksum verification).
112    #[new(skip)]
113    #[serde(default)]
114    #[get(type(copy))]
115    pub(crate) checksum: u64,
116    /// Hint indicating whether external page state should be preserved across hot-swaps.
117    #[new(skip)]
118    #[serde(default)]
119    #[get(type(copy))]
120    pub(crate) preserve_state: bool,
121    /// Whether this update may apply a version lower than or equal to the currently active version.
122    #[new(skip)]
123    #[serde(default)]
124    #[get(type(copy))]
125    pub(crate) allow_downgrade: bool,
126}
127
128/// Atomic multi-target update manifest delivering component-level and page-level updates together.
129#[derive(Clone, Data, Debug, Default, Deserialize, New, PartialEq, Serialize)]
130pub struct DynamicUpdateBundle {
131    /// Unique release bundle identifier (e.g. `"ota-2026.11.25-01"`).
132    pub(crate) bundle_id: String,
133    /// Monotonic bundle release version.
134    #[get(type(copy))]
135    pub(crate) version: u64,
136    /// Ordered component-level updates applied atomically.
137    #[new(skip)]
138    #[serde(default)]
139    pub(crate) components: Vec<DynamicComponentUpdate>,
140    /// Ordered page-level updates applied atomically.
141    #[new(skip)]
142    #[serde(default)]
143    pub(crate) pages: Vec<DynamicPageUpdate>,
144}
145
146/// Interactive event emitted when a user activates a button inside a dynamically updated component or page.
147#[derive(Clone, Data, Debug, Eq, Hash, New, PartialEq)]
148pub struct DynamicActionEvent {
149    /// Originating component slot ID or page route path.
150    pub(crate) source_id: String,
151    /// Action identifier declared on the dynamic control.
152    pub(crate) action: String,
153    /// Optional string payload carried with the action.
154    pub(crate) payload: String,
155}
156
157/// Cumulative runtime telemetry counters exposed reactively by `DynamicUpdateHub`.
158#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
159pub struct DynamicTelemetry {
160    /// Total successful component-level dynamic updates applied.
161    #[get(type(copy))]
162    pub(crate) components_applied: u64,
163    /// Total successful page-level dynamic updates applied.
164    #[get(type(copy))]
165    pub(crate) pages_applied: u64,
166    /// Total component or page rollbacks executed.
167    #[get(type(copy))]
168    pub(crate) rollbacks: u64,
169    /// Total rejected updates (checksum mismatch, version regression, invalid JSON).
170    #[get(type(copy))]
171    pub(crate) rejected: u64,
172    /// Total interactive actions dispatched from dynamically rendered controls.
173    #[get(type(copy))]
174    pub(crate) actions_dispatched: u64,
175}
176
177/// Single revision entry stored on a component or page history stack for instant rollback.
178#[derive(Clone, CustomDebug)]
179pub(crate) struct DynamicSlotRecord {
180    /// Revision version number.
181    pub(crate) version: u64,
182    /// Optional page title (empty for component slots).
183    pub(crate) title: String,
184    /// Verified payload checksum (`0` for native Rust closure swaps).
185    pub(crate) checksum: u64,
186    /// Renderable payload (declarative spec or native Rust builder).
187    pub(crate) payload: DynamicPayload,
188}
189
190/// Internal interior-mutable registry storing component/page revision stacks and surgical signals.
191#[derive(CustomDebug)]
192pub(crate) struct DynamicHubState {
193    /// Revision stacks keyed by component slot ID.
194    pub(crate) components: HashMap<String, Vec<DynamicSlotRecord>>,
195    /// Dedicated per-component reactive revision signals for O(1) surgical re-renders.
196    pub(crate) component_signals: HashMap<String, Signal<u64>>,
197    /// Revision stacks keyed by normalized page route path.
198    pub(crate) pages: HashMap<String, Vec<DynamicSlotRecord>>,
199    /// Dedicated per-page reactive revision signals for O(1) surgical page re-renders.
200    pub(crate) page_signals: HashMap<String, Signal<u64>>,
201    /// Reactive counter bumped whenever the set of registered dynamic routes changes.
202    pub(crate) routes_signal: Signal<u64>,
203    /// Reactive telemetry snapshot signal.
204    pub(crate) telemetry_signal: Signal<DynamicTelemetry>,
205    /// Reactive signal carrying the most recent `DynamicActionEvent`.
206    pub(crate) last_action: Signal<Option<DynamicActionEvent>>,
207    /// Registered imperative action callbacks.
208    #[debug(skip)]
209    pub(crate) action_handlers: Vec<DynamicActionCallback>,
210}
211
212/// Shared cross-platform hub orchestrating surgical component-level and page-level dynamic updates.
213#[derive(Clone, CustomDebug)]
214pub struct DynamicUpdateHub {
215    /// Interior-mutable registry state shared across `App`, `Context`, and live UI slots.
216    pub(crate) inner: Rc<RefCell<DynamicHubState>>,
217}
218
219/// Declarative `Component` slot supporting surgical component-level dynamic updates and rollbacks.
220#[derive(CustomDebug, Data, New)]
221pub struct DynamicComponentSlot {
222    /// Unique component slot identifier targeted by `DynamicUpdateHub::apply_component`.
223    pub(crate) id: String,
224    /// Compiled fallback UI builder rendered when no dynamic update is active for `id`.
225    #[debug(skip)]
226    #[get(skip)]
227    pub(crate) fallback: DynamicBuilderFn,
228    /// Optional explicit `DynamicUpdateHub` override (defaults to the ambient `Context` hub).
229    #[new(skip)]
230    #[get(skip)]
231    pub(crate) hub: Option<DynamicUpdateHub>,
232    /// Optional container `Style` applied to the slot host wrapper node.
233    #[new(skip)]
234    #[get(type(copy))]
235    pub(crate) style: Style,
236}
237
238/// Declarative `Component` slot supporting surgical page-level dynamic updates for a single route.
239#[derive(CustomDebug, Data, New)]
240pub struct DynamicPageSlot {
241    /// Route path targeted by `DynamicUpdateHub::apply_page` (e.g. `"/dynamic/live"`).
242    pub(crate) route: String,
243    /// Compiled fallback page builder rendered when no dynamic page update is active.
244    #[debug(skip)]
245    #[get(skip)]
246    pub(crate) fallback: DynamicBuilderFn,
247    /// Optional explicit `DynamicUpdateHub` override (defaults to the ambient `Context` hub).
248    #[new(skip)]
249    #[get(skip)]
250    pub(crate) hub: Option<DynamicUpdateHub>,
251    /// Optional container `Style` applied to the page slot host node.
252    #[new(skip)]
253    #[get(type(copy))]
254    pub(crate) style: Style,
255}
256
257/// Reactive route-driven page outlet seamlessly rendering compiled routes, hot-updated pages, and runtime-injected routes.
258#[derive(CustomDebug, Data, New)]
259pub struct DynamicPageOutlet {
260    /// Reactive route path signal controlling which page is currently active.
261    #[get(type(copy))]
262    pub(crate) route: Signal<String>,
263    /// Fallback router closure invoked when the active route has no dynamic page override.
264    #[debug(skip)]
265    #[get(skip)]
266    pub(crate) fallback: DynamicRouteFallbackFn,
267    /// Optional explicit `DynamicUpdateHub` override (defaults to the ambient `Context` hub).
268    #[new(skip)]
269    #[get(skip)]
270    pub(crate) hub: Option<DynamicUpdateHub>,
271    /// Optional container `Style` applied to the outlet host wrapper node.
272    #[new(skip)]
273    #[get(type(copy))]
274    pub(crate) style: Style,
275}