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