gpui2.rs

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