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>
This commit is contained in:
1 parent
0760d7ba6c
commit
cff5f49dc3
9 files changed
+511
-15
No files matched your search
+85
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
+103
@@ -43,6 +43,26 @@ MOONBIT_FFI_EXPORT void mm_gtk_set_handler(mm_gcall_t call, void *closure) {
|
||||
g_closure = closure;
|
||||
}
|
||||
|
||||
/* Scroll events over sliders are decided synchronously by MoonBit:
|
||||
* (tag, phase 0 begin / 1 scroll / 2 end, unit 0 wheel / 1 pixels, dx, dy,
|
||||
* microseconds since the list last scrolled, slider value, slider width)
|
||||
* -> new slider value, or < 0 to leave the event to the scrolled list. */
|
||||
typedef double (*mm_scall_t)(void *closure, int32_t tag, int32_t phase,
|
||||
int32_t unit, double dx, double dy,
|
||||
int64_t since_list_us, double value,
|
||||
double extent);
|
||||
|
||||
static mm_scall_t g_scroll_call;
|
||||
static void *g_scroll_closure;
|
||||
|
||||
MOONBIT_FFI_EXPORT void mm_gtk_set_scroll_handler(mm_scall_t call,
|
||||
void *closure) {
|
||||
if (g_scroll_closure)
|
||||
moonbit_decref(g_scroll_closure);
|
||||
g_scroll_call = call;
|
||||
g_scroll_closure = closure;
|
||||
}
|
||||
|
||||
static void add_icon_search_path_if_new(GtkIconTheme *theme, const char *dir) {
|
||||
if (!dir || !*dir)
|
||||
return;
|
||||
@@ -489,10 +509,25 @@ MOONBIT_FFI_EXPORT void mm_gtk_box_reorder_after(GtkWidget *box,
|
||||
|
||||
MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_null(void) { return NULL; }
|
||||
|
||||
/* When a list last moved (any scrolled window: only the visible tab's can
|
||||
* move) or was handed a scroll event by a slider. */
|
||||
static gint64 g_list_scrolled_us = G_MININT64 / 2;
|
||||
|
||||
static void on_list_scrolled(GtkAdjustment *adj, gpointer d) {
|
||||
(void)adj;
|
||||
(void)d;
|
||||
g_list_scrolled_us = g_get_monotonic_time();
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_scrolled_new(GtkWidget *child) {
|
||||
GtkWidget *sw = gtk_scrolled_window_new();
|
||||
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER,
|
||||
GTK_POLICY_AUTOMATIC);
|
||||
/* Includes kinetic deceleration and scrollbar drags: while the list
|
||||
* moves, wheel events over sliders keep scrolling it. */
|
||||
g_signal_connect(
|
||||
gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(sw)),
|
||||
"value-changed", G_CALLBACK(on_list_scrolled), NULL);
|
||||
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), child);
|
||||
gtk_widget_set_vexpand(sw, TRUE);
|
||||
return sw;
|
||||
@@ -595,6 +630,73 @@ static void on_value_changed(GtkRange *r, gpointer d) {
|
||||
emit(get_tag(r), gtk_range_get_value(r));
|
||||
}
|
||||
|
||||
static double scale_scroll_call(GtkEventController *c, int32_t phase,
|
||||
double dx, double dy) {
|
||||
GtkWidget *sc = gtk_event_controller_get_widget(c);
|
||||
if (!g_scroll_call)
|
||||
return -1;
|
||||
int32_t unit = gtk_event_controller_scroll_get_unit(
|
||||
GTK_EVENT_CONTROLLER_SCROLL(c)) == GDK_SCROLL_UNIT_SURFACE;
|
||||
moonbit_incref(g_scroll_closure);
|
||||
return g_scroll_call(g_scroll_closure, get_tag(sc), phase, unit, dx, dy,
|
||||
g_get_monotonic_time() - g_list_scrolled_us,
|
||||
gtk_range_get_value(GTK_RANGE(sc)),
|
||||
(double)gtk_widget_get_width(sc));
|
||||
}
|
||||
|
||||
static void on_scale_scroll_begin(GtkEventControllerScroll *c, gpointer d) {
|
||||
(void)d;
|
||||
scale_scroll_call(GTK_EVENT_CONTROLLER(c), 0, 0, 0);
|
||||
}
|
||||
|
||||
static void on_scale_scroll_end(GtkEventControllerScroll *c, gpointer d) {
|
||||
(void)d;
|
||||
scale_scroll_call(GTK_EVENT_CONTROLLER(c), 2, 0, 0);
|
||||
}
|
||||
|
||||
static gboolean on_scale_scroll(GtkEventControllerScroll *c, double dx,
|
||||
double dy, gpointer d) {
|
||||
(void)d;
|
||||
double v = scale_scroll_call(GTK_EVENT_CONTROLLER(c), 1, dx, dy);
|
||||
if (v < 0) {
|
||||
/* The scrolled window's own handler (further up) takes it. */
|
||||
g_list_scrolled_us = g_get_monotonic_time();
|
||||
return GDK_EVENT_PROPAGATE;
|
||||
}
|
||||
GtkRange *r =
|
||||
GTK_RANGE(gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(c)));
|
||||
if (v != gtk_range_get_value(r))
|
||||
gtk_range_set_value(r, v); /* value-changed -> volume */
|
||||
return GDK_EVENT_STOP;
|
||||
}
|
||||
|
||||
/* GtkRange's built-in scroll handling moves by the page increment (10%) per
|
||||
* wheel detent, ignores horizontal wheels and swallows every scroll event, so
|
||||
* a list scrolled with the pointer over a slider stops and changes a volume.
|
||||
* Replace it with one routed by @model.SliderWheel. */
|
||||
static void scale_replace_scroll_controller(GtkWidget *sc) {
|
||||
GListModel *controllers = gtk_widget_observe_controllers(sc);
|
||||
for (guint i = g_list_model_get_n_items(controllers); i-- > 0;) {
|
||||
GtkEventController *c = g_list_model_get_item(controllers, i);
|
||||
if (GTK_IS_EVENT_CONTROLLER_SCROLL(c))
|
||||
gtk_widget_remove_controller(sc, c);
|
||||
g_object_unref(c);
|
||||
}
|
||||
g_object_unref(controllers);
|
||||
GtkEventControllerScrollFlags flags = GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES;
|
||||
#if GTK_CHECK_VERSION(4, 20, 0)
|
||||
/* The knob follows the fingers whether or not natural scrolling is on. */
|
||||
flags |= GTK_EVENT_CONTROLLER_SCROLL_PHYSICAL_DIRECTION;
|
||||
#endif
|
||||
GtkEventController *scroll = gtk_event_controller_scroll_new(flags);
|
||||
g_signal_connect(scroll, "scroll-begin", G_CALLBACK(on_scale_scroll_begin),
|
||||
NULL);
|
||||
g_signal_connect(scroll, "scroll", G_CALLBACK(on_scale_scroll), NULL);
|
||||
g_signal_connect(scroll, "scroll-end", G_CALLBACK(on_scale_scroll_end),
|
||||
NULL);
|
||||
gtk_widget_add_controller(sc, scroll);
|
||||
}
|
||||
|
||||
/* Horizontal slider 0..max with a "100%" mark. */
|
||||
MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_scale_new(double max, double norm,
|
||||
int32_t tag) {
|
||||
@@ -606,6 +708,7 @@ MOONBIT_FFI_EXPORT GtkWidget *mm_gtk_scale_new(double max, double norm,
|
||||
gtk_widget_set_hexpand(sc, TRUE);
|
||||
set_tag(sc, tag);
|
||||
g_signal_connect(sc, "value-changed", G_CALLBACK(on_value_changed), NULL);
|
||||
scale_replace_scroll_controller(sc);
|
||||
return sc;
|
||||
}
|
||||
|
||||
|
||||
+22
-1
@@ -1,6 +1,10 @@
|
||||
// Generated using `moon info`, DON'T EDIT IT
|
||||
package "geordi/moonmix/gtk"
|
||||
|
||||
import {
|
||||
"moonbitlang/core/debug",
|
||||
}
|
||||
|
||||
// Values
|
||||
pub fn app_icon(String) -> String
|
||||
|
||||
@@ -32,7 +36,7 @@ pub fn resolve_icon(Array[String]) -> String
|
||||
|
||||
pub fn run() -> Unit
|
||||
|
||||
pub fn scale(Double, Double, (Double) -> Unit) -> (Widget, Int32)
|
||||
pub fn scale(Double, Double, (Double) -> Unit, on_scroll? : (ScrollEvent) -> Double?) -> (Widget, Int32)
|
||||
|
||||
pub fn scrolled(Widget) -> Widget
|
||||
|
||||
@@ -64,6 +68,23 @@ pub(all) struct Appearance {
|
||||
color_scheme : Int32
|
||||
}
|
||||
|
||||
pub(all) struct ScrollEvent {
|
||||
phase : ScrollPhase
|
||||
pixels : Bool
|
||||
dx : Double
|
||||
dy : Double
|
||||
since_list_us : Int64
|
||||
value : Double
|
||||
extent : Double
|
||||
}
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum ScrollPhase {
|
||||
Begin
|
||||
Scroll
|
||||
End
|
||||
}
|
||||
|
||||
#external
|
||||
pub type Widget
|
||||
pub fn Widget::add_css_class(Self, String) -> Unit
|
||||
|
||||
Reference in new issue
Block a user