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