1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-03 20:32:07 -04:00

workaround for all the terrible default QT scrolling behavior

- DankGridView, DankFlickable, DankListView\
- Touchpad behavior greatly improved by preserving momentum
- Fixed janky nested scrollers in control center
This commit is contained in:
bbedward
2025-08-07 15:22:40 -04:00
parent 02ab8e2db5
commit 98d2ca24a8
25 changed files with 973 additions and 788 deletions

View File

@@ -269,7 +269,7 @@ Rectangle {
visible: root.enableFuzzySearch
}
ListView {
DankListView {
id: listView
width: parent.width

163
Widgets/DankFlickable.qml Normal file
View File

@@ -0,0 +1,163 @@
import QtQuick
import QtQuick.Controls
Flickable {
id: flickable
property real mouseWheelSpeed: 12
property real momentumVelocity: 0
property bool isMomentumActive: false
property real friction: 0.95
property real minMomentumVelocity: 50
property real maxMomentumVelocity: 2500
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
WheelHandler {
id: wheelHandler
property real touchpadSpeed: 1.8
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
property var velocitySamples: []
function startMomentum() {
flickable.isMomentumActive = true;
momentumTimer.start();
}
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
onWheel: (event) => {
let currentTime = Date.now();
let timeDelta = currentTime - lastWheelTime;
lastWheelTime = currentTime;
const deltaY = event.angleDelta.y;
const isMouseWheel = Math.abs(deltaY) >= 120 && (Math.abs(deltaY) % 120) === 0;
if (isMouseWheel) {
momentumTimer.stop();
flickable.isMomentumActive = false;
velocitySamples = [];
momentum = 0;
const lines = Math.floor(Math.abs(deltaY) / 120);
const scrollAmount = (deltaY > 0 ? -lines : lines) * flickable.mouseWheelSpeed;
let newY = flickable.contentY + scrollAmount;
newY = Math.max(0, Math.min(flickable.contentHeight - flickable.height, newY));
if (flickable.flicking)
flickable.cancelFlick();
flickable.contentY = newY;
} else {
momentumTimer.stop();
flickable.isMomentumActive = false;
let delta = 0;
if (event.pixelDelta.y !== 0) {
delta = event.pixelDelta.y * touchpadSpeed;
} else {
delta = event.angleDelta.y / 8 * touchpadSpeed;
}
velocitySamples.push({
"delta": delta,
"time": currentTime
});
velocitySamples = velocitySamples.filter((s) => {
return currentTime - s.time < 100;
});
if (velocitySamples.length > 1) {
let totalDelta = velocitySamples.reduce((sum, s) => {
return sum + s.delta;
}, 0);
let timeSpan = currentTime - velocitySamples[0].time;
if (timeSpan > 0)
flickable.momentumVelocity = Math.max(-flickable.maxMomentumVelocity,
Math.min(flickable.maxMomentumVelocity,
totalDelta / timeSpan * 1000));
}
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15;
delta += momentum;
} else {
momentum = 0;
}
let newY = flickable.contentY - delta;
newY = Math.max(0, Math.min(flickable.contentHeight - flickable.height, newY));
if (flickable.flicking)
flickable.cancelFlick();
flickable.contentY = newY;
}
event.accepted = true;
}
onActiveChanged: {
if (!active) {
if (Math.abs(flickable.momentumVelocity) >= flickable.minMomentumVelocity) {
startMomentum();
} else {
velocitySamples = [];
flickable.momentumVelocity = 0;
}
}
}
}
Timer {
id: momentumTimer
interval: 16
repeat: true
onTriggered: {
let newY = flickable.contentY - flickable.momentumVelocity * 0.016;
let maxY = Math.max(0, flickable.contentHeight - flickable.height);
if (newY < 0) {
flickable.contentY = 0;
stop();
flickable.isMomentumActive = false;
flickable.momentumVelocity = 0;
return;
} else if (newY > maxY) {
flickable.contentY = maxY;
stop();
flickable.isMomentumActive = false;
flickable.momentumVelocity = 0;
return;
}
flickable.contentY = newY;
flickable.momentumVelocity *= flickable.friction;
if (Math.abs(flickable.momentumVelocity) < 5) {
stop();
flickable.isMomentumActive = false;
flickable.momentumVelocity = 0;
}
}
}
NumberAnimation {
id: returnToBoundsAnimation
target: flickable
property: "contentY"
duration: 300
easing.type: Easing.OutQuad
}
}

179
Widgets/DankGridView.qml Normal file
View File

@@ -0,0 +1,179 @@
import QtQuick
import QtQuick.Controls
GridView {
id: gridView
// Kinetic scrolling momentum properties
property real momentumVelocity: 0
property bool isMomentumActive: false
property real friction: 0.95
property real minMomentumVelocity: 50
property real maxMomentumVelocity: 2500
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
WheelHandler {
id: wheelHandler
// Tunable parameters for responsive scrolling
property real mouseWheelSpeed: 20
// Higher = faster mouse wheel
property real touchpadSpeed: 1.8
// Touchpad sensitivity
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
property var velocitySamples: []
function startMomentum() {
isMomentumActive = true;
momentumTimer.start();
}
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
onWheel: (event) => {
let currentTime = Date.now();
let timeDelta = currentTime - lastWheelTime;
lastWheelTime = currentTime;
// Detect mouse wheel vs touchpad
const deltaY = event.angleDelta.y;
const isMouseWheel = Math.abs(deltaY) >= 120 && (Math.abs(deltaY) % 120) === 0;
if (isMouseWheel) {
// Fixed scrolling for mouse wheel - 2 cells per click
momentumTimer.stop();
isMomentumActive = false;
velocitySamples = [];
momentum = 0;
const lines = Math.floor(Math.abs(deltaY) / 120);
const scrollAmount = (deltaY > 0 ? -lines : lines) * cellHeight * 0.15; // 0.15 cells per wheel click
let newY = contentY + scrollAmount;
newY = Math.max(0, Math.min(contentHeight - height, newY));
if (flicking)
cancelFlick();
contentY = newY;
} else {
// Touchpad - existing smooth kinetic scrolling
// Stop any existing momentum
momentumTimer.stop();
isMomentumActive = false;
// Calculate scroll delta based on input type
let delta = 0;
if (event.pixelDelta.y !== 0)
// Touchpad with pixel precision
delta = event.pixelDelta.y * touchpadSpeed;
else
// Fallback for touchpad without pixel delta
delta = event.angleDelta.y / 120 * cellHeight * 1.2;
// Track velocity for momentum
velocitySamples.push({
"delta": delta,
"time": currentTime
});
velocitySamples = velocitySamples.filter((s) => {
return currentTime - s.time < 100;
});
// Calculate momentum velocity from samples
if (velocitySamples.length > 1) {
let totalDelta = velocitySamples.reduce((sum, s) => {
return sum + s.delta;
}, 0);
let timeSpan = currentTime - velocitySamples[0].time;
if (timeSpan > 0)
momentumVelocity = Math.max(-maxMomentumVelocity, Math.min(maxMomentumVelocity, totalDelta / timeSpan * 1000));
}
// Apply momentum for touchpad (smooth continuous scrolling)
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15;
delta += momentum;
} else {
momentum = 0;
}
// Apply scrolling with proper bounds checking
let newY = contentY - delta;
newY = Math.max(0, Math.min(contentHeight - height, newY));
// Cancel any conflicting flicks and apply new position
if (flicking)
cancelFlick();
contentY = newY;
}
event.accepted = true;
}
onActiveChanged: {
if (!active && Math.abs(momentumVelocity) >= minMomentumVelocity) {
startMomentum();
} else if (!active) {
velocitySamples = [];
momentumVelocity = 0;
}
}
}
// Physics-based momentum timer for kinetic scrolling
Timer {
id: momentumTimer
interval: 16 // ~60 FPS
repeat: true
onTriggered: {
// Apply velocity to position
let newY = contentY - momentumVelocity * 0.016;
let maxY = Math.max(0, contentHeight - height);
// Stop momentum at boundaries instead of bouncing
if (newY < 0) {
contentY = 0;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
} else if (newY > maxY) {
contentY = maxY;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
}
contentY = newY;
// Apply friction
momentumVelocity *= friction;
// Stop if velocity too low
if (Math.abs(momentumVelocity) < 5) {
stop();
isMomentumActive = false;
momentumVelocity = 0;
}
}
}
// Smooth return to bounds animation
NumberAnimation {
id: returnToBoundsAnimation
target: gridView
property: "contentY"
duration: 300
easing.type: Easing.OutQuad
}
}

