strata-ui 0.1.3

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
//! Native windowing backends for X11, Wayland, Win32, and macOS Cocoa.

use super::*;

pub(crate) fn resolved_cursor_at(
    runtime: &AppRuntime,
    x: f32,
    y: f32,
) -> strata_platform::window::CursorIcon {
    let is_over_scrollbar = runtime.active_scrollbars.iter().any(|bar| {
        bar.max_scroll_y > 1.0
            && (x >= bar.track_bounds.x
                && x <= bar.bounds.x + bar.bounds.width + 1.0
                && y >= bar.track_bounds.y
                && y <= bar.track_bounds.y + bar.track_bounds.height)
    });
    if is_over_scrollbar {
        strata_platform::window::CursorIcon::Default
    } else {
        AppRuntime::hit_test_cursor(&runtime.root_blueprint, &runtime.layout_root, x, y)
    }
}

#[inline]
fn try_retained_canvas3d_redraw_only(
    runtime: &mut AppRuntime,
    x: f32,
    y: f32,
    needs_rebuild: &mut bool,
    needs_redraw: &mut bool,
) {
    if runtime.target_is_retained_canvas3d(x, y) && !strata_dom::reactive::has_dirty_signals() {
        runtime.mark_viewport_3d_dirty();
        *needs_redraw = true;
    } else {
        *needs_rebuild = true;
    }
}

