strata-ui 0.1.7

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
592
593
594
595
596
597
598
599
//! Runtime data structures for active focus, scrollbars, hit targets, and editing.

/// Hit-test target result representing a clickable text input element in the blueprint tree.
#[derive(Clone)]
pub struct InputHitTarget {
    pub input_index: usize,
    pub input_id: Option<String>,
    pub entity_id: Option<strata_primitives::EntityId>,
    pub is_multiline: bool,
    pub current_value: String,
    pub font_size: f32,
    pub line_height: f32,
    pub is_mono: bool,
    pub font_family: strata_primitives::AtomId,
    pub content_box_x: f32,
    pub content_box_y: f32,
    pub content_box_width: f32,
    pub content_box_height: f32,
    pub on_input: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_change: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_submit: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_focus: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_blur: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
}

/// Active text input state tracked by the runtime while an input has keyboard focus.
#[derive(Clone)]
pub struct ActiveInput {
    pub input_index: usize,
    pub input_id: Option<String>,
    pub entity_id: Option<strata_primitives::EntityId>,
    pub is_multiline: bool,
    pub state: strata_dom::text_input::TextInputState,
    pub on_input: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_change: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_submit: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
    pub on_blur: Option<std::rc::Rc<dyn Fn(strata_dom::Event)>>,
}

impl std::fmt::Debug for ActiveInput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ActiveInput")
            .field("text", &self.state.text.get())
            .field("cursor", &self.state.cursor_index.get())
            .finish()
    }
}

/// Active scrollbar state for an overflow container.
#[derive(Debug, Clone)]
pub struct ActiveScrollbar {
    pub bounds: strata_layout::Rect,
    pub clip_bounds: strata_layout::Rect,
    pub border_radius: strata_style::types::BorderRadius,
    pub is_top_layer: bool,
    pub track_bounds: strata_layout::Rect,
    pub thumb_bounds: strata_layout::Rect,
    pub scroll_y: f32,
    pub max_scroll_y: f32,
    pub content_h: f32,
    pub is_id: bool,
    pub id: String,
    pub path: Vec<usize>,
    pub available_travel: f32,
}

/// Active drag operation on a scrollbar thumb.
#[derive(Debug, Clone)]
pub struct ScrollbarDrag {
    pub is_id: bool,
    pub id: String,
    pub path: Vec<usize>,
    pub start_pointer_y: f32,
    pub start_scroll_y: f32,
    pub available_travel: f32,
    pub max_scroll_y: f32,
}

/// Active kinetic smooth scroll animation tracking state.
#[derive(Debug, Clone)]
pub struct SmoothScrollTracker {
    pub container_path: Vec<usize>,
    pub container_id: String,
    pub is_id: bool,
    pub max_scroll_y: f32,
    pub available_travel: f32,
    pub bounds_y: f32,
    /// Current interpolated scroll offset in pixels.
    pub current_y: f32,
    /// Target scroll position in pixels.
    pub target_y: f32,
    /// Initial position at the start of the current impulse.
    pub start_y: f32,
    /// Initial velocity in px/s at the start of the current impulse.
    pub initial_velocity: f32,
    /// Timestamp when the current impulse started.
    pub start_time: std::time::Instant,
    /// Timestamp of the last incoming wheel event (for flick velocity calculation).
    pub last_event_time: std::time::Instant,
    /// Critically damped angular frequency (rad/s).
    pub omega: f32,
}

/// Internal descriptor of a scrollable target container under the cursor.
#[derive(Debug, Clone)]
pub struct ScrollTarget {
    pub path: Vec<usize>,
    pub id: Option<String>,
    pub bounds: strata_layout::Rect,
    pub max_scroll_x: f32,
    pub max_scroll_y: f32,
}
/// Computes a deterministic 64-bit ID from a hierarchical tree path.
pub fn path_to_node_id(path: &[usize]) -> u64 {
    let mut hash = 0xcbf29ce484222325u64;
    for &idx in path {
        hash ^= idx as u64;
        hash = hash.wrapping_mul(0x100000001b3u64);
    }
    hash
}

