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