/// Dispatches a native platform event into the AppRuntime, updating geometry, focus, input, and styling.
/// Returns `(needs_rebuild, needs_redraw, should_exit)`.
pub fn dispatch_native_platform_event<E>(
    event: &strata_platform::PlatformEvent,
    runtime: &mut AppRuntime,
    compositor: &mut SwapchainCompositor,
    presenter: &mut SoftwareSurfacePresenter,
    event_handler: &mut E,
) -> (bool, bool, bool)
where
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool,
{
    let mut needs_rebuild = false;
    let mut needs_redraw = false;
    let mut should_exit = false;

    if event_handler(event, runtime) {
        needs_rebuild = true;
    }

    match event {
        strata_platform::PlatformEvent::WindowCloseRequested => {
            should_exit = true;
        }
        strata_platform::PlatformEvent::WindowResized {
            width: w,
            height: h,
        } => {
            let wf = (*w).max(1) as f32;
            let hf = (*h).max(1) as f32;
            compositor.resize(*w, *h, runtime.dpr);
            let _ = presenter.resize(*w, *h);
            let size_changed = (runtime.viewport_width - wf).abs() > 0.01
                || (runtime.viewport_height - hf).abs() > 0.01;
            runtime.viewport_width = wf;
            runtime.viewport_height = hf;
            if size_changed {
                let signals_before = strata_dom::reactive::has_dirty_signals();
                runtime.unapply_scroll_offsets();
                runtime.layout_root.compute_layout(wf, hf);
                AppRuntime::sync_layout_to_ir(&runtime.layout_root, &mut runtime.ir_graph);
                runtime.apply_viewport_clamps();
                runtime.apply_scroll_offsets();
                runtime
                    .spatial_index
                    .rebuild(&runtime.root_blueprint, &runtime.layout_root);
                let resize_fired = runtime.dispatch_resize_observers();
                if resize_fired
                    || (!signals_before && strata_dom::reactive::has_dirty_signals())
                {
                    needs_rebuild = true;
                }
                runtime.mark_paint_dirty();
            }
            needs_redraw = true;
        }
        strata_platform::PlatformEvent::ScaleFactorChanged { scale_factor } => {
            compositor.resize(
                runtime.viewport_width as u32,
                runtime.viewport_height as u32,
                *scale_factor,
            );
            runtime.dpr = *scale_factor;
            needs_redraw = true;
        }
        strata_platform::PlatformEvent::Scroll {
            delta_x, delta_y, ..
        } => {
            let (px, py) = runtime.last_mouse_pos;
            let (wheel_handled, scroll_changed) =
                runtime.dispatch_smooth_scroll_action(px, py, *delta_x, *delta_y);
            if wheel_handled {
                try_retained_canvas3d_redraw_only(
                    runtime,
                    px,
                    py,
                    &mut needs_rebuild,
                    &mut needs_redraw,
                );
            }
            if runtime.dismiss_hover_tooltip_and_repaint() {
                needs_redraw = true;
            }
            if scroll_changed {
                needs_redraw = true;
            }
        }
        strata_platform::PlatformEvent::AnimationTick => {
            let scroll_changed = runtime.poll_smooth_scroll();
            let anim_changed = runtime.poll_animations();
            let caret_changed = runtime.poll_caret_blink();
            let tooltip_changed = runtime.poll_hover_tooltip();
            let mcp_changed = if let Some(ref mut c) = runtime.mcp_cursor {
                c.poll_tick()
            } else {
                false
            };
            if scroll_changed || anim_changed || caret_changed || tooltip_changed || mcp_changed {
                needs_redraw = true;
            }
        }
        strata_platform::PlatformEvent::DoubleClick { button, x, y } => {
            let _btn_idx = button.to_button_index();
            if *button == strata_platform::MouseButton::Left {
                let region = AppRuntime::hit_test_property(
                    &runtime.root_blueprint,
                    &runtime.layout_root,
                    *x,
                    *y,
                    "data-app-region",
                );
                if region.as_deref() == Some("drag") {
                    needs_rebuild = true;
                }
            }
            if runtime.dispatch_dblclick(*x, *y) {
                needs_rebuild = true;
            }
        }
        strata_platform::PlatformEvent::MouseDown { button, x, y } => {
            runtime.last_mouse_pos = (*x, *y);
            if runtime.dismiss_hover_tooltip_and_repaint() {
                needs_redraw = true;
            }
            let btn_idx = button.to_button_index();
            if *button == strata_platform::MouseButton::Left {
                runtime.is_mouse_down = true;
                if runtime.check_scrollbar_thumb_press(*x, *y) {
                    needs_redraw = true;
                } else {
                    runtime.begin_window_drag_candidate(*x, *y);
                    let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
                        *x,
                        *y,
                        btn_idx,
                        Default::default(),
                    );
                    if action.needs_rebuild() {
                        needs_rebuild = true;
                    } else if action.needs_redraw() {
                        needs_redraw = true;
                    }
                    if runtime.dispatch_mouse_click(*x, *y, btn_idx, 1) {
                        needs_rebuild = true;
                    }
                    if runtime.drain_edit_commands() {
                        needs_rebuild = true;
                    }
                    if !needs_rebuild && runtime.restyle_for_active_change() {
                        needs_redraw = true;
                    }
                }
            } else if *button == strata_platform::MouseButton::Middle {
                let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
                    *x,
                    *y,
                    btn_idx,
                    Default::default(),
                );
                if action.needs_rebuild() {
                    needs_rebuild = true;
                } else if action.needs_redraw() {
                    needs_redraw = true;
                }
                if runtime.dispatch_mouse_click(*x, *y, btn_idx, 1) {
                    needs_rebuild = true;
                }
            } else if *button == strata_platform::MouseButton::Right {
                let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
                    *x,
                    *y,
                    btn_idx,
                    Default::default(),
                );
                if action.needs_rebuild() {
                    needs_rebuild = true;
                } else if action.needs_redraw() {
                    needs_redraw = true;
                }
                if runtime.dispatch_context_menu(*x, *y) {
                    needs_rebuild = true;
                }
                if runtime.drain_edit_commands() {
                    needs_rebuild = true;
                }
            }
            runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
        }
        strata_platform::PlatformEvent::MouseUp { button, x, y } => {
            runtime.last_mouse_pos = (*x, *y);
            let btn_idx = button.to_button_index();
            let was_mouse_down = runtime.is_mouse_down;
            if *button == strata_platform::MouseButton::Left {
                runtime.is_mouse_down = false;
            }
            if runtime.scrollbar_drag.is_some() {
                runtime.scrollbar_drag = None;
                needs_redraw = true;
            }
            runtime.pending_window_drag = None;
            let action = runtime.dispatch_pointer_up_action_with_button(*x, *y, btn_idx);
            if action.needs_rebuild() {
                needs_rebuild = true;
            } else if action.needs_redraw() {
                needs_redraw = true;
            }
            if was_mouse_down && !needs_rebuild && runtime.restyle_for_active_change() {
                needs_redraw = true;
            }
            runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
        }
        strata_platform::PlatformEvent::MouseMove { x, y } => {
            runtime.last_mouse_pos = (*x, *y);
            if runtime.scrollbar_drag.is_some() {
                if runtime.poll_scrollbar_drag(*x, *y) {
                    needs_redraw = true;
                }
            } else {
                if runtime.poll_window_drag(*x, *y) {
                    needs_rebuild = true;
                }
                let action = runtime.dispatch_pointer_move_action(*x, *y);
                if action.needs_rebuild() {
                    try_retained_canvas3d_redraw_only(
                        runtime,
                        *x,
                        *y,
                        &mut needs_rebuild,
                        &mut needs_redraw,
                    );
                } else if action.needs_redraw() {
                    needs_redraw = true;
                }
                if runtime.update_hover(*x, *y) {
                    if runtime.hover_is_paint_only() {
                        needs_redraw = true;
                    } else {
                        needs_rebuild = true;
                    }
                }
                runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
            }
        }
        strata_platform::PlatformEvent::CursorLeft => {
            runtime.is_mouse_down = false;
            runtime.current_cursor = strata_platform::window::CursorIcon::Default;
            if runtime.clear_hover() {
                if runtime.hover_is_paint_only() {
                    needs_redraw = true;
                } else {
                    needs_rebuild = true;
                }
            }
        }
        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 { .. })
                && runtime.dismiss_hover_tooltip_and_repaint()
            {
                needs_redraw = true;
            }
            let res = runtime.dispatch_key_event(event);
            if res.needs_rebuild() {
                needs_rebuild = true;
            } else if res.needs_redraw() {
                needs_redraw = true;
            }
        }
        strata_platform::PlatformEvent::WindowMaximized(_) => {
            needs_rebuild = true;
            needs_redraw = true;
        }
        strata_platform::PlatformEvent::FileDrop(paths) => {
            if runtime.dismiss_hover_tooltip_and_repaint() {
                needs_redraw = true;
            }
            if runtime.dispatch_file_drop(paths.clone()) {
                needs_rebuild = true;
                needs_redraw = true;
            }
        }
        _ => {}
    }

    (needs_rebuild, needs_redraw, should_exit)
}

