Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Removed ScrollableAxis use Axis instead. #509

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 12 additions & 28 deletions crates/ui/src/scroll/scrollable_mask.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
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.
///
/// When the mouse wheel is scrolled, will move the `scroll_handle` scrolling with the `axis` direction.
/// 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<Hsla>,
}

impl ScrollableMask {
/// Create a new scrollable mask element.
pub fn new(
view: impl Into<AnyView>,
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/ui/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down