strata-ui 0.1.0

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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
//! Window launching, headless execution, and Linux GTK platform lifecycle hooks.

use super::*;

/// Standard public entry point launching an interactive Strata desktop window.
/// Automatically detects operating system display server, opens native window,
/// and runs the event pump until the window is closed.
pub fn launch<F>(root: F)
where
    F: FnMut() -> View + 'static,
{
    let config = WindowConfig::default();
    if let Err(e) = launch_with_config(root, config) {
        eprintln!("[Strata] Fatal runtime launch error: {}", e);
    }
}

/// Standard public entry point launching with custom window configuration.
pub fn launch_with_config<F>(root: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_with_events(root, config, |_, _| false)
}

/// Launches a Strata window with custom window configuration and low-level platform event hook.
pub fn launch_with_events<F, E>(
    root: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    launch_auto(root, config, event_handler)
}

/// Auto-detects the host platform display server and launches an interactive desktop window.
pub fn launch_auto<F, E>(root_fn: F, config: WindowConfig, event_handler: E) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    // 1. Explicit or detected headless execution
    if std::env::var("STRATA_HEADLESS").is_ok() {
        return launch_headless_pump(root_fn, config, event_handler);
    }

    let root_rc = std::rc::Rc::new(std::cell::RefCell::new(root_fn));
    let event_rc = std::rc::Rc::new(std::cell::RefCell::new(event_handler));

    #[cfg(target_os = "linux")]
    {
        let backend = std::env::var("STRATA_BACKEND")
            .unwrap_or_default()
            .to_lowercase();
        if backend == "x11" {
            let r = root_rc.clone();
            let ev = event_rc.clone();
            return launch_x11_with_events(
                move || (r.borrow_mut())(),
                config,
                move |event, rt| (ev.borrow_mut())(event, rt),
            );
        } else if backend == "wayland" {
            let r = root_rc.clone();
            let ev = event_rc.clone();
            return launch_wayland_with_events(
                move || (r.borrow_mut())(),
                config,
                move |event, rt| (ev.borrow_mut())(event, rt),
            );
        } else if backend == "gtk" {
            let r = root_rc.clone();
            let ev = event_rc.clone();
            return launch_gtk_with_events(
                move || (r.borrow_mut())(),
                config,
                move |event, rt| (ev.borrow_mut())(event, rt),
            );
        }

        // Automatic Linux detection:
        // If DISPLAY or WAYLAND_DISPLAY is present, attempt GTK first for Cairo/Pango typography.
        if std::env::var("DISPLAY").is_ok() || std::env::var("WAYLAND_DISPLAY").is_ok() {
            let r = root_rc.clone();
            let ev = event_rc.clone();
            let gtk_res = launch_gtk_with_events(
                move || (r.borrow_mut())(),
                config.clone(),
                move |event, rt| (ev.borrow_mut())(event, rt),
            );
            match gtk_res {
                Ok(()) => return Ok(()),
                Err(e) => {
                    eprintln!("[Strata] GTK display launch failed: {}. Falling back to native display servers...", e);
                }
            }

            if std::env::var("WAYLAND_DISPLAY").is_ok() {
                let r = root_rc.clone();
                let ev = event_rc.clone();
                if let Ok(()) = launch_wayland_with_events(
                    move || (r.borrow_mut())(),
                    config.clone(),
                    move |event, rt| (ev.borrow_mut())(event, rt),
                ) {
                    return Ok(());
                }
            }

            if std::env::var("DISPLAY").is_ok() {
                let r = root_rc.clone();
                let ev = event_rc.clone();
                if let Ok(()) = launch_x11_with_events(
                    move || (r.borrow_mut())(),
                    config.clone(),
                    move |event, rt| (ev.borrow_mut())(event, rt),
                ) {
                    return Ok(());
                }
            }
        }

        // Fallback to headless pump if no display servers are connected
        let r = root_rc.clone();
        let ev = event_rc.clone();
        launch_headless_pump(
            move || (r.borrow_mut())(),
            config,
            move |event, rt| (ev.borrow_mut())(event, rt),
        )
    }

    #[cfg(windows)]
    {
        let r = root_rc.clone();
        let ev = event_rc.clone();
        launch_win32_with_events(
            move || (r.borrow_mut())(),
            config,
            move |event, rt| (ev.borrow_mut())(event, rt),
        )
    }

    #[cfg(target_os = "macos")]
    {
        let r = root_rc.clone();
        let ev = event_rc.clone();
        launch_macos_with_events(
            move || (r.borrow_mut())(),
            config,
            move |event, rt| (ev.borrow_mut())(event, rt),
        )
    }

    #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
    {
        let r = root_rc.clone();
        let ev = event_rc.clone();
        launch_headless_pump(
            move || (r.borrow_mut())(),
            config,
            move |event, rt| (ev.borrow_mut())(event, rt),
        )
    }
}