212
Widgets/DankListView.qml Normal file
View File

@@ -0,0 +1,212 @@
import QtQuick
import QtQuick.Controls
ListView {
id: listView
property real mouseWheelSpeed: 12
// Simple position preservation
property real savedY: 0
property bool justChanged: false
property bool isUserScrolling: false
// Kinetic scrolling momentum properties
property real momentumVelocity: 0
property bool isMomentumActive: false
property real friction: 0.95
property real minMomentumVelocity: 50
property real maxMomentumVelocity: 2500
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
onMovementStarted: isUserScrolling = true
onMovementEnded: isUserScrolling = false
onContentYChanged: {
if (!justChanged && isUserScrolling) {
savedY = contentY;
}
justChanged = false;
}
// Restore position when model changes
onModelChanged: {
justChanged = true;
contentY = savedY;
}
WheelHandler {
id: wheelHandler
// Tunable parameters for responsive scrolling
property real touchpadSpeed: 1.8 // Touchpad sensitivity
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
property var velocitySamples: []
function startMomentum() {
isMomentumActive = true;
momentumTimer.start();
}
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
onWheel: (event) => {
isUserScrolling = true; // Mark as user interaction
let currentTime = Date.now();
let timeDelta = currentTime - lastWheelTime;
lastWheelTime = currentTime;
// Detect mouse wheel vs touchpad, seems like assuming based on the increments is the only way in QT
const deltaY = event.angleDelta.y;
const isMouseWheel = Math.abs(deltaY) >= 120 && (Math.abs(deltaY) % 120) === 0;
if (isMouseWheel) {
momentumTimer.stop();
isMomentumActive = false;
velocitySamples = [];
momentum = 0;
const lines = Math.floor(Math.abs(deltaY) / 120);
const scrollAmount = (deltaY > 0 ? -lines : lines) * mouseWheelSpeed;
let newY = listView.contentY + scrollAmount;
newY = Math.max(0, Math.min(listView.contentHeight - listView.height, newY));
if (listView.flicking)
listView.cancelFlick();
listView.contentY = newY;
savedY = newY;
} else {
momentumTimer.stop();
isMomentumActive = false;
// Calculate scroll delta based on input type
let delta = 0;
if (event.pixelDelta.y !== 0) {
// Touchpad with pixel precision
delta = event.pixelDelta.y * touchpadSpeed;
} else {
// Fallback for touchpad without pixel delta
delta = event.angleDelta.y / 8 * touchpadSpeed;
}
// Track velocity for momentum
velocitySamples.push({
"delta": delta,
"time": currentTime
});
velocitySamples = velocitySamples.filter((s) => {
return currentTime - s.time < 100;
});
// Calculate momentum velocity from samples
if (velocitySamples.length > 1) {
let totalDelta = velocitySamples.reduce((sum, s) => {
return sum + s.delta;
}, 0);
let timeSpan = currentTime - velocitySamples[0].time;
if (timeSpan > 0)
momentumVelocity = Math.max(-maxMomentumVelocity,
Math.min(maxMomentumVelocity,
totalDelta / timeSpan * 1000));
}
// Apply momentum for touchpad (smooth continuous scrolling)
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15;
delta += momentum;
} else {
momentum = 0;
}
// Apply scrolling with proper bounds checking
let newY = listView.contentY - delta;
newY = Math.max(0, Math.min(listView.contentHeight - listView.height, newY));
// Cancel any conflicting flicks and apply new position
if (listView.flicking)
listView.cancelFlick();
listView.contentY = newY;
savedY = newY; // Update saved position
}
event.accepted = true;
}
onActiveChanged: {
if (!active) {
isUserScrolling = false;
// Start momentum if applicable (touchpad only)
if (Math.abs(momentumVelocity) >= minMomentumVelocity) {
startMomentum();
} else {
velocitySamples = [];
momentumVelocity = 0;
}
}
}
}
// Physics-based momentum timer for kinetic scrolling (touchpad only)
Timer {
id: momentumTimer
interval: 16 // ~60 FPS
repeat: true
onTriggered: {
// Apply velocity to position
let newY = contentY - momentumVelocity * 0.016;
let maxY = Math.max(0, contentHeight - height);
// Stop momentum at boundaries instead of bouncing
if (newY < 0) {
contentY = 0;
savedY = 0;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
} else if (newY > maxY) {
contentY = maxY;
savedY = maxY;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
}
contentY = newY;
savedY = newY; // Keep updating saved position during momentum
// Apply friction
momentumVelocity *= friction;
// Stop if velocity too low
if (Math.abs(momentumVelocity) < 5) {
stop();
isMomentumActive = false;
momentumVelocity = 0;
}
}
}
// Smooth return to bounds animation
NumberAnimation {
id: returnToBoundsAnimation
target: listView
property: "contentY"
duration: 300
easing.type: Easing.OutQuad
}
}

