zed/crates/gpui3/src/gpui3.rs

278 lines
6.2 KiB
Rust
Raw Normal View History

2023-10-17 19:54:28 +00:00
mod active;
2023-09-19 19:19:22 +00:00
mod app;
2023-10-04 03:23:32 +00:00
mod assets;
2023-09-19 19:19:22 +00:00
mod color;
mod element;
mod elements;
2023-10-10 03:19:56 +00:00
mod events;
2023-09-20 02:55:13 +00:00
mod executor;
2023-10-18 13:46:17 +00:00
mod focus;
2023-09-19 19:19:22 +00:00
mod geometry;
2023-10-17 19:54:28 +00:00
mod hover;
2023-10-04 13:03:21 +00:00
mod image_cache;
2023-10-17 19:37:09 +00:00
mod interactive;
2023-10-19 17:09:11 +00:00
mod keymap;
2023-09-19 19:19:22 +00:00
mod platform;
mod scene;
mod style;
2023-09-20 03:55:49 +00:00
mod styled;
2023-10-11 10:45:09 +00:00
mod subscription;
2023-10-04 08:51:47 +00:00
mod svg_renderer;
2023-09-19 19:19:22 +00:00
mod taffy;
2023-09-20 18:03:37 +00:00
mod text_system;
2023-09-20 02:55:13 +00:00
mod util;
2023-09-21 19:46:31 +00:00
mod view;
2023-09-19 19:19:22 +00:00
mod window;
2023-10-17 19:54:28 +00:00
pub use active::*;
2023-09-21 18:18:09 +00:00
pub use anyhow::Result;
2023-09-19 19:19:22 +00:00
pub use app::*;
2023-10-04 03:23:32 +00:00
pub use assets::*;
2023-09-19 19:19:22 +00:00
pub use color::*;
pub use element::*;
pub use elements::*;
2023-10-10 03:19:56 +00:00
pub use events::*;
2023-09-20 02:55:13 +00:00
pub use executor::*;
2023-10-18 13:46:17 +00:00
pub use focus::*;
2023-09-19 19:19:22 +00:00
pub use geometry::*;
2023-09-21 18:18:09 +00:00
pub use gpui3_macros::*;
2023-10-17 19:54:28 +00:00
pub use hover::*;
2023-10-05 02:35:20 +00:00
pub use image_cache::*;
2023-10-17 19:37:09 +00:00
pub use interactive::*;
2023-10-19 17:09:11 +00:00
pub use keymap::*;
2023-09-19 19:19:22 +00:00
pub use platform::*;
2023-09-20 03:55:49 +00:00
pub use refineable::*;
2023-09-19 19:19:22 +00:00
pub use scene::*;
2023-09-20 20:32:55 +00:00
pub use serde;
pub use serde_json;
2023-09-20 03:55:49 +00:00
pub use smallvec;
2023-09-20 02:55:13 +00:00
pub use smol::Timer;
2023-09-19 19:19:22 +00:00
pub use style::*;
2023-09-20 03:55:49 +00:00
pub use styled::*;
2023-10-11 10:45:09 +00:00
pub use subscription::*;
2023-10-04 19:43:21 +00:00
pub use svg_renderer::*;
2023-09-21 02:28:32 +00:00
pub use taffy::{AvailableSpace, LayoutId};
2023-09-20 18:03:37 +00:00
pub use text_system::*;
2023-09-19 19:19:22 +00:00
pub use util::arc_cow::ArcCow;
2023-09-21 19:46:31 +00:00
pub use view::*;
2023-09-19 19:19:22 +00:00
pub use window::*;
2023-10-17 04:08:53 +00:00
use derive_more::{Deref, DerefMut};
2023-10-10 17:48:32 +00:00
use std::{
2023-10-11 23:18:33 +00:00
any::{Any, TypeId},
2023-10-10 17:48:32 +00:00
mem,
ops::{Deref, DerefMut},
sync::Arc,
};
use taffy::TaffyLayoutEngine;
2023-10-19 15:19:25 +00:00
type AnyBox = Box<dyn Any + Send + Sync>;
2023-10-11 04:14:47 +00:00
2023-09-19 19:19:22 +00:00
pub trait Context {
2023-10-02 18:47:45 +00:00
type EntityContext<'a, 'w, T: 'static + Send + Sync>;
2023-09-26 17:29:44 +00:00
type Result<T>;
2023-09-19 19:19:22 +00:00
2023-09-25 17:47:37 +00:00
fn entity<T: Send + Sync + 'static>(
2023-09-19 19:19:22 +00:00
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
2023-09-26 17:29:44 +00:00
) -> Self::Result<Handle<T>>;
2023-09-19 19:19:22 +00:00
2023-09-25 17:47:37 +00:00
fn update_entity<T: Send + Sync + 'static, R>(
2023-09-19 19:19:22 +00:00
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
2023-09-26 17:29:44 +00:00
) -> Self::Result<R>;
}
2023-10-11 23:18:33 +00:00
pub enum GlobalKey {
Numeric(usize),
View(EntityId),
Type(TypeId),
}
2023-10-02 18:47:45 +00:00
#[repr(transparent)]
pub struct MainThread<T>(T);
impl<T> Deref for MainThread<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for MainThread<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
2023-10-04 15:48:28 +00:00
impl<C: Context> Context for MainThread<C> {
type EntityContext<'a, 'w, T: 'static + Send + Sync> = MainThread<C::EntityContext<'a, 'w, T>>;
type Result<T> = C::Result<T>;
fn entity<T: Send + Sync + 'static>(
&mut self,
build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
) -> Self::Result<Handle<T>> {
self.0.entity(|cx| {
let cx = unsafe {
mem::transmute::<
&mut C::EntityContext<'_, '_, T>,
&mut MainThread<C::EntityContext<'_, '_, T>>,
>(cx)
};
build_entity(cx)
})
}
fn update_entity<T: Send + Sync + 'static, R>(
&mut self,
handle: &Handle<T>,
update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
) -> Self::Result<R> {
self.0.update_entity(handle, |entity, cx| {
let cx = unsafe {
mem::transmute::<
&mut C::EntityContext<'_, '_, T>,
&mut MainThread<C::EntityContext<'_, '_, T>>,
>(cx)
};
update(entity, cx)
})
}
}
2023-10-04 02:04:17 +00:00
pub trait BorrowAppContext {
fn app_mut(&mut self) -> &mut AppContext;
2023-09-29 19:22:53 +00:00
fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
2023-10-04 02:04:17 +00:00
self.app_mut().push_text_style(style);
2023-09-29 19:22:53 +00:00
let result = f(self);
2023-10-04 02:04:17 +00:00
self.app_mut().pop_text_style();
2023-09-29 19:22:53 +00:00
result
}
2023-10-11 23:18:33 +00:00
fn with_global<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
2023-09-29 19:22:53 +00:00
where
F: FnOnce(&mut Self) -> R,
{
2023-10-11 23:18:33 +00:00
self.app_mut().push_global(state);
2023-09-29 19:22:53 +00:00
let result = f(self);
2023-10-11 23:18:33 +00:00
self.app_mut().pop_global::<T>();
2023-09-29 19:22:53 +00:00
result
}
}
2023-10-11 08:45:57 +00:00
pub trait EventEmitter {
type Event: Any + Send + Sync + 'static;
}
2023-09-26 17:29:44 +00:00
pub trait Flatten<T> {
fn flatten(self) -> Result<T>;
}
impl<T> Flatten<T> for Result<Result<T>> {
fn flatten(self) -> Result<T> {
self?
}
}
impl<T> Flatten<T> for Result<T> {
fn flatten(self) -> Result<T> {
self
}
2023-09-19 19:19:22 +00:00
}
2023-10-17 04:08:53 +00:00
#[derive(Deref, DerefMut, Eq, PartialEq, Hash, Clone)]
2023-09-19 19:19:22 +00:00
pub struct SharedString(ArcCow<'static, str>);
2023-09-21 02:28:32 +00:00
impl Default for SharedString {
fn default() -> Self {
Self(ArcCow::Owned("".into()))
}
}
2023-09-20 02:55:13 +00:00
impl AsRef<str> for SharedString {
fn as_ref(&self) -> &str {
&self.0
}
}
2023-09-19 19:19:22 +00:00
impl std::fmt::Debug for SharedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
2023-10-04 03:23:32 +00:00
impl std::fmt::Display for SharedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_ref())
}
}
2023-09-19 19:19:22 +00:00
impl<T: Into<ArcCow<'static, str>>> From<T> 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<T: ?Sized> {
2023-10-04 17:53:29 +00:00
executor: Executor,
value: Arc<T>,
}
impl<T: ?Sized> Clone for MainThreadOnly<T> {
fn clone(&self) -> Self {
Self {
2023-10-04 17:53:29 +00:00
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<T: 'static + ?Sized> MainThreadOnly<T> {
2023-10-04 17:53:29 +00:00
pub(crate) fn new(value: Arc<T>, executor: Executor) -> Self {
Self { executor, value }
}
2023-09-29 20:34:40 +00:00
pub(crate) fn borrow_on_main_thread(&self) -> &T {
2023-10-04 17:53:29 +00:00
assert!(self.executor.is_main_thread());
&self.value
}
}
unsafe impl<T: ?Sized> Send for MainThreadOnly<T> {}