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