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

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