diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index bbc3454923c488c9b9120a7a762ed5b85fba28ea..6e05b384e15492f6ebd137004f0f13fd4a6d549c 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -613,10 +613,10 @@ pub trait InteractiveElement: Sized { /// Track the focus state of the given focus handle on this element. /// If the focus handle is focused by the application, this element will /// apply its focused styles. - fn track_focus(mut self, focus_handle: &FocusHandle) -> FocusableWrapper { + fn track_focus(mut self, focus_handle: &FocusHandle) -> Self { self.interactivity().focusable = true; self.interactivity().tracked_focus_handle = Some(focus_handle.clone()); - FocusableWrapper { element: self } + self } /// Set the keymap context for this element. This will be used to determine @@ -980,15 +980,35 @@ pub trait InteractiveElement: Sized { self.interactivity().block_mouse_except_scroll(); self } + + /// Set the given styles to be applied when this element, specifically, is focused. + /// Requires that the element is focusable. Elements can be made focusable using [`InteractiveElement::track_focus`]. + fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self + where + Self: Sized, + { + self.interactivity().focus_style = Some(Box::new(f(StyleRefinement::default()))); + self + } + + /// Set the given styles to be applied when this element is inside another element that is focused. + /// Requires that the element is focusable. Elements can be made focusable using [`InteractiveElement::track_focus`]. + fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self + where + Self: Sized, + { + self.interactivity().in_focus_style = Some(Box::new(f(StyleRefinement::default()))); + self + } } /// A trait for elements that want to use the standard GPUI interactivity features /// that require state. pub trait StatefulInteractiveElement: InteractiveElement { /// Set this element to focusable. - fn focusable(mut self) -> FocusableWrapper { + fn focusable(mut self) -> Self { self.interactivity().focusable = true; - FocusableWrapper { element: self } + self } /// Set the overflow x and y to scroll. @@ -1118,27 +1138,6 @@ pub trait StatefulInteractiveElement: InteractiveElement { } } -/// A trait for providing focus related APIs to interactive elements -pub trait FocusableElement: InteractiveElement { - /// Set the given styles to be applied when this element, specifically, is focused. - fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self - where - Self: Sized, - { - self.interactivity().focus_style = Some(Box::new(f(StyleRefinement::default()))); - self - } - - /// Set the given styles to be applied when this element is inside another element that is focused. - fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self - where - Self: Sized, - { - self.interactivity().in_focus_style = Some(Box::new(f(StyleRefinement::default()))); - self - } -} - pub(crate) type MouseDownListener = Box; pub(crate) type MouseUpListener = @@ -2777,126 +2776,6 @@ impl GroupHitboxes { } } -/// A wrapper around an element that can be focused. -pub struct FocusableWrapper { - /// The element that is focusable - pub element: E, -} - -impl FocusableElement for FocusableWrapper {} - -impl InteractiveElement for FocusableWrapper -where - E: InteractiveElement, -{ - fn interactivity(&mut self) -> &mut Interactivity { - self.element.interactivity() - } -} - -impl StatefulInteractiveElement for FocusableWrapper {} - -impl Styled for FocusableWrapper -where - E: Styled, -{ - fn style(&mut self) -> &mut StyleRefinement { - self.element.style() - } -} - -impl FocusableWrapper
{ - /// Add a listener to be called when the children of this `Div` are prepainted. - /// This allows you to store the [`Bounds`] of the children for later use. - pub fn on_children_prepainted( - mut self, - listener: impl Fn(Vec>, &mut Window, &mut App) + 'static, - ) -> Self { - self.element = self.element.on_children_prepainted(listener); - self - } -} - -impl Element for FocusableWrapper -where - E: Element, -{ - type RequestLayoutState = E::RequestLayoutState; - type PrepaintState = E::PrepaintState; - - fn id(&self) -> Option { - self.element.id() - } - - fn source_location(&self) -> Option<&'static core::panic::Location<'static>> { - self.element.source_location() - } - - fn request_layout( - &mut self, - id: Option<&GlobalElementId>, - inspector_id: Option<&InspectorElementId>, - window: &mut Window, - cx: &mut App, - ) -> (LayoutId, Self::RequestLayoutState) { - self.element.request_layout(id, inspector_id, window, cx) - } - - fn prepaint( - &mut self, - id: Option<&GlobalElementId>, - inspector_id: Option<&InspectorElementId>, - bounds: Bounds, - state: &mut Self::RequestLayoutState, - window: &mut Window, - cx: &mut App, - ) -> E::PrepaintState { - self.element - .prepaint(id, inspector_id, bounds, state, window, cx) - } - - fn paint( - &mut self, - id: Option<&GlobalElementId>, - inspector_id: Option<&InspectorElementId>, - bounds: Bounds, - request_layout: &mut Self::RequestLayoutState, - prepaint: &mut Self::PrepaintState, - window: &mut Window, - cx: &mut App, - ) { - self.element.paint( - id, - inspector_id, - bounds, - request_layout, - prepaint, - window, - cx, - ) - } -} - -impl IntoElement for FocusableWrapper -where - E: IntoElement, -{ - type Element = E::Element; - - fn into_element(self) -> Self::Element { - self.element.into_element() - } -} - -impl ParentElement for FocusableWrapper -where - E: ParentElement, -{ - fn extend(&mut self, elements: impl IntoIterator) { - self.element.extend(elements) - } -} - /// A wrapper around an element that can store state, produced after assigning an ElementId. pub struct Stateful { pub(crate) element: E, @@ -2927,8 +2806,6 @@ where } } -impl FocusableElement for Stateful {} - impl Element for Stateful where E: Element, diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rs index c6130667776351bf4aa2230a1c37454f598320a3..993b319b697ece386ad8af6d6164c1b85bf3a1c7 100644 --- a/crates/gpui/src/elements/img.rs +++ b/crates/gpui/src/elements/img.rs @@ -25,7 +25,7 @@ use std::{ use thiserror::Error; use util::ResultExt; -use super::{FocusableElement, Stateful, StatefulInteractiveElement}; +use super::{Stateful, StatefulInteractiveElement}; /// The delay before showing the loading state. pub const LOADING_DELAY: Duration = Duration::from_millis(200); @@ -509,8 +509,6 @@ impl IntoElement for Img { } } -impl FocusableElement for Img {} - impl StatefulInteractiveElement for Img {} impl ImageSource { diff --git a/crates/gpui/src/prelude.rs b/crates/gpui/src/prelude.rs index 270f0a9341181e35669db16b9bddfb3aa6afd519..191d0a0e6d4019df9a6584fc2c15a406bbb08287 100644 --- a/crates/gpui/src/prelude.rs +++ b/crates/gpui/src/prelude.rs @@ -3,7 +3,7 @@ //! application to avoid having to import each trait individually. pub use crate::{ - AppContext as _, BorrowAppContext, Context, Element, FocusableElement, InteractiveElement, - IntoElement, ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement, Styled, - StyledImage, VisualContext, util::FluentBuilder, + AppContext as _, BorrowAppContext, Context, Element, InteractiveElement, IntoElement, + ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement, Styled, StyledImage, + VisualContext, util::FluentBuilder, }; diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rs index c0671048f6e54a1da6a54c1a0aea217706a82571..c70114a55733221c2d9562140ea6a4479e0cf38e 100644 --- a/crates/terminal_view/src/terminal_element.rs +++ b/crates/terminal_view/src/terminal_element.rs @@ -196,7 +196,6 @@ impl TerminalElement { interactivity: Default::default(), } .track_focus(&focus) - .element } //Vec> -> Clip out the parts of the ranges