gpui2.rs

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