1#![warn(missing_docs)]
22#![allow(clippy::type_complexity)] #![allow(clippy::collapsible_else_if)] #![allow(unused_mut)] extern crate self as gpui;
29
30#[macro_use]
31mod action;
32mod app;
33
34mod arena;
35mod asset_cache;
36mod assets;
37pub mod colors;
39#[cfg(feature = "profiler")]
40mod debug_overlay;
41mod element;
42mod elements;
43mod engine_layout;
44mod executor;
45mod gestures;
46mod global;
47mod input;
48mod inspector;
49mod interactive;
50mod key_dispatch;
51mod keymap;
52mod path_builder;
53mod platform;
54pub mod prelude;
55pub mod profiler;
57mod shared_uri;
58mod spring;
59mod style;
60mod styled;
61mod subscription;
62mod svg_renderer;
63mod tab_stop;
64#[cfg(any(test, feature = "test-support"))]
65pub mod test;
66mod text_system;
67mod util;
68mod view;
69mod window;
70
71#[cfg(any(test, feature = "test-support"))]
72pub use proptest;
73
74pub mod _accessibility;
76pub mod _authoring;
78pub mod _frame_pipeline;
80pub mod _ownership_and_data_flow;
82
83#[doc(hidden)]
85pub mod private {
86 pub use anyhow;
87 pub use inventory;
88 pub use schemars;
89 pub use serde;
90 pub use serde_json;
91}
92
93mod seal {
94 pub trait Sealed {}
97}
98
99pub use accesskit;
100pub use accesskit::Action as AccessibleAction;
101pub use accesskit::{Orientation, Role, Toggled};
102pub use action::*;
103pub use anyhow::Result;
104pub use app::*;
105pub(crate) use arena::*;
106pub use asset_cache::*;
107pub use assets::*;
108pub use ctor::ctor;
109#[cfg(feature = "profiler")]
110pub use debug_overlay::*;
111pub use element::*;
112pub use elements::*;
113pub use executor::*;
114pub use gestures::*;
115pub use global::*;
116pub use gpui_macros::{
117 AppContext, IntoElement, Render, VisualContext, bench, property_test, register_action, test,
118};
119pub use spring::*;
120
121pub use gpui_engine::*;
122pub use gpui_engine_default::*;
123pub use gpui_platform::*;
124pub use gpui_util::arc_cow::ArcCow;
125pub use http_client;
126pub use input::*;
127pub use inspector::*;
128pub use interactive::*;
129use key_dispatch::*;
130pub use keymap::*;
131pub use path_builder::*;
132pub use platform::*;
133pub use profiler::*;
134pub use refineable::*;
135pub use shared_uri::*;
136use std::{any::Any, future::Future};
137pub use style::*;
138pub use styled::*;
139pub use subscription::*;
140pub use svg_renderer::*;
141pub(crate) use tab_stop::*;
142#[cfg(any(test, feature = "test-support"))]
143pub use test::*;
144pub use text_system::*;
145pub use util::{FluentBuilder, FutureExt, Timeout};
146pub use view::*;
147pub use window::*;
148
149#[cfg(not(target_family = "wasm"))]
150pub use pollster::block_on;
151
152pub trait BoundsExt: Sized {
157 fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &App) -> Self;
159
160 fn maximized(display_id: Option<DisplayId>, cx: &App) -> Self;
162}
163
164impl BoundsExt for Bounds<Pixels> {
165 fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &App) -> Self {
166 let display = display_id
167 .and_then(|id| cx.find_display(id))
168 .or_else(|| cx.primary_display());
169
170 display
171 .map(|display| Bounds::centered_at(display.bounds().center(), size))
172 .unwrap_or_else(|| Bounds {
173 origin: point(px(0.), px(0.)),
174 size,
175 })
176 }
177
178 fn maximized(display_id: Option<DisplayId>, cx: &App) -> Self {
179 let display = display_id
180 .and_then(|id| cx.find_display(id))
181 .or_else(|| cx.primary_display());
182
183 display
184 .map(|display| display.bounds())
185 .unwrap_or_else(|| Bounds {
186 origin: point(px(0.), px(0.)),
187 size: size(px(1024.), px(768.)),
188 })
189 }
190}
191
192impl HasRemSize for Window<'_> {
193 fn rem_size(&self) -> Pixels {
194 Window::rem_size(self)
195 }
196}
197
198pub trait AppContext {
201 #[expect(
203 clippy::wrong_self_convention,
204 reason = "`App::new` is an ubiquitous function for creating entities"
205 )]
206 fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T>;
207
208 fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>;
211
212 fn insert_entity<T: 'static>(
216 &mut self,
217 reservation: Reservation<T>,
218 build_entity: impl FnOnce(&mut Context<T>) -> T,
219 ) -> Entity<T>;
220
221 fn update_entity<T, R>(
223 &mut self,
224 handle: &Entity<T>,
225 update: impl FnOnce(&mut T, &mut Context<T>) -> R,
226 ) -> R
227 where
228 T: 'static;
229
230 fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
232 where
233 T: 'static;
234
235 fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R
237 where
238 T: 'static;
239
240 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
242 where
243 F: FnOnce(AnyView, &mut Window, &mut App) -> T;
244
245 fn with_window<R>(
250 &mut self,
251 entity_id: EntityId,
252 f: impl FnOnce(&mut Window, &mut App) -> R,
253 ) -> Option<R>;
254
255 fn read_window<T, R>(
257 &self,
258 window: &WindowHandle<T>,
259 read: impl FnOnce(Entity<T>, &App) -> R,
260 ) -> Result<R>
261 where
262 T: 'static;
263
264 fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
266 where
267 R: Send + 'static;
268
269 fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
271 where
272 G: Global;
273}
274
275pub struct Reservation<T>(pub(crate) Slot<T>);
278
279impl<T: 'static> Reservation<T> {
280 pub fn entity_id(&self) -> EntityId {
282 self.0.entity_id()
283 }
284}
285
286pub trait VisualContext: AppContext {
289 type Result<T>;
291
292 fn window_handle(&self) -> AnyWindowHandle;
294
295 fn update_window_entity<T: 'static, R>(
297 &mut self,
298 entity: &Entity<T>,
299 update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
300 ) -> Self::Result<R>;
301
302 fn new_window_entity<T: 'static>(
304 &mut self,
305 build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
306 ) -> Self::Result<Entity<T>>;
307
308 fn replace_root_view<V>(
310 &mut self,
311 build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,
312 ) -> Self::Result<Entity<V>>
313 where
314 V: 'static + Render;
315
316 fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()>
318 where
319 V: Focusable;
320}
321
322pub trait EventEmitter<E: Any>: 'static {}
325
326pub trait BorrowAppContext {
329 fn set_global<T: Global>(&mut self, global: T);
331 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
333 where
334 G: Global;
335 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
337 where
338 G: Global + Default;
339}
340
341impl<C> BorrowAppContext for C
342where
343 C: std::borrow::BorrowMut<App>,
344{
345 fn set_global<G: Global>(&mut self, global: G) {
346 self.borrow_mut().set_global(global)
347 }
348
349 #[track_caller]
350 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
351 where
352 G: Global,
353 {
354 let mut global = self.borrow_mut().lease_global::<G>();
355 let result = f(&mut global, self);
356 self.borrow_mut().end_global_lease(global);
357 result
358 }
359
360 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
361 where
362 G: Global + Default,
363 {
364 self.borrow_mut().default_global::<G>();
365 self.update_global(f)
366 }
367}