impl AppRuntime {
    /// Dynamically computes the appropriate thread sleep duration based on runtime activity state.
    /// Returns:
    /// - 16ms if animations, smooth scrolling, scrollbar dragging, window dragging, active interactions, or dirty signals are active.
    /// - 50ms if a text input currently has active focus (to service responsive caret blinking).
    /// - 200ms if the runtime is completely idle (dropping CPU consumption to ~0%).
    pub fn compute_idle_sleep(&self, was_active: bool) -> std::time::Duration {
        let mut base = if was_active
            || self.has_active_animations()
            || self.active_smooth_scroll.is_some()
            || self.scrollbar_drag.is_some()
            || self.pending_window_drag.is_some()
            || self.drag_selection_active
            || strata_dom::reactive::has_dirty_signals()
        {
            std::time::Duration::from_millis(16)
        } else if self.active_focus.is_some() {
            std::time::Duration::from_millis(50)
        } else {
            std::time::Duration::from_millis(200)
        };
        if let Some(due) = self.next_hover_tooltip_due_in() {
            base = base.min(due.max(std::time::Duration::from_millis(1)));
        }
        if let Some(next_timer) = strata_js::web::time_until_next_timer() {
            base.min(next_timer.max(std::time::Duration::from_millis(1)))
        } else {
            base
        }
    }
}

