gpui2.rs

  1mod action;
  2mod app;
  3mod assets;
  4mod color;
  5mod element;
  6mod elements;
  7mod executor;
  8mod focusable;
  9mod geometry;
 10mod image_cache;
 11mod interactive;
 12mod keymap;
 13mod platform;
 14mod scene;
 15mod style;
 16mod styled;
 17mod subscription;
 18mod svg_renderer;
 19mod taffy;
 20#[cfg(any(test, feature = "test-support"))]
 21mod test;
 22mod text_system;
 23mod util;
 24mod view;
 25mod window;
 26
 27mod private {
 28    /// A mechanism for restricting implementations of a trait to only those in GPUI.
 29    /// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
 30    pub trait Sealed {}
 31}
 32
 33pub use action::*;
 34pub use anyhow::Result;
 35pub use app::*;
 36pub use assets::*;
 37pub use color::*;
 38pub use element::*;
 39pub use elements::*;
 40pub use executor::*;
 41pub use focusable::*;
 42pub use geometry::*;
 43pub use gpui2_macros::*;
 44pub use image_cache::*;
 45pub use interactive::*;
 46pub use keymap::*;
 47pub use platform::*;
 48use private::Sealed;
 49pub use refineable::*;
 50pub use scene::*;
 51pub use serde;
 52pub use serde_json;
 53pub use smallvec;
 54pub use smol::Timer;
 55pub use style::*;
 56pub use styled::*;
 57pub use subscription::*;
 58pub use svg_renderer::*;
 59pub use taffy::{AvailableSpace, LayoutId};
 60#[cfg(any(test, feature = "test-support"))]
 61pub use test::*;
 62pub use text_system::*;
 63pub use util::arc_cow::ArcCow;
 64pub use view::*;
 65pub use window::*;
 66
 67use derive_more::{Deref, DerefMut};
 68use std::{
 69    any::{Any, TypeId},
 70    borrow::{Borrow, BorrowMut},
 71    ops::{Deref, DerefMut},
 72};
 73use taffy::TaffyLayoutEngine;
 74
 75type AnyBox = Box<dyn Any>;
 76
 77pub trait Context {
 78    type ModelContext<'a, T>;
 79    type Result<T>;
 80
 81    fn build_model<T: 'static>(
 82        &mut self,
 83        build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
 84    ) -> Self::Result<Model<T>>;
 85
 86    fn update_model<T: 'static, R>(
 87        &mut self,
 88        handle: &Model<T>,
 89        update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
 90    ) -> Self::Result<R>;
 91}
 92
 93pub trait VisualContext: Context {
 94    type ViewContext<'a, 'w, V>;
 95
 96    fn build_view<V>(
 97        &mut self,
 98        build_view_state: impl FnOnce(&mut Self::ViewContext<'_, '_, V>) -> V,
 99    ) -> Self::Result<View<V>>
100    where
101        V: 'static;
102
103    fn update_view<V: 'static, R>(
104        &mut self,
105        view: &View<V>,
106        update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, '_, V>) -> R,
107    ) -> Self::Result<R>;
108}
109
110pub trait Entity<T>: Sealed {
111    type Weak: 'static + Send;
112
113    fn entity_id(&self) -> EntityId;
114    fn downgrade(&self) -> Self::Weak;
115    fn upgrade_from(weak: &Self::Weak) -> Option<Self>
116    where
117        Self: Sized;
118}
119
120pub enum GlobalKey {
121    Numeric(usize),
122    View(EntityId),
123    Type(TypeId),
124}
125
126pub trait BorrowAppContext {
127    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
128    where
129        F: FnOnce(&mut Self) -> R;
130
131    fn set_global<T: Send + 'static>(&mut self, global: T);
132}
133
134impl<C> BorrowAppContext for C
135where
136    C: BorrowMut<AppContext>,
137{
138    fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
139    where
140        F: FnOnce(&mut Self) -> R,
141    {
142        self.borrow_mut().push_text_style(style);
143        let result = f(self);
144        self.borrow_mut().pop_text_style();
145        result
146    }
147
148    fn set_global<G: 'static + Send>(&mut self, global: G) {
149        self.borrow_mut().set_global(global)
150    }
151}
152
153pub trait EventEmitter: 'static {
154    type Event: Any;
155}
156
157pub trait Flatten<T> {
158    fn flatten(self) -> Result<T>;
159}
160
161impl<T> Flatten<T> for Result<Result<T>> {
162    fn flatten(self) -> Result<T> {
163        self?
164    }
165}
166
167impl<T> Flatten<T> for Result<T> {
168    fn flatten(self) -> Result<T> {
169        self
170    }
171}
172
173#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
174pub struct SharedString(ArcCow<'static, str>);
175
176impl Default for SharedString {
177    fn default() -> Self {
178        Self(ArcCow::Owned("".into()))
179    }
180}
181
182impl AsRef<str> for SharedString {
183    fn as_ref(&self) -> &str {
184        &self.0
185    }
186}
187
188impl Borrow<str> for SharedString {
189    fn borrow(&self) -> &str {
190        self.as_ref()
191    }
192}
193
194impl std::fmt::Debug for SharedString {
195    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
196        self.0.fmt(f)
197    }
198}
199
200impl std::fmt::Display for SharedString {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        write!(f, "{}", self.0.as_ref())
203    }
204}
205
206impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
207    fn from(value: T) -> Self {
208        Self(value.into())
209    }
210}
211
212pub enum Reference<'a, T> {
213    Immutable(&'a T),
214    Mutable(&'a mut T),
215}
216
217impl<'a, T> Deref for Reference<'a, T> {
218    type Target = T;
219
220    fn deref(&self) -> &Self::Target {
221        match self {
222            Reference::Immutable(target) => target,
223            Reference::Mutable(target) => target,
224        }
225    }
226}
227
228impl<'a, T> DerefMut for Reference<'a, T> {
229    fn deref_mut(&mut self) -> &mut Self::Target {
230        match self {
231            Reference::Immutable(_) => {
232                panic!("cannot mutably deref an immutable reference. this is a bug in GPUI.");
233            }
234            Reference::Mutable(target) => target,
235        }
236    }
237}