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    fn read_window<T, R>(
110        &self,
111        window: &WindowHandle<T>,
112        read: impl FnOnce(View<T>, &AppContext) -> R,
113    ) -> Result<R>
114    where
115        T: 'static;
116}
117
118pub trait VisualContext: Context {
119    fn build_view<V>(
120        &mut self,
121        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
122    ) -> Self::Result<View<V>>
123    where
124        V: 'static + Render;
125
126    fn update_view<V: 'static, R>(
127        &mut self,
128        view: &View<V>,
129        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
130    ) -> Self::Result<R>;
131
132    fn replace_root_view<V>(
133        &mut self,
134        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
135    ) -> Self::Result<View<V>>
136    where
137        V: Render;
138}
139
140pub trait Entity<T>: Sealed {
141    type Weak: 'static;
142
143    fn entity_id(&self) -> EntityId;
144    fn downgrade(&self) -> Self::Weak;
145    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
146    where
147        Self: Sized;
148}
149
150pub trait EventEmitter<E: Any>: 'static {}
151
152pub enum GlobalKey {
153    Numeric(usize),
154    View(EntityId),
155    Type(TypeId),
156}
157
158pub trait BorrowAppContext {
159    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
160    where
161        F: FnOnce(&mut Self) -> R;
162
163    fn set_global<T: 'static>(&mut self, global: T);
164}
165
166impl<C> BorrowAppContext for C
167where
168    C: BorrowMut<AppContext>,
169{
170    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
171    where
172        F: FnOnce(&mut Self) -> R,
173    {
174        self.borrow_mut().push_text_style(style);
175        let result = f(self);
176        self.borrow_mut().pop_text_style();
177        result
178    }
179
180    fn set_global<G: 'static>(&mut self, global: G) {
181        self.borrow_mut().set_global(global)
182    }
183}
184
185pub trait Flatten<T> {
186    fn flatten(self) -> Result<T>;
187}
188
189impl<T> Flatten<T> for Result<Result<T>> {
190    fn flatten(self) -> Result<T> {
191        self?
192    }
193}
194
195impl<T> Flatten<T> for Result<T> {
196    fn flatten(self) -> Result<T> {
197        self
198    }
199}
200
201#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
202pub struct SharedString(ArcCow<'static, str>);
203
204impl Default for SharedString {
205    fn default() -> Self {
206        Self(ArcCow::Owned("".into()))
207    }
208}
209
210impl AsRef<str> for SharedString {
211    fn as_ref(&self) -> &str {
212        &self.0
213    }
214}
215
216impl Borrow<str> for SharedString {
217    fn borrow(&self) -> &str {
218        self.as_ref()
219    }
220}
221
222impl std::fmt::Debug for SharedString {
223    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
224        self.0.fmt(f)
225    }
226}
227
228impl std::fmt::Display for SharedString {
229    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
230        write!(f, "{}", self.0.as_ref())
231    }
232}
233
234impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
235    fn from(value: T) -> Self {
236        Self(value.into())
237    }
238}