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(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(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 /// Create a new entity in the app context.
122 #[expect(
123 clippy::wrong_self_convention,
124 reason = "`App::new` is an ubiquitous function for creating entities"
125 )]
126 fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T>;
127
128 /// Reserve a slot for a entity to be inserted later.
129 /// The returned [Reservation] allows you to obtain the [EntityId] for the future entity.
130 fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>;
131
132 /// Insert a new entity in the app context based on a [Reservation] previously obtained from [`reserve_entity`].
133 ///
134 /// [`reserve_entity`]: Self::reserve_entity
135 fn insert_entity<T: 'static>(
136 &mut self,
137 reservation: Reservation<T>,
138 build_entity: impl FnOnce(&mut Context<T>) -> T,
139 ) -> Entity<T>;
140
141 /// Update a entity in the app context.
142 fn update_entity<T, R>(
143 &mut self,
144 handle: &Entity<T>,
145 update: impl FnOnce(&mut T, &mut Context<T>) -> R,
146 ) -> R
147 where
148 T: 'static;
149
150 /// Update a entity in the app context.
151 fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
152 where
153 T: 'static;
154
155 /// Read a entity from the app context.
156 fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R
157 where
158 T: 'static;
159
160 /// Update a window for the given handle.
161 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
162 where
163 F: FnOnce(AnyView, &mut Window, &mut App) -> T;
164
165 /// Read a window off of the application context.
166 fn read_window<T, R>(
167 &self,
168 window: &WindowHandle<T>,
169 read: impl FnOnce(Entity<T>, &App) -> R,
170 ) -> Result<R>
171 where
172 T: 'static;
173
174 /// Spawn a future on a background thread
175 fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
176 where
177 R: Send + 'static;
178
179 /// Read a global from this app context
180 fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
181 where
182 G: Global;
183}
184
185/// Returned by [Context::reserve_entity] to later be passed to [Context::insert_entity].
186/// Allows you to obtain the [EntityId] for a entity before it is created.
187pub struct Reservation<T>(pub(crate) Slot<T>);
188
189impl<T: 'static> Reservation<T> {
190 /// Returns the [EntityId] that will be associated with the entity once it is inserted.
191 pub fn entity_id(&self) -> EntityId {
192 self.0.entity_id()
193 }
194}
195
196/// This trait is used for the different visual contexts in GPUI that
197/// require a window to be present.
198pub trait VisualContext: AppContext {
199 /// Returns the handle of the window associated with this context.
200 fn window_handle(&self) -> AnyWindowHandle;
201
202 /// Update a view with the given callback
203 fn update_window_entity<T: 'static, R>(
204 &mut self,
205 entity: &Entity<T>,
206 update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
207 ) -> Result<R>;
208
209 /// Create a new entity, with access to `Window`.
210 fn new_window_entity<T: 'static>(
211 &mut self,
212 build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
213 ) -> Result<Entity<T>>;
214
215 /// Replace the root view of a window with a new view.
216 fn replace_root_view<V>(
217 &mut self,
218 build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,
219 ) -> Result<Entity<V>>
220 where
221 V: 'static + Render;
222
223 /// Focus a entity in the window, if it implements the [`Focusable`] trait.
224 fn focus<V>(&mut self, entity: &Entity<V>) -> Result<()>
225 where
226 V: Focusable;
227}
228
229/// A trait for tying together the types of a GPUI entity and the events it can
230/// emit.
231pub trait EventEmitter<E: Any>: 'static {}
232
233/// A helper trait for auto-implementing certain methods on contexts that
234/// can be used interchangeably.
235pub trait BorrowAppContext {
236 /// Set a global value on the context.
237 fn set_global<T: Global>(&mut self, global: T);
238 /// Updates the global state of the given type.
239 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
240 where
241 G: Global;
242 /// Updates the global state of the given type, creating a default if it didn't exist before.
243 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
244 where
245 G: Global + Default;
246}
247
248impl<C> BorrowAppContext for C
249where
250 C: std::borrow::BorrowMut<App>,
251{
252 fn set_global<G: Global>(&mut self, global: G) {
253 self.borrow_mut().set_global(global)
254 }
255
256 #[track_caller]
257 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
258 where
259 G: Global,
260 {
261 let mut global = self.borrow_mut().lease_global::<G>();
262 let result = f(&mut global, self);
263 self.borrow_mut().end_global_lease(global);
264 result
265 }
266
267 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
268 where
269 G: Global + Default,
270 {
271 self.borrow_mut().default_global::<G>();
272 self.update_global(f)
273 }
274}
275
276/// Information about the GPU GPUI is running on.
277#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)]
278pub struct GpuSpecs {
279 /// Whether the GPU is really a fake (like `llvmpipe`) running on the CPU.
280 pub is_software_emulated: bool,
281 /// The name of the device, as reported by Vulkan.
282 pub device_name: String,
283 /// The name of the driver, as reported by Vulkan.
284 pub driver_name: String,
285 /// Further information about the driver, as reported by Vulkan.
286 pub driver_info: String,
287}