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

+26
View File
@@ -12,6 +12,8 @@ pub const FILE_ICON_PREFIX : String = "file:"
pub const ICON_MISSING : String = "image-missing"
pub const LIST_LATCH_US : Int64 = 600_000
pub const MAX_PERCENT : Double = 150.0
pub const METER_DECAY : Double = 0.04
@@ -22,6 +24,8 @@ pub const MIN_UI_CONTRAST : Double = 3.0
pub const NORM_PERCENT : Double = 100.0
pub const WHEEL_STEP_PERCENT : Double = 2.0
pub fn accent_css(Rgb, Bool) -> String
pub fn accent_text_color(Rgb, Bool) -> Rgb
@@ -88,6 +92,8 @@ pub fn volume_icon(Double, Bool) -> String
pub fn volume_icon_chain(Double, Bool) -> Array[String]
pub fn wheel_delta_percent(ScrollUnit, Double, Double, Double) -> Double
pub let white : Rgb
pub fn window_bg(Bool) -> Rgb
@@ -199,6 +205,20 @@ pub(all) struct Row {
current_choice : Int32
}
#derive(Eq, @debug.Debug)
pub(all) enum ScrollUnit {
Wheel
Pixels
}
pub struct SliderWheel {
// private fields
}
pub fn SliderWheel::new() -> Self
pub fn SliderWheel::reset(Self) -> Unit
pub fn SliderWheel::route(Self, ScrollUnit, Double, Double, Int64) -> WheelRoute
pub fn SliderWheel::scroll(Self, ScrollUnit, Double, Double, Int64, Double, Double) -> Double?
pub(all) struct State {
nodes : Map[Int32, Node]
mut links : Array[Link]
@@ -224,6 +244,12 @@ pub fn Tab::icon_chain(Self) -> Array[String]
pub fn Tab::label(Self) -> String
pub fn Tab::node_kind(Self) -> NodeKind
#derive(Eq, @debug.Debug)
pub(all) enum WheelRoute {
Slider
List
}
// Type aliases
// Traits
+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)))
}
}
+88
View File
@@ -0,0 +1,88 @@
///|
/// Long enough that the list is not scrolling any more.
const IDLE : Int64 = 10_000_000L
///|
test "a mouse wheel over an idle list changes the volume 2% per detent" {
let w = @model.SliderWheel::new()
// GTK: dy < 0 is up.
assert_eq(w.scroll(Wheel, 0.0, -1.0, IDLE, 50.0, 400.0), Some(52.0))
assert_eq(w.scroll(Wheel, 0.0, 1.0, IDLE, 50.0, 400.0), Some(48.0))
// Tilt wheel: right is louder.
assert_eq(w.scroll(Wheel, 1.0, 0.0, IDLE, 50.0, 400.0), Some(52.0))
}
///|
test "high-resolution wheels move by fractions of a step, unrounded" {
let w = @model.SliderWheel::new()
let mut v = 50.0
// One detent reported as eight 1/8 events (value120 = 15 each).
for _ in 0..<8 {
v = w.scroll(Wheel, 0.0, -0.125, IDLE, v, 400.0).unwrap()
}
assert_true((v - 52.0).abs() < 1.0e-9)
assert_eq(w.scroll(Wheel, 0.0, -0.125, IDLE, 50.0, 400.0), Some(50.25))
}
///|
test "the wheel keeps scrolling the list while it is moving" {
let w = @model.SliderWheel::new()
assert_eq(w.scroll(Wheel, 0.0, 1.0, 0L, 50.0, 400.0), None)
assert_eq(
w.scroll(Wheel, 0.0, 1.0, @model.LIST_LATCH_US - 1L, 50.0, 400.0),
None,
)
assert_eq(
w.scroll(Wheel, 0.0, 1.0, @model.LIST_LATCH_US, 50.0, 400.0),
Some(48.0),
)
}
///|
test "volume is clamped to the slider range" {
let w = @model.SliderWheel::new()
assert_eq(w.scroll(Wheel, 0.0, 1.0, IDLE, 1.0, 400.0), Some(0.0))
assert_eq(
w.scroll(Wheel, 0.0, -1.0, IDLE, @model.MAX_PERCENT - 1.0, 400.0),
Some(@model.MAX_PERCENT),
)
}
///|
test "a vertical touchpad swipe over a slider scrolls the list" {
let w = @model.SliderWheel::new()
w.reset()
assert_eq(w.scroll(Pixels, 0.4, 6.0, IDLE, 50.0, 300.0), None)
// Locked for the whole gesture, even if it turns sideways.
assert_eq(w.scroll(Pixels, 9.0, 0.0, IDLE, 50.0, 300.0), None)
w.reset()
}
///|
test "a horizontal touchpad swipe moves the knob with the fingers" {
let w = @model.SliderWheel::new()
w.reset()
// 300 px wide slider spanning 0..150%: 2 px = 1%.
assert_eq(w.scroll(Pixels, 10.0, 1.0, IDLE, 50.0, 300.0), Some(55.0))
// Vertical drift in the same gesture does not hand it to the list and
// does not change the volume.
assert_eq(w.scroll(Pixels, 0.0, 8.0, IDLE, 55.0, 300.0), Some(55.0))
assert_eq(w.scroll(Pixels, -3.0, 0.0, IDLE, 55.0, 300.0), Some(53.5))
w.reset()
// A new gesture is decided afresh.
assert_eq(w.scroll(Pixels, 0.0, 5.0, IDLE, 53.5, 300.0), None)
}
///|
test "a touchpad gesture starting while the list moves stays with the list" {
let w = @model.SliderWheel::new()
w.reset()
assert_eq(w.scroll(Pixels, 12.0, 0.0, 1_000L, 50.0, 300.0), None)
assert_eq(w.scroll(Pixels, 12.0, 0.0, IDLE, 50.0, 300.0), None)
}
///|
test "wheel_delta_percent tolerates an unallocated slider" {
assert_eq(@model.wheel_delta_percent(Pixels, 2.0, 0.0, 0.0), 1.0)
assert_eq(@model.wheel_delta_percent(Wheel, 0.0, -0.5, 0.0), 1.0)
}