/// Launches a Strata application with an event hook and a periodic `on_frame` callback.
///
/// On Linux GTK windows, `on_frame` is driven by the unified window scheduler at
/// `interval` (or ~16 ms when `None`). Returning [`FrameOutcome::Dirty`] re-invokes
/// `root_fn` and repaints; returning [`FrameOutcome::Unchanged`] skips rebuild and paint.
pub fn launch_with_frame<F, E, H>(
    root_fn: F,
    config: WindowConfig,
    event_handler: E,
    interval: Option<std::time::Duration>,
    on_frame: H,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
    H: FnMut(AppFrameInfo, &mut AppRuntime) -> FrameOutcome + 'static,
{
    #[cfg(target_os = "linux")]
    {
        let backend = std::env::var("STRATA_BACKEND")
            .unwrap_or_default()
            .to_lowercase();
        if (backend.is_empty() || backend == "gtk")
            && (std::env::var("DISPLAY").is_ok() || std::env::var("WAYLAND_DISPLAY").is_ok())
        {
            return launch_gtk_with_frame(root_fn, config, event_handler, interval, on_frame);
        }
        launch_with_events(root_fn, config, event_handler)
    }

    #[cfg(not(target_os = "linux"))]
    {
        let _ = (interval, on_frame);
        launch_with_events(root_fn, config, event_handler)
    }
}

#[inline]
pub(crate) fn primary_refresh_interval() -> std::time::Duration {
    #[cfg(target_os = "linux")]
    {
        strata_platform::gtk_primary_refresh_interval()
    }
    #[cfg(not(target_os = "linux"))]
    {
        std::time::Duration::from_millis(16)
    }
}

pub(crate) fn compute_next_wakeup(
    rt: &AppRuntime,
    frame_hook_due_in: Option<std::time::Duration>,
    uncapped_present: bool,
) -> Option<std::time::Duration> {
    let mut best = strata_js::web::time_until_next_timer();
    let mut consider = |d: std::time::Duration| {
        best = Some(match best {
            Some(cur) => cur.min(d),
            None => d,
        });
    };
    if let Some(d) = frame_hook_due_in {
        consider(d);
    } else if rt.poll_retained_3d_dirty() {
        consider(std::time::Duration::ZERO);
    }
    if let Some(d) = rt.next_hover_tooltip_due_in() {
        consider(d);
    }
    let uncapped = uncapped_present || rt.has_uncapped_canvas3d();
    if rt.has_active_animations()
        || rt.active_smooth_scroll.is_some()
        || rt.scrollbar_drag.is_some()
        || rt.pending_window_drag.is_some()
        || rt.drag_selection_active
        || rt.mcp_cursor.is_some()
    {
        let anim_interval = if uncapped {
            std::time::Duration::ZERO
        } else {
            primary_refresh_interval()
        };
        consider(anim_interval);
    } else if rt.active_focus.is_some() {
        consider(std::time::Duration::from_millis(250));
    }
    best
}

/// Runs a deterministic headless event pump for testing, CI automation, and non-display environments.
pub fn launch_headless_pump<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    mut event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    println!(
        "[+] Launching Strata Headless Engine: \"{}\"...",
        config.title
    );
    let mut window = strata_platform::MockPlatformWindow::new(config.clone());
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let mut runtime = AppRuntime::new(view, width, height, 1.0);
    let mut presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let mut compositor = SwapchainCompositor::new(config.width, config.height, 1.0);

    let max_frames = std::env::var("STRATA_HEADLESS_EXIT_AFTER_FRAMES")
        .ok()
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(1);

    let mut frames_rendered = 0u64;

    while frames_rendered < max_frames {
        let _ = runtime.render_frame(&mut compositor, &mut presenter, frames_rendered == 0);
        let _ =
            window.present_surface(&presenter.backing_buffer, presenter.width, presenter.height);
        frames_rendered += 1;

        let events = window.poll_events();
        let mut should_exit = false;
        for ev in events {
            let (_, _, exit) = platforms::dispatch_native_platform_event(
                &ev,
                &mut runtime,
                &mut compositor,
                &mut presenter,
                &mut event_handler,
            );
            if exit {
                should_exit = true;
                break;
            }
        }
        if should_exit {
            break;
        }
    }

    println!(
        "[OK] Strata Headless execution complete: {} physical frames presented.",
        frames_rendered
    );
    Ok(())
}

/// Headless runner returning the initialized `AppRuntime` for testing and layout inspection.
pub fn launch_headless<F>(root: F, width: f32, height: f32) -> AppRuntime
where
    F: FnOnce() -> View + 'static,
{
    let view = root();
    AppRuntime::new(view, width, height, 1.0)
}

