Skip to main content

guic_gpui_platform/
gpui_platform.rs

1//! Convenience crate that re-exports GPUI's platform traits and the
2//! `current_platform` constructor so consumers don't need `#[cfg]` gating.
3
4pub use gpui::Platform;
5
6use std::rc::Rc;
7
8/// Returns a background executor for the current platform.
9pub fn background_executor() -> gpui::BackgroundExecutor {
10    current_platform(true).background_executor()
11}
12
13pub fn application() -> gpui::Application {
14    #[cfg(target_family = "wasm")]
15    {
16        let platform = Rc::new(gpui_web::WebPlatform::new(true));
17        let http_client = std::sync::Arc::new(platform.fetch_http_client());
18        gpui::Application::with_platform(platform).with_http_client(http_client)
19    }
20
21    #[cfg(not(target_family = "wasm"))]
22    gpui::Application::with_platform(current_platform(false))
23}
24
25pub fn headless() -> gpui::Application {
26    gpui::Application::with_platform(current_platform(true))
27}
28
29/// Unlike `application`, this function returns a single-threaded web application.
30#[cfg(target_family = "wasm")]
31pub fn single_threaded_web() -> gpui::Application {
32    let platform = Rc::new(gpui_web::WebPlatform::new(false));
33    let http_client = std::sync::Arc::new(platform.fetch_http_client());
34    gpui::Application::with_platform(platform).with_http_client(http_client)
35}
36
37/// Initializes panic hooks and logging for the web platform.
38/// Call this before running the application in a wasm_bindgen entrypoint.
39#[cfg(target_family = "wasm")]
40pub fn web_init() {
41    console_error_panic_hook::set_once();
42    gpui_web::init_logging();
43}
44
45/// Returns the default [`Platform`] for the current OS.
46pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
47    #[cfg(target_os = "macos")]
48    {
49        Rc::new(gpui_macos::MacPlatform::new(headless))
50    }
51
52    #[cfg(target_os = "windows")]
53    {
54        Rc::new(
55            gpui_windows::WindowsPlatform::new(headless)
56                .expect("failed to initialize Windows platform"),
57        )
58    }
59
60    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
61    {
62        gpui_linux::current_platform(headless)
63    }
64
65    #[cfg(target_family = "wasm")]
66    {
67        let _ = headless;
68        Rc::new(gpui_web::WebPlatform::new(true))
69    }
70}
71
72/// Returns a new [`gpui::PlatformHeadlessRenderer`] for the current platform, if available.
73#[cfg(feature = "test-support")]
74pub fn current_headless_renderer() -> Option<Box<dyn gpui::PlatformHeadlessRenderer>> {
75    #[cfg(target_os = "macos")]
76    {
77        Some(Box::new(
78            gpui_macos::metal_renderer::MetalHeadlessRenderer::new(),
79        ))
80    }
81
82    #[cfg(not(target_os = "macos"))]
83    {
84        None
85    }
86}
87
88#[cfg(all(test, target_os = "macos"))]
89mod tests {
90    use super::*;
91    use gpui::{AppContext, Empty, VisualTestAppContext};
92    use std::cell::RefCell;
93    use std::time::Duration;
94
95    // Note: All VisualTestAppContext tests are ignored by default because they require
96    // the macOS main thread. Standard Rust tests run on worker threads, which causes
97    // SIGABRT when interacting with macOS AppKit/Cocoa APIs.
98    //
99    // To run these tests, use:
100    // cargo test -p gpui visual_test_context -- --ignored --test-threads=1
101
102    #[test]
103    #[ignore] // Requires macOS main thread
104    fn test_foreground_tasks_run_with_run_until_parked() {
105        let mut cx = VisualTestAppContext::new(current_platform(false));
106
107        let task_ran = Rc::new(RefCell::new(false));
108
109        // Spawn a foreground task via the App's spawn method
110        // This should use our TestDispatcher, not the MacDispatcher
111        {
112            let task_ran = task_ran.clone();
113            cx.update(|cx| {
114                cx.spawn(async move |_| {
115                    *task_ran.borrow_mut() = true;
116                })
117                .detach();
118            });
119        }
120
121        // The task should not have run yet
122        assert!(!*task_ran.borrow());
123
124        // Run until parked should execute the foreground task
125        cx.run_until_parked();
126
127        // Now the task should have run
128        assert!(*task_ran.borrow());
129    }
130
131    #[test]
132    #[ignore] // Requires macOS main thread
133    fn test_advance_clock_triggers_delayed_tasks() {
134        let mut cx = VisualTestAppContext::new(current_platform(false));
135
136        let task_ran = Rc::new(RefCell::new(false));
137
138        // Spawn a task that waits for a timer
139        {
140            let task_ran = task_ran.clone();
141            let executor = cx.background_executor.clone();
142            cx.update(|cx| {
143                cx.spawn(async move |_| {
144                    executor.timer(Duration::from_millis(500)).await;
145                    *task_ran.borrow_mut() = true;
146                })
147                .detach();
148            });
149        }
150
151        // Run until parked - the task should be waiting on the timer
152        cx.run_until_parked();
153        assert!(!*task_ran.borrow());
154
155        // Advance clock past the timer duration
156        cx.advance_clock(Duration::from_millis(600));
157
158        // Now the task should have completed
159        assert!(*task_ran.borrow());
160    }
161
162    #[test]
163    #[ignore] // Requires macOS main thread - window creation fails on test threads
164    fn test_window_spawn_uses_test_dispatcher() {
165        let mut cx = VisualTestAppContext::new(current_platform(false));
166
167        let task_ran = Rc::new(RefCell::new(false));
168
169        let window = cx
170            .open_offscreen_window_default(|_, cx| cx.new(|_| Empty))
171            .expect("Failed to open window");
172
173        // Spawn a task via window.spawn - this is the critical test case
174        // for tooltip behavior, as tooltips use window.spawn for delayed show
175        {
176            let task_ran = task_ran.clone();
177            cx.update_window(window.into(), |_, window, cx| {
178                window
179                    .spawn(cx, async move |_| {
180                        *task_ran.borrow_mut() = true;
181                    })
182                    .detach();
183            })
184            .ok();
185        }
186
187        // The task should not have run yet
188        assert!(!*task_ran.borrow());
189
190        // Run until parked should execute the foreground task spawned via window
191        cx.run_until_parked();
192
193        // Now the task should have run
194        assert!(*task_ran.borrow());
195    }
196}