/// Editing commands that can be applied to the focused text input from any UI surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditCommand {
    Undo,
    Redo,
    Cut,
    Copy,
    Paste,
    SelectAll,
}

/// Action result from a pointer-down event dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointerDownAction {
    /// Not handled by any element or active input.
    Ignored,
    /// Caret or selection inside the already-active focused input moved. Requires redraw only.
    CaretMoved,
    /// Focus transitioned to a new or different input. Requires layout/rebuild update.
    FocusChanged,
    /// DOM PointerDown or MouseDown event listener was invoked.
    DomListenerInvoked,
}

impl PointerDownAction {
    #[inline]
    pub fn is_handled(self) -> bool {
        self != PointerDownAction::Ignored
    }

    #[inline]
    pub fn needs_rebuild(self) -> bool {
        matches!(
            self,
            PointerDownAction::FocusChanged | PointerDownAction::DomListenerInvoked
        )
    }

    #[inline]
    pub fn needs_redraw(self) -> bool {
        self != PointerDownAction::Ignored
    }
}

/// Action result from a pointer-move event dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointerMoveAction {
    /// Not handled.
    Ignored,
    /// Mouse drag text selection range updated in active input. Requires redraw only.
    SelectionDragged,
    /// DOM PointerMove or MouseMove event listener was invoked.
    DomListenerInvoked,
}

impl PointerMoveAction {
    #[inline]
    pub fn is_handled(self) -> bool {
        self != PointerMoveAction::Ignored
    }

    #[inline]
    pub fn needs_rebuild(self) -> bool {
        self == PointerMoveAction::DomListenerInvoked
    }

    #[inline]
    pub fn needs_redraw(self) -> bool {
        self != PointerMoveAction::Ignored
    }
}

/// Action result from a pointer-up event dispatch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointerUpAction {
    /// Not handled.
    Ignored,
    /// Mouse drag text selection finished. Requires redraw only.
    SelectionEnded,
    /// DOM PointerUp or MouseUp event listener was invoked.
    DomListenerInvoked,
}

impl PointerUpAction {
    #[inline]
    pub fn is_handled(self) -> bool {
        self != PointerUpAction::Ignored
    }

    #[inline]
    pub fn needs_rebuild(self) -> bool {
        self == PointerUpAction::DomListenerInvoked
    }

    #[inline]
    pub fn needs_redraw(self) -> bool {
        self != PointerUpAction::Ignored
    }
}

/// Outcome of dispatching a platform keyboard or text event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyDispatchResult {
    /// Key was not handled by any focused input or container scroll.
    Ignored,
    /// Caret moved, selection range updated, or page scrolled without mutating component state. Requires redraw only.
    RedrawOnly,
    /// Text buffer was modified or form submitted/blurred, requiring layout/signal recalculation.
    NeedsRebuild,
}

impl KeyDispatchResult {
    #[inline]
    pub fn is_handled(self) -> bool {
        self != KeyDispatchResult::Ignored
    }

    #[inline]
    pub fn needs_rebuild(self) -> bool {
        self == KeyDispatchResult::NeedsRebuild
    }

    #[inline]
    pub fn needs_redraw(self) -> bool {
        self != KeyDispatchResult::Ignored
    }
}

/// Binding between a dynamic text closure and its target location in the layout hierarchy.
#[derive(Clone)]
pub struct DynamicTextBinding {
    pub path: Vec<usize>,
    pub closure: std::rc::Rc<dyn Fn() -> String>,
    pub last_value: String,
}

impl std::fmt::Debug for DynamicTextBinding {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DynamicTextBinding")
            .field("path", &self.path)
            .field("last_value", &self.last_value)
            .finish()
    }
}

/// Interaction context describing active pointer, keyboard focus, and theme interaction state.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct InteractionContext<'a> {
    pub hovered_path: Option<&'a [usize]>,
    pub focused_path: Option<&'a [usize]>,
    pub is_mouse_down: bool,
    pub is_dark_mode: bool,
}

