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
 91/// Here's a spelling mistake: visibile
 92pub trait Context {
 93    type Result<T>;
 94
 95    fn new_model<T: 'static>(
 96        &mut self,
 97        build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
 98    ) -> Self::Result<Model<T>>;
 99
100    fn update_model<T, R>(
101        &mut self,
102        handle: &Model<T>,
103        update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
104    ) -> Self::Result<R>
105    where
106        T: 'static;
107
108    fn read_model<T, R>(
109        &self,
110        handle: &Model<T>,
111        read: impl FnOnce(&T, &AppContext) -> R,
112    ) -> Self::Result<R>
113    where
114        T: 'static;
115
116    fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
117    where
118        F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
119
120    fn read_window<T, R>(
121        &self,
122        window: &WindowHandle<T>,
123        read: impl FnOnce(View<T>, &AppContext) -> R,
124    ) -> Result<R>
125    where
126        T: 'static;
127}
128
129pub trait VisualContext: Context {
130    fn new_view<V>(
131        &mut self,
132        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
133    ) -> Self::Result<View<V>>
134    where
135        V: 'static + Render;
136
137    fn update_view<V: 'static, R>(
138        &mut self,
139        view: &View<V>,
140        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
141    ) -> Self::Result<R>;
142
143    fn replace_root_view<V>(
144        &mut self,
145        build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
146    ) -> Self::Result<View<V>>
147    where
148        V: 'static + Render;
149
150    fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
151    where
152        V: FocusableView;
153
154    fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
155    where
156        V: ManagedView;
157}
158
159pub trait Entity<T>: Sealed {
160    type Weak: 'static;
161
162    fn entity_id(&self) -> EntityId;
163    fn downgrade(&self) -> Self::Weak;
164    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
165    where
166        Self: Sized;
167}
168
169pub trait EventEmitter<E: Any>: 'static {}
170
171pub enum GlobalKey {
172    Numeric(usize),
173    View(EntityId),
174    Type(TypeId),
175}
176
177pub trait BorrowAppContext {
178    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
179    where
180        F: FnOnce(&mut Self) -> R;
181
182    fn set_global<T: 'static>(&mut self, global: T);
183}
184
185impl<C> BorrowAppContext for C
186where
187    C: BorrowMut<AppContext>,
188{
189    fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
190    where
191        F: FnOnce(&mut Self) -> R,
192    {
193        if let Some(style) = style {
194            self.borrow_mut().push_text_style(style);
195            let result = f(self);
196            self.borrow_mut().pop_text_style();
197            result
198        } else {
199            f(self)
200        }
201    }
202
203    fn set_global<G: 'static>(&mut self, global: G) {
204        self.borrow_mut().set_global(global)
205    }
206}
207
208pub trait Flatten<T> {
209    fn flatten(self) -> Result<T>;
210}
211
212impl<T> Flatten<T> for Result<Result<T>> {
213    fn flatten(self) -> Result<T> {
214        self?
215    }
216}
217
218impl<T> Flatten<T> for Result<T> {
219    fn flatten(self) -> Result<T> {
220        self
221    }
222}