Smooth out auto-scrolling speed scaling in scrollable

This commit is contained in:
Héctor Ramón Jiménez 2025-11-28 06:29:25 +01:00
parent 519e7ae414
commit eadd7b8e81
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 32 additions and 20 deletions

View file

@ -275,7 +275,9 @@ impl Gallery {
.height(grid::aspect_ratio(Preview::WIDTH, Preview::HEIGHT)) .height(grid::aspect_ratio(Preview::WIDTH, Preview::HEIGHT))
.spacing(10); .spacing(10);
let content = container(scrollable(gallery).spacing(10)).padding(10); let content =
container(scrollable(gallery).spacing(10).auto_scroll(true))
.padding(10);
let viewer = self.viewer.view(self.now); let viewer = self.viewer.view(self.now);
stack![content, viewer].into() stack![content, viewer].into()

View file

@ -579,7 +579,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
const AUTOSCROLL_DEADZONE: f32 = 20.0; const AUTOSCROLL_DEADZONE: f32 = 20.0;
const AUTOSCROLL_SPEED: f32 = 10.0; const AUTOSCROLL_SMOOTHNESS: f32 = 1.5;
let state = tree.state.downcast_mut::<State>(); let state = tree.state.downcast_mut::<State>();
let bounds = layout.bounds(); let bounds = layout.bounds();
@ -764,6 +764,21 @@ where
} }
} }
if matches!(state.interaction, Interaction::AutoScrolling { .. })
&& matches!(
event,
Event::Mouse(
mouse::Event::ButtonPressed(_)
| mouse::Event::WheelScrolled { .. }
) | Event::Touch(_)
| Event::Keyboard(_)
)
{
state.interaction = Interaction::None;
shell.capture_event();
return;
}
if state.last_scrolled.is_none() if state.last_scrolled.is_none()
|| !matches!( || !matches!(
event, event,
@ -820,20 +835,6 @@ where
return; return;
} }
if matches!(state.interaction, Interaction::AutoScrolling { .. })
&& matches!(
event,
Event::Mouse(
mouse::Event::ButtonPressed(_)
| mouse::Event::WheelScrolled { .. }
) | Event::Touch(_)
| Event::Keyboard(_)
)
{
state.interaction = Interaction::None;
return;
}
if shell.is_event_captured() { if shell.is_event_captured() {
return; return;
} }
@ -1025,13 +1026,22 @@ where
Duration::ZERO Duration::ZERO
}; };
let scroll_factor = let scroll_factor = time_delta.as_secs_f32();
time_delta.as_secs_f32() * AUTOSCROLL_SPEED;
state.scroll( state.scroll(
self.direction.align(Vector::new( self.direction.align(Vector::new(
delta.x * scroll_factor, delta.x.signum()
delta.y * scroll_factor, * delta
.x
.abs()
.powf(AUTOSCROLL_SMOOTHNESS)
* scroll_factor,
delta.y.signum()
* delta
.y
.abs()
.powf(AUTOSCROLL_SMOOTHNESS)
* scroll_factor,
)), )),
bounds, bounds,
content_bounds, content_bounds,