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

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>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-23 06:44:52 +00:00
1 parent 0760d7ba6c
commit cff5f49dc3
9 files changed
+511 -15

No files matched your search

+85
View File
@@ -11,6 +11,24 @@ extern "C" fn ffi_set_handler(
closure : (Int32, Double) -> Unit,
) = "mm_gtk_set_handler"
///|
extern "C" fn ffi_set_scroll_handler(
call : FuncRef[
(
(Int32, Int32, Int32, Double, Double, Int64, Double, Double) -> Double,
Int32,
Int32,
Int32,
Double,
Double,
Int64,
Double,
Double,
) -> Double,
],
closure : (Int32, Int32, Int32, Double, Double, Int64, Double, Double) -> Double,
) = "mm_gtk_set_scroll_handler"
///|
extern "C" fn ffi_init() -> Int32 = "mm_gtk_init"
@@ -200,6 +218,10 @@ extern "C" fn ffi_save_png(w : Widget, path : Bytes) -> Int32 = "mm_gtk_save_png
///|
let handlers : Map[Int32, (Double) -> Unit] = Map([])
///|
/// Scroll handlers of sliders, by the slider's tag.
let scroll_handlers : Map[Int32, (ScrollEvent) -> Double?] = Map([])
///|
let next_tag : Ref[Int32] = { val: 1, }
@@ -216,6 +238,35 @@ fn install_dispatch() -> Unit {
None => ()
}
})
ffi_set_scroll_handler(
fn(g, tag, phase, unit, dx, dy, since, value, extent) {
g(tag, phase, unit, dx, dy, since, value, extent)
},
fn(tag, phase, unit, dx, dy, since, value, extent) {
match scroll_handlers.get(tag) {
Some(h) => {
let ev = {
phase: match phase {
0 => Begin
2 => End
_ => Scroll
},
pixels: unit == 1,
dx,
dy,
since_list_us: since,
value,
extent,
}
match h(ev) {
Some(v) => v
None => -1.0
}
}
None => -1.0
}
},
)
}
}
@@ -233,6 +284,7 @@ fn register(h : (Double) -> Unit) -> Int32 {
/// Forget a handler (when its widget is destroyed).
pub fn unregister(tag : Int32) -> Unit {
handlers.remove(tag)
scroll_handlers.remove(tag)
}
///|
@@ -478,14 +530,47 @@ pub fn Widget::set_pixel_size(self : Widget, size : Int32) -> Unit {
ffi_image_set_pixel_size(self, size)
}
///|
#derive(Eq, Debug)
pub(all) enum ScrollPhase {
/// A scroll gesture (touchpad sequence) starts.
Begin
Scroll
/// The gesture ends.
End
}
///|
/// A scroll event over a slider.
pub(all) struct ScrollEvent {
phase : ScrollPhase
/// Deltas in surface pixels (touchpad), else in wheel detents.
pixels : Bool
/// Physical direction: dx > 0 right, dy > 0 down. 0 for Begin/End.
dx : Double
dy : Double
/// Microseconds since a list last scrolled.
since_list_us : Int64
/// The slider's value and width (px).
value : Double
extent : Double
}
///|
/// A slider from 0 to `max` with a mark at `norm`. Returns (widget, tag).
///
/// Scroll events over it go to `on_scroll`: it returns the slider's new
/// value (which then reaches `on_change`) to take the event, or `None` to
/// leave it to the scrolled list around the slider. By default sliders never
/// take scroll events.
pub fn scale(
max : Double,
norm : Double,
on_change : (Double) -> Unit,
on_scroll~ : (ScrollEvent) -> Double? = fn(_) { None },
) -> (Widget, Int32) {
let tag = register(on_change)
scroll_handlers.set(tag, on_scroll)
(ffi_scale_new(max, norm, tag), tag)
}