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