Files
moonmix/model/wheel.mbt
T
SpriteandClaude Opus 5.5 cff5f49dc3
CI / check (pull_request) Successful in 9s
CI / native (pull_request) Successful in 1m15s
Smooth wheel scrolling: route scroll over sliders to the list or the volume
GtkRange's built-in scroll controller moved a slider by its page increment
(10%) per wheel detent, ignored the tilt wheel and returned GDK_EVENT_STOP
for every scroll event, including a touchpad's vertical swipe it did nothing
with. So scrolling a list stopped dead whenever a slider passed under the
pointer and changed that stream's volume instead.

Sliders now replace that controller with one that asks MoonBit
(@model.SliderWheel) synchronously where each event goes:
- while the list is moving (it moved or was sent a scroll < 600 ms ago,
  kinetic deceleration included) events keep scrolling the list;
- otherwise a mouse wheel changes the volume by 2% per detent, fractional
  for high-resolution wheels, tilt wheel included;
- a touchpad gesture is decided at its first movement: mainly vertical
  scrolls the list (kinetic), mainly horizontal moves the knob with the
  fingers (physical direction).

Measured under Xvfb with XTest and 8 playback streams: 12 wheel notches
over a row title used to scroll 3 times and drop a stream from 100% to 20%;
now the list scrolls to the end and no volume changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 06:44:52 +00:00

135 lines
3.9 KiB
MoonBit

// 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)))
}
}