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