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