1use crate::{Application, BackgroundExecutor, Platform};
8use std::rc::Rc;
9
10pub fn background_executor() -> BackgroundExecutor {
12 current_platform(true).background_executor()
13}
14
15pub fn application() -> Application {
17 #[cfg(target_family = "wasm")]
18 {
19 application_with_web_backend(gpui_web::WebBackendPreference::Auto)
20 }
21
22 #[cfg(not(target_family = "wasm"))]
23 Application::with_platform(current_platform(false))
24}
25
26pub fn headless() -> Application {
28 Application::with_platform(current_platform(true))
29}
30
31#[cfg(target_family = "wasm")]
32pub use gpui_web::WebBackendPreference;
33
34#[cfg(target_os = "windows")]
39pub use gpui_windows::{DirectXImportedTexture, DirectXRenderer, DirectXTextureExt};
40
41#[cfg(target_os = "macos")]
46pub use gpui_macos::MetalTextureExt;
47
48#[cfg(any(target_os = "linux", target_os = "freebsd"))]
53pub use gpui_linux::ImportedTextureExt;
54
55#[cfg(target_family = "wasm")]
56pub fn application_with_web_backend(backend_preference: WebBackendPreference) -> Application {
57 let platform = Rc::new(gpui_web::WebPlatform::new_with_backend(
58 true,
59 backend_preference,
60 ));
61 let http_client = std::sync::Arc::new(platform.fetch_http_client());
62 Application::with_platform(platform).with_http_client(http_client)
63}
64
65#[cfg(target_family = "wasm")]
67pub fn single_threaded_web() -> Application {
68 let platform = Rc::new(gpui_web::WebPlatform::new(false));
69 let http_client = std::sync::Arc::new(platform.fetch_http_client());
70 Application::with_platform(platform).with_http_client(http_client)
71}
72
73#[cfg(target_family = "wasm")]
77pub fn web_init() {
78 gpui_web::init_logging();
79}
80
81pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
83 #[cfg(target_os = "macos")]
84 {
85 Rc::new(gpui_macos::MacPlatform::new(headless))
86 }
87
88 #[cfg(target_os = "windows")]
89 {
90 Rc::new(
91 gpui_windows::WindowsPlatform::new(headless)
92 .expect("failed to initialize Windows platform"),
93 )
94 }
95
96 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
97 {
98 gpui_linux::current_platform(headless)
99 }
100
101 #[cfg(target_family = "wasm")]
102 {
103 let _ = headless;
104 Rc::new(gpui_web::WebPlatform::new(true))
105 }
106}
107
108#[cfg(any(feature = "bench-support", feature = "test-support"))]
110pub fn current_headless_renderer() -> anyhow::Result<Option<Box<dyn crate::SceneRenderer>>> {
111 #[cfg(target_os = "macos")]
112 {
113 Ok(Some(Box::new(
114 gpui_macos::metal_renderer::MetalHeadlessRenderer::new(),
115 )))
116 }
117
118 #[cfg(all(
119 feature = "platform",
120 any(target_os = "linux", target_os = "freebsd")
121 ))]
122 {
123 gpui_linux::WgpuHeadlessRenderer::new()
126 .map(|renderer| Some(Box::new(renderer) as Box<dyn crate::SceneRenderer>))
127 }
128
129 #[cfg(not(any(
130 target_os = "macos",
131 all(feature = "platform", any(target_os = "linux", target_os = "freebsd")),
132 )))]
133 {
134 Ok(None)
135 }
136}
137
138#[cfg(feature = "bench-support")]
146pub fn bench_text_system() -> std::sync::Arc<dyn crate::PlatformTextSystem> {
147 thread_local! {
148 static TEXT_SYSTEM: std::sync::Arc<dyn crate::PlatformTextSystem> =
149 current_platform(true).text_system();
150 }
151 TEXT_SYSTEM.with(|text_system| text_system.clone())
152}
153
154#[cfg(all(test, target_os = "macos"))]
155mod tests {
156 use super::*;
157 use crate::{AppContext, Empty, VisualTestAppContext};
158 use std::cell::RefCell;
159 use std::time::Duration;
160
161 #[test]
169 #[ignore] fn test_foreground_tasks_run_with_run_until_parked() {
171 let mut cx = VisualTestAppContext::new(current_platform(false));
172
173 let task_ran = Rc::new(RefCell::new(false));
174
175 {
178 let task_ran = task_ran.clone();
179 cx.update(|cx| {
180 cx.spawn(async move |_| {
181 *task_ran.borrow_mut() = true;
182 })
183 .detach();
184 });
185 }
186
187 assert!(!*task_ran.borrow());
189
190 cx.run_until_parked();
192
193 assert!(*task_ran.borrow());
195 }
196
197 #[test]
198 #[ignore] fn test_advance_clock_triggers_delayed_tasks() {
200 let mut cx = VisualTestAppContext::new(current_platform(false));
201
202 let task_ran = Rc::new(RefCell::new(false));
203
204 {
206 let task_ran = task_ran.clone();
207 let executor = cx.background_executor.clone();
208 cx.update(|cx| {
209 cx.spawn(async move |_| {
210 executor.timer(Duration::from_millis(500)).await;
211 *task_ran.borrow_mut() = true;
212 })
213 .detach();
214 });
215 }
216
217 cx.run_until_parked();
219 assert!(!*task_ran.borrow());
220
221 cx.advance_clock(Duration::from_millis(600));
223
224 assert!(*task_ran.borrow());
226 }
227
228 #[test]
229 #[ignore] fn test_window_spawn_uses_test_dispatcher() {
231 let mut cx = VisualTestAppContext::new(current_platform(false));
232
233 let task_ran = Rc::new(RefCell::new(false));
234
235 let window = cx
236 .open_offscreen_window_default(|_, cx| cx.new(|_| Empty))
237 .expect("Failed to open window");
238
239 {
242 let task_ran = task_ran.clone();
243 cx.update_window(window.into(), |_, window, cx| {
244 window
245 .spawn(cx, async move |_| {
246 *task_ran.borrow_mut() = true;
247 })
248 .detach();
249 })
250 .ok();
251 }
252
253 assert!(!*task_ran.borrow());
255
256 cx.run_until_parked();
258
259 assert!(*task_ran.borrow());
261 }
262}