/// Launches a live interactive Strata window with surface presentation and frame pacing.
pub fn launch_window<F>(
    root: F,
    config: WindowConfig,
) -> (AppRuntime, LivePlatformWindow, SoftwareSurfacePresenter)
where
    F: FnOnce() -> View + 'static,
{
    println!(
        "[+] Launching Strata Live Native Window: \"{}\"...",
        config.title
    );
    let view = root();
    let width = config.width as f32;
    let height = config.height as f32;
    let mut runtime = AppRuntime::new(view, width, height, 1.0);

    let mut window = LivePlatformWindow::new(config.clone());
    let mut presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let mut compositor = SwapchainCompositor::new(config.width, config.height, 1.0);

    let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
    let _ = window.present_surface(&presenter.backing_buffer, presenter.width, presenter.height);

    println!(
        "[OK] Strata Live Window active: {}x{} physical pixels, {} commands presented.",
        presenter.width,
        presenter.height,
        runtime.display_list.commands.len()
    );

    (runtime, window, presenter)
}

#[cfg(target_os = "linux")]
/// Launches an interactive native GTK3/Cairo desktop window on Linux with high-fidelity system typography.
pub fn launch_gtk<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_gtk_with_events(root_fn, config, |_, _| false)
}

/// Invokes the application's platform event hook and returns its redraw/rebuild decision.
/// This is the single dispatch seam used by the native window event pumps, so tray,
/// single-instance, and window events all flow through the same user callback.
pub fn invoke_user_event_hook<E>(
    event: &strata_platform::PlatformEvent,
    runtime: &mut AppRuntime,
    hook: &mut E,
) -> bool
where
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool,
{
    hook(event, runtime)
}

#[cfg(target_os = "linux")]
type FrameHookFn = Box<dyn FnMut(AppFrameInfo, &mut AppRuntime) -> FrameOutcome>;

#[cfg(target_os = "linux")]
/// Launches an interactive native GTK3/Cairo desktop window on Linux with a custom event hook.
pub fn launch_gtk_with_events<F, E>(
    root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    launch_gtk_with_frame_inner(root_fn, config, event_handler, None)
}