/// Runs an interactive native window event loop against any NativeSurfaceWindow implementation,
/// pacing frames, evaluating reactive updates, and blitting pixel buffers.
pub fn run_native_surface_window_loop<W, F, E>(
    mut window: W,
    mut root_fn: Option<F>,
    mut runtime: AppRuntime,
    mut compositor: SwapchainCompositor,
    mut presenter: SoftwareSurfacePresenter,
    mut event_handler: E,
    mut hooks: Option<&mut AppLifecycleHooks>,
) -> Result<(), String>
where
    W: strata_platform::NativeSurfaceWindow,
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool,
{
    // Initial frame presentation
    let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
    let _ = window.present_surface(&presenter.backing_buffer, presenter.width, presenter.height);

    if let Some(ref mut h) = hooks {
        if let Some(ref mut on_start) = h.on_start {
            on_start(&mut runtime);
        }
        if let Some(ref mut on_frame) = h.on_frame {
            let info = AppFrameInfo {
                frame_index: runtime.frame_count,
                viewport_width: runtime.viewport_width,
                viewport_height: runtime.viewport_height,
                dpr: runtime.dpr,
                command_count: runtime.display_list.commands.len(),
                dt: std::time::Duration::ZERO,
            };
            on_frame(info, &mut runtime);
        }
    }

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

    let max_headless_frames = if std::env::var("STRATA_HEADLESS").is_ok() {
        Some(
            std::env::var("STRATA_HEADLESS_EXIT_AFTER_FRAMES")
                .ok()
                .and_then(|s| s.parse::<u64>().ok())
                .unwrap_or(1),
        )
    } else {
        None
    };
    let mut frames_presented: u64 = 1;

    let mut running = true;
    while running {
        if let Some(max_f) = max_headless_frames {
            if frames_presented >= max_f {
                break;
            }
        }
        let events = window.poll_events();
        let mut needs_rebuild = false;
        let mut needs_redraw = false;

        for event in events {
            if let Some(ref mut h) = hooks {
                if let Some(ref mut on_ev) = h.on_event {
                    let mut control = strata_platform::EventLoopControl::Poll;
                    on_ev(&event, &mut control, &mut runtime);
                    if control == strata_platform::EventLoopControl::Exit {
                        running = false;
                        break;
                    }
                }
            }

            if let strata_platform::PlatformEvent::DoubleClick { button, x, y } = event {
                if button == strata_platform::MouseButton::Left {
                    let region = AppRuntime::hit_test_property(
                        &runtime.root_blueprint,
                        &runtime.layout_root,
                        x,
                        y,
                        "data-app-region",
                    );
                    let is_drag = region.as_deref() == Some("drag")
                        || runtime.csd_controller.hit_test(
                            runtime.viewport_width,
                            runtime.viewport_height,
                            x,
                            y,
                        ) == strata_platform::CsdHitZone::Titlebar;
                    if is_drag {
                        let is_max = window.toggle_maximize();
                        window.push_event(strata_platform::PlatformEvent::WindowMaximized(is_max));
                        needs_rebuild = true;
                    }
                }
            }

            if let strata_platform::PlatformEvent::MouseMove { x, y } = event {
                if runtime.poll_window_drag(x, y) {
                    window.begin_window_drag(x, y);
                }
            }

            let (reb, red, exit) = dispatch_native_platform_event(
                &event,
                &mut runtime,
                &mut compositor,
                &mut presenter,
                &mut event_handler,
            );
            if reb {
                needs_rebuild = true;
            }
            if red {
                needs_redraw = true;
            }
            if exit {
                running = false;
                break;
            }
        }

        if !running {
            break;
        }

        let anim_changed = runtime.poll_animations();
        let scroll_changed = runtime.poll_smooth_scroll();
        let caret_changed = runtime.poll_caret_blink();
        let timers_fired = strata_js::web::tick_timers() > 0;
        if timers_fired {
            needs_rebuild = true;
            needs_redraw = true;
        }
        if anim_changed || scroll_changed || caret_changed {
            needs_redraw = true;
        }

        if needs_rebuild {
            if runtime.sync_dynamic_text() {
                runtime.mark_paint_dirty();
                let _ = runtime.render_frame(&mut compositor, &mut presenter, false);
                let _ = window.present_surface(
                    &presenter.backing_buffer,
                    presenter.width,
                    presenter.height,
                );
            } else if let Some(ref mut f) = root_fn {
                let _ = strata_dom::reactive::take_dirty_signals();
                let new_view = f();
                runtime.update_view(new_view);
                let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
                let _ = window.present_surface(
                    &presenter.backing_buffer,
                    presenter.width,
                    presenter.height,
                );
            } else {
                runtime.mark_paint_dirty();
                let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
                let _ = window.present_surface(
                    &presenter.backing_buffer,
                    presenter.width,
                    presenter.height,
                );
            }
        } else if needs_redraw || window.is_redraw_requested() {
            // A per-frame draw hook edits the display list, so it forces a fresh compile.
            let has_draw_hook = hooks.as_ref().is_some_and(|h| h.on_draw.is_some());
            if needs_redraw || has_draw_hook {
                runtime.mark_paint_dirty();
            }
            if let Some(ref mut h) = hooks {
                if let Some(ref mut on_draw) = h.on_draw {
                    on_draw(&mut runtime.display_list);
                }
            }
            let _ = runtime.render_frame(&mut compositor, &mut presenter, false);
            let _ = window.present_surface(
                &presenter.backing_buffer,
                presenter.width,
                presenter.height,
            );
            if let Some(ref mut h) = hooks {
                if let Some(ref mut on_frame) = h.on_frame {
                    let info = AppFrameInfo {
                        frame_index: runtime.frame_count,
                        viewport_width: runtime.viewport_width,
                        viewport_height: runtime.viewport_height,
                        dpr: runtime.dpr,
                        command_count: runtime.display_list.commands.len(),
                        dt: std::time::Duration::ZERO,
                    };
                    on_frame(info, &mut runtime);
                }
            }
        }

        frames_presented += 1;
        if max_headless_frames.is_none() {
            let is_active = anim_changed
                || scroll_changed
                || caret_changed
                || timers_fired
                || needs_rebuild
                || needs_redraw;
            std::thread::sleep(runtime.compute_idle_sleep(is_active));
        }
    }

    if let Some(ref mut h) = hooks {
        if let Some(ref mut on_exit) = h.on_exit {
            on_exit(&mut runtime);
        }
    }

    Ok(())
}

