table: Fix to only handle one way scrolling at same time. (#507)

Closes #314
This commit is contained in:
Jason Lee 2024-12-20 22:07:01 +08:00
parent 28463d3a18
commit 500aa2d7c5

View file

@ -126,29 +126,38 @@ impl Element for ScrollableMask {
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 scroll_handle = self.scroll_handle.clone();
let old_offset = scroll_handle.offset(); let last_offset = scroll_handle.offset();
let view_id = self.view.entity_id(); let view_id = self.view.entity_id();
let is_horizontal = self.axis == ScrollableAxis::Horizontal; 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 delta = event.delta.pixel_delta(line_height); let mut delta = event.delta.pixel_delta(line_height);
let mut offset = scroll_handle.offset();
if is_horizontal && !delta.x.is_zero() { // Limit for only one way scrolling at same time.
// When is horizontal scroll, move the horizontal scroll handle to make scrolling. // When use MacBook touchpad we may get both x and y delta,
let mut offset = scroll_handle.offset(); // only allows the one that more to scroll.
offset.x += delta.x; if !delta.x.is_zero() && !delta.y.is_zero() {
scroll_handle.set_offset(offset); if delta.x.abs() > delta.y.abs() {
delta.y = px(0.);
} else {
delta.x = px(0.);
}
} }
if !is_horizontal && !delta.y.is_zero() { if is_horizontal {
// When is vertical scroll, move the vertical scroll handle to make scrolling. if !delta.x.is_zero() {
let mut offset = scroll_handle.offset(); offset.x += delta.x;
offset.y += delta.y; }
scroll_handle.set_offset(offset); } else {
if !delta.y.is_zero() {
offset.y += delta.y;
}
} }
if old_offset != scroll_handle.offset() { if last_offset != offset {
scroll_handle.set_offset(offset);
cx.notify(Some(view_id)); cx.notify(Some(view_id));
cx.stop_propagation(); cx.stop_propagation();
} }