gpui2.rs

  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 platform::*;
 53use private::Sealed;
 54pub use refineable::*;
 55pub use scene::*;
 56pub use serde;
 57pub use serde_json;
 58pub use smallvec;
 59pub use smol::Timer;
 60pub use style::*;
 61pub use styled::*;
 62pub use subscription::*;
 63pub use svg_renderer::*;
 64pub use taffy::{AvailableSpace, LayoutId};
 65#[cfg(any(test, feature = "test-support"))]
 66pub use test::*;
 67pub use text_system::*;
 68pub use util::arc_cow::ArcCow;
 69pub use view::*;
 70pub use window::*;
 71
 72use derive_more::{Deref, DerefMut};
 73use std::{
 74    any::{Any, TypeId},
 75    borrow::{Borrow, BorrowMut},
 76};
 77use taffy::TaffyLayoutEngine;
 78
 79type AnyBox = Box<dyn Any>;
 80
 81pub trait Context {
 82    type Result<T>;
 83
 84    fn build_model<T: 'static>(
 85        &mut self,
 86        build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
 87    ) -> Self::Result<Model<T>>;
 88
 89    fn update_model<T, R>(
 90        &mut self,
 91        handle: &Model<T>,
 92        update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
 93    ) -> Self::Result<R>
 94    where
 95        T: 'static;
 96
 97    fn read_model<T, R>(
 98        &self,
 99        handle: &Model<T>,
100        read: impl FnOnce(&T, &AppContext) -> R,
101    ) -> Self::Result<R>
102    where
103        T: 'static;
104
105    fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
106    where
107        F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
108
109    fn read_window<T, R>(
110        &self,
111        window: &WindowHandle<T>,
112        read: impl FnOnce(View<T>, &AppContext) -> R,
113    ) -> Result<R>
114    where
115        T: 'static;
116}
117
118pub trait VisualContext: Context {
119    fn build_view<V>(
120        &mut self,
121        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
122    ) -> Self::Result<View<V>>
123    where
124        V: 'static + Render;
125
126    fn update_view<V: 'static, R>(
127        &mut self,
128        view: &View<V>,
129        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
130    ) -> Self::Result<R>;
131
132    fn replace_root_view<V>(
133        &mut self,
134        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
135    ) -> Self::Result<View<V>>
136    where
137        V: Render;
138}
139
140pub trait Entity<T>: Sealed {
141    type Weak: 'static;
142
143    fn entity_id(&self) -> EntityId;
144    fn downgrade(&self) -> Self::Weak;
145    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
146    where
147        Self: Sized;
148}
149
150pub trait EventEmitter<E: Any>: 'static {}
151
152pub enum GlobalKey {
153    Numeric(usize),
154    View(EntityId),
155    Type(TypeId),
156}
157
158pub trait BorrowAppContext {
159    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
160    where
161        F: FnOnce(&mut Self) -> R;
162
163    fn set_global<T: 'static>(&mut self, global: T);
164}
165
166impl<C> BorrowAppContext for C
167where
168    C: BorrowMut<AppContext>,
169{
170    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
171    where
172        F: FnOnce(&mut Self) -> R,
173    {
174        if let Some(style) = style {
175            self.borrow_mut().push_text_style(style);
176            let result = f(self);
177            self.borrow_mut().pop_text_style();
178            result
179        } else {
180            f(self)
181        }
182    }
183
184    fn set_global<G: 'static>(&mut self, global: G) {
185        self.borrow_mut().set_global(global)
186    }
187}
188
189pub trait Flatten<T> {
190    fn flatten(self) -> Result<T>;
191}
192
193impl<T> Flatten<T> for Result<Result<T>> {
194    fn flatten(self) -> Result<T> {
195        self?
196    }
197}
198
199impl<T> Flatten<T> for Result<T> {
200    fn flatten(self) -> Result<T> {
201        self
202    }
203}
204
205#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
206pub struct SharedString(ArcCow<'static, str>);
207
208impl Default for SharedString {
209    fn default() -> Self {
210        Self(ArcCow::Owned("".into()))
211    }
212}
213
214impl AsRef<str> for SharedString {
215    fn as_ref(&self) -> &str {
216        &self.0
217    }
218}
219
220impl Borrow<str> for SharedString {
221    fn borrow(&self) -> &str {
222        self.as_ref()
223    }
224}
225
226impl std::fmt::Debug for SharedString {
227    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
228        self.0.fmt(f)
229    }
230}
231
232impl std::fmt::Display for SharedString {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        write!(f, "{}", self.0.as_ref())
235    }
236}
237
238impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
239    fn from(value: T) -> Self {
240        Self(value.into())
241    }
242}