View File

@@ -250,7 +250,7 @@ Item {
anchors.fill: parent
anchors.margins: Theme.spacingS
ListView {
DankListView {
id: searchResultsList
anchors.fill: parent

View File

@@ -20,6 +20,7 @@ Column {
signal compactModeChanged(string widgetId, bool enabled)
width: parent.width
height: implicitHeight
spacing: Theme.spacingM
Row {
@@ -258,6 +259,7 @@ Column {
drag.axis: Drag.YAxis
drag.minimumY: -delegateItem.height
drag.maximumY: itemsList.height
preventStealing: true
onPressed: {
delegateItem.z = 2;
delegateItem.originalY = delegateItem.y;

View File

@@ -96,7 +96,7 @@ Popup {
height: parent.height - 120 // Leave space for header and description
clip: true
ListView {
DankListView {
id: widgetList
spacing: Theme.spacingS

View File

@@ -1,151 +0,0 @@
import QtQuick
import QtQuick.Controls
GridView {
id: gridView
// Kinetic scrolling momentum properties
property real momentumVelocity: 0
property bool isMomentumActive: false
property real friction: 0.95
property real minMomentumVelocity: 50
property real maxMomentumVelocity: 2500
// Qt 6.9+ scrolling: flickDeceleration/maximumFlickVelocity only affect touch now
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
// Custom wheel handler for Qt 6.9+ responsive mouse wheel scrolling
WheelHandler {
id: wheelHandler
// Tunable parameters for responsive scrolling
property real mouseWheelSpeed: 20
// Higher = faster mouse wheel
property real touchpadSpeed: 1.8
// Touchpad sensitivity
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
property var velocitySamples: []
function startMomentum() {
isMomentumActive = true;
momentumTimer.start();
}
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
onWheel: (event) => {
// Stop any existing momentum
momentumTimer.stop();
isMomentumActive = false;
let currentTime = Date.now();
let timeDelta = currentTime - lastWheelTime;
lastWheelTime = currentTime;
// Calculate scroll delta based on input type
let delta = 0;
if (event.pixelDelta.y !== 0)
// Touchpad with pixel precision
delta = event.pixelDelta.y * touchpadSpeed;
else
// Mouse wheel - moderate steps for comfortable scrolling
delta = event.angleDelta.y / 120 * cellHeight * 1.2;
// Track velocity for momentum
velocitySamples.push({
"delta": delta,
"time": currentTime
});
velocitySamples = velocitySamples.filter((s) => {
return currentTime - s.time < 100;
});
// Calculate momentum velocity from samples
if (velocitySamples.length > 1) {
let totalDelta = velocitySamples.reduce((sum, s) => {
return sum + s.delta;
}, 0);
let timeSpan = currentTime - velocitySamples[0].time;
if (timeSpan > 0)
momentumVelocity = Math.max(-maxMomentumVelocity, Math.min(maxMomentumVelocity, totalDelta / timeSpan * 1000));
}
// Apply momentum for touchpad (smooth continuous scrolling)
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15;
delta += momentum;
} else {
momentum = 0;
}
// Apply scrolling with proper bounds checking
let newY = contentY - delta;
newY = Math.max(0, Math.min(contentHeight - height, newY));
// Cancel any conflicting flicks and apply new position
if (flicking)
cancelFlick();
contentY = newY;
event.accepted = true;
}
onActiveChanged: {
if (!active && Math.abs(momentumVelocity) >= minMomentumVelocity) {
startMomentum();
} else if (!active) {
velocitySamples = [];
momentumVelocity = 0;
}
}
}
// Physics-based momentum timer for kinetic scrolling
Timer {
id: momentumTimer
interval: 16 // ~60 FPS
repeat: true
onTriggered: {
// Apply velocity to position
let newY = contentY - momentumVelocity * 0.016;
let maxY = Math.max(0, contentHeight - height);
// Stop momentum at boundaries instead of bouncing
if (newY < 0) {
contentY = 0;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
} else if (newY > maxY) {
contentY = maxY;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
}
contentY = newY;
// Apply friction
momentumVelocity *= friction;
// Stop if velocity too low
if (Math.abs(momentumVelocity) < 5) {
stop();
isMomentumActive = false;
momentumVelocity = 0;
}
}
}
// Smooth return to bounds animation
NumberAnimation {
id: returnToBoundsAnimation
target: gridView
property: "contentY"
duration: 300
easing.type: Easing.OutQuad
}
}

View File

@@ -1,151 +0,0 @@
import QtQuick
import QtQuick.Controls
ListView {
id: listView
// Kinetic scrolling momentum properties
property real momentumVelocity: 0
property bool isMomentumActive: false
property real friction: 0.95
property real minMomentumVelocity: 50
property real maxMomentumVelocity: 2500
// Qt 6.9+ scrolling: flickDeceleration/maximumFlickVelocity only affect touch now
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
// Custom wheel handler for Qt 6.9+ responsive mouse wheel scrolling
WheelHandler {
id: wheelHandler
// Tunable parameters for responsive scrolling
property real mouseWheelSpeed: 20
// Higher = faster mouse wheel
property real touchpadSpeed: 1.8
// Touchpad sensitivity
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
property var velocitySamples: []
function startMomentum() {
isMomentumActive = true;
momentumTimer.start();
}
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
onWheel: (event) => {
// Stop any existing momentum
momentumTimer.stop();
isMomentumActive = false;
let currentTime = Date.now();
let timeDelta = currentTime - lastWheelTime;
lastWheelTime = currentTime;
// Calculate scroll delta based on input type
let delta = 0;
if (event.pixelDelta.y !== 0)
// Touchpad with pixel precision
delta = event.pixelDelta.y * touchpadSpeed;
else
// Mouse wheel - moderate steps for comfortable scrolling
delta = event.angleDelta.y / 120 * 72 * 1.2; // Using default item height
// Track velocity for momentum
velocitySamples.push({
"delta": delta,
"time": currentTime
});
velocitySamples = velocitySamples.filter((s) => {
return currentTime - s.time < 100;
});
// Calculate momentum velocity from samples
if (velocitySamples.length > 1) {
let totalDelta = velocitySamples.reduce((sum, s) => {
return sum + s.delta;
}, 0);
let timeSpan = currentTime - velocitySamples[0].time;
if (timeSpan > 0)
momentumVelocity = Math.max(-maxMomentumVelocity, Math.min(maxMomentumVelocity, totalDelta / timeSpan * 1000));
}
// Apply momentum for touchpad (smooth continuous scrolling)
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15;
delta += momentum;
} else {
momentum = 0;
}
// Apply scrolling with proper bounds checking
let newY = listView.contentY - delta;
newY = Math.max(0, Math.min(listView.contentHeight - listView.height, newY));
// Cancel any conflicting flicks and apply new position
if (listView.flicking)
listView.cancelFlick();
listView.contentY = newY;
event.accepted = true;
}
onActiveChanged: {
if (!active && Math.abs(momentumVelocity) >= minMomentumVelocity) {
startMomentum();
} else if (!active) {
velocitySamples = [];
momentumVelocity = 0;
}
}
}
// Physics-based momentum timer for kinetic scrolling
Timer {
id: momentumTimer
interval: 16 // ~60 FPS
repeat: true
onTriggered: {
// Apply velocity to position
let newY = contentY - momentumVelocity * 0.016;
let maxY = Math.max(0, contentHeight - height);
// Stop momentum at boundaries instead of bouncing
if (newY < 0) {
contentY = 0;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
} else if (newY > maxY) {
contentY = maxY;
stop();
isMomentumActive = false;
momentumVelocity = 0;
return;
}
contentY = newY;
// Apply friction
momentumVelocity *= friction;
// Stop if velocity too low
if (Math.abs(momentumVelocity) < 5) {
stop();
isMomentumActive = false;
momentumVelocity = 0;
}
}
}
// Smooth return to bounds animation
NumberAnimation {
id: returnToBoundsAnimation
target: listView
property: "contentY"
duration: 300
easing.type: Easing.OutQuad
}
}