gpui2.rs

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