From 84ad2cb82795c41f7cf9c537c372c67106afc606 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 10 Oct 2023 19:48:32 +0200 Subject: [PATCH] Checkpoint --- crates/gpui3/src/element.rs | 9 +++++++- crates/gpui3/src/elements/hoverable.rs | 10 +++++++-- crates/gpui3/src/elements/identified.rs | 26 ++++++++++++++--------- crates/gpui3/src/gpui3.rs | 21 ++++++++++++------- crates/gpui3/src/identified.rs | 1 + crates/gpui3/src/window.rs | 28 ++++++++++++++++++------- 6 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 crates/gpui3/src/identified.rs diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index c42d867e9448aabcd1717e2e55650f5ad0250aa2..694b8c776cbbdfc81278816f0898577385a81c25 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -1,4 +1,4 @@ -use crate::Bounds; +use crate::{Bounds, ElementId, ElementWithId}; use super::{LayoutId, Pixels, Point, Result, ViewContext}; pub(crate) use smallvec::SmallVec; @@ -20,6 +20,13 @@ pub trait Element: 'static { frame_state: &mut Self::FrameState, cx: &mut ViewContext, ) -> Result<()>; + + fn id(self, id: ElementId) -> ElementWithId + where + Self: Sized, + { + ElementWithId { element: self, id } + } } pub trait ParentElement { diff --git a/crates/gpui3/src/elements/hoverable.rs b/crates/gpui3/src/elements/hoverable.rs index fc43c895cecb1fc1b183dfe6fb49b07d173950c1..86e4d75c0d375b3db9dc09e61454f75e203278f0 100644 --- a/crates/gpui3/src/elements/hoverable.rs +++ b/crates/gpui3/src/elements/hoverable.rs @@ -1,6 +1,6 @@ use crate::{ - AnyElement, Bounds, DispatchPhase, Element, Interactive, MouseEventListeners, MouseMoveEvent, - ParentElement, Pixels, Styled, ViewContext, + AnyElement, Bounds, DispatchPhase, Element, ElementId, Identified, Interactive, + MouseEventListeners, MouseMoveEvent, ParentElement, Pixels, Styled, ViewContext, }; use anyhow::Result; use refineable::{CascadeSlot, Refineable, RefinementCascade}; @@ -95,3 +95,9 @@ impl ParentElement for Hoverable { self.child.children_mut() } } + +impl Identified for Hoverable { + fn id(&self) -> ElementId { + self.child.id() + } +} diff --git a/crates/gpui3/src/elements/identified.rs b/crates/gpui3/src/elements/identified.rs index 877666a30abc986ca5b19acbb0743d4b65ea5323..6c5741fa4703a6c25c27d4589ba8f11032c41cd5 100644 --- a/crates/gpui3/src/elements/identified.rs +++ b/crates/gpui3/src/elements/identified.rs @@ -1,16 +1,16 @@ -use crate::{ElementId, Element, Bounds, ViewContext, LayoutId}; +use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, ViewContext}; use anyhow::Result; -use derive_more::{Deref, DerefMut} -#[derive(Deref, DerefMut)] -pub struct Identified { - #[deref] - #[deref_mut] - element: E, - id: ElementId, +pub trait Identified { + fn id(&self) -> ElementId; } -impl Element for Identified { +pub struct ElementWithId { + pub(crate) element: E, + pub(crate) id: ElementId, +} + +impl Element for ElementWithId { type State = E::State; type FrameState = E::FrameState; @@ -29,8 +29,14 @@ impl Element for Identified { frame_state: &mut Self::FrameState, cx: &mut ViewContext, ) -> Result<()> { - cx.with_element_id(self.id, |cx| { + cx.with_element_id(self.id.clone(), |cx| { self.element.paint(bounds, state, frame_state, cx) }) } } + +impl Identified for ElementWithId { + fn id(&self) -> ElementId { + self.id.clone() + } +} diff --git a/crates/gpui3/src/gpui3.rs b/crates/gpui3/src/gpui3.rs index 6f8c1f1a74b56aab9124ca5e67096abbe531eea8..ea14f4d11378c56157ff09fafa10dca3204015a4 100644 --- a/crates/gpui3/src/gpui3.rs +++ b/crates/gpui3/src/gpui3.rs @@ -6,6 +6,7 @@ mod elements; mod events; mod executor; mod geometry; +mod identified; mod image_cache; mod interactive; mod platform; @@ -30,6 +31,7 @@ pub use events::*; pub use executor::*; pub use geometry::*; pub use gpui3_macros::*; +pub use identified::*; pub use image_cache::*; pub use interactive::*; pub use platform::*; @@ -39,22 +41,24 @@ 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::*; +use derive_more::{Deref, DerefMut}; +use std::{ + mem, + ops::{Deref, DerefMut}, + sync::Arc, +}; +use taffy::TaffyLayoutEngine; + pub trait Context { type EntityContext<'a, 'w, T: 'static + Send + Sync>; type Result; @@ -167,9 +171,12 @@ impl Flatten for Result { #[derive(Clone, Eq, PartialEq, Hash)] pub struct SharedString(ArcCow<'static, str>); -#[derive(Clone, Eq, PartialEq, Hash)] +#[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct ElementId(ArcCow<'static, [u8]>); +#[derive(Default, Deref, DerefMut, Clone, Debug)] +pub(crate) struct GlobalElementId(SmallVec<[ElementId; 8]>); + impl Default for SharedString { fn default() -> Self { Self(ArcCow::Owned("".into())) diff --git a/crates/gpui3/src/identified.rs b/crates/gpui3/src/identified.rs new file mode 100644 index 0000000000000000000000000000000000000000..8b137891791fe96927ad78e64b0aad7bded08bdc --- /dev/null +++ b/crates/gpui3/src/identified.rs @@ -0,0 +1 @@ + diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 54b5ffdace2e19b0d9984f2ae18ebb23e236e155..add9615476571de6ae5572bb9977edcb55428821 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -1,12 +1,12 @@ use crate::{ image_cache::RenderImageParams, px, size, AnyView, AppContext, AsyncWindowContext, AvailableSpace, BorrowAppContext, Bounds, BoxShadow, Context, Corners, DevicePixels, DisplayId, - Edges, Effect, Element, ElementId, EntityId, Event, FontId, GlyphId, Handle, Hsla, ImageData, - IsZero, LayoutId, MainThread, MainThreadOnly, MonochromeSprite, MouseMoveEvent, Path, Pixels, - PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, Reference, RenderGlyphParams, - RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, - TaffyLayoutEngine, Task, Underline, UnderlineStyle, WeakHandle, WindowOptions, - SUBPIXEL_VARIANTS, + Edges, Effect, Element, ElementId, EntityId, Event, FontId, GlobalElementId, GlyphId, Handle, + Hsla, ImageData, IsZero, LayoutId, MainThread, MainThreadOnly, MonochromeSprite, + MouseMoveEvent, Path, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad, + Reference, RenderGlyphParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, + SharedString, Size, Style, TaffyLayoutEngine, Task, Underline, UnderlineStyle, WeakHandle, + WindowOptions, SUBPIXEL_VARIANTS, }; use anyhow::Result; use collections::HashMap; @@ -21,7 +21,7 @@ use std::{ mem, sync::Arc, }; -use util::{arc_cow::ArcCow, ResultExt}; +use util::ResultExt; #[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default)] pub struct StackingOrder(pub(crate) SmallVec<[u32; 16]>); @@ -51,7 +51,7 @@ pub struct Window { content_size: Size, layout_engine: TaffyLayoutEngine, pub(crate) root_view: Option>, - element_id_stack: SmallVec<[ElementId; 8]>, + pub(crate) element_id_stack: GlobalElementId, z_index_stack: StackingOrder, content_mask_stack: Vec>, mouse_event_handlers: HashMap>, @@ -113,6 +113,7 @@ impl Window { content_size, layout_engine: TaffyLayoutEngine::new(), root_view: None, + element_id_stack: GlobalElementId::default(), z_index_stack: StackingOrder(SmallVec::new()), content_mask_stack: Vec::new(), mouse_event_handlers: HashMap::default(), @@ -729,6 +730,17 @@ pub trait BorrowWindow: BorrowAppContext { fn window(&self) -> &Window; fn window_mut(&mut self) -> &mut Window; + fn with_element_id( + &mut self, + id: impl Into, + f: impl FnOnce(&mut Self) -> R, + ) -> R { + self.window_mut().element_id_stack.push(id.into()); + let result = f(self); + self.window_mut().element_id_stack.pop(); + result + } + fn with_content_mask( &mut self, mask: ContentMask,