chore: Removed ScrollableAxis use Axis instead. (#509)

This commit is contained in:
Jason Lee 2024-12-23 15:30:36 +08:00 committed by GitHub
parent 500aa2d7c5
commit 11dbdb125a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 31 deletions

View file

@ -1,18 +1,10 @@
use gpui::{ 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, GlobalElementId, Hitbox, Hsla, IntoElement, IsZero as _, LayoutId, PaintQuad, Pixels, Point,
Position, ScrollHandle, ScrollWheelEvent, Style, WindowContext, Position, ScrollHandle, ScrollWheelEvent, Style, WindowContext,
}; };
/// The scroll axis direction. use crate::AxisExt;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollableAxis {
/// Horizontal scroll.
Horizontal,
/// Vertical scroll.
Vertical,
}
/// Make a scrollable mask element to cover the parent view with the mouse wheel event listening. /// 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. /// You can use this `scroll_handle` to control what you want to scroll.
/// This is only can handle once axis scrolling. /// This is only can handle once axis scrolling.
pub struct ScrollableMask { pub struct ScrollableMask {
view: AnyView, view_id: EntityId,
axis: ScrollableAxis, axis: Axis,
scroll_handle: ScrollHandle, scroll_handle: ScrollHandle,
debug: Option<Hsla>, debug: Option<Hsla>,
} }
impl ScrollableMask { impl ScrollableMask {
/// Create a new scrollable mask element. /// Create a new scrollable mask element.
pub fn new( pub fn new(view_id: EntityId, axis: Axis, scroll_handle: &ScrollHandle) -> Self {
view: impl Into<AnyView>,
axis: ScrollableAxis,
scroll_handle: &ScrollHandle,
) -> Self {
Self { Self {
view: view.into(), view_id,
scroll_handle: scroll_handle.clone(), scroll_handle: scroll_handle.clone(),
axis, axis,
debug: None, debug: None,
@ -123,17 +111,17 @@ impl Element for ScrollableMask {
} }
cx.on_mouse_event({ 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 hitbox = hitbox.clone();
let mouse_position = cx.mouse_position(); let mouse_position = cx.mouse_position();
let scroll_handle = self.scroll_handle.clone();
let last_offset = scroll_handle.offset(); 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| { move |event: &ScrollWheelEvent, phase, cx| {
if bounds.contains(&mouse_position) && phase.bubble() && hitbox.is_hovered(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 offset = scroll_handle.offset();
let mut delta = event.delta.pixel_delta(line_height);
// Limit for only one way scrolling at same time. // Limit for only one way scrolling at same time.
// When use MacBook touchpad we may get both x and y delta, // When use MacBook touchpad we may get both x and y delta,
@ -147,13 +135,9 @@ impl Element for ScrollableMask {
} }
if is_horizontal { if is_horizontal {
if !delta.x.is_zero() { offset.x += delta.x;
offset.x += delta.x;
}
} else { } else {
if !delta.y.is_zero() { offset.y += delta.y;
offset.y += delta.y;
}
} }
if last_offset != offset { if last_offset != offset {

View file

@ -4,7 +4,7 @@ use crate::{
context_menu::ContextMenuExt, context_menu::ContextMenuExt,
h_flex, h_flex,
popup_menu::PopupMenu, popup_menu::PopupMenu,
scroll::{ScrollableAxis, ScrollableMask, Scrollbar, ScrollbarState}, scroll::{ScrollableMask, Scrollbar, ScrollbarState},
theme::ActiveTheme, theme::ActiveTheme,
v_flex, v_flex,
virtual_list::virtual_list, virtual_list::virtual_list,
@ -1240,8 +1240,8 @@ where
.bg(cx.theme().table) .bg(cx.theme().table)
.child(inner_table) .child(inner_table)
.child(ScrollableMask::new( .child(ScrollableMask::new(
cx.view().clone(), cx.view().entity_id(),
ScrollableAxis::Horizontal, Axis::Horizontal,
&horizontal_scroll_handle, &horizontal_scroll_handle,
)) ))
.child(canvas( .child(canvas(