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