1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3#![allow(clippy::type_complexity)] // Not useful, GPUI makes heavy use of callbacks
4#![allow(clippy::collapsible_else_if)] // False positives in platform specific code
5#![allow(unused_mut)] // False positives in platform specific code
6
7extern crate self as gpui;
8
9#[macro_use]
10mod action;
11mod app;
12
13mod arena;
14mod asset_cache;
15mod assets;
16mod bounds_tree;
17mod color;
18/// The default colors used by GPUI.
19pub mod colors;
20mod element;
21mod elements;
22mod executor;
23mod geometry;
24mod global;
25mod input;
26mod inspector;
27mod interactive;
28mod key_dispatch;
29mod keymap;
30mod path_builder;
31mod platform;
32pub mod prelude;
33mod scene;
34mod shared_string;
35mod shared_uri;
36mod style;
37mod styled;
38mod subscription;
39mod svg_renderer;
40mod tab_stop;
41mod taffy;
42#[cfg(any(test, feature = "test-support"))]
43pub mod test;
44mod text_system;
45mod util;
46mod view;
47mod window;
48
49#[cfg(doc)]
50pub mod _ownership_and_data_flow;
51
52/// Do not touch, here be dragons for use by gpui_macros and such.
53#[doc(hidden)]
54pub mod private {
55 pub use anyhow;
56 pub use inventory;
57 pub use schemars;
58 pub use serde;
59 pub use serde_json;
60}
61
62mod seal {
63 /// A mechanism for restricting implementations of a trait to only those in GPUI.
64 /// See: <https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/>
65 pub trait Sealed {}
66}
67
68pub use action::*;
69pub use anyhow::Result;
70pub use app::*;
71pub(crate) use arena::*;
72pub use asset_cache::*;
73pub use assets::*;
74pub use color::*;
75pub use ctor::ctor;
76pub use element::*;
77pub use elements::*;
78pub use executor::*;
79pub use geometry::*;
80pub use global::*;
81pub use gpui_macros::{AppContext, IntoElement, Render, VisualContext, register_action, test};
82pub use http_client;
83pub use input::*;
84pub use inspector::*;
85pub use interactive::*;
86use key_dispatch::*;
87pub use keymap::*;
88pub use path_builder::*;
89pub use platform::*;
90pub use refineable::*;
91pub use scene::*;
92pub use shared_string::*;
93pub use shared_uri::*;
94pub use smol::Timer;
95pub use style::*;
96pub use styled::*;
97pub use subscription::*;
98use svg_renderer::*;
99pub(crate) use tab_stop::*;
100pub use taffy::{AvailableSpace, LayoutId};
101#[cfg(any(test, feature = "test-support"))]
102pub use test::*;
103pub use text_system::*;
104#[cfg(any(test, feature = "test-support"))]
105pub use util::smol_timeout;
106pub use util::{FutureExt, Timeout, arc_cow::ArcCow};
107pub use view::*;
108pub use window::*;
109
110use std::{any::Any, borrow::BorrowMut, future::Future};
111use taffy::TaffyLayoutEngine;
112
113/// The context trait, allows the different contexts in GPUI to be used
114/// interchangeably for certain operations.
115pub trait AppContext {
116 /// The result type for this context, used for async contexts that
117 /// can't hold a direct reference to the application context.
118 type Result<T>;
119
120 /// Create a new entity in the app context.
121 #[expect(
122 clippy::wrong_self_convention,
123 reason = "`App::new` is an ubiquitous function for creating entities"
124 )]
125 fn new<T: 'static>(
126 &mut self,
127 build_entity: impl FnOnce(&mut Context<T>) -> T,
128 ) -> Self::Result<Entity<T>>;
129
130 /// Reserve a slot for a entity to be inserted later.
131 /// The returned [Reservation] allows you to obtain the [EntityId] for the future entity.
132 fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>>;
133
134 /// Insert a new entity in the app context based on a [Reservation] previously obtained from [`reserve_entity`].
135 ///
136 /// [`reserve_entity`]: Self::reserve_entity
137 fn insert_entity<T: 'static>(
138 &mut self,
139 reservation: Reservation<T>,
140 build_entity: impl FnOnce(&mut Context<T>) -> T,
141 ) -> Self::Result<Entity<T>>;
142
143 /// Update a entity in the app context.
144 fn update_entity<T, R>(
145 &mut self,
146 handle: &Entity<T>,
147 update: impl FnOnce(&mut T, &mut Context<T>) -> R,
148 ) -> Self::Result<R>
149 where
150 T: 'static;
151
152 /// Update a entity in the app context.
153 fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> Self::Result<GpuiBorrow<'a, T>>
154 where
155 T: 'static;
156
157 /// Read a entity from the app context.
158 fn read_entity<T, R>(
159 &self,
160 handle: &Entity<T>,
161 read: impl FnOnce(&T, &App) -> R,
162 ) -> Self::Result<R>
163 where
164 T: 'static;
165
166 /// Update a window for the given handle.
167 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
168 where
169 F: FnOnce(AnyView, &mut Window, &mut App) -> T;
170
171 /// Read a window off of the application context.
172 fn read_window<T, R>(
173 &self,
174 window: &WindowHandle<T>,
175 read: impl FnOnce(Entity<T>, &App) -> R,
176 ) -> Result<R>
177 where
178 T: 'static;
179
180 /// Spawn a future on a background thread
181 fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
182 where
183 R: Send + 'static;
184
185 /// Read a global from this app context
186 fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
187 where
188 G: Global;
189}
190
191/// Returned by [Context::reserve_entity] to later be passed to [Context::insert_entity].
192/// Allows you to obtain the [EntityId] for a entity before it is created.
193pub struct Reservation<T>(pub(crate) Slot<T>);
194
195impl<T: 'static> Reservation<T> {
196 /// Returns the [EntityId] that will be associated with the entity once it is inserted.
197 pub fn entity_id(&self) -> EntityId {
198 self.0.entity_id()
199 }
200}
201
202/// This trait is used for the different visual contexts in GPUI that
203/// require a window to be present.
204pub trait VisualContext: AppContext {
205 /// Returns the handle of the window associated with this context.
206 fn window_handle(&self) -> AnyWindowHandle;
207
208 /// Update a view with the given callback
209 fn update_window_entity<T: 'static, R>(
210 &mut self,
211 entity: &Entity<T>,
212 update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
213 ) -> Self::Result<R>;
214
215 /// Create a new entity, with access to `Window`.
216 fn new_window_entity<T: 'static>(
217 &mut self,
218 build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
219 ) -> Self::Result<Entity<T>>;
220
221 /// Replace the root view of a window with a new view.
222 fn replace_root_view<V>(
223 &mut self,
224 build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,
225 ) -> Self::Result<Entity<V>>
226 where
227 V: 'static + Render;
228
229 /// Focus a entity in the window, if it implements the [`Focusable`] trait.
230 fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()>
231 where
232 V: Focusable;
233}
234
235/// A trait for tying together the types of a GPUI entity and the events it can
236/// emit.
237pub trait EventEmitter<E: Any>: 'static {}
238
239/// A helper trait for auto-implementing certain methods on contexts that
240/// can be used interchangeably.
241pub trait BorrowAppContext {
242 /// Set a global value on the context.
243 fn set_global<T: Global>(&mut self, global: T);
244 /// Updates the global state of the given type.
245 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
246 where
247 G: Global;
248 /// Updates the global state of the given type, creating a default if it didn't exist before.
249 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
250 where
251 G: Global + Default;
252}
253
254impl<C> BorrowAppContext for C
255where
256 C: BorrowMut<App>,
257{
258 fn set_global<G: Global>(&mut self, global: G) {
259 self.borrow_mut().set_global(global)
260 }
261
262 #[track_caller]
263 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
264 where
265 G: Global,
266 {
267 let mut global = self.borrow_mut().lease_global::<G>();
268 let result = f(&mut global, self);
269 self.borrow_mut().end_global_lease(global);
270 result
271 }
272
273 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
274 where
275 G: Global + Default,
276 {
277 self.borrow_mut().default_global::<G>();
278 self.update_global(f)
279 }
280}
281
282/// A flatten equivalent for anyhow `Result`s.
283pub trait Flatten<T> {
284 /// Convert this type into a simple `Result<T>`.
285 fn flatten(self) -> Result<T>;
286}
287
288impl<T> Flatten<T> for Result<Result<T>> {
289 fn flatten(self) -> Result<T> {
290 self?
291 }
292}
293
294impl<T> Flatten<T> for Result<T> {
295 fn flatten(self) -> Result<T> {
296 self
297 }
298}
299
300/// Information about the GPU GPUI is running on.
301#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)]
302pub struct GpuSpecs {
303 /// Whether the GPU is really a fake (like `llvmpipe`) running on the CPU.
304 pub is_software_emulated: bool,
305 /// The name of the device, as reported by Vulkan.
306 pub device_name: String,
307 /// The name of the driver, as reported by Vulkan.
308 pub driver_name: String,
309 /// Further information about the driver, as reported by Vulkan.
310 pub driver_info: String,
311}