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