| 1 | #![doc = include_str!("../README.md")] |
| 2 | #![deny(missing_docs)] |
| 3 | #![allow(clippy::type_complexity)] |
| 4 | #![allow(clippy::collapsible_else_if)] |
| 5 | #![allow(unused_mut)] |
| 6 | |
| 7 | extern crate self as gpui; |
| 8 | |
| 9 | #[macro_use] |
| 10 | mod action; |
| 11 | mod app; |
| 12 | |
| 13 | mod arena; |
| 14 | mod asset_cache; |
| 15 | mod assets; |
| 16 | mod bounds_tree; |
| 17 | mod color; |
| 18 | |
| 19 | pub mod colors; |
| 20 | mod element; |
| 21 | mod elements; |
| 22 | mod executor; |
| 23 | mod geometry; |
| 24 | mod global; |
| 25 | mod input; |
| 26 | mod inspector; |
| 27 | mod interactive; |
| 28 | mod key_dispatch; |
| 29 | mod keymap; |
| 30 | mod path_builder; |
| 31 | mod platform; |
| 32 | pub mod prelude; |
| 33 | mod scene; |
| 34 | mod shared_string; |
| 35 | mod shared_uri; |
| 36 | mod style; |
| 37 | mod styled; |
| 38 | mod subscription; |
| 39 | mod svg_renderer; |
| 40 | mod tab_stop; |
| 41 | mod taffy; |
| 42 | #[cfg(any(test, feature = "test-support"))] |
| 43 | pub mod test; |
| 44 | mod text_system; |
| 45 | mod util; |
| 46 | mod view; |
| 47 | mod window; |
| 48 | |
| 49 | #[cfg(doc)] |
| 50 | pub mod _ownership_and_data_flow; |
| 51 | |
| 52 | |
| 53 | #[doc(hidden)] |
| 54 | pub 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 | |
| 62 | mod seal { |
| 63 | |
| 64 | |
| 65 | pub trait Sealed {} |
| 66 | } |
| 67 | |
| 68 | pub use action::*; |
| 69 | pub use anyhow::Result; |
| 70 | pub use app::*; |
| 71 | pub(crate) use arena::*; |
| 72 | pub use asset_cache::*; |
| 73 | pub use assets::*; |
| 74 | pub use color::*; |
| 75 | pub use ctor::ctor; |
| 76 | pub use element::*; |
| 77 | pub use elements::*; |
| 78 | pub use executor::*; |
| 79 | pub use geometry::*; |
| 80 | pub use global::*; |
| 81 | pub use gpui_macros::{AppContext, IntoElement, Render, VisualContext, register_action, test}; |
| 82 | pub use http_client; |
| 83 | pub use input::*; |
| 84 | pub use inspector::*; |
| 85 | pub use interactive::*; |
| 86 | use key_dispatch::*; |
| 87 | pub use keymap::*; |
| 88 | pub use path_builder::*; |
| 89 | pub use platform::*; |
| 90 | pub use refineable::*; |
| 91 | pub use scene::*; |
| 92 | pub use shared_string::*; |
| 93 | pub use shared_uri::*; |
| 94 | pub use smol::Timer; |
| 95 | pub use style::*; |
| 96 | pub use styled::*; |
| 97 | pub use subscription::*; |
| 98 | use svg_renderer::*; |
| 99 | pub(crate) use tab_stop::*; |
| 100 | pub use taffy::{AvailableSpace, LayoutId}; |
| 101 | #[cfg(any(test, feature = "test-support"))] |
| 102 | pub use test::*; |
| 103 | pub use text_system::*; |
| 104 | #[cfg(any(test, feature = "test-support"))] |
| 105 | pub use util::smol_timeout; |
| 106 | pub use util::{FutureExt, Timeout, arc_cow::ArcCow}; |
| 107 | pub use view::*; |
| 108 | pub use window::*; |
| 109 | |
| 110 | use std::{any::Any, borrow::BorrowMut, future::Future}; |
| 111 | use taffy::TaffyLayoutEngine; |
| 112 | |
| 113 | |
| 114 | |
| 115 | pub trait AppContext { |
| 116 | |
| 117 | |
| 118 | type Result<T>; |
| 119 | |
| 120 | |
| 121 | #[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 | |
| 131 | |
| 132 | fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>>; |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | 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 | |
| 144 | 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 | |
| 153 | fn as_mut<'a, T>(&'a mut self, handle: &Entity<T>) -> Self::Result<GpuiBorrow<'a, T>> |
| 154 | where |
| 155 | T: 'static; |
| 156 | |
| 157 | |
| 158 | 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 | |
| 167 | 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 | |
| 172 | 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 | |
| 181 | fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R> |
| 182 | where |
| 183 | R: Send + 'static; |
| 184 | |
| 185 | |
| 186 | fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R> |
| 187 | where |
| 188 | G: Global; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | pub struct Reservation<T>(pub(crate) Slot<T>); |
| 194 | |
| 195 | impl<T: 'static> Reservation<T> { |
| 196 | |
| 197 | pub fn entity_id(&self) -> EntityId { |
| 198 | self.0.entity_id() |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | pub trait VisualContext: AppContext { |
| 205 | |
| 206 | fn window_handle(&self) -> AnyWindowHandle; |
| 207 | |
| 208 | |
| 209 | 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 | |
| 216 | 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 | |
| 222 | 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 | |
| 230 | fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()> |
| 231 | where |
| 232 | V: Focusable; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | pub trait EventEmitter<E: Any>: 'static {} |
| 238 | |
| 239 | |
| 240 | |
| 241 | pub trait BorrowAppContext { |
| 242 | |
| 243 | fn set_global<T: Global>(&mut self, global: T); |
| 244 | |
| 245 | fn update_global<G, R>(&mut self, f: impl FnOnce(&mut G, &mut Self) -> R) -> R |
| 246 | where |
| 247 | G: Global; |
| 248 | |
| 249 | 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 | |
| 254 | impl<C> BorrowAppContext for C |
| 255 | where |
| 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 | |
| 282 | |
| 283 | pub trait Flatten<T> { |
| 284 | |
| 285 | fn flatten(self) -> Result<T>; |
| 286 | } |
| 287 | |
| 288 | impl<T> Flatten<T> for Result<Result<T>> { |
| 289 | fn flatten(self) -> Result<T> { |
| 290 | self? |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | impl<T> Flatten<T> for Result<T> { |
| 295 | fn flatten(self) -> Result<T> { |
| 296 | self |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | #[derive(Default, Debug, serde::Serialize, serde::Deserialize, Clone)] |
| 302 | pub struct GpuSpecs { |
| 303 | |
| 304 | pub is_software_emulated: bool, |
| 305 | |
| 306 | pub device_name: String, |
| 307 | |
| 308 | pub driver_name: String, |
| 309 | |
| 310 | pub driver_info: String, |
| 311 | } |