gpui.rs

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