strata-ui 0.1.2

Universal frontend framework in pure Rust with pixel-perfect CSS emulation and React/TSX ergonomics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! CSS keyframe animations, transitions, view transitions, and scroll animations.

use super::*;

fn fnv1a_mix(mut hash: u64, bytes: &[u8]) -> u64 {
    for &b in bytes {
        hash ^= b as u64;
        hash = hash.wrapping_mul(0x100000001b3u64);
    }
    hash
}

fn blueprint_has_explicit_identity(bp: Option<&NodeBlueprint>) -> bool {
    let Some(NodeBlueprint::Element { attributes, .. }) = bp else {
        return false;
    };
    attributes.iter().any(|attr| {
        matches!(
            attr,
            strata_dom::Attribute::Property { key, .. }
                if key == "id" || key == "key" || key == "data-key"
        )
    })
}

fn blueprint_node_identity_hash(
    parent_hash: u64,
    child_index: usize,
    bp: Option<&NodeBlueprint>,
) -> u64 {
    let mut hash = fnv1a_mix(parent_hash, &child_index.to_le_bytes());
    match bp {
        Some(NodeBlueprint::Element {
            tag, attributes, ..
        }) => {
            hash = fnv1a_mix(hash, &[1]);
            let mut tag_hasher = std::collections::hash_map::DefaultHasher::new();
            std::hash::Hash::hash(tag, &mut tag_hasher);
            hash = fnv1a_mix(hash, &std::hash::Hasher::finish(&tag_hasher).to_le_bytes());
            for attr in attributes {
                if let strata_dom::Attribute::Property { key, value } = attr {
                    if key == "id" || key == "key" || key == "data-key" {
                        hash = fnv1a_mix(hash, key.as_bytes());
                        hash = fnv1a_mix(hash, &[0xff]);
                        hash = fnv1a_mix(hash, value.as_bytes());
                        hash = fnv1a_mix(hash, &[0xfe]);
                    }
                }
            }
        }
        Some(NodeBlueprint::Text(_)) => {
            hash = fnv1a_mix(hash, &[2]);
        }
        Some(NodeBlueprint::DynamicText(_)) => {
            hash = fnv1a_mix(hash, &[3]);
        }
        Some(NodeBlueprint::Fragment(_)) => {
            hash = fnv1a_mix(hash, &[4]);
        }
        Some(NodeBlueprint::Empty) => {
            hash = fnv1a_mix(hash, &[5]);
        }
        None => {
            hash = fnv1a_mix(hash, &[0]);
        }
    }
    hash
}

impl AppRuntime {
    /// Synchronizes running animations and transitions with the current layout tree.
    pub fn sync_tree_animations(&mut self, now_ms: f64) {
        let mut new_base_styles = std::collections::HashMap::new();
        let mut new_node_paths = std::collections::HashMap::new();
        let mut new_path_node_ids = std::collections::HashMap::new();
        let mut path_buf = Vec::new();

        let mut trans_events = Vec::new();
        let mut anim_events = Vec::new();

        Self::sync_tree_animations_rec(
            &self.layout_root,
            Some(&self.root_blueprint),
            0xcbf29ce484222325u64,
            0,
            &mut path_buf,
            now_ms,
            &self.previous_node_styles,
            &mut new_base_styles,
            &mut new_node_paths,
            &mut new_path_node_ids,
            &mut self.transition_manager,
            &mut self.animation_manager,
            &mut trans_events,
            &mut anim_events,
        );

        self.base_node_styles = new_base_styles;
        self.previous_node_styles = self.base_node_styles.clone();
        self.node_paths = new_node_paths;
        self.path_node_ids = new_path_node_ids;

        // Prune any transitions/animations belonging to unmounted or replaced elements
        // so dead nodes never leak state or keep frame ticks alive.
        self.transition_manager
            .retain_nodes(|id| self.base_node_styles.contains_key(id));
        self.animation_manager
            .retain_nodes(|id| self.base_node_styles.contains_key(id));

        // Evaluate scroll-driven animations progress
        self.evaluate_scroll_animations(&mut anim_events);

        for ev in trans_events {
            self.dispatch_transition_event(ev);
        }
        for ev in anim_events {
            self.dispatch_animation_event(ev);
        }

        self.apply_animations_to_layout_tree();
    }

