pub use gpui::Platform;
use std::rc::Rc;
pub fn background_executor() -> gpui::BackgroundExecutor {
current_platform(true).background_executor()
}
pub fn application() -> gpui::Application {
#[cfg(target_family = "wasm")]
{
application_with_web_backend(gpui_web::WebBackendPreference::Auto)
}
#[cfg(not(target_family = "wasm"))]
with_http_client(gpui::Application::with_platform(current_platform(false)))
}
pub fn headless() -> gpui::Application {
#[cfg(target_family = "wasm")]
{
gpui::Application::with_platform(current_platform(true))
}
#[cfg(not(target_family = "wasm"))]
with_http_client(gpui::Application::with_platform(current_platform(true)))
}
#[cfg(not(target_family = "wasm"))]
fn with_http_client(app: gpui::Application) -> gpui::Application {
app.with_http_client(std::sync::Arc::new(reqwest_client::ReqwestClient::new()))
}
#[cfg(target_family = "wasm")]
pub use gpui_web::WebBackendPreference;
#[cfg(target_family = "wasm")]
pub fn application_with_web_backend(backend_preference: WebBackendPreference) -> gpui::Application {
let platform = Rc::new(gpui_web::WebPlatform::new_with_backend(
true,
backend_preference,
));
let http_client = std::sync::Arc::new(platform.fetch_http_client());
gpui::Application::with_platform(platform).with_http_client(http_client)
}
#[cfg(target_family = "wasm")]
pub fn single_threaded_web() -> gpui::Application {
let platform = Rc::new(gpui_web::WebPlatform::new(false));
let http_client = std::sync::Arc::new(platform.fetch_http_client());
gpui::Application::with_platform(platform).with_http_client(http_client)
}
#[cfg(target_family = "wasm")]
pub fn web_init() {
console_error_panic_hook::set_once();
gpui_web::init_logging();
}
pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
#[cfg(target_os = "macos")]
{
Rc::new(gpui_macos::MacPlatform::new(headless))
}
#[cfg(target_os = "windows")]
{
Rc::new(
gpui_windows::WindowsPlatform::new(headless)
.expect("failed to initialize Windows platform"),
)
}
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
{
gpui_linux::current_platform(headless)
}
#[cfg(target_family = "wasm")]
{
let _ = headless;
Rc::new(gpui_web::WebPlatform::new(true))
}
}
#[cfg(any(feature = "bench-support", feature = "test-support"))]
pub fn current_headless_renderer() -> Option<Box<dyn gpui::PlatformHeadlessRenderer>> {
#[cfg(target_os = "macos")]
{
Some(Box::new(
gpui_macos::metal_renderer::MetalHeadlessRenderer::new(),
))
}
#[cfg(not(target_os = "macos"))]
{
None
}
}
#[cfg(all(test, target_os = "macos"))]
mod tests {
use super::*;
use gpui::{AppContext, Empty, VisualTestAppContext};
use std::cell::RefCell;
use std::time::Duration;
#[test]
#[ignore] fn test_foreground_tasks_run_with_run_until_parked() {
let mut cx = VisualTestAppContext::new(current_platform(false));
let task_ran = Rc::new(RefCell::new(false));
{
let task_ran = task_ran.clone();
cx.update(|cx| {
cx.spawn(async move |_| {
*task_ran.borrow_mut() = true;
})
.detach();
});
}
assert!(!*task_ran.borrow());
cx.run_until_parked();
assert!(*task_ran.borrow());
}
#[test]
#[ignore] fn test_advance_clock_triggers_delayed_tasks() {
let mut cx = VisualTestAppContext::new(current_platform(false));
let task_ran = Rc::new(RefCell::new(false));
{
let task_ran = task_ran.clone();
let executor = cx.background_executor.clone();
cx.update(|cx| {
cx.spawn(async move |_| {
executor.timer(Duration::from_millis(500)).await;
*task_ran.borrow_mut() = true;
})
.detach();
});
}
cx.run_until_parked();
assert!(!*task_ran.borrow());
cx.advance_clock(Duration::from_millis(600));
assert!(*task_ran.borrow());
}
#[test]
#[ignore] fn test_window_spawn_uses_test_dispatcher() {
let mut cx = VisualTestAppContext::new(current_platform(false));
let task_ran = Rc::new(RefCell::new(false));
let window = cx
.open_offscreen_window_default(|_, cx| cx.new(|_| Empty))
.expect("Failed to open window");
{
let task_ran = task_ran.clone();
cx.update_window(window.into(), |_, window, cx| {
window
.spawn(cx, async move |_| {
*task_ran.borrow_mut() = true;
})
.detach();
})
.ok();
}
assert!(!*task_ran.borrow());
cx.run_until_parked();
assert!(*task_ran.borrow());
}
}