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