    #[allow(clippy::too_many_arguments)]
    pub(crate) fn sync_tree_animations_rec(
        node: &LayoutNode,
        blueprint: Option<&NodeBlueprint>,
        parent_hash: u64,
        child_index: usize,
        path: &mut Vec<usize>,
        now_ms: f64,
        previous_styles: &std::collections::HashMap<u64, ComputedStyle>,
        new_base_styles: &mut std::collections::HashMap<u64, ComputedStyle>,
        new_node_paths: &mut std::collections::HashMap<u64, Vec<usize>>,
        new_path_node_ids: &mut std::collections::HashMap<Vec<usize>, u64>,
        transition_manager: &mut strata_style::transition::TransitionManager,
        animation_manager: &mut strata_style::animation::AnimationManager,
        trans_events: &mut Vec<strata_style::transition::TransitionLifecycleEvent>,
        anim_events: &mut Vec<strata_style::animation::AnimationLifecycleEvent>,
    ) {
        let node_id = blueprint_node_identity_hash(parent_hash, child_index, blueprint);
        new_base_styles.insert(node_id, node.style.clone());
        new_node_paths.insert(node_id, path.clone());
        new_path_node_ids.insert(path.clone(), node_id);

        // Check for transitions if we have a previous style or @starting-style
        let effective_old_style = match previous_styles.get(&node_id) {
            Some(old) if old.display != strata_style::Display::None => {
                if old.transitions.is_empty()
                    && !node.style.transitions.is_empty()
                    && !blueprint_has_explicit_identity(blueprint)
                {
                    node.style.starting_style.as_deref()
                } else {
                    Some(old)
                }
            }
            _ => node.style.starting_style.as_deref(),
        };

        if let Some(old_style) = effective_old_style {
            if !node.style.transitions.is_empty() {
                trans_events.extend(transition_manager.update_styles(
                    node_id,
                    old_style,
                    &node.style,
                    now_ms,
                ));
            }
        }

        // Check for keyframe animations
        if !node.style.animations.is_empty() || animation_manager.has_active_animations() {
            anim_events.extend(animation_manager.update_styles(node_id, &node.style, now_ms));
        }

        let bp_children = match blueprint {
            Some(NodeBlueprint::Element { children, .. }) => Some(children.as_slice()),
            Some(NodeBlueprint::Fragment(children)) => Some(children.as_slice()),
            _ => None,
        };

        for (idx, child) in node.children.iter().enumerate() {
            path.push(idx);
            let child_bp = bp_children.and_then(|c| c.get(idx));
            Self::sync_tree_animations_rec(
                child,
                child_bp,
                node_id,
                idx,
                path,
                now_ms,
                previous_styles,
                new_base_styles,
                new_node_paths,
                new_path_node_ids,
                transition_manager,
                animation_manager,
                trans_events,
                anim_events,
            );
            path.pop();
        }
    }

    /// Applies active transition and animation values directly onto the layout tree.
    pub fn apply_animations_to_layout_tree(&mut self) {
        let has_anims = self.transition_manager.has_active_transitions()
            || self.animation_manager.has_active_animations();
        if !has_anims && !self.had_active_animations {
            return;
        }
        self.had_active_animations = has_anims;

        let mut path = Vec::new();
        Self::apply_animations_rec(
            &mut self.layout_root,
            &mut path,
            &self.path_node_ids,
            &self.base_node_styles,
            &self.transition_manager,
            &self.animation_manager,
            None,
        );
    }

