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(doc)]
 58pub mod _ownership_and_data_flow;
 59
 60/// Do not touch, here be dragons for use by gpui_macros and such.
 61#[doc(hidden)]
 62pub mod private {
 63    pub use anyhow;
 64    pub use inventory;
 65    pub use schemars;
 66    pub use serde;
 67    pub use serde_json;
 68}
 69
 70mod seal {
 71    /// A mechanism for restricting implementations of a trait to only those in GPUI.
 72    /// See: <https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/>
 73    pub trait Sealed {}
 74}
 75
 76pub use action::*;
 77pub use anyhow::Result;
 78pub use app::*;
 79pub(crate) use arena::*;
 80pub use asset_cache::*;
 81pub use assets::*;
 82pub use color::*;
 83pub use ctor::ctor;
 84pub use element::*;
 85pub use elements::*;
 86pub use executor::*;
 87pub use geometry::*;
 88pub use global::*;
 89pub use gpui_macros::{AppContext, IntoElement, Render, VisualContext, register_action, test};
 90pub use gpui_util::arc_cow::ArcCow;
 91pub use http_client;
 92pub use input::*;
 93pub use inspector::*;
 94pub use interactive::*;
 95use key_dispatch::*;
 96pub use keymap::*;
 97pub use path_builder::*;
 98pub use platform::*;
 99pub use profiler::*;
100#[cfg(any(target_os = "windows", target_os = "linux", target_family = "wasm"))]
101pub use queue::{PriorityQueueReceiver, PriorityQueueSender};
102pub use refineable::*;
103pub use scene::*;
104pub use shared_string::*;
105pub use shared_uri::*;
106use std::{any::Any, future::Future};
107pub use style::*;
108pub use styled::*;
109pub use subscription::*;
110pub use svg_renderer::*;
111pub(crate) use tab_stop::*;
112use taffy::TaffyLayoutEngine;
113pub use taffy::{AvailableSpace, LayoutId};
114#[cfg(any(test, feature = "test-support"))]
115pub use test::*;
116pub use text_system::*;
117pub use util::{FutureExt, Timeout};
118pub use view::*;
119pub use window::*;
120
121/// The context trait, allows the different contexts in GPUI to be used
122/// interchangeably for certain operations.
123pub trait AppContext {
124    /// Create a new entity in the app context.
125    #[expect(
126        clippy::wrong_self_convention,
127        reason = "`App::new` is an ubiquitous function for creating entities"
128    )]
129    fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T>;
130
131    /// Reserve a slot for a entity to be inserted later.
132    /// The returned [Reservation] allows you to obtain the [EntityId] for the future entity.
133    fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>;
134
135    /// Insert a new entity in the app context based on a [Reservation] previously obtained from [`reserve_entity`].
136    ///
137    /// [`reserve_entity`]: Self::reserve_entity
138    fn insert_entity<T: 'static>(
139        &mut self,
140        reservation: Reservation<T>,
141        build_entity: impl FnOnce(&mut Context<T>) -> T,
142    ) -> Entity<T>;
143
144    /// Update a entity in the app context.
145    fn update_entity<T, R>(
146        &mut self,
147        handle: &Entity<T>,
148        update: impl FnOnce(&mut T, &mut Context<T>) -> R,
149    ) -> R
150    where
151        T: 'static;
152
153    /// Update a entity in the app context.
154    fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
155    where
156        T: 'static;
157
158    /// Read a entity from the app context.
159    fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R
160    where
161        T: 'static;
162
163    /// Update a window for the given handle.
164    fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
165    where
166        F: FnOnce(AnyView, &mut Window, &mut App) -> T;
167
168    /// Read a window off of the application context.
169    fn read_window<T, R>(
170        &self,
171        window: &WindowHandle<T>,
172        read: impl FnOnce(Entity<T>, &App) -> R,
173    ) -> Result<R>
174    where
175        T: 'static;
176
177    /// Spawn a future on a background thread
178    fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
179    where
180        R: Send + 'static;
181
182    /// Read a global from this app context
183    fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
184    where
185        G: Global;
186}
187
188/// Returned by [Context::reserve_entity] to later be passed to [Context::insert_entity].
189/// Allows you to obtain the [EntityId] for a entity before it is created.
190pub struct Reservation<T>(pub(crate) Slot<T>);
191
192impl<T: 'static> Reservation<T> {
193    /// Returns the [EntityId] that will be associated with the entity once it is inserted.
194    pub fn entity_id(&self) -> EntityId {
195        self.0.entity_id()
196    }
197}
198
199/// This trait is used for the different visual contexts in GPUI that
200/// require a window to be present.
201pub trait VisualContext: AppContext {
202    /// The result type for window operations.
203    type Result<T>;
204
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: std::borrow::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/// Information about the GPU GPUI is running on.
283#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)]
284pub struct GpuSpecs {
285    /// Whether the GPU is really a fake (like `llvmpipe`) running on the CPU.
286    pub is_software_emulated: bool,
287    /// The name of the device, as reported by Vulkan.
288    pub device_name: String,
289    /// The name of the driver, as reported by Vulkan.
290    pub driver_name: String,
291    /// Further information about the driver, as reported by Vulkan.
292    pub driver_info: String,
293}