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 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 enum GlobalKey {
142    Numeric(usize),
143    View(EntityId),
144    Type(TypeId),
145}
146
147pub trait BorrowAppContext {
148    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
149    where
150        F: FnOnce(&mut Self) -> R;
151
152    fn set_global<T: 'static>(&mut self, global: T);
153}
154
155impl<C> BorrowAppContext for C
156where
157    C: BorrowMut<AppContext>,
158{
159    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
160    where
161        F: FnOnce(&mut Self) -> R,
162    {
163        self.borrow_mut().push_text_style(style);
164        let result = f(self);
165        self.borrow_mut().pop_text_style();
166        result
167    }
168
169    fn set_global<G: 'static>(&mut self, global: G) {
170        self.borrow_mut().set_global(global)
171    }
172}
173
174pub trait EventEmitter: 'static {
175    type Event: Any;
176}
177
178pub trait Flatten<T> {
179    fn flatten(self) -> Result<T>;
180}
181
182impl<T> Flatten<T> for Result<Result<T>> {
183    fn flatten(self) -> Result<T> {
184        self?
185    }
186}
187
188impl<T> Flatten<T> for Result<T> {
189    fn flatten(self) -> Result<T> {
190        self
191    }
192}
193
194#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
195pub struct SharedString(ArcCow<'static, str>);
196
197impl Default for SharedString {
198    fn default() -> Self {
199        Self(ArcCow::Owned("".into()))
200    }
201}
202
203impl AsRef<str> for SharedString {
204    fn as_ref(&self) -> &str {
205        &self.0
206    }
207}
208
209impl Borrow<str> for SharedString {
210    fn borrow(&self) -> &str {
211        self.as_ref()
212    }
213}
214
215impl std::fmt::Debug for SharedString {
216    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
217        self.0.fmt(f)
218    }
219}
220
221impl std::fmt::Display for SharedString {
222    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223        write!(f, "{}", self.0.as_ref())
224    }
225}
226
227impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
228    fn from(value: T) -> Self {
229        Self(value.into())
230    }
231}