/// Element selector metadata used when matching `<style>` rules across ancestors and siblings.
#[derive(Debug, Clone)]
pub(crate) struct CascadeElementMeta {
    pub tag: &'static str,
    pub classes: Vec<String>,
    pub id: Option<String>,
    pub attributes: Vec<(String, String)>,
    pub pseudo_state: strata_style::selector::PseudoState,
}

/// Active DOM drag-and-drop interaction state (`ondragstart`, `ondrag`, `ondragenter`, `ondragover`, `ondragleave`, `ondrop`, `ondragend`).
#[derive(Debug, Clone)]
pub struct ActiveDomDrag {
    pub source_path: Vec<usize>,
    pub start_pos: (f32, f32),
    pub started: bool,
    pub last_over_path: Option<Vec<usize>>,
    pub data_transfer: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<String, String>>>,
}

/// Precomputed stylesheet selector dependency index using 64-bit Bloom filters and
/// exact tag/class/id sets so static `<style>` sheets do not disable incremental
/// layout reconciliation or fast paint-only hover restyling across unrelated subtrees.
#[derive(Debug, Clone, Default)]
pub(crate) struct StylesheetSelectorIndex {
    pub hover_bloom: u64,
    pub focus_bloom: u64,
    pub hover_tags: std::collections::HashSet<String>,
    pub hover_classes: std::collections::HashSet<String>,
    pub hover_ids: std::collections::HashSet<String>,
    pub focus_tags: std::collections::HashSet<String>,
    pub focus_classes: std::collections::HashSet<String>,
    pub focus_ids: std::collections::HashSet<String>,
    pub has_any_hover_rule: bool,
    pub has_any_focus_rule: bool,
    pub has_unbounded_hover_rule: bool,
    pub has_unbounded_focus_rule: bool,
    pub has_sibling_hover_rule: bool,
    pub has_sibling_combinator_or_positional_rules: bool,
    pub has_descendant_combinator_rules: bool,
}

impl StylesheetSelectorIndex {
    #[inline]
    fn bloom_bits(s: &str) -> u64 {
        let mut h1: u64 = 0xcbf29ce484222325;
        let mut h2: u64 = 0x84222325cbf29ce4;
        for b in s.bytes() {
            let lb = b.to_ascii_lowercase() as u64;
            h1 ^= lb;
            h1 = h1.wrapping_mul(0x100000001b3);
            h2 = h2.wrapping_mul(0x9e3779b97f4a7c15) ^ lb;
        }
        (1u64 << (h1 & 63)) | (1u64 << ((h2 >> 16) & 63))
    }

    fn compound_has_pseudo(comp: &strata_style::selector::CompoundSelector, target: &str) -> bool {
        comp.parts.iter().any(|p| match p {
            strata_style::selector::SelectorPart::PseudoClass(name) => {
                let n = name.trim().to_ascii_lowercase();
                n == target || n.contains(&format!(":{target}"))
            }
            _ => false,
        })
    }

    fn compound_has_positional_pseudo(comp: &strata_style::selector::CompoundSelector) -> bool {
        comp.parts.iter().any(|p| match p {
            strata_style::selector::SelectorPart::PseudoClass(name) => {
                let n = name.trim().to_ascii_lowercase();
                n.starts_with("nth-")
                    || n == "first-child"
                    || n == "last-child"
                    || n == "only-child"
                    || n == "first-of-type"
                    || n == "last-of-type"
                    || n == "only-of-type"
                    || n == "empty"
            }
            _ => false,
        })
    }

