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;
57#[cfg(any(test, feature = "bench-support"))]
59pub mod randomized_element_tree;
60#[cfg(any(test, feature = "test-support", feature = "bench-support"))]
61mod seeds;
62mod shared_uri;
63mod spring;
64mod style;
65mod styled;
66mod subscription;
67mod svg_renderer;
68mod tab_stop;
69#[cfg(any(test, feature = "test-support"))]
70pub mod test;
71mod text_system;
72mod util;
73mod view;
74mod window;
75
76#[cfg(any(test, feature = "test-support"))]
77pub use proptest;
78#[cfg(any(test, feature = "test-support", feature = "bench-support"))]
79pub use seeds::calculate_seeds;
80
81pub mod _accessibility;
83pub mod _authoring;
85pub mod _frame_pipeline;
87pub mod _ownership_and_data_flow;
89
90#[doc(hidden)]
92pub mod private {
93 pub use anyhow;
94 pub use inventory;
95 pub use rand;
96 pub use schemars;
97 pub use serde;
98 pub use serde_json;
99}
100
101mod seal {
102 pub trait Sealed {}
105}
106
107pub use accesskit;
108pub use accesskit::Action as AccessibleAction;
109pub use accesskit::{Orientation, Role, Toggled};
110pub use action::*;
111pub use anyhow::Result;
112pub use app::*;
113pub(crate) use arena::*;
114pub use asset_cache::*;
115pub use assets::*;
116pub use ctor::ctor;
117#[cfg(feature = "profiler")]
118pub use debug_overlay::*;
119pub use element::*;
120pub use elements::*;
121pub use executor::*;
122pub use gestures::*;
123pub use global::*;
124pub use gpui_macros::{
125 AppContext, IntoElement, Render, VisualContext, bench, property_test, register_action, test,
126};
127pub use spring::*;
128
129#[macro_export]
146macro_rules! bench_group {
147 (name = $name:ident; config = $config:expr; targets = $($target:path),+ $(,)?) => {
148 criterion::criterion_group! {
149 name = $name;
150 config = ($config).with_measurement($crate::BenchMeasurement::from_env_or_exit());
151 targets = $($target),+
152 }
153 };
154 ($name:ident, $($target:path),+ $(,)?) => {
155 $crate::bench_group! {
156 name = $name;
157 config = criterion::Criterion::default();
158 targets = $($target),+
159 }
160 };
161}
162
163#[macro_export]
179macro_rules! bench_main {
180 (allocator = none; $($groups:tt)*) => {
181 criterion::criterion_main!($($groups)*);
182 };
183 (allocator = $allocator:path; $($groups:tt)*) => {
184 #[global_allocator]
185 static GPUI_BENCH_ALLOCATOR: $crate::CountingAllocator<$allocator> =
186 $crate::CountingAllocator::new($allocator);
187
188 criterion::criterion_main!($($groups)*);
189 };
190 ($($groups:tt)*) => {
191 $crate::bench_main!(allocator = ::std::alloc::System; $($groups)*);
192 };
193}
194pub use gpui_engine::*;
195pub use gpui_engine_default::*;
196pub use gpui_platform::*;
197pub use gpui_util::arc_cow::ArcCow;
198pub use http_client;
199pub use input::*;
200pub use inspector::*;
201pub use interactive::*;
202use key_dispatch::*;
203pub use keymap::*;
204pub use path_builder::*;
205pub use platform::*;
206pub use profiler::*;
207pub use refineable::*;
208pub use shared_uri::*;
209use std::{any::Any, future::Future};
210pub use style::*;
211pub use styled::*;
212pub use subscription::*;
213pub use svg_renderer::*;
214pub(crate) use tab_stop::*;
215#[cfg(any(test, feature = "test-support"))]
216pub use test::*;
217pub use text_system::*;
218pub use util::{FluentBuilder, FutureExt, Timeout};
219pub use view::*;
220pub use window::*;
221
222#[cfg(not(target_family = "wasm"))]
223pub use pollster::block_on;
224
225pub trait BoundsExt: Sized {
230 fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &App) -> Self;
232
233 fn maximized(display_id: Option<DisplayId>, cx: &App) -> Self;
235}
236
237impl BoundsExt for Bounds<Pixels> {
238 fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &App) -> Self {
239 let display = display_id
240 .and_then(|id| cx.find_display(id))
241 .or_else(|| cx.primary_display());
242
243 display
244 .map(|display| {
245 let visible_bounds = display.visible_bounds();
246 Bounds::centered_at(visible_bounds.center(), size.min(&visible_bounds.size))
247 })
248 .unwrap_or_else(|| Bounds {
249 origin: point(px(0.), px(0.)),
250 size,
251 })
252 }
253
254 fn maximized(display_id: Option<DisplayId>, cx: &App) -> Self {
255 let display = display_id
256 .and_then(|id| cx.find_display(id))
257 .or_else(|| cx.primary_display());
258
259 display
260 .map(|display| display.bounds())
261 .unwrap_or_else(|| Bounds {
262 origin: point(px(0.), px(0.)),
263 size: size(px(1024.), px(768.)),
264 })
265 }
266}
267
268impl HasRemSize for Window<'_> {
269 fn rem_size(&self) -> Pixels {
270 Window::rem_size(self)
271 }
272}
273
274pub trait AppContext {
277 #[expect(
279 clippy::wrong_self_convention,
280 reason = "`App::new` is an ubiquitous function for creating entities"
281 )]
282 fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T>;
283
284 fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>;
287
288 fn insert_entity<T: 'static>(
292 &mut self,
293 reservation: Reservation<T>,
294 build_entity: impl FnOnce(&mut Context<T>) -> T,
295 ) -> Entity<T>;
296
297 fn update_entity<T, R>(
299 &mut self,
300 handle: &Entity<T>,
301 update: impl FnOnce(&mut T, &mut Context<T>) -> R,
302 ) -> R
303 where
304 T: 'static;
305
306 fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> GpuiBorrow<'a, T>
308 where
309 T: 'static;
310
311 fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R
313 where
314 T: 'static;
315
316 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
318 where
319 F: FnOnce(AnyView, &mut Window, &mut App) -> T;
320
321 fn with_window<R>(
326 &mut self,
327 entity_id: EntityId,
328 f: impl FnOnce(&mut Window, &mut App) -> R,
329 ) -> Option<R>;
330
331 fn read_window<T, R>(
333 &self,
334 window: &WindowHandle<T>,
335 read: impl FnOnce(Entity<T>, &App) -> R,
336 ) -> Result<R>
337 where
338 T: 'static;
339
340 fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
342 where
343 R: Send + 'static;
344
345 fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
347 where
348 G: Global;
349}
350
351pub struct Reservation<T>(pub(crate) Slot<T>);
354
355impl<T: 'static> Reservation<T> {
356 pub fn entity_id(&self) -> EntityId {
358 self.0.entity_id()
359 }
360}
361
362pub trait VisualContext: AppContext {
365 type Result<T>;
367
368 fn window_handle(&self) -> AnyWindowHandle;
370
371 fn update_window_entity<T: 'static, R>(
373 &mut self,
374 entity: &Entity<T>,
375 update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
376 ) -> Self::Result<R>;
377
378 fn new_window_entity<T: 'static>(
380 &mut self,
381 build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
382 ) -> Self::Result<Entity<T>>;
383
384 fn replace_root_view<V>(
386 &mut self,
387 build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,
388 ) -> Self::Result<Entity<V>>
389 where
390 V: 'static + Render;
391
392 fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()>
394 where
395 V: Focusable;
396}
397
398pub trait EventEmitter<E: Any>: 'static {}
401
402pub trait BorrowAppContext {
405 fn set_global<T: Global>(&mut self, global: T);
407 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
409 where
410 G: Global;
411 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
413 where
414 G: Global + Default;
415}
416
417impl<C> BorrowAppContext for C
418where
419 C: std::borrow::BorrowMut<App>,
420{
421 fn set_global<G: Global>(&mut self, global: G) {
422 self.borrow_mut().set_global(global)
423 }
424
425 #[track_caller]
426 fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
427 where
428 G: Global,
429 {
430 let mut global = self.borrow_mut().lease_global::<G>();
431 let result = f(&mut global, self);
432 self.borrow_mut().end_global_lease(global);
433 result
434 }
435
436 fn update_default_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R
437 where
438 G: Global + Default,
439 {
440 self.borrow_mut().default_global::<G>();
441 self.update_global(f)
442 }
443}