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 std::{
 78    any::{Any, TypeId},
 79    borrow::BorrowMut,
 80};
 81use taffy::TaffyLayoutEngine;
 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: 'static + 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}