gpui2.rs

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