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 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<T, R>(
109        &self,
110        window: &WindowHandle<T>,
111        read: impl FnOnce(View<T>, &AppContext) -> R,
112    ) -> Result<R>
113    where
114        T: 'static;
115}
116
117pub trait VisualContext: Context {
118    fn build_view<V>(
119        &mut self,
120        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
121    ) -> Self::Result<View<V>>
122    where
123        V: 'static + Render;
124
125    fn update_view<V: 'static, R>(
126        &mut self,
127        view: &View<V>,
128        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
129    ) -> Self::Result<R>;
130
131    fn replace_root_view<V>(
132        &mut self,
133        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
134    ) -> Self::Result<View<V>>
135    where
136        V: Render;
137}
138
139pub trait Entity<T>: Sealed {
140    type Weak: 'static;
141
142    fn entity_id(&self) -> EntityId;
143    fn downgrade(&self) -> Self::Weak;
144    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
145    where
146        Self: Sized;
147}
148
149pub trait EventEmitter<E: Any>: 'static {}
150
151pub enum GlobalKey {
152    Numeric(usize),
153    View(EntityId),
154    Type(TypeId),
155}
156
157pub trait BorrowAppContext {
158    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
159    where
160        F: FnOnce(&mut Self) -> R;
161
162    fn set_global<T: 'static>(&mut self, global: T);
163}
164
165impl<C> BorrowAppContext for C
166where
167    C: BorrowMut<AppContext>,
168{
169    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
170    where
171        F: FnOnce(&mut Self) -> R,
172    {
173        self.borrow_mut().push_text_style(style);
174        let result = f(self);
175        self.borrow_mut().pop_text_style();
176        result
177    }
178
179    fn set_global<G: 'static>(&mut self, global: G) {
180        self.borrow_mut().set_global(global)
181    }
182}
183
184pub trait Flatten<T> {
185    fn flatten(self) -> Result<T>;
186}
187
188impl<T> Flatten<T> for Result<Result<T>> {
189    fn flatten(self) -> Result<T> {
190        self?
191    }
192}
193
194impl<T> Flatten<T> for Result<T> {
195    fn flatten(self) -> Result<T> {
196        self
197    }
198}
199
200#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
201pub struct SharedString(ArcCow<'static, str>);
202
203impl Default for SharedString {
204    fn default() -> Self {
205        Self(ArcCow::Owned("".into()))
206    }
207}
208
209impl AsRef<str> for SharedString {
210    fn as_ref(&self) -> &str {
211        &self.0
212    }
213}
214
215impl Borrow<str> for SharedString {
216    fn borrow(&self) -> &str {
217        self.as_ref()
218    }
219}
220
221impl std::fmt::Debug for SharedString {
222    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223        self.0.fmt(f)
224    }
225}
226
227impl std::fmt::Display for SharedString {
228    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
229        write!(f, "{}", self.0.as_ref())
230    }
231}
232
233impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
234    fn from(value: T) -> Self {
235        Self(value.into())
236    }
237}