From f72544e3d05b4788edfa108b268bce6c4d95d574 Mon Sep 17 00:00:00 2001 From: vslg Date: Fri, 15 May 2020 20:32:44 -0300 Subject: [PATCH] Improve trackpad fix - Add it to remaining components - Do not use Timer, as flickDeceleration from Flickable is more performant - Add a setting option for it: useTrackpadFix --- src/backend/user_files.py | 1 + src/gui/Base/HFlickable.qml | 12 ++++- src/gui/Base/HGridView.qml | 8 +--- src/gui/Base/HListView.qml | 4 +- ...{HScrollMouseArea.qml => HTrackpadFix.qml} | 44 ++++++++----------- 5 files changed, 33 insertions(+), 36 deletions(-) rename src/gui/Base/{HScrollMouseArea.qml => HTrackpadFix.qml} (55%) diff --git a/src/backend/user_files.py b/src/backend/user_files.py index 68f23fbf..5769a96e 100644 --- a/src/backend/user_files.py +++ b/src/backend/user_files.py @@ -241,6 +241,7 @@ class UISettings(JSONDataFile): "maxMessageCharactersPerLine": 65, "ownMessagesOnLeftAboveWidth": 895, "theme": "Midnight.qpl", + "useTrackpadFix": False, "writeAliases": {}, "media": { "autoLoad": True, diff --git a/src/gui/Base/HFlickable.qml b/src/gui/Base/HFlickable.qml index 92d024d7..7f1b5576 100644 --- a/src/gui/Base/HFlickable.qml +++ b/src/gui/Base/HFlickable.qml @@ -4,6 +4,16 @@ import QtQuick 2.12 import QtQuick.Controls 2.12 Flickable { + id: flickable interactive: contentWidth > width || contentHeight > height - ScrollBar.vertical: ScrollBar {} + ScrollBar.vertical: ScrollBar { + visible: flickable.interactive + } + + + readonly property HTrackpadFix trackpadFix: HTrackpadFix { + flickable: flickable + width: flickable.width + height: flickable.height + } } diff --git a/src/gui/Base/HGridView.qml b/src/gui/Base/HGridView.qml index 4c97e66d..f59f88d3 100644 --- a/src/gui/Base/HGridView.qml +++ b/src/gui/Base/HGridView.qml @@ -117,14 +117,8 @@ GridView { } - MouseArea { + HTrackpadFix { id: mouseArea anchors.fill: parent - acceptedButtons: Qt.NoButton - onWheel: { - // Allow wheel usage, will be back to false on any drag attempt - parent.interactive = true - wheel.accepted = false - } } } diff --git a/src/gui/Base/HListView.qml b/src/gui/Base/HListView.qml index 6687b15a..a5c65ee3 100644 --- a/src/gui/Base/HListView.qml +++ b/src/gui/Base/HListView.qml @@ -118,10 +118,8 @@ ListView { } - HScrollMouseArea { + HTrackpadFix { id: mouseArea anchors.fill: parent - acceptedButtons: Qt.NoButton - flickable: listView } } diff --git a/src/gui/Base/HScrollMouseArea.qml b/src/gui/Base/HTrackpadFix.qml similarity index 55% rename from src/gui/Base/HScrollMouseArea.qml rename to src/gui/Base/HTrackpadFix.qml index 22f235ec..6db63bc3 100644 --- a/src/gui/Base/HScrollMouseArea.qml +++ b/src/gui/Base/HTrackpadFix.qml @@ -1,44 +1,46 @@ // SPDX-License-Identifier: LGPL-3.0-or-later import QtQuick 2.12 -import QtQuick.Controls 2.12 // Mouse area model to fix scroll on trackpad MouseArea { id: mouseArea + enabled: window.settings.useTrackpadFix propagateComposedEvents: true - z: flickable.z + 1 + acceptedButtons: Qt.NoButton + onWheel: { wheel.accepted = false // Disable wheel to avoid too much scroll - var pos = getNewPosition(flickable, wheel) + const pos = getNewPosition(flickable, wheel) flickable.flick(0, 0) flickable.contentY = pos - cancelFlickTimer.start() // Stop the flick } - // Required assignment for flickable scroll to work. - // It must be specified when using this class. - property Flickable flickable + + property Flickable flickable: parent + function getNewPosition(flickable, wheel) { // wheel.pixelDelta will be available on high resolution trackpads. // Otherwise use wheel.angleDelta, which is available from mouses and // low resolution trackpads. // When higher pixelDelta, more scroll will be applied - var pixelDelta = wheel.pixelDelta.y || (wheel.angleDelta.y / 8) + const pixelDelta = + wheel.pixelDelta.y || + wheel.angleDelta.y / 24 * Qt.styleHints.wheelScrollLines // Return current position if there was not any movement if (flickable.contentHeight < flickable.height || !pixelDelta) return flickable.contentY - var maxScroll = ( + const maxScroll = flickable.contentHeight + flickable.originY + - flickable.bottomMargin - ) - flickable.height - var minScroll = flickable.topMargin + flickable.originY + flickable.bottomMargin - + flickable.height + const minScroll = flickable.topMargin + flickable.originY // Avoid overscrolling return Math.max( @@ -47,19 +49,11 @@ MouseArea { ) } - Timer { - id: cancelFlickTimer - interval: 100 // Flick duration in ms - onTriggered: { - flickable.cancelFlick() - // print("aaa") - } - } - Rectangle { - anchors.fill: parent - color: "transparent" - border.color: "red" - border.width: 5 + Binding { + target: flickable + property: "flickDeceleration" + value: 8000 + when: mouseArea.enabled } }