gpui2.rs

  1#[macro_use]
  2mod action;
  3mod app;
  4mod assets;
  5mod color;
  6mod element;
  7mod elements;
  8mod executor;
  9mod geometry;
 10mod image_cache;
 11mod input;
 12mod interactive;
 13mod key_dispatch;
 14mod keymap;
 15mod platform;
 16pub mod prelude;
 17mod scene;
 18mod style;
 19mod styled;
 20mod subscription;
 21mod svg_renderer;
 22mod taffy;
 23#[cfg(any(test, feature = "test-support"))]
 24mod test;
 25mod text_system;
 26mod util;
 27mod view;
 28mod window;
 29
 30mod private {
 31    /// A mechanism for restricting implementations of a trait to only those in GPUI.
 32    /// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
 33    pub trait Sealed {}
 34}
 35
 36pub use action::*;
 37pub use anyhow::Result;
 38pub use app::*;
 39pub use assets::*;
 40pub use color::*;
 41pub use ctor::ctor;
 42pub use element::*;
 43pub use elements::*;
 44pub use executor::*;
 45pub use geometry::*;
 46pub use gpui2_macros::*;
 47pub use image_cache::*;
 48pub use input::*;
 49pub use interactive::*;
 50pub use key_dispatch::*;
 51pub use keymap::*;
 52pub use platform::*;
 53use private::Sealed;
 54pub use refineable::*;
 55pub use scene::*;
 56pub use serde;
 57pub use serde_json;
 58pub use smallvec;
 59pub use smol::Timer;
 60pub use style::*;
 61pub use styled::*;
 62pub use subscription::*;
 63pub use svg_renderer::*;
 64pub use taffy::{AvailableSpace, LayoutId};
 65#[cfg(any(test, feature = "test-support"))]
 66pub use test::*;
 67pub use text_system::*;
 68pub use util::arc_cow::ArcCow;
 69pub use view::*;
 70pub use window::*;
 71
 72use derive_more::{Deref, DerefMut};
 73use std::{
 74    any::{Any, TypeId},
 75    borrow::{Borrow, BorrowMut},
 76};
 77use taffy::TaffyLayoutEngine;
 78
 79type AnyBox = Box<dyn Any>;
 80
 81pub trait Context {
 82    type Result<T>;
 83
 84    fn build_model<T: 'static>(
 85        &mut self,
 86        build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
 87    ) -> Self::Result<Model<T>>;
 88
 89    fn update_model<T, R>(
 90        &mut self,
 91        handle: &Model<T>,
 92        update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
 93    ) -> Self::Result<R>
 94    where
 95        T: 'static;
 96
 97    fn read_model<T, R>(
 98        &self,
 99        handle: &Model<T>,
100        read: impl FnOnce(&T, &AppContext) -> R,
101    ) -> Self::Result<R>
102    where
103        T: 'static;
104
105    fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
106    where
107        F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
108}
109
110pub trait VisualContext: Context {
111    fn build_view<V>(
112        &mut self,
113        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
114    ) -> Self::Result<View<V>>
115    where
116        V: 'static + Render;
117
118    fn update_view<V: 'static, R>(
119        &mut self,
120        view: &View<V>,
121        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
122    ) -> Self::Result<R>;
123
124    fn replace_root_view<V>(
125        &mut self,
126        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
127    ) -> Self::Result<View<V>>
128    where
129        V: Render;
130}
131
132pub trait Entity<T>: Sealed {
133    type Weak: 'static;
134
135    fn entity_id(&self) -> EntityId;
136    fn downgrade(&self) -> Self::Weak;
137    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
138    where
139        Self: Sized;
140}
141
142pub trait EventEmitter<E: Any>: 'static {}
143
144pub enum GlobalKey {
145    Numeric(usize),
146    View(EntityId),
147    Type(TypeId),
148}
149
150pub trait BorrowAppContext {
151    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
152    where
153        F: FnOnce(&mut Self) -> R;
154
155    fn set_global<T: 'static>(&mut self, global: T);
156}
157
158impl<C> BorrowAppContext for C
159where
160    C: BorrowMut<AppContext>,
161{
162    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
163    where
164        F: FnOnce(&mut Self) -> R,
165    {
166        self.borrow_mut().push_text_style(style);
167        let result = f(self);
168        self.borrow_mut().pop_text_style();
169        result
170    }
171
172    fn set_global<G: 'static>(&mut self, global: G) {
173        self.borrow_mut().set_global(global)
174    }
175}
176
177pub trait Flatten<T> {
178    fn flatten(self) -> Result<T>;
179}
180
181impl<T> Flatten<T> for Result<Result<T>> {
182    fn flatten(self) -> Result<T> {
183        self?
184    }
185}
186
187impl<T> Flatten<T> for Result<T> {
188    fn flatten(self) -> Result<T> {
189        self
190    }
191}
192
193#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
194pub struct SharedString(ArcCow<'static, str>);
195
196impl Default for SharedString {
197    fn default() -> Self {
198        Self(ArcCow::Owned("".into()))
199    }
200}
201
202impl AsRef<str> for SharedString {
203    fn as_ref(&self) -> &str {
204        &self.0
205    }
206}
207
208impl Borrow<str> for SharedString {
209    fn borrow(&self) -> &str {
210        self.as_ref()
211    }
212}
213
214impl std::fmt::Debug for SharedString {
215    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
216        self.0.fmt(f)
217    }
218}
219
220impl std::fmt::Display for SharedString {
221    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
222        write!(f, "{}", self.0.as_ref())
223    }
224}
225
226impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
227    fn from(value: T) -> Self {
228        Self(value.into())
229    }
230}