From 11dbdb125ad89d9516d9d83370573695e59ac8ef Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 23 Dec 2024 15:30:36 +0800 Subject: [PATCH] chore: Removed ScrollableAxis use Axis instead. (#509) --- crates/ui/src/scroll/scrollable_mask.rs | 40 ++++++++----------------- crates/ui/src/table.rs | 6 ++-- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/crates/ui/src/scroll/scrollable_mask.rs b/crates/ui/src/scroll/scrollable_mask.rs index ce4e52a6..51c514dc 100644 --- a/crates/ui/src/scroll/scrollable_mask.rs +++ b/crates/ui/src/scroll/scrollable_mask.rs @@ -1,18 +1,10 @@ use gpui::{ - px, relative, AnyView, Bounds, ContentMask, Corners, Edges, Element, ElementId, + px, relative, Axis, Bounds, ContentMask, Corners, Edges, Element, ElementId, EntityId, GlobalElementId, Hitbox, Hsla, IntoElement, IsZero as _, LayoutId, PaintQuad, Pixels, Point, Position, ScrollHandle, ScrollWheelEvent, Style, WindowContext, }; -/// The scroll axis direction. -#[allow(dead_code)] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ScrollableAxis { - /// Horizontal scroll. - Horizontal, - /// Vertical scroll. - Vertical, -} +use crate::AxisExt; /// Make a scrollable mask element to cover the parent view with the mouse wheel event listening. /// @@ -20,21 +12,17 @@ pub enum ScrollableAxis { /// You can use this `scroll_handle` to control what you want to scroll. /// This is only can handle once axis scrolling. pub struct ScrollableMask { - view: AnyView, - axis: ScrollableAxis, + view_id: EntityId, + axis: Axis, scroll_handle: ScrollHandle, debug: Option, } impl ScrollableMask { /// Create a new scrollable mask element. - pub fn new( - view: impl Into, - axis: ScrollableAxis, - scroll_handle: &ScrollHandle, - ) -> Self { + pub fn new(view_id: EntityId, axis: Axis, scroll_handle: &ScrollHandle) -> Self { Self { - view: view.into(), + view_id, scroll_handle: scroll_handle.clone(), axis, debug: None, @@ -123,17 +111,17 @@ impl Element for ScrollableMask { } cx.on_mouse_event({ + let view_id = self.view_id; + let is_horizontal = self.axis.is_horizontal(); + let scroll_handle = self.scroll_handle.clone(); let hitbox = hitbox.clone(); let mouse_position = cx.mouse_position(); - let scroll_handle = self.scroll_handle.clone(); let last_offset = scroll_handle.offset(); - let view_id = self.view.entity_id(); - let is_horizontal = matches!(self.axis, ScrollableAxis::Horizontal); move |event: &ScrollWheelEvent, phase, cx| { if bounds.contains(&mouse_position) && phase.bubble() && hitbox.is_hovered(cx) { - let mut delta = event.delta.pixel_delta(line_height); let mut offset = scroll_handle.offset(); + let mut delta = event.delta.pixel_delta(line_height); // Limit for only one way scrolling at same time. // When use MacBook touchpad we may get both x and y delta, @@ -147,13 +135,9 @@ impl Element for ScrollableMask { } if is_horizontal { - if !delta.x.is_zero() { - offset.x += delta.x; - } + offset.x += delta.x; } else { - if !delta.y.is_zero() { - offset.y += delta.y; - } + offset.y += delta.y; } if last_offset != offset { diff --git a/crates/ui/src/table.rs b/crates/ui/src/table.rs index 6cf7d8e7..cf215a45 100644 --- a/crates/ui/src/table.rs +++ b/crates/ui/src/table.rs @@ -4,7 +4,7 @@ use crate::{ context_menu::ContextMenuExt, h_flex, popup_menu::PopupMenu, - scroll::{ScrollableAxis, ScrollableMask, Scrollbar, ScrollbarState}, + scroll::{ScrollableMask, Scrollbar, ScrollbarState}, theme::ActiveTheme, v_flex, virtual_list::virtual_list, @@ -1240,8 +1240,8 @@ where .bg(cx.theme().table) .child(inner_table) .child(ScrollableMask::new( - cx.view().clone(), - ScrollableAxis::Horizontal, + cx.view().entity_id(), + Axis::Horizontal, &horizontal_scroll_handle, )) .child(canvas(