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
This commit is contained in:
vicrodh committed 2026-09-08 22:19:32 -06:00
1 parent 49247f2ee7
commit 55ddc2c768
6 files changed
+245 -46

No files matched your search

@@ -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() }
+108 -18
View File
@@ -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;
+3 -2
View File
@@ -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);
}
-25
View File
@@ -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
@@ -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 }
+120
View File
@@ -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<data.count;++i) { turn(view,delta); wait(data.spacing) }
tryCompare(scroll,"wheelScrolling",false,400)
fuzzyCompare(view.contentY,10000 + data.count * -delta / 120 * step,0.5)
const settled = view.contentY; wait(200); compare(view.contentY,settled)
}
function test_reverse() {
for (let i=0;i<6;++i) { turn(view,-120); wait(10) }
const reversed = view.contentY
turn(view,120)
tryCompare(scroll,"wheelScrolling",false,400)
fuzzyCompare(view.contentY,reversed-step,0.5)
}
function test_external_position() {
turn(view,-120); wait(30); view.contentY = 2500
wait(250); compare(view.contentY,2500); verify(!scroll.wheelScrolling)
}
function test_hidden() {
turn(view,-120); wait(30); view.visible = false
const at = view.contentY; wait(250); compare(view.contentY,at)
}
function test_bounds() {
view.contentY = 0; turn(view,120); wait(200); compare(view.contentY,0)
view.contentY = view.contentHeight-view.height-10
turn(view,-120); tryCompare(scroll,"wheelScrolling",false,400)
compare(view.contentY,view.contentHeight-view.height)
}
function test_nested_data() {
return [{tag:"flickable",item:nested,handler:nestedScroll},
{tag:"list",item:list,handler:listScroll}, {tag:"grid",item:grid,handler:gridScroll}]
}
function test_nested(data) {
data.item.visible=true; data.item.contentY=0; wait(50)
turn(data.item,-120)
tryCompare(data.handler,"wheelScrolling",false,400)
fuzzyCompare(data.item.contentY-data.item.originY,step,0.5)
compare(view.contentY,10000)
}
function test_nested_boundary_chains() {
nested.visible=true; nested.contentY=nested.contentHeight-nested.height; wait(50)
turn(nested,-120)
tryCompare(scroll,"wheelScrolling",false,400)
fuzzyCompare(view.contentY,10000+step,0.5)
compare(nested.contentY,nested.contentHeight-nested.height)
}
function test_horizontal_passthrough() {
mouseWheel(view,20,20,-120,0); wait(200)
compare(view.contentY,10000); verify(!scroll.wheelScrolling)
}
function test_restore_yields() {
QbzShell.scrollRestore=10000; QbzShell.restoreScope="wheel-test"
verify(memory._armed)
turn(view,-120)
verify(!memory._armed)
tryCompare(scroll,"wheelScrolling",false,400)
fuzzyCompare(view.contentY,10000+step,0.5)
}
function test_scrollbar_takeover() {
gutterView.contentY=1000
turn(gutterView,-120); wait(30)
mousePress(bar,7,200)
const at=gutterView.contentY; wait(250); compare(gutterView.contentY,at)
mouseRelease(bar,7,200)
}
}
}