Skip to content

Commit

Permalink
Implement mouse wheel transactions for scrollable
Browse files Browse the repository at this point in the history
See https://wiki.mozilla.org/Gecko:Mouse_Wheel_Scrolling#Mouse_wheel_transaction

Co-authored-by: Daniel Yoon <101683475+Koranir@users.noreply.github.com>
  • Loading branch information
hecrj and Koranir committed Sep 8, 2024
1 parent dff14bd commit 502c5fd
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::core::layout;
use crate::core::mouse;
use crate::core::overlay;
use crate::core::renderer;
use crate::core::time::{Duration, Instant};
use crate::core::touch;
use crate::core::widget;
use crate::core::widget::operation::{self, Operation};
Expand Down Expand Up @@ -470,6 +471,24 @@ where
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
scrollbars.is_mouse_over(cursor);

if let Some(last_scrolled) = state.last_scrolled {
let clear_transaction = match event {
Event::Mouse(
mouse::Event::ButtonPressed(_)
| mouse::Event::ButtonReleased(_)
| mouse::Event::CursorLeft,
) => true,
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
last_scrolled.elapsed() > Duration::from_millis(100)
}
_ => last_scrolled.elapsed() > Duration::from_millis(1500),
};

if clear_transaction {
state.last_scrolled = None;
}
}

if let Some(scroller_grabbed_at) = state.y_scroller_grabbed_at {
match event {
Event::Mouse(mouse::Event::CursorMoved { .. })
Expand Down Expand Up @@ -612,7 +631,11 @@ where
}
}

let mut event_status = {
let content_status = if state.last_scrolled.is_some()
&& matches!(event, Event::Mouse(mouse::Event::WheelScrolled { .. }))
{
event::Status::Ignored
} else {
let cursor = match cursor_over_scrollable {
Some(cursor_position)
if !(mouse_over_x_scrollbar || mouse_over_y_scrollbar) =>
Expand Down Expand Up @@ -660,10 +683,10 @@ where
state.x_scroller_grabbed_at = None;
state.y_scroller_grabbed_at = None;

return event_status;
return content_status;
}

if let event::Status::Captured = event_status {
if let event::Status::Captured = content_status {
return event::Status::Captured;
}

Expand Down Expand Up @@ -699,7 +722,7 @@ where

state.scroll(delta, self.direction, bounds, content_bounds);

event_status = if notify_on_scroll(
if notify_on_scroll(
state,
&self.on_scroll,
bounds,
Expand All @@ -709,7 +732,7 @@ where
event::Status::Captured
} else {
event::Status::Ignored
};
}
}
Event::Touch(event)
if state.scroll_area_touched_at.is_some()
Expand Down Expand Up @@ -760,12 +783,10 @@ where
_ => {}
}

event_status = event::Status::Captured;
event::Status::Captured
}
_ => {}
_ => event::Status::Ignored,
}

event_status
}

fn draw(
Expand Down Expand Up @@ -1133,7 +1154,9 @@ fn notify_on_scroll<Message>(
if let Some(on_scroll) = on_scroll {
shell.publish(on_scroll(viewport));
}

state.last_notified = Some(viewport);
state.last_scrolled = Some(Instant::now());

true
}
Expand All @@ -1147,6 +1170,7 @@ struct State {
x_scroller_grabbed_at: Option<f32>,
keyboard_modifiers: keyboard::Modifiers,
last_notified: Option<Viewport>,
last_scrolled: Option<Instant>,
}

impl Default for State {
Expand All @@ -1159,6 +1183,7 @@ impl Default for State {
x_scroller_grabbed_at: None,
keyboard_modifiers: keyboard::Modifiers::default(),
last_notified: None,
last_scrolled: None,
}
}
}
Expand Down

0 comments on commit 502c5fd

Please sign in to comment.