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