    pub(crate) fn apply_animations_rec(
        node: &mut LayoutNode,
        path: &mut Vec<usize>,
        path_node_ids: &std::collections::HashMap<Vec<usize>, u64>,
        base_styles: &std::collections::HashMap<u64, ComputedStyle>,
        transition_manager: &strata_style::transition::TransitionManager,
        animation_manager: &strata_style::animation::AnimationManager,
        inherited_color_override: Option<(strata_style::types::Color, strata_style::types::Color)>,
    ) {
        let node_id = path_node_ids
            .get(path)
            .copied()
            .unwrap_or_else(|| path_to_node_id(path));
        if let Some(base) = base_styles.get(&node_id) {
            node.style = base.clone();
        }
        let own_base_color = node.style.color;
        if let Some((anc_base_color, anc_anim_color)) = inherited_color_override {
            if own_base_color == anc_base_color {
                node.style.color = anc_anim_color;
            }
        }
        transition_manager.apply_to_style(node_id, &mut node.style);
        animation_manager.apply_to_style(node_id, &mut node.style);
        node.active_transitions = transition_manager.has_node_transitions(node_id);

        let next_color_override = if node.style.color != own_base_color {
            Some((own_base_color, node.style.color))
        } else {
            None
        };

        for (idx, child) in node.children.iter_mut().enumerate() {
            path.push(idx);
            Self::apply_animations_rec(
                child,
                path,
                path_node_ids,
                base_styles,
                transition_manager,
                animation_manager,
                next_color_override,
            );
            path.pop();
        }
    }

