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