guic_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 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#[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#[cfg(target_family = "wasm")]
40pub fn web_init() {
41 console_error_panic_hook::set_once();
42 gpui_web::init_logging();
43}
44
45pub 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#[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 #[test]
103 #[ignore] 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 {
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 assert!(!*task_ran.borrow());
123
124 cx.run_until_parked();
126
127 assert!(*task_ran.borrow());
129 }
130
131 #[test]
132 #[ignore] 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 {
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 cx.run_until_parked();
153 assert!(!*task_ran.borrow());
154
155 cx.advance_clock(Duration::from_millis(600));
157
158 assert!(*task_ran.borrow());
160 }
161
162 #[test]
163 #[ignore] 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 {
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 assert!(!*task_ran.borrow());
189
190 cx.run_until_parked();
192
193 assert!(*task_ran.borrow());
195 }
196}