    /// Evaluates all active scroll-driven animations based on current container scroll positions.
    pub fn evaluate_scroll_animations(
        &mut self,
        anim_events: &mut Vec<strata_style::animation::AnimationLifecycleEvent>,
    ) {
        for (node_id, path) in &self.node_paths {
            if let Some(style) = self.base_node_styles.get(node_id) {
                for anim in &style.animations {
                    match &anim.timeline {
                        strata_style::AnimationTimeline::Scroll(src, _axis) => {
                            let scroll_progress = match src {
                                strata_style::ScrollTimelineSource::Root => self
                                    .active_scrollbars
                                    .iter()
                                    .max_by(|a, b| {
                                        (a.bounds.width * a.bounds.height)
                                            .partial_cmp(&(b.bounds.width * b.bounds.height))
                                            .unwrap_or(std::cmp::Ordering::Equal)
                                    })
                                    .map(|b| {
                                        if b.max_scroll_y > 0.0 {
                                            (b.scroll_y / b.max_scroll_y).clamp(0.0, 1.0)
                                        } else {
                                            0.0
                                        }
                                    })
                                    .unwrap_or(0.0),
                                strata_style::ScrollTimelineSource::Nearest
                                | strata_style::ScrollTimelineSource::SelfNode => {
                                    let mut p = 0.0;
                                    let mut check_path = path.clone();
                                    while !check_path.is_empty() {
                                        if let Some((_, sy)) = self.scroll_offsets.get(&check_path)
                                        {
                                            if let Some(bar) = self
                                                .active_scrollbars
                                                .iter()
                                                .find(|b| b.path == check_path)
                                            {
                                                if bar.max_scroll_y > 0.0 {
                                                    p = (*sy / bar.max_scroll_y).clamp(0.0, 1.0);
                                                    break;
                                                }
                                            }
                                        }
                                        check_path.pop();
                                    }
                                    p
                                }
                            };
                            anim_events.extend(self.animation_manager.step_node_scroll_progress(
                                *node_id,
                                scroll_progress,
                                style,
                            ));
                        }
                        strata_style::AnimationTimeline::View(_axis, _start, _end) => {
                            let p = 0.5;
                            anim_events.extend(
                                self.animation_manager
                                    .step_node_scroll_progress(*node_id, p, style),
                            );
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    /// Evaluates scroll-driven animations synchronously during container scroll, consuming 0% CPU when stationary.
    pub fn sync_scroll_animations(&mut self) {
        let mut anim_events = Vec::new();
        self.evaluate_scroll_animations(&mut anim_events);
        for ev in anim_events {
            self.dispatch_animation_event(ev);
        }
        self.apply_animations_to_layout_tree();
    }

    /// Captures element layout bounding boxes for elements with `view-transition-name` before DOM mutation.
    pub fn capture_view_transitions(&mut self) {
        let mut named = Vec::new();
        Self::collect_view_transition_elements(&self.layout_root, &mut named);
        self.view_transition_manager.capture_old_state(&named);
    }

    /// Triggers a view transition after DOM mutation, pairing old bounds with new layout bounds.
    pub fn start_view_transition(&mut self, duration_ms: f32) {
        let mut named = Vec::new();
        Self::collect_view_transition_elements(&self.layout_root, &mut named);
        let now_ms = self.runtime_start_epoch.elapsed().as_secs_f64() * 1000.0;
        self.view_transition_manager
            .start_transition(&named, now_ms, duration_ms);
    }

    pub(crate) fn collect_view_transition_elements(
        node: &LayoutNode,
        out: &mut Vec<(String, strata_primitives::geom::Rect)>,
    ) {
        if let Some(name) = &node.style.view_transition_name {
            if name != "none" {
                out.push((name.clone(), node.geometry.border_box.into()));
            }
        }
        for child in &node.children {
            Self::collect_view_transition_elements(child, out);
        }
    }

    /// Returns true if any CSS property transitions or view transitions are currently active.
    pub fn has_active_transitions(&self) -> bool {
        self.transition_manager.has_active_transitions()
            || self.view_transition_manager.has_active_transitions()
            || self.layout_root.has_active_transitions()
    }

    /// Returns true if there are any active transitions or keyframe animations running.
    pub fn has_active_animations(&self) -> bool {
        self.transition_manager.has_active_transitions()
            || self.animation_manager.has_time_based_animations()
            || self.view_transition_manager.has_active_transitions()
    }

    /// Advances active animations and transitions by current clock time.
    /// Returns true if any animation stepped and a redraw is needed.
    pub fn poll_animations(&mut self) -> bool {
        if !self.has_active_animations() {
            return false;
        }

        // Capture this before advancing: a transition or animation that finishes on this
        // tick is removed/settled by `advance`, but the final layout value it produced still
        // has to be laid out once.
        let layout_affecting = self.transition_manager.has_layout_affecting_transitions()
            || self.animation_manager.has_layout_affecting_animations();

        let now_ms = self.runtime_start_epoch.elapsed().as_secs_f64() * 1000.0;
        let trans_events = self.transition_manager.advance(now_ms);
        let anim_events = self
            .animation_manager
            .advance(now_ms, &self.base_node_styles);
        let _vt_active = self.view_transition_manager.step(now_ms);

        for ev in trans_events {
            self.dispatch_transition_event(ev);
        }
        for ev in anim_events {
            self.dispatch_animation_event(ev);
        }

        self.apply_animations_to_layout_tree();

        // Paint-only animations (colour, opacity, transform, shadow, ...) write new style
        // values onto the existing layout tree. Only properties that feed box layout require
        // a fresh layout pass; everything else just needs the display list recompiled.
        if layout_affecting {
            self.tick_frame();
        } else {
            strata_layout::box_model::refresh_inline_fragment_styles(&mut self.layout_root);
            self.compile_display_list();
        }

        true
    }

    /// Dispatches a synthetic W3C animation lifecycle event to matching RSX element event listeners.
    pub fn dispatch_animation_event(
        &self,
        event: strata_style::animation::AnimationLifecycleEvent,
    ) {
        let (node_id, name, elapsed, event_type) = match event {
            strata_style::animation::AnimationLifecycleEvent::Start {
                node_id,
                name,
                elapsed_time_sec,
            } => (
                node_id,
                name,
                elapsed_time_sec,
                strata_dom::EventType::AnimationStart,
            ),
            strata_style::animation::AnimationLifecycleEvent::Iteration {
                node_id,
                name,
                elapsed_time_sec,
            } => (
                node_id,
                name,
                elapsed_time_sec,
                strata_dom::EventType::AnimationIteration,
            ),
            strata_style::animation::AnimationLifecycleEvent::End {
                node_id,
                name,
                elapsed_time_sec,
            } => (
                node_id,
                name,
                elapsed_time_sec,
                strata_dom::EventType::AnimationEnd,
            ),
            strata_style::animation::AnimationLifecycleEvent::Cancel {
                node_id,
                name,
                elapsed_time_sec,
            } => (
                node_id,
                name,
                elapsed_time_sec,
                strata_dom::EventType::AnimationCancel,
            ),
        };

        if let Some(path) = self.node_paths.get(&node_id) {
            if let Some(bp) = Self::find_blueprint_at_path(&self.root_blueprint, path) {
                if let NodeBlueprint::Element { attributes, .. } = bp {
                    for attr in attributes {
                        if let strata_dom::Attribute::EventListener {
                            event: ev_type,
                            handler,
                        } = attr
                        {
                            if *ev_type == event_type {
                                let ev =
                                    strata_dom::Event::animation(event_type, name.clone(), elapsed);
                                handler(ev);
                            }
                        }
                    }
                }
            }
        }
    }

    /// Dispatches a synthetic W3C transition lifecycle event to matching RSX element event listeners.
    pub fn dispatch_transition_event(
        &self,
        event: strata_style::transition::TransitionLifecycleEvent,
    ) {
        let (node_id, name, elapsed, event_type) = match event {
            strata_style::transition::TransitionLifecycleEvent::Run {
                node_id,
                property,
                elapsed_time_sec,
            } => (
                node_id,
                property,
                elapsed_time_sec,
                strata_dom::EventType::TransitionRun,
            ),
            strata_style::transition::TransitionLifecycleEvent::Start {
                node_id,
                property,
                elapsed_time_sec,
            } => (
                node_id,
                property,
                elapsed_time_sec,
                strata_dom::EventType::TransitionStart,
            ),
            strata_style::transition::TransitionLifecycleEvent::End {
                node_id,
                property,
                elapsed_time_sec,
            } => (
                node_id,
                property,
                elapsed_time_sec,
                strata_dom::EventType::TransitionEnd,
            ),
            strata_style::transition::TransitionLifecycleEvent::Cancel {
                node_id,
                property,
                elapsed_time_sec,
            } => (
                node_id,
                property,
                elapsed_time_sec,
                strata_dom::EventType::TransitionCancel,
            ),
        };

        if let Some(path) = self.node_paths.get(&node_id) {
            if let Some(bp) = Self::find_blueprint_at_path(&self.root_blueprint, path) {
                if let NodeBlueprint::Element { attributes, .. } = bp {
                    for attr in attributes {
                        if let strata_dom::Attribute::EventListener {
                            event: ev_type,
                            handler,
                        } = attr
                        {
                            if *ev_type == event_type {
                                let ev = strata_dom::Event::transition(
                                    event_type,
                                    name.clone(),
                                    elapsed,
                                );
                                handler(ev);
                            }
                        }
                    }
                }
            }
        }
    }

    /// Registers all parsed `@keyframes` rules from a stylesheet into the runtime animation registry.
    pub fn register_keyframes_stylesheet(&mut self, sheet: &strata_style::StyleSheet) {
        self.animation_manager
            .registry_mut()
            .register_stylesheet(sheet);
    }

    /// Registers a single compiled `@keyframes` rule into the runtime animation registry.
    pub fn register_keyframe_rule(&mut self, rule: &strata_style::KeyframeRule) {
        self.animation_manager.registry_mut().register_rule(rule);
    }
}