    fn index_compound_keys(
        comp: &strata_style::selector::CompoundSelector,
        bloom: &mut u64,
        tags: &mut std::collections::HashSet<String>,
        classes: &mut std::collections::HashSet<String>,
        ids: &mut std::collections::HashSet<String>,
        unbounded: &mut bool,
    ) {
        let mut has_key = false;
        for part in &comp.parts {
            match part {
                strata_style::selector::SelectorPart::Tag(t) => {
                    let k = t.to_ascii_lowercase();
                    *bloom |= Self::bloom_bits(&k);
                    tags.insert(k);
                    has_key = true;
                }
                strata_style::selector::SelectorPart::Class(c) => {
                    let k = c.to_ascii_lowercase();
                    *bloom |= Self::bloom_bits(&k);
                    classes.insert(k);
                    has_key = true;
                }
                strata_style::selector::SelectorPart::Id(id) => {
                    let k = id.to_ascii_lowercase();
                    *bloom |= Self::bloom_bits(&k);
                    ids.insert(k);
                    has_key = true;
                }
                _ => {}
            }
        }
        if !has_key {
            *unbounded = true;
        }
    }

    pub fn from_stylesheets(sheets: &[std::sync::Arc<strata_style::StyleSheet>]) -> Self {
        let mut idx = Self::default();
        for sheet in sheets {
            for rule in &sheet.rules {
                for sel in &rule.selectors {
                    if !sel.steps.is_empty() {
                        idx.has_descendant_combinator_rules = true;
                    }
                    let has_sibling_comb = sel.steps.iter().any(|(comb, _)| {
                        matches!(
                            comb,
                            strata_style::selector::Combinator::AdjacentSibling
                                | strata_style::selector::Combinator::SubsequentSibling
                        )
                    });
                    let has_positional = Self::compound_has_positional_pseudo(&sel.target)
                        || sel
                            .steps
                            .iter()
                            .any(|(_, c)| Self::compound_has_positional_pseudo(c));
                    if has_sibling_comb || has_positional {
                        idx.has_sibling_combinator_or_positional_rules = true;
                    }

                    if sel.has_any_hover_pseudo() {
                        idx.has_any_hover_rule = true;
                        if has_sibling_comb {
                            idx.has_sibling_hover_rule = true;
                        }
                        if Self::compound_has_pseudo(&sel.target, "hover") {
                            Self::index_compound_keys(
                                &sel.target,
                                &mut idx.hover_bloom,
                                &mut idx.hover_tags,
                                &mut idx.hover_classes,
                                &mut idx.hover_ids,
                                &mut idx.has_unbounded_hover_rule,
                            );
                        }
                        for (_, step_comp) in &sel.steps {
                            if Self::compound_has_pseudo(step_comp, "hover") {
                                Self::index_compound_keys(
                                    step_comp,
                                    &mut idx.hover_bloom,
                                    &mut idx.hover_tags,
                                    &mut idx.hover_classes,
                                    &mut idx.hover_ids,
                                    &mut idx.has_unbounded_hover_rule,
                                );
                            }
                        }
                    }

                    let target_focus = Self::compound_has_pseudo(&sel.target, "focus")
                        || Self::compound_has_pseudo(&sel.target, "focus-within")
                        || Self::compound_has_pseudo(&sel.target, "focus-visible");
                    let step_focus = sel.steps.iter().any(|(_, c)| {
                        Self::compound_has_pseudo(c, "focus")
                            || Self::compound_has_pseudo(c, "focus-within")
                            || Self::compound_has_pseudo(c, "focus-visible")
                    });
                    if target_focus || step_focus {
                        idx.has_any_focus_rule = true;
                        if target_focus {
                            Self::index_compound_keys(
                                &sel.target,
                                &mut idx.focus_bloom,
                                &mut idx.focus_tags,
                                &mut idx.focus_classes,
                                &mut idx.focus_ids,
                                &mut idx.has_unbounded_focus_rule,
                            );
                        }
                        for (_, step_comp) in &sel.steps {
                            if Self::compound_has_pseudo(step_comp, "focus")
                                || Self::compound_has_pseudo(step_comp, "focus-within")
                                || Self::compound_has_pseudo(step_comp, "focus-visible")
                            {
                                Self::index_compound_keys(
                                    step_comp,
                                    &mut idx.focus_bloom,
                                    &mut idx.focus_tags,
                                    &mut idx.focus_classes,
                                    &mut idx.focus_ids,
                                    &mut idx.has_unbounded_focus_rule,
                                );
                            }
                        }
                    }
                }
            }
        }
        idx
    }

