gpui_platform/
gpui_platform.rs1pub use gpui::Platform;
5
6use std::rc::Rc;
7
8pub 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 application_with_web_backend(gpui_web::WebBackendPreference::Auto)
17 }
18
19 #[cfg(not(target_family = "wasm"))]
20 gpui::Application::with_platform(current_platform(false))
21}
22
23pub fn headless() -> gpui::Application {
24 gpui::Application::with_platform(current_platform(true))
25}
26
27#[cfg(any(target_os = "linux", target_os = "freebsd"))]
33pub fn linux(allowed_modes: gpui::WindowingModes) -> gpui::Application {
34 gpui::Application::with_platform(gpui_linux::linux_platform(allowed_modes))
35}
36
37#[cfg(target_family = "wasm")]
38pub use gpui_web::WebBackendPreference;
39
40#[cfg(target_family = "wasm")]
41pub fn application_with_web_backend(backend_preference: WebBackendPreference) -> gpui::Application {
42 let platform = Rc::new(gpui_web::WebPlatform::new_with_backend(
43 true,
44 backend_preference,
45 ));
46 let http_client = std::sync::Arc::new(platform.fetch_http_client());
47 gpui::Application::with_platform(platform).with_http_client(http_client)
48}
49
50#[cfg(target_family = "wasm")]
52pub fn single_threaded_web() -> gpui::Application {
53 let platform = Rc::new(gpui_web::WebPlatform::new(false));
54 let http_client = std::sync::Arc::new(platform.fetch_http_client());
55 gpui::Application::with_platform(platform).with_http_client(http_client)
56}
57
58#[cfg(target_family = "wasm")]
61pub fn web_init() {
62 console_error_panic_hook::set_once();
63 gpui_web::init_logging();
64}
65
66pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
68 #[cfg(target_os = "macos")]
69 {
70 Rc::new(gpui_macos::MacPlatform::new(headless))
71 }
72
73 #[cfg(target_os = "windows")]
74 {
75 Rc::new(
76 gpui_windows::WindowsPlatform::new(headless)
77 .expect("failed to initialize Windows platform"),
78 )
79 }
80
81 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
82 {
83 gpui_linux::current_platform(headless)
84 }
85
86 #[cfg(target_family = "wasm")]
87 {
88 let _ = headless;
89 Rc::new(gpui_web::WebPlatform::new(true))
90 }
91}
92
93#[cfg(feature = "bench-support")]
101pub fn bench_text_system() -> std::sync::Arc<dyn gpui::PlatformTextSystem> {
102 thread_local! {
103 static TEXT_SYSTEM: std::sync::Arc<dyn gpui::PlatformTextSystem> =
104 current_platform(true).text_system();
105 }
106 TEXT_SYSTEM.with(|text_system| text_system.clone())
107}
108
109#[cfg(any(feature = "bench-support", feature = "test-support"))]
111pub fn current_headless_renderer() -> anyhow::Result<Option<Box<dyn gpui::PlatformHeadlessRenderer>>>
112{
113 #[cfg(target_os = "macos")]
114 {
115 Ok(Some(Box::new(
116 gpui_macos::metal_renderer::MetalHeadlessRenderer::new(),
117 )))
118 }
119
120 #[cfg(target_os = "linux")]
121 {
122 gpui_wgpu::WgpuHeadlessRenderer::new()
123 .map(|renderer| Some(Box::new(renderer) as Box<dyn gpui::PlatformHeadlessRenderer>))
124 }
125
126 #[cfg(not(any(target_os = "macos", target_os = "linux")))]
127 {
128 Ok(None)
129 }
130}
131
132#[cfg(all(test, target_os = "macos"))]
133mod tests {
134 use super::*;
135 use gpui::{AppContext, Empty, VisualTestAppContext};
136 use std::cell::RefCell;
137 use std::time::Duration;
138
139 #[test]
147 #[ignore] fn test_foreground_tasks_run_with_run_until_parked() {
149 let mut cx = VisualTestAppContext::new(current_platform(false));
150
151 let task_ran = Rc::new(RefCell::new(false));
152
153 {
156 let task_ran = task_ran.clone();
157 cx.update(|cx| {
158 cx.spawn(async move |_| {
159 *task_ran.borrow_mut() = true;
160 })
161 .detach();
162 });
163 }
164
165 assert!(!*task_ran.borrow());
167
168 cx.run_until_parked();
170
171 assert!(*task_ran.borrow());
173 }
174
175 #[test]
176 #[ignore] fn test_advance_clock_triggers_delayed_tasks() {
178 let mut cx = VisualTestAppContext::new(current_platform(false));
179
180 let task_ran = Rc::new(RefCell::new(false));
181
182 {
184 let task_ran = task_ran.clone();
185 let executor = cx.background_executor.clone();
186 cx.update(|cx| {
187 cx.spawn(async move |_| {
188 executor.timer(Duration::from_millis(500)).await;
189 *task_ran.borrow_mut() = true;
190 })
191 .detach();
192 });
193 }
194
195 cx.run_until_parked();
197 assert!(!*task_ran.borrow());
198
199 cx.advance_clock(Duration::from_millis(600));
201
202 assert!(*task_ran.borrow());
204 }
205
206 #[test]
207 #[ignore] fn test_window_spawn_uses_test_dispatcher() {
209 let mut cx = VisualTestAppContext::new(current_platform(false));
210
211 let task_ran = Rc::new(RefCell::new(false));
212
213 let window = cx
214 .open_offscreen_window_default(|_, cx| cx.new(|_| Empty))
215 .expect("Failed to open window");
216
217 {
220 let task_ran = task_ran.clone();
221 cx.update_window(window.into(), |_, window, cx| {
222 window
223 .spawn(cx, async move |_| {
224 *task_ran.borrow_mut() = true;
225 })
226 .detach();
227 })
228 .ok();
229 }
230
231 assert!(!*task_ran.borrow());
233
234 cx.run_until_parked();
236
237 assert!(*task_ran.borrow());
239 }
240}