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 linkme;
 53pub use platform::*;
 54use private::Sealed;
 55pub use refineable::*;
 56pub use scene::*;
 57pub use serde;
 58pub use serde_json;
 59pub use smallvec;
 60pub use smol::Timer;
 61pub use style::*;
 62pub use styled::*;
 63pub use subscription::*;
 64pub use svg_renderer::*;
 65pub use taffy::{AvailableSpace, LayoutId};
 66#[cfg(any(test, feature = "test-support"))]
 67pub use test::*;
 68pub use text_system::*;
 69pub use util::arc_cow::ArcCow;
 70pub use view::*;
 71pub use window::*;
 72
 73use derive_more::{Deref, DerefMut};
 74use std::{
 75    any::{Any, TypeId},
 76    borrow::{Borrow, BorrowMut},
 77};
 78use taffy::TaffyLayoutEngine;
 79
 80type AnyBox = Box<dyn Any>;
 81
 82pub trait Context {
 83    type Result<T>;
 84
 85    fn build_model<T: 'static>(
 86        &mut self,
 87        build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
 88    ) -> Self::Result<Model<T>>;
 89
 90    fn update_model<T, R>(
 91        &mut self,
 92        handle: &Model<T>,
 93        update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
 94    ) -> Self::Result<R>
 95    where
 96        T: 'static;
 97
 98    fn read_model<T, R>(
 99        &self,
100        handle: &Model<T>,
101        read: impl FnOnce(&T, &AppContext) -> R,
102    ) -> Self::Result<R>
103    where
104        T: 'static;
105
106    fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
107    where
108        F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
109
110    fn read_window<T, R>(
111        &self,
112        window: &WindowHandle<T>,
113        read: impl FnOnce(View<T>, &AppContext) -> R,
114    ) -> Result<R>
115    where
116        T: 'static;
117}
118
119pub trait VisualContext: Context {
120    fn build_view<V>(
121        &mut self,
122        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
123    ) -> Self::Result<View<V>>
124    where
125        V: 'static + Render;
126
127    fn update_view<V: 'static, R>(
128        &mut self,
129        view: &View<V>,
130        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
131    ) -> Self::Result<R>;
132
133    fn replace_root_view<V>(
134        &mut self,
135        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
136    ) -> Self::Result<View<V>>
137    where
138        V: Render;
139
140    fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
141    where
142        V: FocusableView;
143}
144
145pub trait Entity<T>: Sealed {
146    type Weak: 'static;
147
148    fn entity_id(&self) -> EntityId;
149    fn downgrade(&self) -> Self::Weak;
150    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
151    where
152        Self: Sized;
153}
154
155pub trait EventEmitter<E: Any>: 'static {}
156
157pub enum GlobalKey {
158    Numeric(usize),
159    View(EntityId),
160    Type(TypeId),
161}
162
163pub trait BorrowAppContext {
164    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
165    where
166        F: FnOnce(&mut Self) -> R;
167
168    fn set_global<T: 'static>(&mut self, global: T);
169}
170
171impl<C> BorrowAppContext for C
172where
173    C: BorrowMut<AppContext>,
174{
175    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
176    where
177        F: FnOnce(&mut Self) -> R,
178    {
179        if let Some(style) = style {
180            self.borrow_mut().push_text_style(style);
181            let result = f(self);
182            self.borrow_mut().pop_text_style();
183            result
184        } else {
185            f(self)
186        }
187    }
188
189    fn set_global<G: 'static>(&mut self, global: G) {
190        self.borrow_mut().set_global(global)
191    }
192}
193
194pub trait Flatten<T> {
195    fn flatten(self) -> Result<T>;
196}
197
198impl<T> Flatten<T> for Result<Result<T>> {
199    fn flatten(self) -> Result<T> {
200        self?
201    }
202}
203
204impl<T> Flatten<T> for Result<T> {
205    fn flatten(self) -> Result<T> {
206        self
207    }
208}
209
210#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
211pub struct SharedString(ArcCow<'static, str>);
212
213impl Default for SharedString {
214    fn default() -> Self {
215        Self(ArcCow::Owned("".into()))
216    }
217}
218
219impl AsRef<str> for SharedString {
220    fn as_ref(&self) -> &str {
221        &self.0
222    }
223}
224
225impl Borrow<str> for SharedString {
226    fn borrow(&self) -> &str {
227        self.as_ref()
228    }
229}
230
231impl std::fmt::Debug for SharedString {
232    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233        self.0.fmt(f)
234    }
235}
236
237impl std::fmt::Display for SharedString {
238    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
239        write!(f, "{}", self.0.as_ref())
240    }
241}
242
243impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
244    fn from(value: T) -> Self {
245        Self(value.into())
246    }
247}