Skip to main content

gpui_runtime/
application.rs

1//! The [`Application`] type: building an app, configuring it before launch, and
2//! starting the platform run loop that drives it.
3
4use anyhow::Result;
5use gpui_authoring::{
6    App, AppCell, AssetSource, AsyncApp, BackgroundExecutor, ForegroundExecutor, FramePipeline,
7    GpuPreference, LayoutEngine, Platform, QuitMode, TextSystem, WindowId, http_client::HttpClient,
8};
9use std::{ffi::OsString, path::PathBuf, rc::Rc, sync::Arc};
10
11/// A reference to a GPUI application, typically constructed in the `main` function of your app.
12/// You won't interact with this type much outside of initial configuration and startup.
13pub struct Application(Rc<AppCell>);
14
15/// A strong handle to an [`Application`] started with [`Application::run_embedded`].
16///
17/// Dropping this handle releases the app, so an embedder must hold it for as long as the
18/// app should run. While held, it is the embedder's entry point back into GPUI each time
19/// the external run loop gives it control.
20pub struct ApplicationHandle {
21    app: Rc<AppCell>,
22}
23
24impl ApplicationHandle {
25    /// Invoke `f` with the app context. Must not be called re-entrantly from code that
26    /// is already inside an update; the app state is a `RefCell` and will panic on a
27    /// double borrow.
28    pub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> R {
29        let cx = &mut *self.app.borrow_mut();
30        f(cx)
31    }
32
33    /// An [`AsyncApp`] for use across await points. It holds the app weakly; keeping the
34    /// app alive remains this handle's job.
35    pub fn to_async(&self) -> AsyncApp {
36        self.update(|cx| cx.to_async())
37    }
38}
39
40/// Represents an application before it is fully launched. Once your app is
41/// configured, you'll start the app with `App::run`.
42impl Application {
43    /// Builds an app with a caller-provided platform implementation.
44    pub fn with_platform(platform: Rc<dyn Platform>) -> Self {
45        Self(App::new_app_for_platform(platform))
46    }
47
48    /// Builds an app with accessibility (AccessKit) integration forcibly
49    /// disabled.
50    ///
51    /// In this mode, accessibility APIs (e.g.
52    /// [`div().role()`][gpui_authoring::StatefulInteractiveElement::role])
53    /// silently no-op.
54    ///
55    /// See the [accessibility guide](gpui_authoring::_accessibility) for an
56    /// overview of the features this disables.
57    pub fn new_inaccessible(platform: Rc<dyn Platform>) -> Self {
58        let this = Self::with_platform(platform);
59        this.0.borrow_mut().set_accessibility_force_disabled(true);
60        this
61    }
62
63    /// Assigns the source of assets for the application.
64    pub fn with_assets(self, asset_source: impl AssetSource) -> Self {
65        self.0.borrow_mut().set_asset_source(Arc::new(asset_source));
66        self
67    }
68
69    /// Configures arguments to pass when restarting the application.
70    pub fn with_restart_arguments(self, arguments: Vec<OsString>) -> Self {
71        self.0.borrow_mut().set_restart_arguments(arguments);
72        self
73    }
74
75    /// Sets the HTTP client for the application.
76    pub fn with_http_client(self, http_client: Arc<dyn HttpClient>) -> Self {
77        self.0.borrow_mut().set_http_client(http_client);
78        self
79    }
80
81    /// Configures when the application should automatically quit.
82    /// By default, [`QuitMode::Default`] is used.
83    pub fn with_quit_mode(self, mode: QuitMode) -> Self {
84        self.0.borrow_mut().set_quit_mode(mode);
85        self
86    }
87
88    /// States which GPU the renderer should run on.
89    ///
90    /// The device is process-wide and chosen before the first window, so set this before
91    /// [`run`](Self::run). When it is not stated, each backend keeps its own default: `LowPower` on
92    /// Linux, and the adapter order the platform already used elsewhere. See decisions `0008` and
93    /// `0009` in the project record.
94    pub fn with_gpu_preference(self, preference: GpuPreference) -> Self {
95        self.0.borrow().platform().set_gpu_preference(preference);
96        self
97    }
98
99    /// Sets the factory that creates each window's layout engine.
100    ///
101    /// Defaults to the engine bundled with GPUI. Supply a different
102    /// implementation to swap the layout engine without touching windows.
103    pub fn with_layout_engine(
104        self,
105        layout_engine: impl Fn() -> Box<dyn LayoutEngine> + 'static,
106    ) -> Self {
107        self.0
108            .borrow_mut()
109            .set_layout_engine_factory(Rc::new(layout_engine));
110        self
111    }
112
113    /// Sets the factory that creates each window's frame pipeline.
114    ///
115    /// Defaults to the immediate-mode pipeline bundled with GPUI, which
116    /// re-evaluates the view tree every frame. Supply a different implementation
117    /// to change how a window draws its frames.
118    pub fn with_frame_pipeline(
119        self,
120        frame_pipeline: impl Fn(WindowId) -> Box<dyn FramePipeline> + 'static,
121    ) -> Self {
122        self.0
123            .borrow_mut()
124            .set_frame_pipeline_factory(Rc::new(frame_pipeline));
125        self
126    }
127
128    /// Sets the text system used to shape and lay out text.
129    ///
130    /// Defaults to the text system bundled with GPUI. Supply a different
131    /// implementation to swap the shaper and line layout engine.
132    pub fn with_text_system(self, text_system: Arc<dyn TextSystem>) -> Self {
133        self.0.borrow_mut().set_text_system(text_system);
134        self
135    }
136
137    /// Start the application. The provided callback will be called once the
138    /// app is fully launched.
139    ///
140    /// On WebAssembly, this returns immediately and retains the app for the lifetime
141    /// of the Wasm instance. Use [`Self::run_embedded`] to control its lifetime explicitly.
142    pub fn run<F>(self, on_finish_launching: F)
143    where
144        F: 'static + FnOnce(&mut App),
145    {
146        let this = self.0.clone();
147        let platform = self.0.borrow().platform();
148        platform.run(Box::new(move || {
149            let cx = &mut *this.borrow_mut();
150            on_finish_launching(cx);
151        }));
152
153        #[cfg(target_family = "wasm")]
154        std::mem::forget(self);
155    }
156
157    /// Start the application for an embedder that drives the run loop itself.
158    ///
159    /// On ordinary platforms `Platform::run` blocks for the lifetime of the app, and the
160    /// app state is kept alive by [`Application::run`]'s stack frame. Embedded platforms —
161    /// where the run loop belongs to someone else, e.g. GPUI compiled into a Wasm guest,
162    /// or a GPUI view hosted inside a foreign native application — implement
163    /// `Platform::run` to invoke the launch callback and return immediately. This method
164    /// supports that shape: it returns an [`ApplicationHandle`] that keeps the app alive
165    /// and lets the embedder re-enter it whenever the external run loop yields control.
166    pub fn run_embedded<F>(self, on_finish_launching: F) -> ApplicationHandle
167    where
168        F: 'static + FnOnce(&mut App),
169    {
170        let this = self.0.clone();
171        let platform = self.0.borrow().platform();
172        platform.run(Box::new(move || {
173            let cx = &mut *this.borrow_mut();
174            on_finish_launching(cx);
175        }));
176        ApplicationHandle { app: self.0 }
177    }
178
179    /// Register a handler to be invoked when the platform instructs the application
180    /// to open one or more URLs.
181    pub fn on_open_urls<F>(&self, callback: F) -> &Self
182    where
183        F: 'static + FnMut(Vec<String>),
184    {
185        let platform = self.0.borrow().platform();
186        platform.on_open_urls(Box::new(callback));
187        self
188    }
189
190    /// Invokes a handler when an already-running application is launched.
191    /// On macOS, this can occur when the application icon is double-clicked or the app is launched via the dock.
192    pub fn on_reopen<F>(&self, mut callback: F) -> &Self
193    where
194        F: 'static + FnMut(&mut App),
195    {
196        let this = Rc::downgrade(&self.0);
197        let platform = self.0.borrow().platform();
198        platform.on_reopen(Box::new(move || {
199            if let Some(app) = this.upgrade() {
200                callback(&mut app.borrow_mut());
201            }
202        }));
203        self
204    }
205
206    /// Returns a handle to the [`BackgroundExecutor`] associated with this app, which can be used to spawn futures in the background.
207    pub fn background_executor(&self) -> BackgroundExecutor {
208        self.0.borrow().background_executor().clone()
209    }
210
211    /// Returns a handle to the [`ForegroundExecutor`] associated with this app, which can be used to spawn futures in the foreground.
212    pub fn foreground_executor(&self) -> ForegroundExecutor {
213        self.0.borrow().foreground_executor().clone()
214    }
215
216    /// Returns the app's [`TextSystem`].
217    pub fn text_system(&self) -> Arc<dyn TextSystem> {
218        self.0.borrow().text_system().clone()
219    }
220
221    /// Returns the file URL of the executable with the specified name in the application bundle
222    pub fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf> {
223        self.0.borrow().path_for_auxiliary_executable(name)
224    }
225}