gpui2.rs

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