From 4a6c8ff80964f0806542eb3bfa6ca2e3d97688fd Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 19 Oct 2023 23:35:09 +0200 Subject: [PATCH] Checkpoint --- crates/gpui3/src/element.rs | 25 ++++++++++++++++--------- crates/gpui3/src/elements/div.rs | 25 +++++++------------------ crates/gpui3/src/elements/img.rs | 11 ++++++----- crates/gpui3/src/elements/svg.rs | 11 ++++++----- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/crates/gpui3/src/element.rs b/crates/gpui3/src/element.rs index e96f5e6b28969ddee66fcde9c5234deb21e0347b..d384498259f366436d2dd13cd5edead3464e2e73 100644 --- a/crates/gpui3/src/element.rs +++ b/crates/gpui3/src/element.rs @@ -85,7 +85,7 @@ pub trait ElementInteractivity: 'static + Send + Sync &self, style: &mut Style, bounds: Bounds, - active_state: &Mutex, + element_state: &InteractiveElementState, cx: &mut ViewContext, ) { let mouse_position = cx.mouse_position(); @@ -102,7 +102,7 @@ pub trait ElementInteractivity: 'static + Send + Sync } if let Some(stateful) = self.as_stateful() { - let active_state = active_state.lock(); + let active_state = element_state.active_state.lock(); if active_state.group { if let Some(group_style) = stateful.group_active_style.as_ref() { style.refine(&group_style.style); @@ -117,8 +117,7 @@ pub trait ElementInteractivity: 'static + Send + Sync fn paint( &mut self, bounds: Bounds, - pending_click: Arc>>, - interactive_state: Arc>, + element_state: &InteractiveElementState, cx: &mut ViewContext, ) { let stateless = self.as_stateless(); @@ -162,6 +161,7 @@ pub trait ElementInteractivity: 'static + Send + Sync if let Some(stateful) = self.as_stateful() { let click_listeners = stateful.mouse_click_listeners.clone(); + let pending_click = element_state.pending_click.clone(); let mouse_down = pending_click.lock().clone(); if let Some(mouse_down) = mouse_down { cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { @@ -185,7 +185,8 @@ pub trait ElementInteractivity: 'static + Send + Sync }); } - if interactive_state.lock().is_none() { + let active_state = element_state.active_state.clone(); + if active_state.lock().is_none() { let active_group_bounds = stateful .group_active_style .as_ref() @@ -196,7 +197,7 @@ pub trait ElementInteractivity: 'static + Send + Sync .map_or(false, |bounds| bounds.contains_point(&down.position)); let element = bounds.contains_point(&down.position); if group || element { - *interactive_state.lock() = InteractiveElementState { group, element }; + *active_state.lock() = ActiveState { group, element }; cx.notify(); } } @@ -204,7 +205,7 @@ pub trait ElementInteractivity: 'static + Send + Sync } else { cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| { if phase == DispatchPhase::Capture { - *interactive_state.lock() = InteractiveElementState::default(); + *active_state.lock() = ActiveState::default(); cx.notify(); } }); @@ -319,17 +320,23 @@ impl GroupBounds { } #[derive(Copy, Clone, Default, Eq, PartialEq)] -pub struct InteractiveElementState { +struct ActiveState { pub group: bool, pub element: bool, } -impl InteractiveElementState { +impl ActiveState { pub fn is_none(&self) -> bool { !self.group && !self.element } } +#[derive(Default)] +pub struct InteractiveElementState { + active_state: Arc>, + pending_click: Arc>>, +} + impl Default for StatelessInteractivity { fn default() -> Self { Self { diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index 7df9a58bf929842596f4bc17624ea433bd01d5ec..ec815ea92135e47d0a9dac8f10a21daf8383293b 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -2,21 +2,15 @@ use crate::{ Active, AnyElement, BorrowWindow, Bounds, Element, ElementFocusability, ElementId, ElementInteractivity, Focus, FocusHandle, FocusListeners, Focusable, GlobalElementId, GroupBounds, GroupStyle, Hover, InteractiveElementState, IntoAnyElement, LayoutId, - MouseDownEvent, NonFocusable, Overflow, ParentElement, Pixels, Point, SharedString, - StatefulInteractivity, StatefullyInteractive, StatelessInteractivity, StatelesslyInteractive, - Style, StyleRefinement, Styled, ViewContext, + NonFocusable, Overflow, ParentElement, Pixels, Point, SharedString, StatefulInteractivity, + StatefullyInteractive, StatelessInteractivity, StatelesslyInteractive, Style, StyleRefinement, + Styled, ViewContext, }; use parking_lot::Mutex; use refineable::Refineable; use smallvec::SmallVec; use std::sync::Arc; -#[derive(Default)] -pub struct DivState { - active_state: Arc>, - pending_click: Arc>>, -} - #[derive(Default, Clone)] pub struct ScrollState(Arc>>); @@ -148,14 +142,14 @@ where pub fn compute_style( &self, bounds: Bounds, - state: &DivState, + state: &InteractiveElementState, cx: &mut ViewContext, ) -> Style { let mut computed_style = Style::default(); computed_style.refine(&self.base_style); self.focusability.refine_style(&mut computed_style, cx); self.interactivity - .refine_style(&mut computed_style, bounds, &state.active_state, cx); + .refine_style(&mut computed_style, bounds, state, cx); computed_style } } @@ -209,7 +203,7 @@ where V: 'static + Send + Sync, { type ViewState = V; - type ElementState = DivState; + type ElementState = InteractiveElementState; fn id(&self) -> Option { self.interactivity @@ -273,12 +267,7 @@ where style.paint(bounds, cx); this.focusability.paint(bounds, cx); - this.interactivity.paint( - bounds, - element_state.pending_click.clone(), - element_state.active_state.clone(), - cx, - ); + this.interactivity.paint(bounds, element_state, cx); }); cx.stack(1, |cx| { diff --git a/crates/gpui3/src/elements/img.rs b/crates/gpui3/src/elements/img.rs index 5bf46f40df8f165da8c8f4fbe6427eb52a14a1f5..e6ebc986e27cee778aa66d0ce8c68f587a6191ff 100644 --- a/crates/gpui3/src/elements/img.rs +++ b/crates/gpui3/src/elements/img.rs @@ -1,8 +1,9 @@ use crate::{ - div, Active, AnyElement, BorrowWindow, Bounds, Div, DivState, Element, ElementFocusability, - ElementId, ElementInteractivity, Focus, FocusListeners, Focusable, Hover, IntoAnyElement, - LayoutId, NonFocusable, Pixels, SharedString, StatefulInteractivity, StatefullyInteractive, - StatelessInteractivity, StatelesslyInteractive, StyleRefinement, Styled, ViewContext, + div, Active, AnyElement, BorrowWindow, Bounds, Div, Element, ElementFocusability, ElementId, + ElementInteractivity, Focus, FocusListeners, Focusable, Hover, InteractiveElementState, + IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString, StatefulInteractivity, + StatefullyInteractive, StatelessInteractivity, StatelesslyInteractive, StyleRefinement, Styled, + ViewContext, }; use futures::FutureExt; use util::ResultExt; @@ -77,7 +78,7 @@ where F: ElementFocusability, { type ViewState = V; - type ElementState = DivState; + type ElementState = InteractiveElementState; fn id(&self) -> Option { self.base.id() diff --git a/crates/gpui3/src/elements/svg.rs b/crates/gpui3/src/elements/svg.rs index eed94cf28bb43c2565520e89173cfd96acfed502..2af0e5aa6b5e99dd27259d66b4053a5c63c420ce 100644 --- a/crates/gpui3/src/elements/svg.rs +++ b/crates/gpui3/src/elements/svg.rs @@ -1,8 +1,9 @@ use crate::{ - div, Active, AnyElement, Bounds, Div, DivState, Element, ElementFocusability, ElementId, - ElementInteractivity, Focus, FocusListeners, Focusable, Hover, IntoAnyElement, LayoutId, - NonFocusable, Pixels, SharedString, StatefulInteractivity, StatefullyInteractive, - StatelessInteractivity, StatelesslyInteractive, StyleRefinement, Styled, ViewContext, + div, Active, AnyElement, Bounds, Div, Element, ElementFocusability, ElementId, + ElementInteractivity, Focus, FocusListeners, Focusable, Hover, InteractiveElementState, + IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString, StatefulInteractivity, + StatefullyInteractive, StatelessInteractivity, StatelesslyInteractive, StyleRefinement, Styled, + ViewContext, }; use util::ResultExt; @@ -68,7 +69,7 @@ where F: ElementFocusability, { type ViewState = V; - type ElementState = DivState; + type ElementState = InteractiveElementState; fn id(&self) -> Option { self.base.id()