gpui.rs

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