#[cfg(target_os = "linux")]
pub fn launch_x11<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_x11_with_events(root_fn, config, |_, _| false)
}

#[cfg(target_os = "linux")]
/// Launches an interactive native X11 desktop window on Linux with a custom event hook.
pub fn launch_x11_with_events<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    println!(
        "[+] Launching Strata Native Desktop Window on X11: \"{}\"...",
        config.title
    );
    let x11_window = strata_platform::x11_window::X11Window::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);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, 1.0);

    run_native_surface_window_loop(
        x11_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}

#[cfg(target_os = "linux")]
/// Launches an interactive native Wayland desktop window on Linux displaying the Strata application.
pub fn launch_wayland<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_wayland_with_events(root_fn, config, |_, _| false)
}

#[cfg(target_os = "linux")]
/// Launches an interactive native Wayland desktop window on Linux with a custom event hook.
pub fn launch_wayland_with_events<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    println!(
        "[+] Launching Strata Native Desktop Window on Wayland: \"{}\"...",
        config.title
    );
    let wayland_window = strata_platform::WaylandWindow::new(config.clone())?;
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let dpr = wayland_window.scale_factor();
    let mut runtime = AppRuntime::new(view, width, height, dpr);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);

    run_native_surface_window_loop(
        wayland_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}

#[cfg(windows)]
/// Launches an interactive native Win32 desktop window on Windows displaying the Strata application.
pub fn launch_win32<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_win32_with_events(root_fn, config, |_, _| false)
}

#[cfg(windows)]
/// Launches an interactive native Win32 desktop window on Windows with a custom event hook.
pub fn launch_win32_with_events<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    println!(
        "[+] Launching Strata Native Desktop Window on Windows (Win32): \"{}\"...",
        config.title
    );
    let win32_window = strata_platform::Win32Window::new(config.clone())?;
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let dpr = win32_window.scale_factor();
    let mut runtime = AppRuntime::new(view, width, height, dpr);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);

    run_native_surface_window_loop(
        win32_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}

#[cfg(target_os = "macos")]
/// Launches an interactive native macOS Cocoa desktop window displaying the Strata application.
pub fn launch_macos<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_macos_with_events(root_fn, config, |_, _| false)
}

#[cfg(target_os = "macos")]
/// Launches an interactive native macOS Cocoa desktop window with a custom event hook.
pub fn launch_macos_with_events<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    println!(
        "[+] Launching Strata Native Desktop Window on macOS (Cocoa): \"{}\"...",
        config.title
    );
    let macos_window = strata_platform::MacOsWindow::new(config.clone())?;
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let dpr = macos_window.scale_factor();
    let mut runtime = AppRuntime::new(view, width, height, dpr);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);

    run_native_surface_window_loop(
        macos_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}

#[cfg(target_os = "android")]
/// Launches an interactive native Android application window via Android NDK NativeActivity.
pub fn launch_android<F>(
    native_window: *mut std::ffi::c_void,
    root_fn: F,
    config: WindowConfig,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_android_with_events(native_window, root_fn, config, |_, _| false)
}

#[cfg(target_os = "android")]
/// Launches an interactive native Android application window with a custom event hook.
pub fn launch_android_with_events<F, E>(
    native_window: *mut std::ffi::c_void,
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    let android_window = strata_platform::AndroidWindow::new(native_window, config.clone())?;
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let dpr = android_window.scale_factor();
    let mut runtime = AppRuntime::new(view, width, height, dpr);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);

    run_native_surface_window_loop(
        android_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}

#[cfg(target_os = "ios")]
/// Launches an interactive native iOS application window via UIKit and CAMetalLayer.
pub fn launch_ios<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
{
    launch_ios_with_events(root_fn, config, |_, _| false)
}

#[cfg(target_os = "ios")]
/// Launches an interactive native iOS application window with a custom event hook.
pub fn launch_ios_with_events<F, E>(
    mut root_fn: F,
    config: WindowConfig,
    event_handler: E,
) -> Result<(), String>
where
    F: FnMut() -> View + 'static,
    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
{
    let ios_window = strata_platform::IosWindow::new(config.clone())?;
    let view = root_fn();
    let width = config.width as f32;
    let height = config.height as f32;
    let dpr = ios_window.scale_factor();
    let mut runtime = AppRuntime::new(view, width, height, dpr);
    runtime.apply_window_config(&config);
    let presenter =
        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);

    run_native_surface_window_loop(
        ios_window,
        Some(root_fn),
        runtime,
        compositor,
        presenter,
        event_handler,
        None,
    )
}