    #[inline]
    pub fn node_may_match_hover(&self, tag: &str, id: Option<&str>, classes: &[String]) -> bool {
        if !self.has_any_hover_rule {
            return false;
        }
        if self.has_unbounded_hover_rule {
            return true;
        }
        let tb = Self::bloom_bits(tag);
        if (self.hover_bloom & tb) == tb && self.hover_tags.contains(&tag.to_ascii_lowercase()) {
            return true;
        }
        if let Some(id_str) = id {
            let ib = Self::bloom_bits(id_str);
            if (self.hover_bloom & ib) == ib && self.hover_ids.contains(&id_str.to_ascii_lowercase())
            {
                return true;
            }
        }
        for cls in classes {
            let cb = Self::bloom_bits(cls);
            if (self.hover_bloom & cb) == cb
                && self.hover_classes.contains(&cls.to_ascii_lowercase())
            {
                return true;
            }
        }
        false
    }

    #[inline]
    #[allow(dead_code)]
    pub fn node_may_match_focus(&self, tag: &str, id: Option<&str>, classes: &[String]) -> bool {
        if !self.has_any_focus_rule {
            return false;
        }
        if self.has_unbounded_focus_rule {
            return true;
        }
        let tb = Self::bloom_bits(tag);
        if (self.focus_bloom & tb) == tb && self.focus_tags.contains(&tag.to_ascii_lowercase()) {
            return true;
        }
        if let Some(id_str) = id {
            let ib = Self::bloom_bits(id_str);
            if (self.focus_bloom & ib) == ib && self.focus_ids.contains(&id_str.to_ascii_lowercase())
            {
                return true;
            }
        }
        for cls in classes {
            let cb = Self::bloom_bits(cls);
            if (self.focus_bloom & cb) == cb
                && self.focus_classes.contains(&cls.to_ascii_lowercase())
            {
                return true;
            }
        }
        false
    }
}

/// Per-update reconciliation statistics exposed for runtime profiling and observability.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ReconcileStats {
    pub reused_subtrees: u32,
    pub rebuilt_nodes: u32,
}

/// Structured attribution explaining why a frame triggered work (`Dirty`, `RepaintCanvas3D`, or `Clean`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RebuildReason {
    Clean,
    InitialMount,
    SignalWrite(String),
    PlatformEvent(String),
    HoverFastPath,
    HoverFastPathFailed(String),
    LayoutReconcileBailout(String),
    RepaintCanvas3D,
    AnimationTick,
    JsTimerOrMutation,
    FrameHookDirty,
    WindowResize,
    McpCommand(String),
    HmrPatch(u64),
}

impl std::fmt::Display for RebuildReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Clean => write!(f, "Clean"),
            Self::InitialMount => write!(f, "InitialMount"),
            Self::SignalWrite(sig) => write!(f, "SignalWrite({sig})"),
            Self::PlatformEvent(ev) => write!(f, "PlatformEvent({ev})"),
            Self::HoverFastPath => write!(f, "HoverFastPath"),
            Self::HoverFastPathFailed(r) => write!(f, "HoverFastPathFailed({r})"),
            Self::LayoutReconcileBailout(r) => write!(f, "LayoutReconcileBailout({r})"),
            Self::RepaintCanvas3D => write!(f, "RepaintCanvas3D"),
            Self::AnimationTick => write!(f, "AnimationTick"),
            Self::JsTimerOrMutation => write!(f, "JsTimerOrMutation"),
            Self::FrameHookDirty => write!(f, "FrameHookDirty"),
            Self::WindowResize => write!(f, "WindowResize"),
            Self::McpCommand(cmd) => write!(f, "McpCommand({cmd})"),
            Self::HmrPatch(epoch) => write!(f, "HmrPatch(epoch={epoch})"),
        }
    }
}