Skip to main content

gpui/
gpui.rs

1#![doc = include_str!("../README.md")]
2
3extern crate self as gpui;
4
5use std::{path::PathBuf, rc::Rc, sync::Arc};
6
7pub use gpui_core::*;
8
9/// Returns a background executor for the current platform.
10pub fn background_executor() -> BackgroundExecutor {
11    gpui_platform::background_executor()
12}
13
14/// Returns the current desktop platform implementation.
15pub fn current_platform(headless: bool) -> Rc<dyn Platform> {
16    gpui_platform::current_platform(headless)
17}
18
19/// Real-renderer visual test context composed with the current desktop platform.
20#[cfg(all(
21    feature = "test-support",
22    any(
23        target_os = "macos",
24        target_os = "linux",
25        target_os = "freebsd",
26        target_os = "windows"
27    )
28))]
29pub struct RealVisualTestContext(gpui_core::RealVisualTestContext);
30
31#[cfg(all(
32    feature = "test-support",
33    any(
34        target_os = "macos",
35        target_os = "linux",
36        target_os = "freebsd",
37        target_os = "windows"
38    )
39))]
40impl RealVisualTestContext {
41    /// Creates a context when the current platform supports real visual capture.
42    pub fn new_if_supported() -> Option<Self> {
43        let capabilities = VisualTestCapabilities::detect();
44        (capabilities.real_renderer && capabilities.screenshot_capture).then(Self::new)
45    }
46
47    /// Creates a context with the default empty asset source.
48    pub fn new() -> Self {
49        Self(gpui_core::RealVisualTestContext::with_platform(
50            gpui_platform::current_platform(false),
51        ))
52    }
53
54    /// Creates a context with a custom asset source.
55    pub fn with_asset_source(asset_source: Arc<dyn AssetSource>) -> Self {
56        Self(
57            gpui_core::RealVisualTestContext::with_platform_and_asset_source(
58                gpui_platform::current_platform(false),
59                asset_source,
60            ),
61        )
62    }
63
64    /// Starts the real platform run loop and invokes the callback after launch.
65    pub fn run<F>(self, on_finish_launching: F)
66    where
67        F: 'static + FnOnce(&mut Self),
68    {
69        let platform = self.0.visual_test_platform();
70        let mut cx = Some(self);
71        platform.run(Box::new(move || {
72            if let Some(mut cx) = cx.take() {
73                on_finish_launching(&mut cx);
74            }
75        }));
76    }
77}
78
79#[cfg(all(
80    feature = "test-support",
81    any(
82        target_os = "macos",
83        target_os = "linux",
84        target_os = "freebsd",
85        target_os = "windows"
86    )
87))]
88impl std::ops::Deref for RealVisualTestContext {
89    type Target = gpui_core::RealVisualTestContext;
90
91    fn deref(&self) -> &Self::Target {
92        &self.0
93    }
94}
95
96#[cfg(all(
97    feature = "test-support",
98    any(
99        target_os = "macos",
100        target_os = "linux",
101        target_os = "freebsd",
102        target_os = "windows"
103    )
104))]
105impl std::ops::DerefMut for RealVisualTestContext {
106    fn deref_mut(&mut self) -> &mut Self::Target {
107        &mut self.0
108    }
109}
110
111/// Configures and launches a GPUI application.
112///
113/// This compatibility wrapper keeps platform selection in the published
114/// package while all application state and callback types remain core types.
115pub struct Application(gpui_core::Application);
116
117impl Application {
118    /// Builds an application using the current desktop platform.
119    // `new` selects and initializes the current desktop backend; there is no
120    // backend-independent default application to construct.
121    #[allow(clippy::new_without_default)]
122    pub fn new() -> Self {
123        #[cfg(any(test, feature = "test-support"))]
124        log::info!("GPUI was compiled in test mode");
125
126        Self(gpui_platform::application())
127    }
128
129    /// Builds an application using the current platform in headless mode.
130    pub fn headless() -> Self {
131        Self(gpui_platform::headless())
132    }
133
134    /// Builds an application with a caller-provided platform implementation.
135    pub fn with_platform(platform: Rc<dyn Platform>) -> Self {
136        Self(gpui_core::Application::with_platform(platform))
137    }
138
139    /// Builds this app with accessibility integration forcibly disabled.
140    pub fn inaccessible(self) -> Self {
141        Self(self.0.inaccessible())
142    }
143
144    /// Builds this app with the given asset source.
145    pub fn with_assets(self, asset_source: impl AssetSource) -> Self {
146        Self(self.0.with_assets(asset_source))
147    }
148
149    /// Sets the HTTP client for the application.
150    pub fn with_http_client(self, http_client: Arc<dyn http_client::HttpClient>) -> Self {
151        Self(self.0.with_http_client(http_client))
152    }
153
154    /// Sets the resource profile for the application.
155    pub fn with_resource_profile(self, profile: impl Into<AppResourceProfile>) -> Self {
156        Self(self.0.with_resource_profile(profile))
157    }
158
159    /// Configures when the application should automatically quit.
160    /// By default, [`QuitMode::Default`] is used.
161    pub fn with_quit_mode(self, mode: QuitMode) -> Self {
162        Self(self.0.with_quit_mode(mode))
163    }
164
165    /// Starts the application.
166    pub fn run<F>(self, on_finish_launching: F)
167    where
168        F: 'static + FnOnce(&mut App),
169    {
170        self.0.run(on_finish_launching)
171    }
172
173    /// Starts an application whose run loop is driven by an embedder.
174    pub fn run_embedded<F>(self, on_finish_launching: F) -> ApplicationHandle
175    where
176        F: 'static + FnOnce(&mut App),
177    {
178        self.0.run_embedded(on_finish_launching)
179    }
180
181    /// Registers a handler for platform open-URL requests.
182    pub fn on_open_urls<F>(&self, callback: F) -> &Self
183    where
184        F: 'static + FnMut(Vec<String>),
185    {
186        self.0.on_open_urls(callback);
187        self
188    }
189
190    /// Registers a handler for reopening an already-running application.
191    pub fn on_reopen<F>(&self, callback: F) -> &Self
192    where
193        F: 'static + FnMut(&mut App),
194    {
195        self.0.on_reopen(callback);
196        self
197    }
198
199    /// Returns the application's background executor.
200    pub fn background_executor(&self) -> BackgroundExecutor {
201        self.0.background_executor()
202    }
203
204    /// Returns the application's foreground executor.
205    pub fn foreground_executor(&self) -> ForegroundExecutor {
206        self.0.foreground_executor()
207    }
208
209    /// Returns the application's text system.
210    pub fn text_system(&self) -> Arc<TextSystem> {
211        self.0.text_system()
212    }
213
214    /// Returns the bundle path for an auxiliary executable.
215    pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
216        self.0.path_for_auxiliary_executable(name)
217    }
218}