From 55ddc2c768d52fdae73b9dd541531ed4fde37612 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Tue, 8 Sep 2026 22:19:32 -0600 Subject: [PATCH] fix(qt): make mouse wheel scrolling predictable Accumulate wheel distance with short easing instead of kinetic acceleration. Preserve rapid and fractional wheel input, immediate direction changes, nested scroll chaining and scroll-memory takeover. Validated with 38 Qt Quick tests and desktop smoke testing. cx --- crates/qbz-qt/qml/controls/ScrollMemory.qml | 9 ++ crates/qbz-qt/qml/theme/QbzKineticScroll.qml | 126 +++++++++++++++--- crates/qbz-qt/qml/theme/QbzScrollBar.qml | 5 +- crates/qbz-qt/src/main.rs | 25 ---- .../imports/com/blitzfc/qbz/QbzShell.qml | 6 +- scripts/qml-tests/tst_wheel.qml | 120 +++++++++++++++++ 6 files changed, 245 insertions(+), 46 deletions(-) create mode 100644 scripts/qml-tests/tst_wheel.qml diff --git a/crates/qbz-qt/qml/controls/ScrollMemory.qml b/crates/qbz-qt/qml/controls/ScrollMemory.qml index f9687969b..cb6925528 100644 --- a/crates/qbz-qt/qml/controls/ScrollMemory.qml +++ b/crates/qbz-qt/qml/controls/ScrollMemory.qml @@ -246,6 +246,15 @@ Item { } } + // The distance-based wheel animation writes contentY without setting + // Flickable.moving. Yield before its first frame, just as for a native drag. + Connections { + target: root.target ? root.target.children.find(function (child) { + return child.objectName === "qbzWheelScroll"; + }) || null : null + function onUserScrollStarted() { root._disarm("wheel takeover"); } + } + Connections { target: QbzShell function onRestoreScopeChanged() { root._begin() } diff --git a/crates/qbz-qt/qml/theme/QbzKineticScroll.qml b/crates/qbz-qt/qml/theme/QbzKineticScroll.qml index 128651877..c9447b680 100644 --- a/crates/qbz-qt/qml/theme/QbzKineticScroll.qml +++ b/crates/qbz-qt/qml/theme/QbzKineticScroll.qml @@ -1,22 +1,14 @@ -// Shared touchpad kinetic tail for a Flickable/ListView/GridView. -// -// Qt 6.11 already owns the direct pixel-delta drag while the fingers are on -// the pad, including nested-axis arbitration and bounds. On Linux/Wayland the -// gesture commonly ends with ScrollEnd and NO compositor momentum stream, so -// Qt stops the content on the same frame the fingers lift. This component only -// observes that native gesture and hands its final vertical velocity back to -// Flickable.flick(). It never writes contentY and never owns an animation. -// -// RESOURCE CONTRACT (GPU-COST-INVESTIGATION.md): WheelHandler is event-driven; -// there is no Timer, Behavior or permanent clock. The one callLater crosses -// Qt's ScrollEnd cleanup, then Flickable's bounded native timeline owns motion -// and stops itself. At rest this item writes nothing and schedules no frame. +// Shared scrolling for the house scrollbar. Mouse-wheel steps accumulate a +// destination and finish with a short easing; touchpad pixel gestures keep +// their native handling and existing missing-momentum fallback. +// Animation runs only while consuming wheel input, never while idle. import QtQuick Item { id: root + objectName: "qbzWheelScroll" required property Flickable target /// User-originated wheel/touchpad takeover. Consumers with an opt-in /// auto-follow mode use this to yield immediately; programmatic @@ -30,6 +22,93 @@ Item { anchors.fill: parent z: 2147483646 + property real _destination: 0 + property real _position: 0 + property int _direction: 0 + property bool _writing: false + readonly property bool wheelScrolling: wheelAnimation.running + + function stopWheel() { + wheelAnimation.stop(); + root._direction = 0; + } + + // Let nested vertical views consume their own wheel. Horizontal rails keep + // tilt/shift gestures; an ordinary vertical wheel still scrolls the page. + function _nestedVerticalAt(x, y, angleY) { + let item = root.target.contentItem; + let point = root.mapToItem(item, x, y); + while (item) { + const child = item.childAt(point.x, point.y); + if (!child) + return false; + const flickable = child as Flickable; + if (flickable && flickable.interactive) { + const minimum = flickable.originY - flickable.topMargin; + const maximum = flickable.originY + flickable.contentHeight + - flickable.height + flickable.bottomMargin; + if (angleY > 0 ? flickable.contentY > minimum : flickable.contentY < maximum) + return true; + } + point = item.mapToItem(child, point.x, point.y); + item = child; + } + return false; + } + + function _scrollWheel(delta) { + const direction = delta > 0 ? 1 : -1; + const base = wheelAnimation.running && root._direction === direction + ? root._destination : root.target.contentY; + const minimum = root.target.originY - root.target.topMargin; + const maximum = Math.max(minimum, root.target.originY + root.target.contentHeight + - root.target.height + root.target.bottomMargin); + const destination = Math.max(minimum, Math.min(maximum, base + delta)); + root.stopWheel(); + root.target.cancelFlick(); + if (Math.abs(destination - root.target.contentY) < 0.01) + return; + root._position = root.target.contentY; + root._direction = direction; + root._destination = destination; + wheelAnimation.from = root._position; + wheelAnimation.to = destination; + wheelAnimation.start(); + } + + NumberAnimation { + id: wheelAnimation + target: root + property: "_position" + duration: 150 + easing.type: Easing.OutCubic + } + on_PositionChanged: { + if (!wheelAnimation.running) + return; + root._writing = true; + root.target.contentY = root._position; + root._writing = false; + } + Connections { + target: root.target + function onContentYChanged() { if (!root._writing) root.stopWheel(); } + function onContentHeightChanged() { + // Virtualized delegates refine contentHeight during a scroll. + // Keep the destination unless it has actually left the new bounds. + if (root.wheelScrolling && root._destination > root.target.originY + + root.target.contentHeight - root.target.height + root.target.bottomMargin) { + root.stopWheel(); + root.target.returnToBounds(); + } + } + function onOriginYChanged() { root.stopWheel(); } + function onHeightChanged() { root.stopWheel(); } + function onVisibleChanged() { if (!root.target.visible) root.stopWheel(); } + function onInteractiveChanged() { if (!root.target.interactive) root.stopWheel(); } + function onDraggingChanged() { if (root.target.dragging) root.stopWheel(); } + } + property real _sumX: 0 property real _sumY: 0 property real _velocityY: 0 @@ -105,10 +184,23 @@ Item { enabled: root.target.interactive onWheel: function (event) { - // Observer only. Native Flickable still applies the direct delta, - // arbitrates nested axes and owns bounds/overshoot. + // Pixel gestures and horizontal input remain with native Qt. event.accepted = false; + if (event.phase === Qt.NoScrollPhase && event.pixelDelta.x === 0 + && event.pixelDelta.y === 0 && event.angleDelta.y !== 0 + && Math.abs(event.angleDelta.y) > Math.abs(event.angleDelta.x) + && !(event.modifiers & (Qt.ShiftModifier | Qt.ControlModifier | Qt.MetaModifier)) + && !root._nestedVerticalAt(event.x, event.y, event.angleDelta.y)) { + root.userScrollStarted(); + // Honor system scroll lines and high-resolution fractional + // notches. Natural-scroll direction is already in Qt's delta. + root._scrollWheel(-event.angleDelta.y / 120 * Qt.styleHints.wheelScrollLines * 24); + event.accepted = true; + return; + } + root.stopWheel(); + if (event.phase === Qt.ScrollBegin) { root.userScrollStarted(); root._resetGesture(); @@ -135,9 +227,7 @@ Item { return; } - // A physical wheel normally has angleDelta only and is handled by - // Qt's process-wide wheel-deceleration path (main.rs). This half is - // deliberately limited to high-resolution pixel gestures. + // Only high-resolution pixel gestures need the touchpad sampler. root.userScrollStarted(); const dx = event.pixelDelta.x; const dy = event.pixelDelta.y; diff --git a/crates/qbz-qt/qml/theme/QbzScrollBar.qml b/crates/qbz-qt/qml/theme/QbzScrollBar.qml index 958843b6a..3797462a6 100644 --- a/crates/qbz-qt/qml/theme/QbzScrollBar.qml +++ b/crates/qbz-qt/qml/theme/QbzScrollBar.qml @@ -41,9 +41,9 @@ Item { } // One shared input seam for every page that uses the house scrollbar: - // physical-wheel acceleration is Qt's native process policy (main.rs), - // while this observer supplies only a missing touchpad tail. + // wheel distance/easing and touchpad momentum share one input handler. QbzKineticScroll { + id: wheelScroll target: root.target onUserScrollStarted: root.userScrollStarted() } @@ -93,6 +93,7 @@ Item { // Public Flickable semantics already cancel on a contentY write; // make the takeover explicit so scrollbar drag remains exact even // if the first pressed position equals the current one. + wheelScroll.stopWheel(); root.target.cancelFlick(); position(mouseY); } diff --git a/crates/qbz-qt/src/main.rs b/crates/qbz-qt/src/main.rs index 89d0dc938..661db7592 100644 --- a/crates/qbz-qt/src/main.rs +++ b/crates/qbz-qt/src/main.rs @@ -3649,30 +3649,6 @@ fn apply_renderer_preference() { } } -/// Enable Qt 6's native accumulated mouse-wheel flicks. -/// -/// Qt 6.11 ships `wheelDeceleration = 15000`, exactly one unit above its -/// `_q_MaximumWheelDeceleration = 14999` switch. That selects proportional -/// scrolling: every notch travels the same distance and a rapid wheel spin -/// cannot build velocity. A value below the switch enables the already-built -/// Flickable wheel timeline. 1500 matches this platform's ordinary touch-flick -/// deceleration, so one isolated notch keeps Qt's 72px distance while a rapid -/// burst earns a bounded kinetic tail. -/// -/// This is read when each QQuickFlickable is constructed, hence before the -/// QGuiApplication/QML engine. Respect an explicit environment value for -/// diagnostics and owner tuning; no hidden QBZ setting existed in the Slint or -/// retired web frontend to migrate. -fn apply_scroll_physics() { - const QT_WHEEL_DECELERATION: &str = "QT_QUICK_FLICKABLE_WHEEL_DECELERATION"; - if std::env::var_os(QT_WHEEL_DECELERATION).is_none() { - std::env::set_var(QT_WHEEL_DECELERATION, "1500"); - log::info!("[qbz-qt] native kinetic wheel enabled (deceleration=1500)"); - } else { - log::info!("[qbz-qt] explicit {QT_WHEEL_DECELERATION} preserved"); - } -} - /// Cap glibc's malloc arenas before the first thread exists. Every thread /// that first touches malloc gets its own 64 MB arena, and with ~50 threads /// (tokio workers, Qt, PipeWire, zbus) the process held 82 of them: measured @@ -3950,7 +3926,6 @@ fn main() { // before the first QQuickWindow/QRhi is constructed. #[cfg(not(target_os = "linux"))] apply_renderer_preference(); - apply_scroll_physics(); // FONT ENGINE (Windows). Qt's default rasteriser on Windows renders the // lyric text thin and jagged - the owner's words were "como fuente de diff --git a/scripts/qml-tests/imports/com/blitzfc/qbz/QbzShell.qml b/scripts/qml-tests/imports/com/blitzfc/qbz/QbzShell.qml index 0372389d4..2af30ca4b 100644 --- a/scripts/qml-tests/imports/com/blitzfc/qbz/QbzShell.qml +++ b/scripts/qml-tests/imports/com/blitzfc/qbz/QbzShell.qml @@ -1,3 +1,7 @@ pragma Singleton import QtQuick -QtObject { property string themeJson: ""; property int ambientMode: 0 } +QtObject { + property string restoreScope: "" + property real scrollRestore: 0 + function reportScroll(scope, y) {} + property string themeJson: ""; property int ambientMode: 0 } diff --git a/scripts/qml-tests/tst_wheel.qml b/scripts/qml-tests/tst_wheel.qml new file mode 100644 index 000000000..8b4408d78 --- /dev/null +++ b/scripts/qml-tests/tst_wheel.qml @@ -0,0 +1,120 @@ +import QtQuick +import QtTest +import "../../crates/qbz-qt/qml/theme" as Theme +import "../../crates/qbz-qt/qml/controls" as Controls +import com.blitzfc.qbz + +Item { + width: 500; height: 500 + Flickable { + id: view + anchors.fill: parent + contentHeight: 100000 + contentWidth: width + boundsBehavior: Flickable.StopAtBounds + Flickable { + id: nested + x: 50; y: view.contentY + 50; width: 200; height: 100 + contentHeight: 1000; contentWidth: width; visible: false + Theme.QbzKineticScroll { id: nestedScroll; target: nested } + } + ListView { + id: list + x: 50; y: view.contentY + 50; width: 200; height: 100 + model: 1000; visible: false + delegate: Item { width: 200; height: 30 } + Theme.QbzKineticScroll { id: listScroll; target: list } + } + GridView { + id: grid + x: 50; y: view.contentY + 50; width: 200; height: 100 + cellWidth: 100; cellHeight: 50; model: 1000; visible: false + delegate: Item { width: 100; height: 50 } + Theme.QbzKineticScroll { id: gridScroll; target: grid } + } + } + Theme.QbzKineticScroll { id: scroll; target: view } + Theme.QbzScrollBar { id: bar; target: gutterView; x: 450; height: 400 } + Flickable { id: gutterView; x: 420; width: 20; height: 400; contentHeight: 10000 } + Controls.ScrollMemory { id: memory; target: view; scope: "wheel-test" } + TestCase { + name: "WheelPhysics"; when: windowShown + readonly property real step: Qt.styleHints.wheelScrollLines * 24 + function init() { + memory._disarm("test reset"); scroll.stopWheel(); view.cancelFlick() + view.visible = true; view.interactive = true; view.contentY = 10000 + nested.visible = false; list.visible = false; grid.visible = false + wait(50) + } + function turn(target, delta, x, y) { mouseWheel(target,x || 20,y || 20,0,delta) } + function test_distance_data() { + return [{tag:"single",count:1,spacing:40}, {tag:"burst",count:10,spacing:20}, + {tag:"slow",count:10,spacing:100}, {tag:"fractional",count:10,spacing:20,delta:-12}] + } + function test_distance(data) { + const delta = data.delta || -120 + for (let i=0;i