Smooth wheel scrolling: route scroll over sliders to the list or the volume (#5)
CI / check (push) Successful in 9s
CI / native (push) Successful in 1m14s

This commit was merged in pull request #5.
This commit is contained in:
nandi committed 2026-11-23 06:55:50 +00:00
1 parent 0760d7ba6c
commit d360b51bb2
9 files changed
+511 -15

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
// Scrolling over a volume slider: does it scroll the list or change the
// volume, and by how much?
//
// GTK's own GtkRange handler moves a slider by its page increment (10% here)
// per wheel detent, ignores a horizontal wheel and swallows every scroll
// event, including a touchpad's vertical swipe it does nothing with. So a
// list scroll that passes over a slider stops dead and changes that stream's
// volume instead. moonmix replaces that handler with this policy:
//
// - While the list is scrolling (it moved, or was sent a scroll, less than
// `LIST_LATCH_US` ago), scrolling stays with the list, sliders or not.
// - Otherwise a mouse wheel over a slider changes the volume by
// `WHEEL_STEP_PERCENT` per detent, proportionally for high-resolution
// wheels' partial detents (no rounding). Up or right is louder.
// - A touchpad gesture is decided once, at its first movement: a mainly
// horizontal swipe moves the slider (the knob follows the fingers), a
// mainly vertical one scrolls the list. Touchpad scrolling never gets a
// kinetic tail on a slider.
///|
/// Volume change per mouse-wheel detent, in percent (GNOME Shell's sliders
/// also step 2% per detent).
pub const WHEEL_STEP_PERCENT : Double = 2.0
///|
/// How long after the list last moved (or was sent a scroll) scroll events
/// over a slider still go to the list.
pub const LIST_LATCH_US : Int64 = 600_000L
///|
/// The unit of a scroll event's deltas (GdkScrollUnit).
#derive(Eq, Debug)
pub(all) enum ScrollUnit {
/// Wheel detents; high-resolution wheels report fractions of one.
Wheel
/// Surface pixels, from touchpads and other smooth devices.
Pixels
}
///|
#derive(Eq, Debug)
pub(all) enum WheelRoute {
/// Change the slider's value.
Slider
/// Leave the event to the scrolled list.
List
}
///|
/// Routing state of one slider. A touchpad gesture keeps the route of its
/// first movement until it ends.
pub struct SliderWheel {
priv mut gesture : WheelRoute?
}
///|
pub fn SliderWheel::new() -> SliderWheel {
{ gesture: None, }
}
///|
/// A scroll gesture (touchpad sequence) begins or ends.
pub fn SliderWheel::reset(self : SliderWheel) -> Unit {
self.gesture = None
}
///|
/// Where one scroll event over the slider goes. `since_list_us` is the time
/// since the list last scrolled.
pub fn SliderWheel::route(
self : SliderWheel,
unit : ScrollUnit,
dx : Double,
dy : Double,
since_list_us : Int64,
) -> WheelRoute {
let latched = since_list_us < LIST_LATCH_US
match unit {
// A mouse wheel has no gesture: every detent is decided on its own.
Wheel => if latched { List } else { Slider }
Pixels =>
match self.gesture {
Some(route) => route
None => {
let route = if latched || dy.abs() >= dx.abs() {
List
} else {
Slider
}
self.gesture = Some(route)
route
}
}
}
}
///|
/// Volume change (percent) for one scroll event routed to a slider that is
/// `extent_px` wide. Deltas are in GTK's convention (dy > 0 is down, dx > 0
/// is right, in physical direction).
pub fn wheel_delta_percent(
unit : ScrollUnit,
dx : Double,
dy : Double,
extent_px : Double,
) -> Double {
match unit {
Wheel => (dx - dy) * WHEEL_STEP_PERCENT
Pixels => {
// Move the knob with the fingers; the slider spans 0..MAX_PERCENT.
let extent = if extent_px > 1.0 { extent_px } else { 300.0 }
dx * MAX_PERCENT / extent
}
}
}
///|
/// Handle one scroll event over a slider showing `value` percent: the new
/// value if the slider takes it, `None` if it belongs to the list.
pub fn SliderWheel::scroll(
self : SliderWheel,
unit : ScrollUnit,
dx : Double,
dy : Double,
since_list_us : Int64,
value : Double,
extent_px : Double,
) -> Double? {
match self.route(unit, dx, dy, since_list_us) {
List => None
Slider =>
Some(clamp_percent(value + wheel_delta_percent(unit, dx, dy, extent_px)))
}
}