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