Smooth wheel scrolling: route scroll over sliders to the list or the volume #5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
type: fix
|
||||
---
|
||||
|
||||
Smoother wheel and touchpad scrolling: scrolling the stream and device lists no longer stops (or changes a volume) when a slider passes under the pointer, and a wheel over a resting pointer changes the volume in fine 2% steps (high-resolution and tilt wheels included) instead of 10% jumps; touchpad swipes are routed by direction.
|
||||
@@ -35,6 +35,12 @@ on top of libpipewire-0.3. It never runs `wpctl`, `pactl`, `pw-cli`,
|
||||
- Volumes in percent exactly like pavucontrol/wpctl: the cubic mapping of
|
||||
PipeWire's linear `channelVolumes`, up to 150%, with the dB value next to
|
||||
it. One slider moves all channels, keeping the channel balance.
|
||||
- **Scrolling that does what you mean.** The mouse wheel scrolls the list
|
||||
even when a slider passes under the pointer; with the pointer resting on a
|
||||
slider it changes that volume by 2% per detent (high-resolution wheels in
|
||||
proportion, a tilt wheel too). On a touchpad a vertical swipe always
|
||||
scrolls the list, with kinetic momentum, and a horizontal swipe over a
|
||||
slider moves its knob with your fingers.
|
||||
- **Follows the desktop's accent colour and dark/light style**, live (see
|
||||
[Appearance](#appearance)).
|
||||
- Live updates: devices and streams appearing, disappearing or changing
|
||||
@@ -84,7 +90,8 @@ flowchart LR
|
||||
- **One main loop.** WirePlumber and GTK4 both run on the default GLib main
|
||||
context, so events arrive on the GUI thread with no locking or polling.
|
||||
- **Pure core.** Everything else is plain MoonBit in `model/`: the cubic and
|
||||
linear conversions, dB formatting, linked-slider scaling, the reducer that
|
||||
linear conversions, dB formatting, linked-slider scaling, wheel routing
|
||||
over sliders, the reducer that
|
||||
applies node, link and default events, the per-tab rows, and the row diff
|
||||
the GUI applies. `moon test` covers it on any target.
|
||||
- **GTK binding.** It is hand-written and minimal. Each widget gets a tag
|
||||
@@ -92,7 +99,9 @@ flowchart LR
|
||||
single callback. While MoonBit updates widgets from the model, signals are
|
||||
suppressed so model updates never echo back into PipeWire. For a short
|
||||
time after the user moves a slider, remote updates to that slider are
|
||||
ignored so it doesn't jump while being dragged.
|
||||
ignored so it doesn't jump while being dragged. Sliders replace GtkRange's
|
||||
scroll handling (10% per detent, swallowing every scroll) with one that
|
||||
asks MoonBit synchronously whether the slider or the list gets the event.
|
||||
|
||||
## Appearance
|
||||
|
||||
|
||||
+29
-4
@@ -208,9 +208,12 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
|
||||
let slider_row = @gtk.hbox(8)
|
||||
let volume_label = @gtk.label(row.volume_text)
|
||||
volume_label.set_width_chars(16)
|
||||
let (scale, scale_tag) = @gtk.scale(@model.MAX_PERCENT, @model.NORM_PERCENT, fn(
|
||||
percent,
|
||||
) {
|
||||
// Wheel/touchpad over the slider: see @model.SliderWheel.
|
||||
let wheel = @model.SliderWheel::new()
|
||||
let (scale, scale_tag) = @gtk.scale(
|
||||
@model.MAX_PERCENT,
|
||||
@model.NORM_PERCENT,
|
||||
fn(percent) {
|
||||
match app.find_view(tab, id) {
|
||||
Some(v) => {
|
||||
let vols = @model.scale_linked(v.row.volumes, percent)
|
||||
@@ -220,7 +223,29 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
})
|
||||
},
|
||||
on_scroll=fn(ev) {
|
||||
match ev.phase {
|
||||
Begin | End => {
|
||||
wheel.reset()
|
||||
None
|
||||
}
|
||||
Scroll =>
|
||||
wheel.scroll(
|
||||
if ev.pixels {
|
||||
Pixels
|
||||
} else {
|
||||
Wheel
|
||||
},
|
||||
ev.dx,
|
||||
ev.dy,
|
||||
ev.since_list_us,
|
||||
ev.value,
|
||||
ev.extent,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
tags.push(scale_tag)
|
||||
// Peak meter under the slider, as wide as it.
|
||||
let meter = @gtk.level_bar()
|
||||
|
||||
+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
|
||||
|
||||
@@ -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
@@ -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)))
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user