1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3#![allow(clippy::type_complexity)] #![allow(clippy::collapsible_else_if)] #![allow(unused_mut)] extern crate self as gpui;
8
9#[macro_use]
10mod action;
11mod app;
12
13mod arena;
14mod asset_cache;
15mod assets;
16mod bounds_tree;
17mod color;
18pub mod colors;
20mod element;
21mod elements;
22mod executor;
23mod geometry;
24mod global;
25mod input;
26mod inspector;
27mod interactive;
28mod key_dispatch;
29mod keymap;
30mod path_builder;
31mod platform;
32pub mod prelude;
33mod scene;
34mod shared_string;
35mod shared_uri;
36mod style;
37mod styled;
38mod subscription;
39mod svg_renderer;
40mod tab_stop;
41mod taffy;
42#[cfg(any(test, feature = "test-support"))]
43pub mod test;
44mod text_system;
45mod util;
46mod view;
47mod window;
48
49#[cfg(doc)]
50pub mod _ownership_and_data_flow;
51
52#[doc(hidden)]
54pub mod private {
55 pub use anyhow;
56 pub use inventory;
57 pub use schemars;
58 pub use serde;
59 pub use serde_json;
60}
61
62mod seal {
63 pub trait Sealed {}
66}
67
68pub use action::*;
69pub use anyhow::Result;
70pub use app::*;
71pub(crate) use arena::*;
72pub use asset_cache::*;
73pub use assets::*;
74pub use color::*;
75pub use ctor::ctor;
76pub use element::*;
77pub use elements::*;
78pub use executor::*;
79pub use geometry::*;
80pub use global::*;
81pub use gpui_macros::{AppContext, IntoElement, Render, VisualContext, register_action, test};
82pub use http_client;
83pub use input::*;
84pub use inspector::*;
85pub use interactive::*;
86use key_dispatch::*;
87pub use keymap::*;
88pub use path_builder::*;
89pub use platform::*;
90pub use refineable::*;
91pub use scene::*;
92pub use shared_string::*;
93pub use shared_uri::*;
94pub use smol::Timer;
95pub use style::*;
96pub use styled::*;
97pub use subscription::*;
98use svg_renderer::*;
99pub(crate) use tab_stop::*;
100pub use taffy::{AvailableSpace, LayoutId};
101#[cfg(any(test, feature = "test-support"))]
102pub use test::*;
103pub use text_system::*;
104#[cfg(any(test, feature = "test-support"))]
105pub use util::smol_timeout;
106pub use util::{FutureExt, Timeout, arc_cow::ArcCow};
107pub use view::*;
108pub use window::*;
109
110use std::{any::Any, borrow::BorrowMut, future::Future};
111use taffy::TaffyLayoutEngine;
112
113pub trait AppContext {
116 type Result<T>;
119
120 #[expect(
122 clippy::wrong_self_convention,
123 reason = "`App::new` is an ubiquitous function for creating entities"
124 )]
125 fn new<T: 'static>(
126 &mut self,
127 build_entity: impl FnOnce(&mut Context<T>) -> T,
128 ) -> Self::Result<Entity<T>>;
129
130 fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>>;
133
134 fn insert_entity<T: 'static>(
138 &mut self,
139 reservation: Reservation<T>,
140 build_entity: impl FnOnce(&mut Context<T>) -> T,
141 ) -> Self::Result<Entity<T>>;
142
143 fn update_entity<T, R>(
145 &mut self,
146 handle: &Entity<T>,
147 update: impl FnOnce(&mut T, &mut Context<T>) -> R,
148 ) -> Self::Result<R>
149 where
150 T: 'static;
151
152 fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> Self::Result<GpuiBorrow<'a, T>>
154 where
155 T: 'static;
156
157 fn read_entity<T, R>(
159 &self,
160 handle: &Entity<T>,
161 read: impl FnOnce(&T, &App) -> R,
162 ) -> Self::Result<R>
163 where
164 T: 'static;
165
166 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
168 where
169 F: FnOnce(AnyView, &mut Window, &mut App) -> T;
170
171 fn read_window<T, R>(
173 &self,
174 window: &WindowHandle<T>,
175 read: impl FnOnce(Entity<T>, &App) -> R,
176 ) -> Result<R>
177 where
178 T: 'static;
179
180 fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
182 where
183 R: Send + 'static;
184
185 fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
187 where
188 G: Global;
189}
190
191pub struct Reservation<T>(pub(crate) Slot<T>);
194
195impl<T: 'static> Reservation<T> {
196 pub fn entity_id(&self) -> EntityId {
198 self.0.entity_id()
199 }
200}
201
202pub trait VisualContext: AppContext {
205 fn window_handle(&self) -> AnyWindowHandle;
207
208 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 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 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 fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()>
231 where
232 V: Focusable;
233}
234
235pub trait EventEmitter<E: Any>: 'static {}
238
239pub trait BorrowAppContext {
242 fn set_global<T: Global>(&mut self, global: T);
244 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
246 where
247 G: Global;
248 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: 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
282pub trait Flatten<T> {
284 fn flatten(self) -> Result<T>;
286}
287
288impl<T> Flatten<T> for Result<Result<T>> {
289 fn flatten(self) -> Result<T> {
290 self?
291 }
292}
293
294impl<T> Flatten<T> for Result<T> {
295 fn flatten(self) -> Result<T> {
296 self
297 }
298}
299
300#[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)]
302pub struct GpuSpecs {
303 pub is_software_emulated: bool,
305 pub device_name: String,
307 pub driver_name: String,
309 pub driver_info: String,
311}