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