mod app; mod assets; mod color; mod element; mod elements; mod executor; mod geometry; mod image_cache; mod platform; mod scene; mod style; mod style_helpers; mod styled; mod svg_renderer; mod taffy; mod text_system; mod util; mod view; mod window; pub use anyhow::Result; pub use app::*; pub use assets::*; pub use color::*; pub use element::*; pub use elements::*; pub use executor::*; pub use geometry::*; pub use gpui3_macros::*; pub use platform::*; pub use refineable::*; pub use scene::*; pub use serde; pub use serde_json; pub use smallvec; pub use smol::Timer; use std::{ mem, ops::{Deref, DerefMut}, sync::Arc, }; pub use style::*; pub use style_helpers::*; pub use styled::*; pub use svg_renderer::*; use taffy::TaffyLayoutEngine; pub use taffy::{AvailableSpace, LayoutId}; pub use text_system::*; pub use util::arc_cow::ArcCow; pub use view::*; pub use window::*; pub trait Context { type EntityContext<'a, 'w, T: 'static + Send + Sync>; type Result; fn entity( &mut self, build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, ) -> Self::Result>; fn update_entity( &mut self, handle: &Handle, update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R, ) -> Self::Result; } #[repr(transparent)] pub struct MainThread(T); impl Deref for MainThread { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for MainThread { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Context for MainThread { type EntityContext<'a, 'w, T: 'static + Send + Sync> = MainThread>; type Result = C::Result; fn entity( &mut self, build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T, ) -> Self::Result> { self.0.entity(|cx| { let cx = unsafe { mem::transmute::< &mut C::EntityContext<'_, '_, T>, &mut MainThread>, >(cx) }; build_entity(cx) }) } fn update_entity( &mut self, handle: &Handle, update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R, ) -> Self::Result { self.0.update_entity(handle, |entity, cx| { let cx = unsafe { mem::transmute::< &mut C::EntityContext<'_, '_, T>, &mut MainThread>, >(cx) }; update(entity, cx) }) } } pub trait BorrowAppContext { fn app_mut(&mut self) -> &mut AppContext; fn with_text_style(&mut self, style: TextStyleRefinement, f: F) -> R where F: FnOnce(&mut Self) -> R, { self.app_mut().push_text_style(style); let result = f(self); self.app_mut().pop_text_style(); result } fn with_state(&mut self, state: T, f: F) -> R where F: FnOnce(&mut Self) -> R, { self.app_mut().push_state(state); let result = f(self); self.app_mut().pop_state::(); result } } pub trait Flatten { fn flatten(self) -> Result; } impl Flatten for Result> { fn flatten(self) -> Result { self? } } impl Flatten for Result { fn flatten(self) -> Result { self } } #[derive(Clone, Eq, PartialEq, Hash)] pub struct SharedString(ArcCow<'static, str>); impl Default for SharedString { fn default() -> Self { Self(ArcCow::Owned("".into())) } } impl AsRef for SharedString { fn as_ref(&self) -> &str { &self.0 } } impl std::fmt::Debug for SharedString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl std::fmt::Display for SharedString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0.as_ref()) } } impl>> From for SharedString { fn from(value: T) -> Self { Self(value.into()) } } pub enum Reference<'a, T> { Immutable(&'a T), Mutable(&'a mut T), } impl<'a, T> Deref for Reference<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { match self { Reference::Immutable(target) => target, Reference::Mutable(target) => target, } } } impl<'a, T> DerefMut for Reference<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { match self { Reference::Immutable(_) => { panic!("cannot mutably deref an immutable reference. this is a bug in GPUI."); } Reference::Mutable(target) => target, } } } pub(crate) struct MainThreadOnly { executor: Executor, value: Arc, } impl Clone for MainThreadOnly { fn clone(&self) -> Self { Self { executor: self.executor.clone(), value: self.value.clone(), } } } /// Allows a value to be accessed only on the main thread, allowing a non-`Send` type /// to become `Send`. impl MainThreadOnly { pub(crate) fn new(value: Arc, executor: Executor) -> Self { Self { executor, value } } pub(crate) fn borrow_on_main_thread(&self) -> &T { assert!(self.executor.is_main_thread()); &self.value } } unsafe impl Send for MainThreadOnly {}