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