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
//! Model Context Protocol (MCP) cursor control, keyboard typing, and element inspection.

use super::*;

impl AppRuntime {
    /// Ensures that an emulated MCP cursor state is active, initializing it at center viewport if needed.
    pub fn ensure_mcp_cursor(&mut self) -> &mut strata_platform::McpCursorState {
        if self.mcp_cursor.is_none() {
            self.mcp_cursor = Some(strata_platform::McpCursorState::new(
                self.viewport_width / 2.0,
                self.viewport_height / 2.0,
            ));
        }
        self.mcp_cursor.as_mut().unwrap()
    }

    /// Emulates AI pointer movement to `(x, y)` and returns `(needs_rebuild, needs_redraw)`.
    ///
    /// Paint-only hover transitions and cursor-only motion return `(false, true)` so automation
    /// benchmarks exercise the real fast hover path instead of forcing full DOM rebuilds.
    pub fn mcp_move_pointer_outcome(&mut self, x: f32, y: f32, smooth: bool) -> (bool, bool) {
        let cursor_icon = Self::hit_test_cursor(&self.root_blueprint, &self.layout_root, x, y);
        let cursor = self.ensure_mcp_cursor();
        let cursor_moved = (cursor.target_x - x).abs() > 0.01
            || (cursor.target_y - y).abs() > 0.01
            || (cursor.current_x - x).abs() > 0.01
            || (cursor.current_y - y).abs() > 0.01
            || cursor.cursor_icon != cursor_icon;
        cursor.move_to(x, y, smooth);
        cursor.cursor_icon = cursor_icon;

        self.last_mouse_pos = (x, y);
        let mut needs_rebuild = false;
        let mut needs_redraw = cursor_moved;

        let signals_before = strata_dom::reactive::has_dirty_signals();
        if self.dispatch_pointer_move(x, y) {
            let retained_only = !signals_before
                && !strata_dom::reactive::has_dirty_signals()
                && self.target_is_retained_canvas3d(x, y);
            if !retained_only {
                needs_rebuild = true;
            }
        }
        if self.update_hover(x, y) {
            if self.hover_is_paint_only() {
                needs_redraw = true;
            } else {
                needs_rebuild = true;
            }
        }
        if needs_redraw {
            self.mark_paint_dirty();
        }

        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI pointer movement to `(x, y)`. Smooth interpolation is enabled by default.
    ///
    /// Automatically resolves and applies the element's CSS `cursor` style (e.g. pointer, text).
    /// Returns true only when a view rebuild or display list redraw is genuinely required.
    pub fn mcp_move_pointer(&mut self, x: f32, y: f32, smooth: bool) -> bool {
        let (rebuild, redraw) = self.mcp_move_pointer_outcome(x, y, smooth);
        rebuild || redraw
    }

    /// Emulates AI mouse button down event, returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_mouse_down_outcome(
        &mut self,
        button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
    ) -> (bool, bool) {
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        let (px, py) = match (x, y) {
            (Some(px), Some(py)) => {
                let (rb, rd) = self.mcp_move_pointer_outcome(px, py, false);
                needs_rebuild |= rb;
                needs_redraw |= rd;
                (px, py)
            }
            _ => {
                let c = self.ensure_mcp_cursor();
                (c.current_x, c.current_y)
            }
        };

        let cursor = self.ensure_mcp_cursor();
        let down_changed = !cursor.is_down;
        cursor.set_down(true);
        if down_changed {
            needs_redraw = true;
        }

        let btn_idx = button.to_button_index();
        let signals_before = strata_dom::reactive::has_dirty_signals();

        if button == strata_platform::MouseButton::Left {
            if self.check_scrollbar_thumb_press(px, py) {
                self.mark_paint_dirty();
                return (needs_rebuild, true);
            }
            self.begin_window_drag_candidate(px, py);
            if self.dispatch_pointer_down_with_modifiers(
                px,
                py,
                strata_platform::Modifiers::default(),
            ) {
                let retained_only = !signals_before
                    && !strata_dom::reactive::has_dirty_signals()
                    && self.target_is_retained_canvas3d(px, py);
                if !retained_only {
                    needs_rebuild = true;
                }
            }
            if self.dispatch_mouse_click(px, py, btn_idx, 1) {
                needs_rebuild = true;
            }
            if self.drain_edit_commands() {
                needs_rebuild = true;
            }
        } else if button == strata_platform::MouseButton::Middle {
            if self.dispatch_pointer_down(px, py) {
                let retained_only = !signals_before
                    && !strata_dom::reactive::has_dirty_signals()
                    && self.target_is_retained_canvas3d(px, py);
                if !retained_only {
                    needs_rebuild = true;
                }
            }
            if self.dispatch_mouse_click(px, py, btn_idx, 1) {
                needs_rebuild = true;
            }
        } else if button == strata_platform::MouseButton::Right {
            if self.dispatch_context_menu(px, py) {
                needs_rebuild = true;
            }
            if self.drain_edit_commands() {
                needs_rebuild = true;
            }
        }

        if needs_redraw {
            self.mark_paint_dirty();
        }
        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI mouse button down event.
    pub fn mcp_mouse_down(
        &mut self,
        button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
    ) -> bool {
        let (rebuild, redraw) = self.mcp_mouse_down_outcome(button, x, y);
        rebuild || redraw
    }

    /// Emulates AI mouse button release up event, returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_mouse_up_outcome(
        &mut self,
        _button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
    ) -> (bool, bool) {
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        let (px, py) = match (x, y) {
            (Some(px), Some(py)) => {
                let (rb, rd) = self.mcp_move_pointer_outcome(px, py, false);
                needs_rebuild |= rb;
                needs_redraw |= rd;
                (px, py)
            }
            _ => {
                let c = self.ensure_mcp_cursor();
                (c.current_x, c.current_y)
            }
        };

        let cursor = self.ensure_mcp_cursor();
        if cursor.is_down {
            needs_redraw = true;
        }
        cursor.set_down(false);

        if self.scrollbar_drag.is_some() {
            self.scrollbar_drag = None;
            needs_redraw = true;
        }
        self.pending_window_drag = None;
        let signals_before = strata_dom::reactive::has_dirty_signals();
        if self.dispatch_pointer_up(px, py) {
            let retained_only = !signals_before
                && !strata_dom::reactive::has_dirty_signals()
                && self.target_is_retained_canvas3d(px, py);
            if !retained_only {
                needs_rebuild = true;
            }
        }

        if needs_redraw {
            self.mark_paint_dirty();
        }
        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI mouse button release up event.
    pub fn mcp_mouse_up(
        &mut self,
        button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
    ) -> bool {
        let (rebuild, redraw) = self.mcp_mouse_up_outcome(button, x, y);
        rebuild || redraw
    }

    /// Emulates AI mouse click (press down and release), returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_mouse_click_outcome(
        &mut self,
        button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
        count: usize,
    ) -> (bool, bool) {
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        let (px, py) = match (x, y) {
            (Some(px), Some(py)) => {
                let (rb, rd) = self.mcp_move_pointer_outcome(px, py, false);
                needs_rebuild |= rb;
                needs_redraw |= rd;
                (px, py)
            }
            _ => {
                let c = self.ensure_mcp_cursor();
                (c.current_x, c.current_y)
            }
        };

        for _ in 0..count.max(1) {
            let (d_rb, d_rd) = self.mcp_mouse_down_outcome(button, Some(px), Some(py));
            let (u_rb, u_rd) = self.mcp_mouse_up_outcome(button, Some(px), Some(py));
            needs_rebuild |= d_rb || u_rb;
            needs_redraw |= d_rd || u_rd;
        }

        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI mouse click (press down and release).
    pub fn mcp_mouse_click(
        &mut self,
        button: strata_platform::MouseButton,
        x: Option<f32>,
        y: Option<f32>,
        count: usize,
    ) -> bool {
        let (rebuild, redraw) = self.mcp_mouse_click_outcome(button, x, y, count);
        rebuild || redraw
    }

    /// Emulates AI mouse wheel scroll event, returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_mouse_scroll_outcome(
        &mut self,
        delta_x: f32,
        delta_y: f32,
        x: Option<f32>,
        y: Option<f32>,
    ) -> (bool, bool) {
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        let (px, py) = match (x, y) {
            (Some(px), Some(py)) => {
                let (rb, rd) = self.mcp_move_pointer_outcome(px, py, false);
                needs_rebuild |= rb;
                needs_redraw |= rd;
                (px, py)
            }
            _ => {
                let c = self.ensure_mcp_cursor();
                (c.current_x, c.current_y)
            }
        };

        self.ensure_mcp_cursor().touch_activity();
        let signals_before = strata_dom::reactive::has_dirty_signals();
        let (wheel_handled, scroll_changed) =
            self.dispatch_smooth_scroll_action(px, py, delta_x, delta_y);
        if wheel_handled {
            let retained_only = !signals_before
                && !strata_dom::reactive::has_dirty_signals()
                && self.target_is_retained_canvas3d(px, py);
            if !retained_only {
                needs_rebuild = true;
            }
        }
        if scroll_changed {
            needs_redraw = true;
            self.mark_paint_dirty();
        }
        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI mouse wheel scroll event.
    pub fn mcp_mouse_scroll(
        &mut self,
        delta_x: f32,
        delta_y: f32,
        x: Option<f32>,
        y: Option<f32>,
    ) -> bool {
        let (rebuild, redraw) = self.mcp_mouse_scroll_outcome(delta_x, delta_y, x, y);
        rebuild || redraw
    }

    /// Emulates AI keyboard character input, returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_keyboard_type_outcome(&mut self, text: &str) -> (bool, bool) {
        self.ensure_mcp_cursor().touch_activity();
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        for ch in text.chars() {
            let ev = strata_platform::PlatformEvent::ReceivedCharacter(ch);
            let res = self.dispatch_key_event(&ev);
            if res.needs_rebuild() {
                needs_rebuild = true;
            } else if res.needs_redraw() {
                needs_redraw = true;
            }
        }
        if needs_redraw {
            self.mark_paint_dirty();
        }
        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI keyboard character input.
    pub fn mcp_keyboard_type(&mut self, text: &str) -> bool {
        let (rebuild, redraw) = self.mcp_keyboard_type_outcome(text);
        rebuild || redraw
    }

    /// Emulates AI key combination or non-character navigation key, returning `(needs_rebuild, needs_redraw)`.
    pub fn mcp_keyboard_key_outcome(
        &mut self,
        key: strata_platform::KeyCode,
        modifiers: strata_platform::Modifiers,
    ) -> (bool, bool) {
        self.ensure_mcp_cursor().touch_activity();
        let ev_down = strata_platform::PlatformEvent::KeyDown { key, modifiers };
        let res_down = self.dispatch_key_event(&ev_down);
        let ev_up = strata_platform::PlatformEvent::KeyUp { key, modifiers };
        let res_up = self.dispatch_key_event(&ev_up);
        let needs_rebuild = res_down.needs_rebuild() || res_up.needs_rebuild();
        let needs_redraw = res_down.needs_redraw() || res_up.needs_redraw();
        if needs_redraw {
            self.mark_paint_dirty();
        }
        (needs_rebuild, needs_redraw)
    }

    /// Emulates AI key combination or non-character navigation key.
    pub fn mcp_keyboard_key(
        &mut self,
        key: strata_platform::KeyCode,
        modifiers: strata_platform::Modifiers,
    ) -> bool {
        let (rebuild, redraw) = self.mcp_keyboard_key_outcome(key, modifiers);
        rebuild || redraw
    }

    fn mcp_input_text(
        &self,
        bp: &NodeBlueprint,
        attributes: &[strata_dom::Attribute],
        layout: &LayoutNode,
    ) -> String {
        let is_password = Self::is_password_input_attrs(attributes);
        let mask_if_pw = |s: String| {
            if is_password {
                Self::mask_password_text(&s)
            } else {
                s
            }
        };
        let input_id = attributes.iter().find_map(|attr| match attr {
            strata_dom::Attribute::Property { key, value } if key == "id" && !value.is_empty() => {
                Some(value.as_str())
            }
            _ => None,
        });

        if let Some(ref active) = self.active_focus {
            let matches_active = Self::find_layout_for_focus(
                &self.root_blueprint,
                &self.layout_root,
                active.input_index,
                active.input_id.as_deref(),
                active.entity_id,
            )
            .is_some_and(|l| std::ptr::eq(l, layout));
            if matches_active {
                return mask_if_pw(active.state.text.get());
            }
        }

        if let Some(id) = input_id {
            if let Some(state) = self.input_states.get(id) {
                return mask_if_pw(state.text.get());
            }
        } else {
            let mut idx = 0usize;
            while let Some(found) =
                Self::find_input_layout(&self.root_blueprint, &self.layout_root, idx, None, &mut 0)
            {
                if std::ptr::eq(found, layout) {
                    if let Some(state) = self.input_states.get(&format!("input:{idx}")) {
                        return mask_if_pw(state.text.get());
                    }
                    break;
                }
                idx += 1;
            }
        }

        for attr in attributes {
            if let strata_dom::Attribute::Property { key, value } = attr {
                if key == "value" && !value.is_empty() {
                    return mask_if_pw(value.clone());
                }
            }
        }

        Self::extract_text_content(bp)
    }

    /// Inspects the layout element at coordinates `(x, y)` returning tag, CSS classes, bounds, text, and cursor style.
    pub fn mcp_inspect_element(
        &self,
        x: f32,
        y: f32,
    ) -> (
        String,
        Vec<String>,
        strata_layout::Rect,
        String,
        strata_platform::window::CursorIcon,
    ) {
        let cursor_icon = Self::hit_test_cursor(&self.root_blueprint, &self.layout_root, x, y);
        if let Some(path) = Self::hit_test_node_path(&self.root_blueprint, &self.layout_root, x, y)
        {
            if let Some((bp, layout)) =
                Self::find_node_by_path(&self.root_blueprint, &self.layout_root, &path)
            {
                let (tag_name, classes) = match bp {
                    NodeBlueprint::Element { tag, classes, .. } => {
                        (format!("{:?}", tag).to_ascii_lowercase(), classes.clone())
                    }
                    NodeBlueprint::Text(_) | NodeBlueprint::DynamicText(_) => {
                        ("#text".to_string(), strata_dom::ClassList::Empty)
                    }
                    _ => ("div".to_string(), strata_dom::ClassList::Empty),
                };
                let text = match bp {
                    NodeBlueprint::Element {
                        tag: Tag::Input | Tag::Textarea,
                        attributes,
                        ..
                    } => self.mcp_input_text(bp, attributes, layout),
                    _ => Self::extract_text_content(bp),
                };
                return (
                    tag_name,
                    classes.to_vec(),
                    layout.geometry.border_box,
                    text,
                    cursor_icon,
                );
            }
        }

        (
            "body".to_string(),
            Vec::new(),
            strata_layout::Rect {
                x: 0.0,
                y: 0.0,
                width: self.viewport_width,
                height: self.viewport_height,
            },
            String::new(),
            cursor_icon,
        )
    }
}