1#[macro_use]
2mod action;
3mod app;
4mod assets;
5mod color;
6mod element;
7mod elements;
8mod executor;
9mod geometry;
10mod image_cache;
11mod input;
12mod interactive;
13mod key_dispatch;
14mod keymap;
15mod platform;
16pub mod prelude;
17mod scene;
18mod style;
19mod styled;
20mod subscription;
21mod svg_renderer;
22mod taffy;
23#[cfg(any(test, feature = "test-support"))]
24mod test;
25mod text_system;
26mod util;
27mod view;
28mod window;
29
30mod private {
31 /// A mechanism for restricting implementations of a trait to only those in GPUI.
32 /// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
33 pub trait Sealed {}
34}
35
36pub use action::*;
37pub use anyhow::Result;
38pub use app::*;
39pub use assets::*;
40pub use color::*;
41pub use ctor::ctor;
42pub use element::*;
43pub use elements::*;
44pub use executor::*;
45pub use geometry::*;
46pub use gpui2_macros::*;
47pub use image_cache::*;
48pub use input::*;
49pub use interactive::*;
50pub use key_dispatch::*;
51pub use keymap::*;
52pub use linkme;
53pub use platform::*;
54use private::Sealed;
55pub use refineable::*;
56pub use scene::*;
57pub use serde;
58pub use serde_derive;
59pub use serde_json;
60pub use smallvec;
61pub use smol::Timer;
62pub use style::*;
63pub use styled::*;
64pub use subscription::*;
65pub use svg_renderer::*;
66pub use taffy::{AvailableSpace, LayoutId};
67#[cfg(any(test, feature = "test-support"))]
68pub use test::*;
69pub use text_system::*;
70pub use util::arc_cow::ArcCow;
71pub use view::*;
72pub use window::*;
73
74use derive_more::{Deref, DerefMut};
75use std::{
76 any::{Any, TypeId},
77 borrow::{Borrow, BorrowMut},
78};
79use taffy::TaffyLayoutEngine;
80
81type AnyBox = Box<dyn Any>;
82
83pub trait Context {
84 type Result<T>;
85
86 fn build_model<T: 'static>(
87 &mut self,
88 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
89 ) -> Self::Result<Model<T>>;
90
91 fn update_model<T, R>(
92 &mut self,
93 handle: &Model<T>,
94 update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
95 ) -> Self::Result<R>
96 where
97 T: 'static;
98
99 fn read_model<T, R>(
100 &self,
101 handle: &Model<T>,
102 read: impl FnOnce(&T, &AppContext) -> R,
103 ) -> Self::Result<R>
104 where
105 T: 'static;
106
107 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
108 where
109 F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
110
111 fn read_window<T, R>(
112 &self,
113 window: &WindowHandle<T>,
114 read: impl FnOnce(View<T>, &AppContext) -> R,
115 ) -> Result<R>
116 where
117 T: 'static;
118}
119
120pub trait VisualContext: Context {
121 fn build_view<V>(
122 &mut self,
123 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
124 ) -> Self::Result<View<V>>
125 where
126 V: 'static + Render;
127
128 fn update_view<V: 'static, R>(
129 &mut self,
130 view: &View<V>,
131 update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
132 ) -> Self::Result<R>;
133
134 fn replace_root_view<V>(
135 &mut self,
136 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
137 ) -> Self::Result<View<V>>
138 where
139 V: Render;
140
141 fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
142 where
143 V: FocusableView;
144}
145
146pub trait Entity<T>: Sealed {
147 type Weak: 'static;
148
149 fn entity_id(&self) -> EntityId;
150 fn downgrade(&self) -> Self::Weak;
151 fn upgrade_from(weak: &Self::Weak) -> Option<Self>
152 where
153 Self: Sized;
154}
155
156pub trait EventEmitter<E: Any>: 'static {}
157
158pub enum GlobalKey {
159 Numeric(usize),
160 View(EntityId),
161 Type(TypeId),
162}
163
164pub trait BorrowAppContext {
165 fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
166 where
167 F: FnOnce(&mut Self) -> R;
168
169 fn set_global<T: 'static>(&mut self, global: T);
170}
171
172impl<C> BorrowAppContext for C
173where
174 C: BorrowMut<AppContext>,
175{
176 fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
177 where
178 F: FnOnce(&mut Self) -> R,
179 {
180 if let Some(style) = style {
181 self.borrow_mut().push_text_style(style);
182 let result = f(self);
183 self.borrow_mut().pop_text_style();
184 result
185 } else {
186 f(self)
187 }
188 }
189
190 fn set_global<G: 'static>(&mut self, global: G) {
191 self.borrow_mut().set_global(global)
192 }
193}
194
195pub trait Flatten<T> {
196 fn flatten(self) -> Result<T>;
197}
198
199impl<T> Flatten<T> for Result<Result<T>> {
200 fn flatten(self) -> Result<T> {
201 self?
202 }
203}
204
205impl<T> Flatten<T> for Result<T> {
206 fn flatten(self) -> Result<T> {
207 self
208 }
209}
210
211#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
212pub struct SharedString(ArcCow<'static, str>);
213
214impl Default for SharedString {
215 fn default() -> Self {
216 Self(ArcCow::Owned("".into()))
217 }
218}
219
220impl AsRef<str> for SharedString {
221 fn as_ref(&self) -> &str {
222 &self.0
223 }
224}
225
226impl Borrow<str> for SharedString {
227 fn borrow(&self) -> &str {
228 self.as_ref()
229 }
230}
231
232impl std::fmt::Debug for SharedString {
233 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234 self.0.fmt(f)
235 }
236}
237
238impl std::fmt::Display for SharedString {
239 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
240 write!(f, "{}", self.0.as_ref())
241 }
242}
243
244impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
245 fn from(value: T) -> Self {
246 Self(value.into())
247 }
248}