From 97edec6e72ab7f72d1fed7581bc189816b65e641 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 10 Oct 2023 17:31:42 +0200 Subject: [PATCH 1/7] WIP --- crates/gpui3/src/elements.rs | 2 ++ crates/gpui3/src/elements/identified.rs | 36 ++++++++++++++++++++++++ crates/gpui3/src/gpui3.rs | 3 ++ crates/gpui3/src/window.rs | 37 +++++++++++++------------ 4 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 crates/gpui3/src/elements/identified.rs diff --git a/crates/gpui3/src/elements.rs b/crates/gpui3/src/elements.rs index d879153488566220e7164d5137f63110911929ab..5c8381ad77d9ee1467abbe5c312f860e7daef77f 100644 --- a/crates/gpui3/src/elements.rs +++ b/crates/gpui3/src/elements.rs @@ -1,5 +1,6 @@ mod div; mod hoverable; +mod identified; mod img; mod stateless; mod svg; @@ -7,6 +8,7 @@ mod text; pub use div::*; pub use hoverable::*; +pub use identified::*; pub use img::*; pub use stateless::*; pub use svg::*; diff --git a/crates/gpui3/src/elements/identified.rs b/crates/gpui3/src/elements/identified.rs new file mode 100644 index 0000000000000000000000000000000000000000..877666a30abc986ca5b19acbb0743d4b65ea5323 --- /dev/null +++ b/crates/gpui3/src/elements/identified.rs @@ -0,0 +1,36 @@ +use crate::{ElementId, Element, Bounds, ViewContext, LayoutId}; +use anyhow::Result; +use derive_more::{Deref, DerefMut} + +#[derive(Deref, DerefMut)] +pub struct Identified { + #[deref] + #[deref_mut] + element: E, + id: ElementId, +} + +impl Element for Identified { + type State = E::State; + type FrameState = E::FrameState; + + fn layout( + &mut self, + state: &mut Self::State, + cx: &mut ViewContext, + ) -> Result<(LayoutId, Self::FrameState)> { + self.element.layout(state, cx) + } + + fn paint( + &mut self, + bounds: Bounds, + state: &mut Self::State, + frame_state: &mut Self::FrameState, + cx: &mut ViewContext, + ) -> Result<()> { + cx.with_element_id(self.id, |cx| { + self.element.paint(bounds, state, frame_state, cx) + }) + } +} diff --git a/crates/gpui3/src/gpui3.rs b/crates/gpui3/src/gpui3.rs index 1b0de7498080ce105f706bc5aae798389609b82b..6f8c1f1a74b56aab9124ca5e67096abbe531eea8 100644 --- a/crates/gpui3/src/gpui3.rs +++ b/crates/gpui3/src/gpui3.rs @@ -167,6 +167,9 @@ impl Flatten for Result { #[derive(Clone, Eq, PartialEq, Hash)] pub struct SharedString(ArcCow<'static, str>); +#[derive(Clone, Eq, PartialEq, Hash)] +pub struct ElementId(ArcCow<'static, [u8]>); + impl Default for SharedString { fn default() -> Self { Self(ArcCow::Owned("".into())) diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 47aa7b7b5fbb6f82e72b3ab2b823d519a55a7e6d..54b5ffdace2e19b0d9984f2ae18ebb23e236e155 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -1,8 +1,8 @@ use crate::{ image_cache::RenderImageParams, px, size, AnyView, AppContext, AsyncWindowContext, AvailableSpace, BorrowAppContext, Bounds, BoxShadow, Context, Corners, DevicePixels, DisplayId, - Edges, Effect, Element, EntityId, Event, FontId, GlyphId, Handle, Hsla, ImageData, IsZero, - LayoutId, MainThread, MainThreadOnly, MonochromeSprite, MouseMoveEvent, Path, Pixels, + 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, @@ -21,7 +21,7 @@ use std::{ mem, sync::Arc, }; -use util::ResultExt; +use util::{arc_cow::ArcCow, ResultExt}; #[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default)] pub struct StackingOrder(pub(crate) SmallVec<[u32; 16]>); @@ -51,7 +51,8 @@ pub struct Window { content_size: Size, layout_engine: TaffyLayoutEngine, pub(crate) root_view: Option>, - current_stacking_order: StackingOrder, + element_id_stack: SmallVec<[ElementId; 8]>, + z_index_stack: StackingOrder, content_mask_stack: Vec>, mouse_event_handlers: HashMap>, propagate_event: bool, @@ -112,7 +113,7 @@ impl Window { content_size, layout_engine: TaffyLayoutEngine::new(), root_view: None, - current_stacking_order: StackingOrder(SmallVec::new()), + z_index_stack: StackingOrder(SmallVec::new()), content_mask_stack: Vec::new(), mouse_event_handlers: HashMap::default(), propagate_event: true, @@ -287,7 +288,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { &mut self, handler: impl Fn(&Event, DispatchPhase, &mut WindowContext) + Send + Sync + 'static, ) { - let order = self.window.current_stacking_order.clone(); + let order = self.window.z_index_stack.clone(); self.window .mouse_event_handlers .entry(TypeId::of::()) @@ -305,9 +306,9 @@ impl<'a, 'w> WindowContext<'a, 'w> { } pub fn stack(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R { - self.window.current_stacking_order.push(order); + self.window.z_index_stack.push(order); let result = f(self); - self.window.current_stacking_order.pop(); + self.window.z_index_stack.pop(); result } @@ -325,7 +326,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { shadow_bounds.origin += shadow.offset; shadow_bounds.dilate(shadow.spread_radius); window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, Shadow { order: 0, bounds: shadow_bounds.scale(scale_factor), @@ -351,7 +352,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, Quad { order: 0, bounds: bounds.scale(scale_factor), @@ -372,7 +373,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let window = &mut *self.window; window .scene_builder - .insert(&window.current_stacking_order, path.scale(scale_factor)); + .insert(&window.z_index_stack, path.scale(scale_factor)); } pub fn paint_underline( @@ -394,7 +395,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let content_mask = self.content_mask(); let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, Underline { order: 0, bounds: bounds.scale(scale_factor), @@ -446,7 +447,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let content_mask = self.content_mask().scale(scale_factor); let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, MonochromeSprite { order: 0, bounds, @@ -495,7 +496,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, PolychromeSprite { order: 0, bounds, @@ -536,7 +537,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, MonochromeSprite { order: 0, bounds, @@ -571,7 +572,7 @@ impl<'a, 'w> WindowContext<'a, 'w> { let window = &mut *self.window; window.scene_builder.insert( - &window.current_stacking_order, + &window.z_index_stack, PolychromeSprite { order: 0, bounds, @@ -824,9 +825,9 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { } pub fn stack(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R { - self.window.current_stacking_order.push(order); + self.window.z_index_stack.push(order); let result = f(self); - self.window.current_stacking_order.pop(); + self.window.z_index_stack.pop(); result } From 84ad2cb82795c41f7cf9c537c372c67106afc606 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 10 Oct 2023 19:48:32 +0200 Subject: [PATCH 2/7] 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, From 4855b8f3dea4f765d7684899955fe1756a22fcd6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 10 Oct 2023 20:02:23 +0200 Subject: [PATCH 3/7] WIP --- crates/gpui3/src/elements.rs | 2 + crates/gpui3/src/elements/pressable.rs | 229 ++++++++++++++++++------- 2 files changed, 169 insertions(+), 62 deletions(-) diff --git a/crates/gpui3/src/elements.rs b/crates/gpui3/src/elements.rs index 5c8381ad77d9ee1467abbe5c312f860e7daef77f..b2653b22a3231aae904dbc39ea405cb0296e3ccd 100644 --- a/crates/gpui3/src/elements.rs +++ b/crates/gpui3/src/elements.rs @@ -2,6 +2,7 @@ mod div; mod hoverable; mod identified; mod img; +mod pressable; mod stateless; mod svg; mod text; @@ -10,6 +11,7 @@ pub use div::*; pub use hoverable::*; pub use identified::*; pub use img::*; +pub use pressable::*; pub use stateless::*; pub use svg::*; pub use text::*; diff --git a/crates/gpui3/src/elements/pressable.rs b/crates/gpui3/src/elements/pressable.rs index 6e9b031edae0905541d14bcd6b232589936bb3cd..9390785ba853092ec9f573142ea64ba6f83af49a 100644 --- a/crates/gpui3/src/elements/pressable.rs +++ b/crates/gpui3/src/elements/pressable.rs @@ -1,106 +1,211 @@ use crate::{ - element::{AnyElement, Element, IntoElement, Layout, ParentElement}, - interactive::{InteractionHandlers, Interactive}, - style::{Style, StyleHelpers, Styleable}, - ViewContext, + AnyElement, Bounds, DispatchPhase, Element, Identified, Interactive, MouseDownEvent, + MouseEventListeners, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Styled, ViewContext, }; use anyhow::Result; -use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId}; use refineable::{CascadeSlot, Refineable, RefinementCascade}; use smallvec::SmallVec; -use std::{cell::Cell, rc::Rc}; +use std::sync::{ + atomic::{AtomicBool, Ordering::SeqCst}, + Arc, +}; -pub struct Pressable { - pressed: Rc>, - pressed_style: ::Refinement, +pub struct Pressable { + pressed: Arc, cascade_slot: CascadeSlot, + pressed_style: ::Refinement, child: E, } -pub fn pressable(mut child: E) -> Pressable { - Pressable { - pressed: Rc::new(Cell::new(false)), - pressed_style: Default::default(), - cascade_slot: child.style_cascade().reserve(), - child, +impl Pressable { + pub fn new(mut child: E) -> Self { + Self { + pressed: Arc::new(AtomicBool::new(false)), + cascade_slot: child.style_cascade().reserve(), + pressed_style: Default::default(), + child, + } } } -impl Styleable for Pressable { +impl Styled for Pressable +where + E: Styled, +{ type Style = E::Style; - fn declared_style(&mut self) -> &mut ::Refinement { + fn style_cascade(&mut self) -> &mut RefinementCascade { + self.child.style_cascade() + } + + fn declared_style(&mut self) -> &mut ::Refinement { &mut self.pressed_style } +} - fn style_cascade(&mut self) -> &mut RefinementCascade { - self.child.style_cascade() +impl + Styled> Interactive for Pressable { + fn listeners(&mut self) -> &mut MouseEventListeners { + self.child.listeners() } } -impl + Styleable> Element for Pressable { - type PaintState = E::PaintState; +impl Element for Pressable { + type State = E::State; + type FrameState = E::FrameState; fn layout( &mut self, - view: &mut V, - cx: &mut ViewContext, - ) -> Result<(LayoutId, Self::PaintState)> - where - Self: Sized, - { - self.child.layout(view, cx) + state: &mut Self::State, + cx: &mut ViewContext, + ) -> Result<(crate::LayoutId, Self::FrameState)> { + Ok(self.child.layout(state, cx)?) } fn paint( &mut self, - view: &mut V, - parent_origin: Vector2F, - layout: &Layout, - paint_state: &mut Self::PaintState, - cx: &mut ViewContext, - ) where - Self: Sized, - { + bounds: Bounds, + state: &mut Self::State, + frame_state: &mut Self::FrameState, + cx: &mut ViewContext, + ) -> Result<()> { + let pressed = bounds.contains_point(cx.mouse_position()); let slot = self.cascade_slot; - let style = self.pressed.get().then_some(self.pressed_style.clone()); + let style = pressed.then_some(self.pressed_style.clone()); self.style_cascade().set(slot, style); + self.pressed.store(pressed, SeqCst); - let pressed = self.pressed.clone(); - let bounds = layout.bounds + parent_origin; - cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| { - if event.is_down { - if bounds.contains_point(event.position) { - pressed.set(true); - cx.repaint(); + let hovered = self.pressed.clone(); + cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| { + if phase == DispatchPhase::Capture { + if bounds.contains_point(event.position) != hovered.load(SeqCst) { + cx.notify(); + } + } + }); + cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { + if phase == DispatchPhase::Capture { + if bounds.contains_point(event.position) != hovered.load(SeqCst) { + cx.notify(); } - } else if pressed.get() { - pressed.set(false); - cx.repaint(); } }); - self.child - .paint(view, parent_origin, layout, paint_state, cx); + self.child.paint(bounds, state, frame_state, cx)?; + Ok(()) } } -impl + Styleable> Interactive for Pressable { - fn interaction_handlers(&mut self) -> &mut InteractionHandlers { - self.child.interaction_handlers() - } -} +impl ParentElement for Pressable { + type State = E::State; -impl + Styleable> ParentElement for Pressable { - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { + fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { self.child.children_mut() } } -impl + Styleable> IntoElement for Pressable { - type Element = Self; +// use crate::{ +// element::{AnyElement, Element, IntoElement, Layout, ParentElement}, +// interactive::{InteractionHandlers, Interactive}, +// style::{Style, StyleHelpers, Styleable}, +// ViewContext, +// }; +// use anyhow::Result; +// use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId}; +// use refineable::{CascadeSlot, Refineable, RefinementCascade}; +// use smallvec::SmallVec; +// use std::{cell::Cell, rc::Rc}; - fn into_element(self) -> Self::Element { - self - } -} +// pub struct Pressable { +// pressed: Rc>, +// pressed_style: ::Refinement, +// cascade_slot: CascadeSlot, +// child: E, +// } + +// pub fn pressable(mut child: E) -> Pressable { +// Pressable { +// pressed: Rc::new(Cell::new(false)), +// pressed_style: Default::default(), +// cascade_slot: child.style_cascade().reserve(), +// child, +// } +// } + +// impl Styleable for Pressable { +// type Style = E::Style; + +// fn declared_style(&mut self) -> &mut ::Refinement { +// &mut self.pressed_style +// } + +// fn style_cascade(&mut self) -> &mut RefinementCascade { +// self.child.style_cascade() +// } +// } + +// impl + Styleable> Element for Pressable { +// type PaintState = E::PaintState; + +// fn layout( +// &mut self, +// view: &mut V, +// cx: &mut ViewContext, +// ) -> Result<(LayoutId, Self::PaintState)> +// where +// Self: Sized, +// { +// self.child.layout(view, cx) +// } + +// fn paint( +// &mut self, +// view: &mut V, +// parent_origin: Vector2F, +// layout: &Layout, +// paint_state: &mut Self::PaintState, +// cx: &mut ViewContext, +// ) where +// Self: Sized, +// { +// let slot = self.cascade_slot; +// let style = self.pressed.get().then_some(self.pressed_style.clone()); +// self.style_cascade().set(slot, style); + +// let pressed = self.pressed.clone(); +// let bounds = layout.bounds + parent_origin; +// cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| { +// if event.is_down { +// if bounds.contains_point(event.position) { +// pressed.set(true); +// cx.repaint(); +// } +// } else if pressed.get() { +// pressed.set(false); +// cx.repaint(); +// } +// }); + +// self.child +// .paint(view, parent_origin, layout, paint_state, cx); +// } +// } + +// impl + Styleable> Interactive for Pressable { +// fn interaction_handlers(&mut self) -> &mut InteractionHandlers { +// self.child.interaction_handlers() +// } +// } + +// impl + Styleable> ParentElement for Pressable { +// fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { +// self.child.children_mut() +// } +// } + +// impl + Styleable> IntoElement for Pressable { +// type Element = Self; + +// fn into_element(self) -> Self::Element { +// self +// } +// } From 9eff99de49145c7e66b0e92aace3034a8ada9e49 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 10 Oct 2023 20:02:34 +0200 Subject: [PATCH 4/7] --amend --- crates/gpui3/src/elements/pressable.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/gpui3/src/elements/pressable.rs b/crates/gpui3/src/elements/pressable.rs index 9390785ba853092ec9f573142ea64ba6f83af49a..67d68be9d8f644fe4c28e185e8042075a3e98f59 100644 --- a/crates/gpui3/src/elements/pressable.rs +++ b/crates/gpui3/src/elements/pressable.rs @@ -90,6 +90,9 @@ impl Element for Pressable { } }); + cx.frame_state::(self.id().to_global(cx)) + // cx.frame_state + self.child.paint(bounds, state, frame_state, cx)?; Ok(()) } From d70b4f04f60877b84d0d4c9674674b55549dadaa Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 10 Oct 2023 12:41:28 -0600 Subject: [PATCH 5/7] Checkpoint --- crates/gpui3/src/arc_cow.rs | 0 crates/gpui3/src/element.rs | 20 +- crates/gpui3/src/elements/hoverable.rs | 14 +- crates/gpui3/src/elements/identified.rs | 20 +- crates/gpui3/src/elements/pressable.rs | 339 ++++++++++++------------ crates/gpui3/src/gpui3.rs | 9 - crates/gpui3/src/identified.rs | 1 - 7 files changed, 201 insertions(+), 202 deletions(-) delete mode 100644 crates/gpui3/src/arc_cow.rs delete mode 100644 crates/gpui3/src/identified.rs diff --git a/crates/gpui3/src/arc_cow.rs b/crates/gpui3/src/arc_cow.rs deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index 694b8c776cbbdfc81278816f0898577385a81c25..f205a01433c609f372f5da20e5a5e955e5b15de8 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -1,12 +1,16 @@ -use crate::{Bounds, ElementId, ElementWithId}; - -use super::{LayoutId, Pixels, Point, Result, ViewContext}; +use crate::{Bounds, Identified, LayoutId, Pixels, Point, Result, ViewContext}; +use derive_more::{Deref, DerefMut}; pub(crate) use smallvec::SmallVec; +use util::arc_cow::ArcCow; pub trait Element: 'static { type State; type FrameState; + fn element_id(&self) -> Option { + None + } + fn layout( &mut self, state: &mut Self::State, @@ -21,14 +25,20 @@ pub trait Element: 'static { cx: &mut ViewContext, ) -> Result<()>; - fn id(self, id: ElementId) -> ElementWithId + fn id(self, id: ElementId) -> Identified where Self: Sized, { - ElementWithId { element: self, id } + Identified { element: self, id } } } +#[derive(Clone, Debug, Eq, PartialEq, Hash)] +pub struct ElementId(ArcCow<'static, [u8]>); + +#[derive(Deref, DerefMut, Default, Clone, Debug)] +pub(crate) struct GlobalElementId(SmallVec<[ElementId; 8]>); + pub trait ParentElement { type State; diff --git a/crates/gpui3/src/elements/hoverable.rs b/crates/gpui3/src/elements/hoverable.rs index 86e4d75c0d375b3db9dc09e61454f75e203278f0..4595ddf6f8f4cee2e4f83c9eb6280f5430bd41c7 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, ElementId, Identified, Interactive, - MouseEventListeners, MouseMoveEvent, ParentElement, Pixels, Styled, ViewContext, + AnyElement, Bounds, DispatchPhase, Element, ElementId, Interactive, MouseEventListeners, + MouseMoveEvent, ParentElement, Pixels, Stateful, Styled, ViewContext, }; use anyhow::Result; use refineable::{CascadeSlot, Refineable, RefinementCascade}; @@ -53,6 +53,10 @@ impl Element for Hoverable { type State = E::State; type FrameState = E::FrameState; + fn element_id(&self) -> Option { + self.child.element_id() + } + fn layout( &mut self, state: &mut Self::State, @@ -96,8 +100,4 @@ impl ParentElement for Hoverable { } } -impl Identified for Hoverable { - fn id(&self) -> ElementId { - self.child.id() - } -} +impl Stateful for Hoverable {} diff --git a/crates/gpui3/src/elements/identified.rs b/crates/gpui3/src/elements/identified.rs index 6c5741fa4703a6c25c27d4589ba8f11032c41cd5..b93a3fd1cf4e6ef0b44aba1ca6c1336a4a0f6e3c 100644 --- a/crates/gpui3/src/elements/identified.rs +++ b/crates/gpui3/src/elements/identified.rs @@ -1,19 +1,25 @@ use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, ViewContext}; use anyhow::Result; -pub trait Identified { - fn id(&self) -> ElementId; +pub trait Stateful: Element { + fn element_id(&self) -> ElementId { + Element::element_id(self).unwrap() + } } -pub struct ElementWithId { +pub struct Identified { pub(crate) element: E, pub(crate) id: ElementId, } -impl Element for ElementWithId { +impl Element for Identified { type State = E::State; type FrameState = E::FrameState; + fn element_id(&self) -> Option { + Some(self.id.clone()) + } + fn layout( &mut self, state: &mut Self::State, @@ -35,8 +41,4 @@ impl Element for ElementWithId { } } -impl Identified for ElementWithId { - fn id(&self) -> ElementId { - self.id.clone() - } -} +impl Stateful for Identified {} diff --git a/crates/gpui3/src/elements/pressable.rs b/crates/gpui3/src/elements/pressable.rs index 67d68be9d8f644fe4c28e185e8042075a3e98f59..d9e52066eb1b81b4a48a2a15506603e771df67e0 100644 --- a/crates/gpui3/src/elements/pressable.rs +++ b/crates/gpui3/src/elements/pressable.rs @@ -1,214 +1,211 @@ -use crate::{ - AnyElement, Bounds, DispatchPhase, Element, Identified, Interactive, MouseDownEvent, - MouseEventListeners, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Styled, ViewContext, -}; -use anyhow::Result; -use refineable::{CascadeSlot, Refineable, RefinementCascade}; -use smallvec::SmallVec; -use std::sync::{ - atomic::{AtomicBool, Ordering::SeqCst}, - Arc, -}; - -pub struct Pressable { - pressed: Arc, - cascade_slot: CascadeSlot, - pressed_style: ::Refinement, - child: E, -} - -impl Pressable { - pub fn new(mut child: E) -> Self { - Self { - pressed: Arc::new(AtomicBool::new(false)), - cascade_slot: child.style_cascade().reserve(), - pressed_style: Default::default(), - child, - } - } -} - -impl Styled for Pressable -where - E: Styled, -{ - type Style = E::Style; - - fn style_cascade(&mut self) -> &mut RefinementCascade { - self.child.style_cascade() - } - - fn declared_style(&mut self) -> &mut ::Refinement { - &mut self.pressed_style - } -} - -impl + Styled> Interactive for Pressable { - fn listeners(&mut self) -> &mut MouseEventListeners { - self.child.listeners() - } -} - -impl Element for Pressable { - type State = E::State; - type FrameState = E::FrameState; - - fn layout( - &mut self, - state: &mut Self::State, - cx: &mut ViewContext, - ) -> Result<(crate::LayoutId, Self::FrameState)> { - Ok(self.child.layout(state, cx)?) - } - - fn paint( - &mut self, - bounds: Bounds, - state: &mut Self::State, - frame_state: &mut Self::FrameState, - cx: &mut ViewContext, - ) -> Result<()> { - let pressed = bounds.contains_point(cx.mouse_position()); - let slot = self.cascade_slot; - let style = pressed.then_some(self.pressed_style.clone()); - self.style_cascade().set(slot, style); - self.pressed.store(pressed, SeqCst); - - let hovered = self.pressed.clone(); - cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| { - if phase == DispatchPhase::Capture { - if bounds.contains_point(event.position) != hovered.load(SeqCst) { - cx.notify(); - } - } - }); - cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { - if phase == DispatchPhase::Capture { - if bounds.contains_point(event.position) != hovered.load(SeqCst) { - cx.notify(); - } - } - }); - - cx.frame_state::(self.id().to_global(cx)) - // cx.frame_state - - self.child.paint(bounds, state, frame_state, cx)?; - Ok(()) - } -} - -impl ParentElement for Pressable { - type State = E::State; - - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { - self.child.children_mut() - } -} - // use crate::{ -// element::{AnyElement, Element, IntoElement, Layout, ParentElement}, -// interactive::{InteractionHandlers, Interactive}, -// style::{Style, StyleHelpers, Styleable}, -// ViewContext, +// AnyElement, Bounds, DispatchPhase, Element, Identified, Interactive, MouseDownEvent, +// MouseEventListeners, MouseUpEvent, ParentElement, Pixels, Styled, ViewContext, // }; // use anyhow::Result; -// use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId}; // use refineable::{CascadeSlot, Refineable, RefinementCascade}; // use smallvec::SmallVec; -// use std::{cell::Cell, rc::Rc}; +// use std::sync::{ +// atomic::{AtomicBool, Ordering::SeqCst}, +// Arc, +// }; -// pub struct Pressable { -// pressed: Rc>, -// pressed_style: ::Refinement, +// pub struct Pressable { +// pressed: Arc, // cascade_slot: CascadeSlot, +// pressed_style: ::Refinement, // child: E, // } -// pub fn pressable(mut child: E) -> Pressable { -// Pressable { -// pressed: Rc::new(Cell::new(false)), -// pressed_style: Default::default(), -// cascade_slot: child.style_cascade().reserve(), -// child, +// impl Pressable { +// pub fn new(mut child: E) -> Self { +// Self { +// pressed: Arc::new(AtomicBool::new(false)), +// cascade_slot: child.style_cascade().reserve(), +// pressed_style: Default::default(), +// child, +// } // } // } -// impl Styleable for Pressable { +// impl Styled for Pressable +// where +// E: Styled, +// { // type Style = E::Style; -// fn declared_style(&mut self) -> &mut ::Refinement { +// fn style_cascade(&mut self) -> &mut RefinementCascade { +// self.child.style_cascade() +// } + +// fn declared_style(&mut self) -> &mut ::Refinement { // &mut self.pressed_style // } +// } -// fn style_cascade(&mut self) -> &mut RefinementCascade { -// self.child.style_cascade() +// impl + Styled> Interactive for Pressable { +// fn listeners(&mut self) -> &mut MouseEventListeners { +// self.child.listeners() // } // } -// impl + Styleable> Element for Pressable { -// type PaintState = E::PaintState; +// impl Element for Pressable { +// type State = E::State; +// type FrameState = E::FrameState; // fn layout( // &mut self, -// view: &mut V, -// cx: &mut ViewContext, -// ) -> Result<(LayoutId, Self::PaintState)> -// where -// Self: Sized, -// { -// self.child.layout(view, cx) +// state: &mut Self::State, +// cx: &mut ViewContext, +// ) -> Result<(crate::LayoutId, Self::FrameState)> { +// Ok(self.child.layout(state, cx)?) // } // fn paint( // &mut self, -// view: &mut V, -// parent_origin: Vector2F, -// layout: &Layout, -// paint_state: &mut Self::PaintState, -// cx: &mut ViewContext, -// ) where -// Self: Sized, -// { +// bounds: Bounds, +// state: &mut Self::State, +// frame_state: &mut Self::FrameState, +// cx: &mut ViewContext, +// ) -> Result<()> { +// let pressed = bounds.contains_point(cx.mouse_position()); // let slot = self.cascade_slot; -// let style = self.pressed.get().then_some(self.pressed_style.clone()); +// let style = pressed.then_some(self.pressed_style.clone()); // self.style_cascade().set(slot, style); +// self.pressed.store(pressed, SeqCst); -// let pressed = self.pressed.clone(); -// let bounds = layout.bounds + parent_origin; -// cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| { -// if event.is_down { -// if bounds.contains_point(event.position) { -// pressed.set(true); -// cx.repaint(); +// let hovered = self.pressed.clone(); +// cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| { +// if phase == DispatchPhase::Capture { +// if bounds.contains_point(event.position) != hovered.load(SeqCst) { +// cx.notify(); +// } +// } +// }); +// cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| { +// if phase == DispatchPhase::Capture { +// if bounds.contains_point(event.position) != hovered.load(SeqCst) { +// cx.notify(); // } -// } else if pressed.get() { -// pressed.set(false); -// cx.repaint(); // } // }); -// self.child -// .paint(view, parent_origin, layout, paint_state, cx); +// self.child.paint(bounds, state, frame_state, cx)?; +// Ok(()) // } // } -// impl + Styleable> Interactive for Pressable { -// fn interaction_handlers(&mut self) -> &mut InteractionHandlers { -// self.child.interaction_handlers() -// } -// } +// impl ParentElement for Pressable { +// type State = E::State; -// impl + Styleable> ParentElement for Pressable { -// fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { +// fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { // self.child.children_mut() // } // } -// impl + Styleable> IntoElement for Pressable { -// type Element = Self; - -// fn into_element(self) -> Self::Element { -// self -// } -// } +// // use crate::{ +// // element::{AnyElement, Element, IntoElement, Layout, ParentElement}, +// // interactive::{InteractionHandlers, Interactive}, +// // style::{Style, StyleHelpers, Styleable}, +// // ViewContext, +// // }; +// // use anyhow::Result; +// // use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId}; +// // use refineable::{CascadeSlot, Refineable, RefinementCascade}; +// // use smallvec::SmallVec; +// // use std::{cell::Cell, rc::Rc}; + +// // pub struct Pressable { +// // pressed: Rc>, +// // pressed_style: ::Refinement, +// // cascade_slot: CascadeSlot, +// // child: E, +// // } + +// // pub fn pressable(mut child: E) -> Pressable { +// // Pressable { +// // pressed: Rc::new(Cell::new(false)), +// // pressed_style: Default::default(), +// // cascade_slot: child.style_cascade().reserve(), +// // child, +// // } +// // } + +// // impl Styleable for Pressable { +// // type Style = E::Style; + +// // fn declared_style(&mut self) -> &mut ::Refinement { +// // &mut self.pressed_style +// // } + +// // fn style_cascade(&mut self) -> &mut RefinementCascade { +// // self.child.style_cascade() +// // } +// // } + +// // impl + Styleable> Element for Pressable { +// // type PaintState = E::PaintState; + +// // fn layout( +// // &mut self, +// // view: &mut V, +// // cx: &mut ViewContext, +// // ) -> Result<(LayoutId, Self::PaintState)> +// // where +// // Self: Sized, +// // { +// // self.child.layout(view, cx) +// // } + +// // fn paint( +// // &mut self, +// // view: &mut V, +// // parent_origin: Vector2F, +// // layout: &Layout, +// // paint_state: &mut Self::PaintState, +// // cx: &mut ViewContext, +// // ) where +// // Self: Sized, +// // { +// // let slot = self.cascade_slot; +// // let style = self.pressed.get().then_some(self.pressed_style.clone()); +// // self.style_cascade().set(slot, style); + +// // let pressed = self.pressed.clone(); +// // let bounds = layout.bounds + parent_origin; +// // cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| { +// // if event.is_down { +// // if bounds.contains_point(event.position) { +// // pressed.set(true); +// // cx.repaint(); +// // } +// // } else if pressed.get() { +// // pressed.set(false); +// // cx.repaint(); +// // } +// // }); + +// // self.child +// // .paint(view, parent_origin, layout, paint_state, cx); +// // } +// // } + +// // impl + Styleable> Interactive for Pressable { +// // fn interaction_handlers(&mut self) -> &mut InteractionHandlers { +// // self.child.interaction_handlers() +// // } +// // } + +// // impl + Styleable> ParentElement for Pressable { +// // fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { +// // self.child.children_mut() +// // } +// // } + +// // impl + Styleable> IntoElement for Pressable { +// // type Element = Self; + +// // fn into_element(self) -> Self::Element { +// // self +// // } +// // } diff --git a/crates/gpui3/src/gpui3.rs b/crates/gpui3/src/gpui3.rs index ea14f4d11378c56157ff09fafa10dca3204015a4..d3b5145c4e4e3ce46e21f5eecb5d9702bc5217c1 100644 --- a/crates/gpui3/src/gpui3.rs +++ b/crates/gpui3/src/gpui3.rs @@ -6,7 +6,6 @@ mod elements; mod events; mod executor; mod geometry; -mod identified; mod image_cache; mod interactive; mod platform; @@ -31,7 +30,6 @@ 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::*; @@ -51,7 +49,6 @@ pub use util::arc_cow::ArcCow; pub use view::*; pub use window::*; -use derive_more::{Deref, DerefMut}; use std::{ mem, ops::{Deref, DerefMut}, @@ -171,12 +168,6 @@ impl Flatten for Result { #[derive(Clone, Eq, PartialEq, Hash)] pub struct SharedString(ArcCow<'static, str>); -#[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 deleted file mode 100644 index 8b137891791fe96927ad78e64b0aad7bded08bdc..0000000000000000000000000000000000000000 --- a/crates/gpui3/src/identified.rs +++ /dev/null @@ -1 +0,0 @@ - From e71465347896447fcf9c75d4b248ce724b4760de Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 10 Oct 2023 12:42:44 -0600 Subject: [PATCH 6/7] Checkpoint --- crates/gpui3/src/element.rs | 6 ++++++ crates/gpui3/src/elements/hoverable.rs | 4 ++-- crates/gpui3/src/elements/identified.rs | 10 ++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index f205a01433c609f372f5da20e5a5e955e5b15de8..3eb2af914ef7b177d8a131d9e856c538aa1b2073 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -33,6 +33,12 @@ pub trait Element: 'static { } } +pub trait StatefulElement: Element { + fn element_id(&self) -> ElementId { + Element::element_id(self).unwrap() + } +} + #[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct ElementId(ArcCow<'static, [u8]>); diff --git a/crates/gpui3/src/elements/hoverable.rs b/crates/gpui3/src/elements/hoverable.rs index 4595ddf6f8f4cee2e4f83c9eb6280f5430bd41c7..ddd6782afc46cd364c4ba77715e846df649f141a 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, ElementId, Interactive, MouseEventListeners, - MouseMoveEvent, ParentElement, Pixels, Stateful, Styled, ViewContext, + MouseMoveEvent, ParentElement, Pixels, StatefulElement, Styled, ViewContext, }; use anyhow::Result; use refineable::{CascadeSlot, Refineable, RefinementCascade}; @@ -100,4 +100,4 @@ impl ParentElement for Hoverable { } } -impl Stateful for Hoverable {} +impl StatefulElement for Hoverable {} diff --git a/crates/gpui3/src/elements/identified.rs b/crates/gpui3/src/elements/identified.rs index b93a3fd1cf4e6ef0b44aba1ca6c1336a4a0f6e3c..1cbbbb99ce7c84d3a8683a96f2b59b2dedba6710 100644 --- a/crates/gpui3/src/elements/identified.rs +++ b/crates/gpui3/src/elements/identified.rs @@ -1,12 +1,6 @@ -use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, ViewContext}; +use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, StatefulElement, ViewContext}; use anyhow::Result; -pub trait Stateful: Element { - fn element_id(&self) -> ElementId { - Element::element_id(self).unwrap() - } -} - pub struct Identified { pub(crate) element: E, pub(crate) id: ElementId, @@ -41,4 +35,4 @@ impl Element for Identified { } } -impl Stateful for Identified {} +impl StatefulElement for Identified {} From 61b8ad38bd022f0ce1efadcf37b6d8ed7e021c2c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 10 Oct 2023 12:44:40 -0600 Subject: [PATCH 7/7] Remove state erasure for now --- crates/gpui3/src/elements.rs | 2 -- crates/gpui3/src/elements/stateless.rs | 30 -------------------------- crates/gpui3/src/window.rs | 10 --------- 3 files changed, 42 deletions(-) delete mode 100644 crates/gpui3/src/elements/stateless.rs diff --git a/crates/gpui3/src/elements.rs b/crates/gpui3/src/elements.rs index b2653b22a3231aae904dbc39ea405cb0296e3ccd..5ee517b6e913ff16361dcc4d2acddb3e4212f83f 100644 --- a/crates/gpui3/src/elements.rs +++ b/crates/gpui3/src/elements.rs @@ -3,7 +3,6 @@ mod hoverable; mod identified; mod img; mod pressable; -mod stateless; mod svg; mod text; @@ -12,6 +11,5 @@ pub use hoverable::*; pub use identified::*; pub use img::*; pub use pressable::*; -pub use stateless::*; pub use svg::*; pub use text::*; diff --git a/crates/gpui3/src/elements/stateless.rs b/crates/gpui3/src/elements/stateless.rs deleted file mode 100644 index b1cd31a146bd61a4cc7cdabe2f78a595df5b332d..0000000000000000000000000000000000000000 --- a/crates/gpui3/src/elements/stateless.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::{Bounds, Element, Pixels}; -use std::marker::PhantomData; - -pub struct Stateless, S> { - element: E, - parent_state_type: PhantomData, -} - -impl, S: Send + Sync + 'static> Element for Stateless { - type State = S; - type FrameState = E::FrameState; - - fn layout( - &mut self, - _: &mut Self::State, - cx: &mut crate::ViewContext, - ) -> anyhow::Result<(crate::LayoutId, Self::FrameState)> { - cx.erase_state(|cx| self.element.layout(&mut (), cx)) - } - - fn paint( - &mut self, - bounds: Bounds, - _: &mut Self::State, - frame_state: &mut Self::FrameState, - cx: &mut crate::ViewContext, - ) -> anyhow::Result<()> { - cx.erase_state(|cx| self.element.paint(bounds, &mut (), frame_state, cx)) - } -} diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index add9615476571de6ae5572bb9977edcb55428821..8c71284cc93730d0919834e4ddd04e920a9d8eeb 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -926,16 +926,6 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { }) }); } - - pub(crate) fn erase_state(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R { - let entity_id = self.unit_entity.id; - let mut cx = ViewContext::mutable( - &mut *self.window_cx.app, - &mut *self.window_cx.window, - entity_id, - ); - f(&mut cx) - } } impl<'a, 'w, S> Context for ViewContext<'a, 'w, S> {