Skip to main content

strata/runtime/
platforms.rs

1//! Native windowing backends for X11, Wayland, Win32, and macOS Cocoa.
2
3use super::*;
4
5pub(crate) fn resolved_cursor_at(
6    runtime: &AppRuntime,
7    x: f32,
8    y: f32,
9) -> strata_platform::window::CursorIcon {
10    let is_over_scrollbar = runtime.active_scrollbars.iter().any(|bar| {
11        bar.max_scroll_y > 1.0
12            && bar.clip_bounds.contains_point(x, y)
13            && (x >= bar.track_bounds.x
14                && x <= bar.bounds.x + bar.bounds.width + 1.0
15                && y >= bar.track_bounds.y
16                && y <= bar.track_bounds.y + bar.track_bounds.height)
17    });
18    if is_over_scrollbar {
19        strata_platform::window::CursorIcon::Default
20    } else {
21        AppRuntime::hit_test_cursor(&runtime.root_blueprint, &runtime.layout_root, x, y)
22    }
23}
24
25#[inline]
26fn try_retained_canvas3d_redraw_only(
27    runtime: &mut AppRuntime,
28    x: f32,
29    y: f32,
30    needs_rebuild: &mut bool,
31    needs_redraw: &mut bool,
32) {
33    if runtime.target_is_retained_canvas3d(x, y) && !strata_dom::reactive::has_dirty_signals() {
34        runtime.mark_viewport_3d_dirty();
35        *needs_redraw = true;
36    } else {
37        *needs_rebuild = true;
38    }
39}
40
41/// Dispatches a native platform event into the AppRuntime, updating geometry, focus, input, and styling.
42/// Returns `(needs_rebuild, needs_redraw, should_exit)`.
43pub fn dispatch_native_platform_event<E>(
44    event: &strata_platform::PlatformEvent,
45    runtime: &mut AppRuntime,
46    compositor: &mut SwapchainCompositor,
47    presenter: &mut SoftwareSurfacePresenter,
48    event_handler: &mut E,
49) -> (bool, bool, bool)
50where
51    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool,
52{
53    let mut needs_rebuild = false;
54    let mut needs_redraw = false;
55    let mut should_exit = false;
56
57    if event_handler(event, runtime) {
58        needs_rebuild = true;
59    }
60
61    match event {
62        strata_platform::PlatformEvent::WindowCloseRequested => {
63            should_exit = true;
64        }
65        strata_platform::PlatformEvent::WindowResized {
66            width: w,
67            height: h,
68        } => {
69            let wf = (*w).max(1) as f32;
70            let hf = (*h).max(1) as f32;
71            compositor.resize(*w, *h, runtime.dpr);
72            let _ = presenter.resize(*w, *h);
73            let size_changed = (runtime.viewport_width - wf).abs() > 0.01
74                || (runtime.viewport_height - hf).abs() > 0.01;
75            runtime.viewport_width = wf;
76            runtime.viewport_height = hf;
77            if size_changed {
78                let signals_before = strata_dom::reactive::has_dirty_signals();
79                runtime.unapply_scroll_offsets();
80                runtime.layout_root.compute_layout(wf, hf);
81                AppRuntime::sync_layout_to_ir(&runtime.layout_root, &mut runtime.ir_graph);
82                runtime.apply_viewport_clamps();
83                runtime.apply_scroll_offsets();
84                runtime
85                    .spatial_index
86                    .rebuild(&runtime.root_blueprint, &runtime.layout_root);
87                let resize_fired = runtime.dispatch_resize_observers();
88                if resize_fired
89                    || (!signals_before && strata_dom::reactive::has_dirty_signals())
90                {
91                    needs_rebuild = true;
92                }
93                runtime.mark_paint_dirty();
94            }
95            needs_redraw = true;
96        }
97        strata_platform::PlatformEvent::ScaleFactorChanged { scale_factor } => {
98            compositor.resize(
99                runtime.viewport_width as u32,
100                runtime.viewport_height as u32,
101                *scale_factor,
102            );
103            runtime.dpr = *scale_factor;
104            needs_redraw = true;
105        }
106        strata_platform::PlatformEvent::Scroll {
107            delta_x, delta_y, ..
108        } => {
109            let (px, py) = runtime.last_mouse_pos;
110            let (wheel_handled, scroll_changed) =
111                runtime.dispatch_smooth_scroll_action(px, py, *delta_x, *delta_y);
112            if wheel_handled {
113                try_retained_canvas3d_redraw_only(
114                    runtime,
115                    px,
116                    py,
117                    &mut needs_rebuild,
118                    &mut needs_redraw,
119                );
120            }
121            if runtime.dismiss_hover_tooltip_and_repaint() {
122                needs_redraw = true;
123            }
124            if scroll_changed {
125                needs_redraw = true;
126            }
127        }
128        strata_platform::PlatformEvent::AnimationTick => {
129            let scroll_changed = runtime.poll_smooth_scroll();
130            let anim_changed = runtime.poll_animations();
131            let caret_changed = runtime.poll_caret_blink();
132            let tooltip_changed = runtime.poll_hover_tooltip();
133            let mcp_changed = if let Some(ref mut c) = runtime.mcp_cursor {
134                c.poll_tick()
135            } else {
136                false
137            };
138            if scroll_changed || anim_changed || caret_changed || tooltip_changed || mcp_changed {
139                needs_redraw = true;
140            }
141            if strata_dom::reactive::has_dirty_signals() {
142                needs_rebuild = true;
143                needs_redraw = true;
144            }
145        }
146        strata_platform::PlatformEvent::DoubleClick { button, x, y } => {
147            let _btn_idx = button.to_button_index();
148            if *button == strata_platform::MouseButton::Left {
149                let region = AppRuntime::hit_test_property(
150                    &runtime.root_blueprint,
151                    &runtime.layout_root,
152                    *x,
153                    *y,
154                    "data-app-region",
155                );
156                if region.as_deref() == Some("drag") {
157                    needs_rebuild = true;
158                }
159            }
160            if runtime.dispatch_dblclick(*x, *y) {
161                needs_rebuild = true;
162            }
163        }
164        strata_platform::PlatformEvent::MouseDown { button, x, y } => {
165            runtime.last_mouse_pos = (*x, *y);
166            if runtime.dismiss_hover_tooltip_and_repaint() {
167                needs_redraw = true;
168            }
169            let btn_idx = button.to_button_index();
170            if *button == strata_platform::MouseButton::Left {
171                runtime.is_mouse_down = true;
172                if runtime.check_scrollbar_thumb_press(*x, *y) {
173                    needs_redraw = true;
174                } else {
175                    runtime.begin_window_drag_candidate(*x, *y);
176                    let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
177                        *x,
178                        *y,
179                        btn_idx,
180                        Default::default(),
181                    );
182                    if action.needs_rebuild() {
183                        needs_rebuild = true;
184                    } else if action.needs_redraw() {
185                        needs_redraw = true;
186                    }
187                    if runtime.dispatch_mouse_click(*x, *y, btn_idx, 1) {
188                        needs_rebuild = true;
189                    }
190                    if runtime.drain_edit_commands() {
191                        needs_rebuild = true;
192                    }
193                    if !needs_rebuild && runtime.restyle_for_active_change() {
194                        needs_redraw = true;
195                    }
196                }
197            } else if *button == strata_platform::MouseButton::Middle {
198                let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
199                    *x,
200                    *y,
201                    btn_idx,
202                    Default::default(),
203                );
204                if action.needs_rebuild() {
205                    needs_rebuild = true;
206                } else if action.needs_redraw() {
207                    needs_redraw = true;
208                }
209                if runtime.dispatch_mouse_click(*x, *y, btn_idx, 1) {
210                    needs_rebuild = true;
211                }
212            } else if *button == strata_platform::MouseButton::Right {
213                let action = runtime.dispatch_pointer_down_action_with_button_and_modifiers(
214                    *x,
215                    *y,
216                    btn_idx,
217                    Default::default(),
218                );
219                if action.needs_rebuild() {
220                    needs_rebuild = true;
221                } else if action.needs_redraw() {
222                    needs_redraw = true;
223                }
224                if runtime.dispatch_context_menu(*x, *y) {
225                    needs_rebuild = true;
226                }
227                if runtime.drain_edit_commands() {
228                    needs_rebuild = true;
229                }
230            }
231            runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
232        }
233        strata_platform::PlatformEvent::MouseUp { button, x, y } => {
234            runtime.last_mouse_pos = (*x, *y);
235            let btn_idx = button.to_button_index();
236            let was_mouse_down = runtime.is_mouse_down;
237            if *button == strata_platform::MouseButton::Left {
238                runtime.is_mouse_down = false;
239            }
240            if runtime.scrollbar_drag.is_some() {
241                runtime.scrollbar_drag = None;
242                needs_redraw = true;
243            }
244            runtime.pending_window_drag = None;
245            let action = runtime.dispatch_pointer_up_action_with_button(*x, *y, btn_idx);
246            if action.needs_rebuild() {
247                needs_rebuild = true;
248            } else if action.needs_redraw() {
249                needs_redraw = true;
250            }
251            if was_mouse_down && !needs_rebuild && runtime.restyle_for_active_change() {
252                needs_redraw = true;
253            }
254            runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
255        }
256        strata_platform::PlatformEvent::MouseMove { x, y } => {
257            runtime.last_mouse_pos = (*x, *y);
258            if runtime.scrollbar_drag.is_some() {
259                if runtime.poll_scrollbar_drag(*x, *y) {
260                    needs_redraw = true;
261                }
262            } else {
263                if runtime.poll_window_drag(*x, *y) {
264                    needs_rebuild = true;
265                }
266                let action = runtime.dispatch_pointer_move_action(*x, *y);
267                if action.needs_rebuild() {
268                    try_retained_canvas3d_redraw_only(
269                        runtime,
270                        *x,
271                        *y,
272                        &mut needs_rebuild,
273                        &mut needs_redraw,
274                    );
275                } else if action.needs_redraw() {
276                    needs_redraw = true;
277                }
278                if runtime.update_hover(*x, *y) {
279                    if runtime.hover_is_paint_only() {
280                        needs_redraw = true;
281                    } else {
282                        needs_rebuild = true;
283                    }
284                }
285                runtime.current_cursor = resolved_cursor_at(runtime, *x, *y);
286            }
287        }
288        strata_platform::PlatformEvent::CursorLeft => {
289            runtime.is_mouse_down = false;
290            runtime.current_cursor = strata_platform::window::CursorIcon::Default;
291            if runtime.clear_hover() {
292                if runtime.hover_is_paint_only() {
293                    needs_redraw = true;
294                } else {
295                    needs_rebuild = true;
296                }
297            }
298        }
299        strata_platform::PlatformEvent::KeyDown { .. }
300        | strata_platform::PlatformEvent::KeyUp { .. }
301        | strata_platform::PlatformEvent::ReceivedCharacter(_)
302        | strata_platform::PlatformEvent::ImePreedit { .. }
303        | strata_platform::PlatformEvent::ImeCommit(_) => {
304            if matches!(event, strata_platform::PlatformEvent::KeyDown { .. })
305                && runtime.dismiss_hover_tooltip_and_repaint()
306            {
307                needs_redraw = true;
308            }
309            let res = runtime.dispatch_key_event(event);
310            if res.needs_rebuild() {
311                needs_rebuild = true;
312            } else if res.needs_redraw() {
313                needs_redraw = true;
314            }
315        }
316        strata_platform::PlatformEvent::WindowMaximized(_) => {
317            needs_rebuild = true;
318            needs_redraw = true;
319        }
320        strata_platform::PlatformEvent::FileDrop(paths) => {
321            if runtime.dismiss_hover_tooltip_and_repaint() {
322                needs_redraw = true;
323            }
324            if runtime.dispatch_file_drop(paths.clone()) {
325                needs_rebuild = true;
326                needs_redraw = true;
327            }
328        }
329        _ => {}
330    }
331
332    (needs_rebuild, needs_redraw, should_exit)
333}
334
335impl AppRuntime {
336    /// Dynamically computes the appropriate thread sleep duration based on runtime activity state.
337    /// Returns:
338    /// - 16ms if animations, smooth scrolling, scrollbar dragging, window dragging, active interactions, or dirty signals are active.
339    /// - 50ms if a text input currently has active focus (to service responsive caret blinking).
340    /// - 200ms if the runtime is completely idle (dropping CPU consumption to ~0%).
341    pub fn compute_idle_sleep(&self, was_active: bool) -> std::time::Duration {
342        let mut base = if was_active
343            || self.has_active_animations()
344            || self.active_smooth_scroll.is_some()
345            || self.scrollbar_drag.is_some()
346            || self.pending_window_drag.is_some()
347            || self.drag_selection_active
348            || strata_dom::reactive::has_dirty_signals()
349        {
350            std::time::Duration::from_millis(16)
351        } else if self.active_focus.is_some() {
352            std::time::Duration::from_millis(50)
353        } else {
354            std::time::Duration::from_millis(200)
355        };
356        if let Some(due) = self.next_hover_tooltip_due_in() {
357            base = base.min(due.max(std::time::Duration::from_millis(1)));
358        }
359        if let Some(next_timer) = strata_js::web::time_until_next_timer() {
360            base.min(next_timer.max(std::time::Duration::from_millis(1)))
361        } else {
362            base
363        }
364    }
365}
366
367/// Runs an interactive native window event loop against any NativeSurfaceWindow implementation,
368/// pacing frames, evaluating reactive updates, and blitting pixel buffers.
369pub fn run_native_surface_window_loop<W, F, E>(
370    mut window: W,
371    mut root_fn: Option<F>,
372    mut runtime: AppRuntime,
373    mut compositor: SwapchainCompositor,
374    mut presenter: SoftwareSurfacePresenter,
375    mut event_handler: E,
376    mut hooks: Option<&mut AppLifecycleHooks>,
377) -> Result<(), String>
378where
379    W: strata_platform::NativeSurfaceWindow,
380    F: FnMut() -> View + 'static,
381    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool,
382{
383    // Initial frame presentation
384    let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
385    let _ = window.present_surface(&presenter.backing_buffer, presenter.width, presenter.height);
386
387    if let Some(ref mut h) = hooks {
388        if let Some(ref mut on_start) = h.on_start {
389            on_start(&mut runtime);
390        }
391        if let Some(ref mut on_frame) = h.on_frame {
392            let info = AppFrameInfo {
393                frame_index: runtime.frame_count,
394                viewport_width: runtime.viewport_width,
395                viewport_height: runtime.viewport_height,
396                dpr: runtime.dpr,
397                command_count: runtime.display_list.commands.len(),
398                dt: std::time::Duration::ZERO,
399            };
400            on_frame(info, &mut runtime);
401        }
402    }
403
404    println!(
405        "[OK] Strata Native Window active: {}x{} physical pixels, {} commands presented.",
406        presenter.width,
407        presenter.height,
408        runtime.display_list.commands.len()
409    );
410
411    let max_headless_frames = if std::env::var("STRATA_HEADLESS").is_ok() {
412        Some(
413            std::env::var("STRATA_HEADLESS_EXIT_AFTER_FRAMES")
414                .ok()
415                .and_then(|s| s.parse::<u64>().ok())
416                .unwrap_or(1),
417        )
418    } else {
419        None
420    };
421    let mut frames_presented: u64 = 1;
422
423    let mut running = true;
424    while running {
425        if let Some(max_f) = max_headless_frames {
426            if frames_presented >= max_f {
427                break;
428            }
429        }
430        let events = window.poll_events();
431        let mut needs_rebuild = false;
432        let mut needs_redraw = false;
433
434        for event in events {
435            if let Some(ref mut h) = hooks {
436                if let Some(ref mut on_ev) = h.on_event {
437                    let mut control = strata_platform::EventLoopControl::Poll;
438                    on_ev(&event, &mut control, &mut runtime);
439                    if control == strata_platform::EventLoopControl::Exit {
440                        running = false;
441                        break;
442                    }
443                }
444            }
445
446            if let strata_platform::PlatformEvent::DoubleClick { button, x, y } = event {
447                if button == strata_platform::MouseButton::Left {
448                    let region = AppRuntime::hit_test_property(
449                        &runtime.root_blueprint,
450                        &runtime.layout_root,
451                        x,
452                        y,
453                        "data-app-region",
454                    );
455                    let is_drag = region.as_deref() == Some("drag")
456                        || runtime.csd_controller.hit_test(
457                            runtime.viewport_width,
458                            runtime.viewport_height,
459                            x,
460                            y,
461                        ) == strata_platform::CsdHitZone::Titlebar;
462                    if is_drag {
463                        let is_max = window.toggle_maximize();
464                        window.push_event(strata_platform::PlatformEvent::WindowMaximized(is_max));
465                        needs_rebuild = true;
466                    }
467                }
468            }
469
470            if let strata_platform::PlatformEvent::MouseMove { x, y } = event {
471                if runtime.poll_window_drag(x, y) {
472                    window.begin_window_drag(x, y);
473                }
474            }
475
476            let (reb, red, exit) = dispatch_native_platform_event(
477                &event,
478                &mut runtime,
479                &mut compositor,
480                &mut presenter,
481                &mut event_handler,
482            );
483            if reb {
484                needs_rebuild = true;
485            }
486            if red {
487                needs_redraw = true;
488            }
489            if exit {
490                running = false;
491                break;
492            }
493        }
494
495        if !running {
496            break;
497        }
498
499        let anim_changed = runtime.poll_animations();
500        let scroll_changed = runtime.poll_smooth_scroll();
501        let caret_changed = runtime.poll_caret_blink();
502        let timers_fired = strata_js::web::tick_timers() > 0;
503        if timers_fired || strata_dom::reactive::has_dirty_signals() {
504            needs_rebuild = true;
505            needs_redraw = true;
506        }
507        if anim_changed || scroll_changed || caret_changed {
508            needs_redraw = true;
509        }
510
511        if needs_rebuild {
512            if runtime.sync_dynamic_text() {
513                let _ = strata_dom::reactive::take_dirty_signals();
514                runtime.mark_paint_dirty();
515                let _ = runtime.render_frame(&mut compositor, &mut presenter, false);
516                let _ = window.present_surface(
517                    &presenter.backing_buffer,
518                    presenter.width,
519                    presenter.height,
520                );
521            } else if let Some(ref mut f) = root_fn {
522                let _ = strata_dom::reactive::take_dirty_signals();
523                runtime.update_root(|| f());
524                let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
525                let _ = window.present_surface(
526                    &presenter.backing_buffer,
527                    presenter.width,
528                    presenter.height,
529                );
530            } else {
531                runtime.mark_paint_dirty();
532                let _ = runtime.render_frame(&mut compositor, &mut presenter, true);
533                let _ = window.present_surface(
534                    &presenter.backing_buffer,
535                    presenter.width,
536                    presenter.height,
537                );
538            }
539        } else if needs_redraw || window.is_redraw_requested() {
540            // A per-frame draw hook edits the display list, so it forces a fresh compile.
541            let has_draw_hook = hooks.as_ref().is_some_and(|h| h.on_draw.is_some());
542            if needs_redraw || has_draw_hook {
543                runtime.mark_paint_dirty();
544            }
545            if let Some(ref mut h) = hooks {
546                if let Some(ref mut on_draw) = h.on_draw {
547                    on_draw(&mut runtime.display_list);
548                }
549            }
550            let _ = runtime.render_frame(&mut compositor, &mut presenter, false);
551            let _ = window.present_surface(
552                &presenter.backing_buffer,
553                presenter.width,
554                presenter.height,
555            );
556            if let Some(ref mut h) = hooks {
557                if let Some(ref mut on_frame) = h.on_frame {
558                    let info = AppFrameInfo {
559                        frame_index: runtime.frame_count,
560                        viewport_width: runtime.viewport_width,
561                        viewport_height: runtime.viewport_height,
562                        dpr: runtime.dpr,
563                        command_count: runtime.display_list.commands.len(),
564                        dt: std::time::Duration::ZERO,
565                    };
566                    on_frame(info, &mut runtime);
567                }
568            }
569        }
570
571        frames_presented += 1;
572        if max_headless_frames.is_none() {
573            let is_active = anim_changed
574                || scroll_changed
575                || caret_changed
576                || timers_fired
577                || needs_rebuild
578                || needs_redraw;
579            std::thread::sleep(runtime.compute_idle_sleep(is_active));
580        }
581    }
582
583    if let Some(ref mut h) = hooks {
584        if let Some(ref mut on_exit) = h.on_exit {
585            on_exit(&mut runtime);
586        }
587    }
588
589    Ok(())
590}
591
592#[cfg(target_os = "linux")]
593pub fn launch_x11<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
594where
595    F: FnMut() -> View + 'static,
596{
597    launch_x11_with_events(root_fn, config, |_, _| false)
598}
599
600#[cfg(target_os = "linux")]
601/// Launches an interactive native X11 desktop window on Linux with a custom event hook.
602pub fn launch_x11_with_events<F, E>(
603    mut root_fn: F,
604    config: WindowConfig,
605    event_handler: E,
606) -> Result<(), String>
607where
608    F: FnMut() -> View + 'static,
609    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
610{
611    println!(
612        "[+] Launching Strata Native Desktop Window on X11: \"{}\"...",
613        config.title
614    );
615    let x11_window = strata_platform::x11_window::X11Window::new(config.clone())?;
616    let view = strata_dom::with_root_component_scope(|| root_fn());
617    let width = config.width as f32;
618    let height = config.height as f32;
619    let mut runtime = AppRuntime::new(view, width, height, 1.0);
620    runtime.apply_window_config(&config);
621    let presenter =
622        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
623    let compositor = SwapchainCompositor::new(config.width, config.height, 1.0);
624
625    run_native_surface_window_loop(
626        x11_window,
627        Some(root_fn),
628        runtime,
629        compositor,
630        presenter,
631        event_handler,
632        None,
633    )
634}
635
636#[cfg(target_os = "linux")]
637/// Launches an interactive native Wayland desktop window on Linux displaying the Strata application.
638pub fn launch_wayland<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
639where
640    F: FnMut() -> View + 'static,
641{
642    launch_wayland_with_events(root_fn, config, |_, _| false)
643}
644
645#[cfg(target_os = "linux")]
646/// Launches an interactive native Wayland desktop window on Linux with a custom event hook.
647pub fn launch_wayland_with_events<F, E>(
648    mut root_fn: F,
649    config: WindowConfig,
650    event_handler: E,
651) -> Result<(), String>
652where
653    F: FnMut() -> View + 'static,
654    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
655{
656    println!(
657        "[+] Launching Strata Native Desktop Window on Wayland: \"{}\"...",
658        config.title
659    );
660    let wayland_window = strata_platform::WaylandWindow::new(config.clone())?;
661    let view = strata_dom::with_root_component_scope(|| root_fn());
662    let width = config.width as f32;
663    let height = config.height as f32;
664    let dpr = wayland_window.scale_factor();
665    let mut runtime = AppRuntime::new(view, width, height, dpr);
666    runtime.apply_window_config(&config);
667    let presenter =
668        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
669    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);
670
671    run_native_surface_window_loop(
672        wayland_window,
673        Some(root_fn),
674        runtime,
675        compositor,
676        presenter,
677        event_handler,
678        None,
679    )
680}
681
682#[cfg(windows)]
683/// Launches an interactive native Win32 desktop window on Windows displaying the Strata application.
684pub fn launch_win32<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
685where
686    F: FnMut() -> View + 'static,
687{
688    launch_win32_with_events(root_fn, config, |_, _| false)
689}
690
691#[cfg(windows)]
692/// Launches an interactive native Win32 desktop window on Windows with a custom event hook.
693pub fn launch_win32_with_events<F, E>(
694    mut root_fn: F,
695    config: WindowConfig,
696    event_handler: E,
697) -> Result<(), String>
698where
699    F: FnMut() -> View + 'static,
700    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
701{
702    println!(
703        "[+] Launching Strata Native Desktop Window on Windows (Win32): \"{}\"...",
704        config.title
705    );
706    let win32_window = strata_platform::Win32Window::new(config.clone())?;
707    let view = strata_dom::with_root_component_scope(|| root_fn());
708    let width = config.width as f32;
709    let height = config.height as f32;
710    let dpr = win32_window.scale_factor();
711    let mut runtime = AppRuntime::new(view, width, height, dpr);
712    runtime.apply_window_config(&config);
713    let presenter =
714        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
715    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);
716
717    run_native_surface_window_loop(
718        win32_window,
719        Some(root_fn),
720        runtime,
721        compositor,
722        presenter,
723        event_handler,
724        None,
725    )
726}
727
728#[cfg(target_os = "macos")]
729/// Launches an interactive native macOS Cocoa desktop window displaying the Strata application.
730pub fn launch_macos<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
731where
732    F: FnMut() -> View + 'static,
733{
734    launch_macos_with_events(root_fn, config, |_, _| false)
735}
736
737#[cfg(target_os = "macos")]
738/// Launches an interactive native macOS Cocoa desktop window with a custom event hook.
739pub fn launch_macos_with_events<F, E>(
740    mut root_fn: F,
741    config: WindowConfig,
742    event_handler: E,
743) -> Result<(), String>
744where
745    F: FnMut() -> View + 'static,
746    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
747{
748    println!(
749        "[+] Launching Strata Native Desktop Window on macOS (Cocoa): \"{}\"...",
750        config.title
751    );
752    let macos_window = strata_platform::MacOsWindow::new(config.clone())?;
753    let view = strata_dom::with_root_component_scope(|| root_fn());
754    let width = config.width as f32;
755    let height = config.height as f32;
756    let dpr = macos_window.scale_factor();
757    let mut runtime = AppRuntime::new(view, width, height, dpr);
758    runtime.apply_window_config(&config);
759    let presenter =
760        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
761    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);
762
763    run_native_surface_window_loop(
764        macos_window,
765        Some(root_fn),
766        runtime,
767        compositor,
768        presenter,
769        event_handler,
770        None,
771    )
772}
773
774#[cfg(target_os = "android")]
775/// Launches an interactive native Android application window via Android NDK NativeActivity.
776pub fn launch_android<F>(
777    native_window: *mut std::ffi::c_void,
778    root_fn: F,
779    config: WindowConfig,
780) -> Result<(), String>
781where
782    F: FnMut() -> View + 'static,
783{
784    launch_android_with_events(native_window, root_fn, config, |_, _| false)
785}
786
787#[cfg(target_os = "android")]
788/// Launches an interactive native Android application window with a custom event hook.
789pub fn launch_android_with_events<F, E>(
790    native_window: *mut std::ffi::c_void,
791    mut root_fn: F,
792    config: WindowConfig,
793    event_handler: E,
794) -> Result<(), String>
795where
796    F: FnMut() -> View + 'static,
797    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
798{
799    let android_window = strata_platform::AndroidWindow::new(native_window, config.clone())?;
800    let view = strata_dom::with_root_component_scope(|| root_fn());
801    let width = config.width as f32;
802    let height = config.height as f32;
803    let dpr = android_window.scale_factor();
804    let mut runtime = AppRuntime::new(view, width, height, dpr);
805    runtime.apply_window_config(&config);
806    let presenter =
807        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
808    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);
809
810    run_native_surface_window_loop(
811        android_window,
812        Some(root_fn),
813        runtime,
814        compositor,
815        presenter,
816        event_handler,
817        None,
818    )
819}
820
821#[cfg(target_os = "ios")]
822/// Launches an interactive native iOS application window via UIKit and CAMetalLayer.
823pub fn launch_ios<F>(root_fn: F, config: WindowConfig) -> Result<(), String>
824where
825    F: FnMut() -> View + 'static,
826{
827    launch_ios_with_events(root_fn, config, |_, _| false)
828}
829
830#[cfg(target_os = "ios")]
831/// Launches an interactive native iOS application window with a custom event hook.
832pub fn launch_ios_with_events<F, E>(
833    mut root_fn: F,
834    config: WindowConfig,
835    event_handler: E,
836) -> Result<(), String>
837where
838    F: FnMut() -> View + 'static,
839    E: FnMut(&strata_platform::PlatformEvent, &mut AppRuntime) -> bool + 'static,
840{
841    let ios_window = strata_platform::IosWindow::new(config.clone())?;
842    let view = strata_dom::with_root_component_scope(|| root_fn());
843    let width = config.width as f32;
844    let height = config.height as f32;
845    let dpr = ios_window.scale_factor();
846    let mut runtime = AppRuntime::new(view, width, height, dpr);
847    runtime.apply_window_config(&config);
848    let presenter =
849        SoftwareSurfacePresenter::new(config.width, config.height, SwapchainFormat::Bgra8Unorm);
850    let compositor = SwapchainCompositor::new(config.width, config.height, dpr);
851
852    run_native_surface_window_loop(
853        ios_window,
854        Some(root_fn),
855        runtime,
856        compositor,
857        presenter,
858        event_handler,
859        None,
860    )
861}