#[cfg(target_os = "linux")]
/// Launches an interactive native GTK3/Cairo desktop window on Linux with a custom
/// event hook and a periodic `on_frame` callback.
pub fn launch_gtk_with_frame<F, E, H>(
    root_fn: F,
    config: WindowConfig,
    event_handler: E,
    interval: Option<std::time::Duration>,
    on_frame: H,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
    H: FnMut(AppFrameInfo, &mut AppRuntime) -> FrameOutcome + 'static,
{
    launch_gtk_with_frame_inner(
        root_fn,
        config,
        event_handler,
        Some((interval, Box::new(on_frame))),
    )
}

#[cfg(target_os = "linux")]
fn launch_gtk_with_frame_inner<F, E>(
    root_fn: F,
    config: WindowConfig,
    event_handler: E,
    on_frame_opt: Option<(Option<std::time::Duration>, FrameHookFn)>,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    let _single_instance_lock = if let Some(ref app_id) = config.single_instance_id {
        let args: Vec<String> = std::env::args().collect();
        match strata_platform::SingleInstanceLock::acquire(app_id, &args, |forwarded_args| {
            #[cfg(target_os = "linux")]
            {
                strata_platform::gtk_focus_window();
                strata_platform::post_platform_event(
                    strata_platform::PlatformEvent::SingleInstanceTriggered(forwarded_args),
                );
            }
        }) {
            Ok(strata_platform::SingleInstanceStatus::Primary(lock)) => Some(lock),
            Ok(strata_platform::SingleInstanceStatus::SecondaryExited) => {
                return Ok(());
            }
            Err(e) => {
                eprintln!("[Strata] SingleInstanceLock warning: {}", e);
                None
            }
        }
    } else {
        None
    };

    let root_fn_rc = std::rc::Rc::new(std::cell::RefCell::new(root_fn));
    let event_handler_rc = std::rc::Rc::new(std::cell::RefCell::new(event_handler));

    let _ = strata_dom::reactive::take_dirty_signals();
    let initial_view = (root_fn_rc.borrow_mut())();
    let runtime = std::rc::Rc::new(std::cell::RefCell::new(AppRuntime::new(
        initial_view,
        config.width as f32,
        config.height as f32,
        1.0,
    )));
    let _ = strata_dom::reactive::take_dirty_signals();

    let transparent_bg = config.transparent
        || config.decoration == strata_platform::WindowDecoration::FramelessTransparent;
    let allow_maximize = config.resizable && !config.is_docked();

    let runtime_draw = runtime.clone();
    let root_fn_draw = root_fn_rc.clone();
    let on_draw = move |width: f32, height: f32, cr: &strata_platform::cairo::Context| {
        let mut resize_fired = false;
        {
            let mut rt = runtime_draw.borrow_mut();
            let size_changed = (rt.viewport_width - width).abs() > 0.01
                || (rt.viewport_height - height).abs() > 0.01;
            rt.viewport_width = width;
            rt.viewport_height = height;
            if size_changed {
                {
                    let rt = &mut *rt;
                    rt.unapply_scroll_offsets();
                    rt.layout_root.compute_layout(width, height);
                    AppRuntime::sync_layout_to_ir(&rt.layout_root, &mut rt.ir_graph);
                    rt.apply_viewport_clamps();
                    rt.apply_scroll_offsets();
                    rt.spatial_index
                        .rebuild(&rt.root_blueprint, &rt.layout_root);
                }
                resize_fired = rt.dispatch_resize_observers();
                rt.mark_paint_dirty();
            }
        }
        if resize_fired {
            let _ = strata_dom::reactive::take_dirty_signals();
            let new_view = (root_fn_draw.borrow_mut())();
            runtime_draw.borrow_mut().update_view(new_view);
        }
        let mut rt = runtime_draw.borrow_mut();
        // Repaints with no state change (expose, focus) reuse the compiled list.
        let dl_recompiled = rt.compile_display_list_if_dirty();
        let mcp_overlay_active = rt
            .mcp_cursor
            .as_ref()
            .is_some_and(|c| c.fade_opacity > 0.001);
        let overlay_dirty = dl_recompiled || resize_fired || mcp_overlay_active;

        let cursor_tuple = rt
            .mcp_cursor
            .as_ref()
            .map(|c| c as *const strata_platform::McpCursorState);
        let theme_ref = if cursor_tuple.is_some() {
            Some(rt.cursor_theme() as *const strata_platform::CursorTheme)
        } else {
            None
        };
        let gpu_presented = {
            let cursor_pair = match (cursor_tuple, theme_ref) {
                // SAFETY: Both pointers borrow disjoint fields of `rt` for the duration of the call.
                (Some(c), Some(t)) => Some(unsafe { (&*c, &*t) }),
                _ => None,
            };
            strata_platform::try_present_display_list_gpu(
                &rt.display_list,
                width,
                height,
                overlay_dirty,
                transparent_bg,
                cursor_pair,
            )
        };

        if gpu_presented {
            rt.acknowledge_retained_3d_presented();
            return;
        }

        // Clear or fill window background
        if transparent_bg {
            cr.set_operator(strata_platform::cairo::Operator::Source);
            cr.set_source_rgba(0.0, 0.0, 0.0, 0.0);
            let _ = cr.paint();
            cr.set_operator(strata_platform::cairo::Operator::Over);
        } else {
            cr.set_source_rgb(0.035, 0.035, 0.043);
            let _ = cr.paint();
        }

        strata_platform::render_display_list_to_cairo(cr, &rt.display_list);
        rt.acknowledge_retained_3d_presented();

        // Render MCP emulated cursor overlay if active and visible
        if let Some(ref mcp_cursor) = rt.mcp_cursor {
            if mcp_cursor.fade_opacity > 0.001 {
                rt.cursor_theme().render_cairo(
                    cr,
                    mcp_cursor.cursor_icon,
                    mcp_cursor.current_x,
                    mcp_cursor.current_y,
                    mcp_cursor.fade_opacity,
                    mcp_cursor.is_down,
                );
            }
        }
    };

    let frame_hook_state = std::rc::Rc::new(std::cell::RefCell::new(on_frame_opt.map(
        |(explicit_interval, cb)| {
            let now = std::time::Instant::now();
            (explicit_interval, now, None::<std::time::Instant>, cb)
        },
    )));
    let frame_hook_tick = frame_hook_state.clone();

    let runtime_event = runtime.clone();
    let root_fn_event = root_fn_rc.clone();
    let event_handler_event = event_handler_rc.clone();
    let runtime_dirty = runtime.clone();
    let on_event_inner = move |event: &strata_platform::PlatformEvent| -> (bool, bool) {
        let mut needs_rebuild = false;
        let mut needs_redraw = false;
        {
            let mut rt = runtime_event.borrow_mut();
            if crate::mcp_bridge::drain_and_execute_mcp_requests(&mut rt) {
                needs_rebuild = true;
            }
            if invoke_user_event_hook(event, &mut rt, &mut *event_handler_event.borrow_mut()) {
                needs_rebuild = true;
            }
            if let strata_platform::PlatformEvent::WindowResized { width, height } = event {
                let wf = (*width).max(1) as f32;
                let hf = (*height).max(1) as f32;
                let size_changed = (rt.viewport_width - wf).abs() > 0.01
                    || (rt.viewport_height - hf).abs() > 0.01;
                rt.viewport_width = wf;
                rt.viewport_height = hf;
                if size_changed {
                    let signals_before = strata_dom::reactive::has_dirty_signals();
                    {
                        let rt = &mut *rt;
                        rt.unapply_scroll_offsets();
                        rt.layout_root.compute_layout(wf, hf);
                        AppRuntime::sync_layout_to_ir(&rt.layout_root, &mut rt.ir_graph);
                        rt.apply_viewport_clamps();
                        rt.apply_scroll_offsets();
                        rt.spatial_index
                            .rebuild(&rt.root_blueprint, &rt.layout_root);
                    }
                    let resize_fired = rt.dispatch_resize_observers();
                    if resize_fired
                        || (!signals_before && strata_dom::reactive::has_dirty_signals())
                    {
                        needs_rebuild = true;
                    }
                    rt.mark_paint_dirty();
                }
                needs_redraw = true;
            }
            if let strata_platform::PlatformEvent::Scroll {
                delta_x, delta_y, ..
            } = event
            {
                let (px, py) = rt.last_mouse_pos;
                let signals_before = strata_dom::reactive::has_dirty_signals();
                let (wheel_handled, scroll_changed) =
                    rt.dispatch_smooth_scroll_action(px, py, *delta_x, *delta_y);
                if wheel_handled {
                    let retained_only = !signals_before
                        && !strata_dom::reactive::has_dirty_signals()
                        && rt.target_is_retained_canvas3d(px, py);
                    if !retained_only {
                        needs_rebuild = true;
                    }
                }
                if rt.dismiss_hover_tooltip_and_repaint() {
                    needs_redraw = true;
                }
                if scroll_changed {
                    needs_redraw = true;
                }
            }
            if let strata_platform::PlatformEvent::AnimationTick = event {
                let scroll_changed = rt.poll_smooth_scroll();
                let anim_changed = rt.poll_animations();
                let caret_changed = rt.poll_caret_blink();
                let tooltip_changed = rt.poll_hover_tooltip();
                let mcp_changed = if let Some(ref mut c) = rt.mcp_cursor {
                    c.poll_tick()
                } else {
                    false
                };
                if scroll_changed
                    || anim_changed
                    || caret_changed
                    || tooltip_changed
                    || mcp_changed
                {
                    needs_redraw = true;
                }
                if strata_js::web::tick_timers() > 0 {
                    needs_rebuild = true;
                    needs_redraw = true;
                }
                if let Some((
                    explicit_interval,
                    ref mut last_called,
                    ref mut next_deadline_opt,
                    ref mut cb,
                )) = *frame_hook_tick.borrow_mut()
                {
                    let interval = explicit_interval.unwrap_or_else(primary_refresh_interval);
                    let now = std::time::Instant::now();
                    let deadline = *next_deadline_opt.get_or_insert_with(|| *last_called + interval);
                    let tolerance = std::time::Duration::from_micros(600).min(interval / 3);
                    if now + tolerance >= deadline {
                        let elapsed = now.saturating_duration_since(*last_called);
                        *last_called = now;
                        let mut next_dl = deadline + interval;
                        if next_dl <= now {
                            next_dl = now + interval;
                        }
                        *next_deadline_opt = Some(next_dl);
                        let info = AppFrameInfo {
                            frame_index: rt.frame_count,
                            viewport_width: rt.viewport_width,
                            viewport_height: rt.viewport_height,
                            dpr: rt.dpr,
                            command_count: rt.display_list.commands.len(),
                            dt: elapsed,
                        };
                        if cb(info, &mut rt).is_dirty() {
                            needs_rebuild = true;
                            needs_redraw = true;
                        }
                    }
                }
            }
            if let strata_platform::PlatformEvent::DoubleClick { button, x, y } = event {
                let _btn_idx = button.to_button_index();
                if *button == strata_platform::MouseButton::Left && allow_maximize {
                    let region = AppRuntime::hit_test_property(
                        &rt.root_blueprint,
                        &rt.layout_root,
                        *x,
                        *y,
                        "data-app-region",
                    );
                    if region.as_deref() == Some("drag") {
                        #[cfg(target_os = "linux")]
                        {
                            let new_max = strata_platform::gtk_toggle_maximize();
                            let _ = invoke_user_event_hook(
                                &strata_platform::PlatformEvent::WindowMaximized(new_max),
                                &mut rt,
                                &mut *event_handler_event.borrow_mut(),
                            );
                        }
                        needs_rebuild = true;
                    }
                }
                if rt.dispatch_dblclick(*x, *y) {
                    needs_rebuild = true;
                }
            }
            if let strata_platform::PlatformEvent::MouseDown { x, y, button } = event {
                rt.last_mouse_pos = (*x, *y);
                if rt.dismiss_hover_tooltip_and_repaint() {
                    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 {
                    rt.is_mouse_down = true;
                    if rt.check_scrollbar_thumb_press(*x, *y) {
                        needs_redraw = true;
                    } else {
                        rt.begin_window_drag_candidate(*x, *y);
                        let action = rt.dispatch_pointer_down_action_with_button_and_modifiers(
                            *x,
                            *y,
                            btn_idx,
                            strata_platform::gtk_current_modifiers(),
                        );
                        let retained_only = action == PointerDownAction::DomListenerInvoked
                            && !signals_before
                            && !strata_dom::reactive::has_dirty_signals()
                            && rt.target_is_retained_canvas3d(*x, *y);
                        if action.needs_rebuild() && !retained_only {
                            needs_rebuild = true;
                        } else if action.needs_redraw() && !retained_only {
                            needs_redraw = true;
                        }
                        if rt.dispatch_mouse_click(*x, *y, btn_idx, 1) {
                            needs_rebuild = true;
                        }
                        if rt.drain_edit_commands() {
                            needs_rebuild = true;
                        }
                        if !needs_rebuild && rt.restyle_for_active_change() {
                            needs_redraw = true;
                        }
                    }
                } else if *button == strata_platform::MouseButton::Middle {
                    let action = rt.dispatch_pointer_down_action_with_button_and_modifiers(
                        *x,
                        *y,
                        btn_idx,
                        strata_platform::gtk_current_modifiers(),
                    );
                    let retained_only = action == PointerDownAction::DomListenerInvoked
                        && !signals_before
                        && !strata_dom::reactive::has_dirty_signals()
                        && rt.target_is_retained_canvas3d(*x, *y);
                    if action.needs_rebuild() && !retained_only {
                        needs_rebuild = true;
                    } else if action.needs_redraw() && !retained_only {
                        needs_redraw = true;
                    }
                    if rt.dispatch_mouse_click(*x, *y, btn_idx, 1) {
                        needs_rebuild = true;
                    }
                } else if *button == strata_platform::MouseButton::Right {
                    let action = rt.dispatch_pointer_down_action_with_button_and_modifiers(
                        *x,
                        *y,
                        btn_idx,
                        strata_platform::gtk_current_modifiers(),
                    );
                    let retained_only = action == PointerDownAction::DomListenerInvoked
                        && !signals_before
                        && !strata_dom::reactive::has_dirty_signals()
                        && rt.target_is_retained_canvas3d(*x, *y);
                    if action.needs_rebuild() && !retained_only {
                        needs_rebuild = true;
                    } else if action.needs_redraw() && !retained_only {
                        needs_redraw = true;
                    }
                    if rt.dispatch_context_menu(*x, *y) {
                        needs_rebuild = true;
                    }
                    if rt.drain_edit_commands() {
                        needs_rebuild = true;
                    }
                }
            }
            if let strata_platform::PlatformEvent::MouseUp { x, y, button } = event {
                rt.last_mouse_pos = (*x, *y);
                let btn_idx = button.to_button_index();
                let was_mouse_down = rt.is_mouse_down;
                if *button == strata_platform::MouseButton::Left {
                    rt.is_mouse_down = false;
                }
                if rt.scrollbar_drag.is_some() {
                    rt.scrollbar_drag = None;
                    needs_redraw = true;
                }
                rt.pending_window_drag = None;
                let signals_before = strata_dom::reactive::has_dirty_signals();
                let was_retained_canvas = rt.target_is_retained_canvas3d(*x, *y);
                let action = rt.dispatch_pointer_up_action_with_button(*x, *y, btn_idx);
                let retained_only = action == PointerUpAction::DomListenerInvoked
                    && !signals_before
                    && !strata_dom::reactive::has_dirty_signals()
                    && was_retained_canvas;
                if action.needs_rebuild() && !retained_only {
                    needs_rebuild = true;
                } else if action.needs_redraw() && !retained_only {
                    needs_redraw = true;
                }
                if was_mouse_down && !needs_rebuild && rt.restyle_for_active_change() {
                    needs_redraw = true;
                }
            }
            if let strata_platform::PlatformEvent::MouseMove { x, y } = event {
                if rt.scrollbar_drag.is_some() {
                    if rt.poll_scrollbar_drag(*x, *y) {
                        needs_redraw = true;
                    }
                } else {
                    if rt.poll_window_drag(*x, *y) {
                        #[cfg(target_os = "linux")]
                        {
                            let is_max = strata_platform::gtk_is_maximized();
                            let _ = invoke_user_event_hook(
                                &strata_platform::PlatformEvent::WindowMaximized(is_max),
                                &mut rt,
                                &mut *event_handler_event.borrow_mut(),
                            );
                        }
                        needs_rebuild = true;
                    }
                    let signals_before = strata_dom::reactive::has_dirty_signals();
                    let is_retained_canvas = rt.target_is_retained_canvas3d(*x, *y);
                    let action = rt.dispatch_pointer_move_action(*x, *y);
                    let retained_only = action == PointerMoveAction::DomListenerInvoked
                        && !signals_before
                        && !strata_dom::reactive::has_dirty_signals()
                        && is_retained_canvas;
                    if action.needs_rebuild() && !retained_only {
                        needs_rebuild = true;
                    } else if action.needs_redraw() && !retained_only {
                        needs_redraw = true;
                    }
                    if rt.update_hover(*x, *y) {
                        if rt.hover_is_paint_only() {
                            needs_redraw = true;
                        } else {
                            needs_rebuild = true;
                        }
                    }
                    let cur = super::platforms::resolved_cursor_at(&rt, *x, *y);
                    rt.current_cursor = cur;
                    strata_platform::gtk_set_cursor(cur);
                }
            }
            if matches!(event, strata_platform::PlatformEvent::CursorLeft) {
                rt.is_mouse_down = false;
                rt.current_cursor = strata_platform::window::CursorIcon::Default;
                strata_platform::gtk_set_cursor(strata_platform::window::CursorIcon::Default);
                if rt.clear_hover() {
                    if rt.hover_is_paint_only() {
                        needs_redraw = true;
                    } else {
                        needs_rebuild = true;
                    }
                }
            }
            if matches!(
                event,
                strata_platform::PlatformEvent::KeyDown { .. }
                    | strata_platform::PlatformEvent::KeyUp { .. }
                    | strata_platform::PlatformEvent::ReceivedCharacter(_)
                    | strata_platform::PlatformEvent::ImePreedit { .. }
                    | strata_platform::PlatformEvent::ImeCommit(_)
            ) {
                if matches!(event, strata_platform::PlatformEvent::KeyDown { .. })
                    && rt.dismiss_hover_tooltip_and_repaint()
                {
                    needs_redraw = true;
                }
                let res = rt.dispatch_key_event(event);
                if res.needs_rebuild() {
                    needs_rebuild = true;
                } else if res.needs_redraw() {
                    needs_redraw = true;
                }
            }
            if let strata_platform::PlatformEvent::FileDrop(paths) = event {
                if rt.dismiss_hover_tooltip_and_repaint() {
                    needs_redraw = true;
                }
                if rt.dispatch_file_drop(paths.clone()) {
                    needs_rebuild = true;
                }
            }
        }

        if perf_logging_enabled() && needs_rebuild {
            eprintln!("[perf] rebuild_trigger: {:?}", event);
        }

        let defer_3d_to_frame_hook = frame_hook_tick.borrow().is_some()
            && matches!(
                event,
                strata_platform::PlatformEvent::MouseMove { .. }
                    | strata_platform::PlatformEvent::Scroll { .. }
            );
        let retained_3d_dirty = !needs_rebuild
            && !needs_redraw
            && !defer_3d_to_frame_hook
            && runtime_event.borrow().poll_retained_3d_dirty();

        let refresh_pointer_cursor = |rt: &mut AppRuntime| {
            let (px, py) = rt.last_mouse_pos;
            let cur = super::platforms::resolved_cursor_at(rt, px, py);
            rt.current_cursor = cur;
            strata_platform::gtk_set_cursor(cur);
        };

        if needs_rebuild {
            #[cfg(target_os = "linux")]
            if strata_platform::gtk_is_closing() {
                return (false, false);
            }
            if runtime_event.borrow_mut().sync_dynamic_text() {
                if !matches!(event, strata_platform::PlatformEvent::CursorLeft)
                    && (matches!(
                        event,
                        strata_platform::PlatformEvent::MouseDown { .. }
                            | strata_platform::PlatformEvent::MouseUp { .. }
                            | strata_platform::PlatformEvent::MouseMove { .. }
                    ) || runtime_event.borrow().current_hover_path.is_some())
                {
                    refresh_pointer_cursor(&mut runtime_event.borrow_mut());
                }
                (true, true)
            } else {
                let _ = strata_dom::reactive::take_dirty_signals();
                let new_view = (root_fn_event.borrow_mut())();
                let mut rt = runtime_event.borrow_mut();
                rt.update_view(new_view);
                if !matches!(event, strata_platform::PlatformEvent::CursorLeft)
                    && (matches!(
                        event,
                        strata_platform::PlatformEvent::MouseDown { .. }
                            | strata_platform::PlatformEvent::MouseUp { .. }
                            | strata_platform::PlatformEvent::MouseMove { .. }
                    ) || rt.current_hover_path.is_some())
                {
                    refresh_pointer_cursor(&mut rt);
                }
                (true, true)
            }
        } else if needs_redraw {
            #[cfg(target_os = "linux")]
            if strata_platform::gtk_is_closing() {
                return (false, false);
            }
            if matches!(
                event,
                strata_platform::PlatformEvent::MouseDown { .. }
                    | strata_platform::PlatformEvent::MouseUp { .. }
            ) {
                refresh_pointer_cursor(&mut runtime_event.borrow_mut());
            }
            (true, true)
        } else if retained_3d_dirty {
            #[cfg(target_os = "linux")]
            if strata_platform::gtk_is_closing() {
                return (false, false);
            }
            if matches!(
                event,
                strata_platform::PlatformEvent::MouseDown { .. }
                    | strata_platform::PlatformEvent::MouseUp { .. }
            ) {
                refresh_pointer_cursor(&mut runtime_event.borrow_mut());
            }
            (true, false)
        } else {
            if matches!(
                event,
                strata_platform::PlatformEvent::MouseDown { .. }
                    | strata_platform::PlatformEvent::MouseUp { .. }
            ) {
                refresh_pointer_cursor(&mut runtime_event.borrow_mut());
            }
            (false, false)
        }
    };

    let perf_launch = perf_logging_enabled();
    let mut last_present_instant = std::time::Instant::now();
    // Only 2D/UI updates invalidate the compiled display list; 3D-only retained updates keep it cached.
    let on_event = move |event: &strata_platform::PlatformEvent| -> bool {
        let t_ev_start = if perf_launch {
            Some(std::time::Instant::now())
        } else {
            None
        };
        let (redraw, paint_dirty) = on_event_inner(event);
        let t_after_inner = t_ev_start.map(|_| std::time::Instant::now());
        if paint_dirty {
            let mut rt = runtime_dirty.borrow_mut();
            let rebuild_done = matches!(event, strata_platform::PlatformEvent::AnimationTick)
                && !rt.is_paint_dirty();
            if !rebuild_done {
                rt.mark_paint_dirty();
            }
            if redraw && strata_platform::gtk_gpu_surface_is_active() {
                let dl_recompiled = rt.compile_display_list_if_dirty();
                let mcp_active = rt
                    .mcp_cursor
                    .as_ref()
                    .is_some_and(|c| c.fade_opacity > 0.001);
                let overlay_dirty = dl_recompiled || rebuild_done || mcp_active;
                let (w, h) = (rt.viewport_width, rt.viewport_height);
                let cursor_tuple = rt
                    .mcp_cursor
                    .as_ref()
                    .map(|c| c as *const strata_platform::McpCursorState);
                let theme_ref =
                    cursor_tuple.map(|_| rt.cursor_theme() as *const strata_platform::CursorTheme);
                let cursor_pair = match (cursor_tuple, theme_ref) {
                    (Some(c), Some(t)) => Some(unsafe { (&*c, &*t) }),
                    _ => None,
                };
                if strata_platform::try_present_display_list_gpu(
                    &rt.display_list,
                    w,
                    h,
                    overlay_dirty,
                    transparent_bg,
                    cursor_pair,
                ) {
                    rt.acknowledge_retained_3d_presented();
                    if let (Some(t0), Some(t1)) = (t_ev_start, t_after_inner) {
                        let now = std::time::Instant::now();
                        let gap_ms = now.duration_since(last_present_instant).as_secs_f64() * 1000.0;
                        last_present_instant = now;
                        let inner_ms = t1.duration_since(t0).as_secs_f64() * 1000.0;
                        let pres_ms = now.duration_since(t1).as_secs_f64() * 1000.0;
                        if gap_ms > 10.5 || inner_ms > 3.5 || pres_ms > 5.0 {
                            eprintln!(
                                "[perf] slow_frame({:?}, ov={}) gap={:.2}ms inner={:.2}ms gpu_ov_pres={:.2}ms",
                                event, overlay_dirty, gap_ms, inner_ms, pres_ms
                            );
                        }
                    }
                    return false;
                }
            }
            return redraw;
        }
        if redraw
            && !runtime_dirty.borrow().is_paint_dirty()
            && strata_platform::gtk_present_active_gpu_surface_now()
        {
            runtime_dirty
                .borrow_mut()
                .acknowledge_retained_3d_presented();
            if let (Some(t0), Some(t1)) = (t_ev_start, t_after_inner) {
                let now = std::time::Instant::now();
                let gap_ms = now.duration_since(last_present_instant).as_secs_f64() * 1000.0;
                last_present_instant = now;
                let inner_ms = t1.duration_since(t0).as_secs_f64() * 1000.0;
                let pres_ms = now.duration_since(t1).as_secs_f64() * 1000.0;
                if gap_ms > 10.5 || inner_ms > 3.5 || pres_ms > 5.0 {
                    eprintln!(
                        "[perf] slow_frame({:?}, active_3d) gap={:.2}ms inner={:.2}ms pres={:.2}ms",
                        event, gap_ms, inner_ms, pres_ms
                    );
                }
            }
            return false;
        }
        redraw
    };

    let uncapped_present = matches!(
        config.present_mode,
        strata_platform::PresentMode::Immediate | strata_platform::PresentMode::Mailbox
    );
    let runtime_wakeup = runtime.clone();
    let frame_hook_wakeup = frame_hook_state;
    let next_wakeup = move || -> Option<std::time::Duration> {
        let hook_due = frame_hook_wakeup
            .borrow_mut()
            .as_mut()
            .map(|(explicit_interval, last_called, next_deadline_opt, _)| {
                let interval = explicit_interval.unwrap_or_else(primary_refresh_interval);
                let now = std::time::Instant::now();
                let deadline = *next_deadline_opt.get_or_insert_with(|| *last_called + interval);
                deadline.saturating_duration_since(now)
            });
        compute_next_wakeup(&runtime_wakeup.borrow(), hook_due, uncapped_present)
    };

    let _ = crate::mcp_bridge::start_mcp_dev_listener(None);
    strata_platform::launch_gtk_window_with_scheduler(&config, on_draw, on_event, next_wakeup)
}