gpui3.rs

  1mod app;
  2mod color;
  3mod element;
  4mod elements;
  5mod executor;
  6mod fonts;
  7mod geometry;
  8mod platform;
  9mod renderer;
 10mod scene;
 11mod style;
 12mod styled;
 13mod taffy;
 14mod text;
 15mod util;
 16mod window;
 17
 18use anyhow::Result;
 19pub use app::*;
 20pub use color::*;
 21pub use element::*;
 22pub use elements::*;
 23pub use executor::*;
 24pub use fonts::*;
 25pub use geometry::*;
 26pub use platform::*;
 27pub use refineable::*;
 28pub use scene::*;
 29pub use smallvec;
 30pub use smol::Timer;
 31use std::ops::{Deref, DerefMut};
 32pub use style::*;
 33pub use styled::*;
 34pub use taffy::LayoutId;
 35use taffy::TaffyLayoutEngine;
 36use text::*;
 37pub use text::{Glyph, GlyphId};
 38pub use util::arc_cow::ArcCow;
 39pub use window::*;
 40
 41pub trait Context {
 42    type EntityContext<'a, 'w, T: 'static>;
 43
 44    fn entity<T: 'static>(
 45        &mut self,
 46        build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
 47    ) -> Handle<T>;
 48
 49    fn update_entity<T: 'static, R>(
 50        &mut self,
 51        handle: &Handle<T>,
 52        update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
 53    ) -> R;
 54}
 55
 56#[derive(Clone, Eq, PartialEq)]
 57pub struct SharedString(ArcCow<'static, str>);
 58
 59impl AsRef<str> for SharedString {
 60    fn as_ref(&self) -> &str {
 61        &self.0
 62    }
 63}
 64
 65impl std::fmt::Debug for SharedString {
 66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 67        self.0.fmt(f)
 68    }
 69}
 70
 71impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
 72    fn from(value: T) -> Self {
 73        Self(value.into())
 74    }
 75}
 76
 77pub enum Reference<'a, T> {
 78    Immutable(&'a T),
 79    Mutable(&'a mut T),
 80}
 81
 82impl<'a, T> Deref for Reference<'a, T> {
 83    type Target = T;
 84
 85    fn deref(&self) -> &Self::Target {
 86        match self {
 87            Reference::Immutable(target) => target,
 88            Reference::Mutable(target) => target,
 89        }
 90    }
 91}
 92
 93impl<'a, T> DerefMut for Reference<'a, T> {
 94    fn deref_mut(&mut self) -> &mut Self::Target {
 95        match self {
 96            Reference::Immutable(_) => {
 97                panic!("cannot mutably deref an immutable reference. this is a bug in GPUI.");
 98            }
 99            Reference::Mutable(target) => target,
100        }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    struct Workspace {
109        left_panel: AnyView<Self>,
110    }
111
112    fn workspace(cx: &mut WindowContext) -> View<Workspace> {
113        let workspace = cx.entity(|cx| Workspace {
114            left_panel: collab_panel(cx).into_any(),
115        });
116        view(workspace, |workspace, _cx| {
117            div().child(workspace.left_panel.clone())
118        })
119    }
120
121    struct CollabPanel {
122        filter_editor: Handle<editor::Editor>,
123    }
124
125    fn collab_panel(cx: &mut WindowContext) -> View<CollabPanel> {
126        let panel = cx.entity(|cx| CollabPanel::new(cx));
127        view(panel, |panel, _cx| {
128            div().child(div()).child(
129                field(panel.filter_editor.clone()).placeholder_text("Search channels, contacts"),
130            )
131        })
132    }
133
134    impl CollabPanel {
135        fn new(cx: &mut ViewContext<Self>) -> Self {
136            Self {
137                filter_editor: cx.entity(|cx| editor::Editor::new(cx)),
138            }
139        }
140    }
141
142    #[test]
143    fn test() {
144        let mut cx = AppContext::test();
145
146        cx.open_window(WindowOptions::default(), |cx| workspace(cx));
147    }
148}