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
This commit is contained in:
vslg 2020-05-15 20:32:44 -03:00
parent b179480327
commit f72544e3d0
5 changed files with 33 additions and 36 deletions

View File

@ -241,6 +241,7 @@ class UISettings(JSONDataFile):
"maxMessageCharactersPerLine": 65, "maxMessageCharactersPerLine": 65,
"ownMessagesOnLeftAboveWidth": 895, "ownMessagesOnLeftAboveWidth": 895,
"theme": "Midnight.qpl", "theme": "Midnight.qpl",
"useTrackpadFix": False,
"writeAliases": {}, "writeAliases": {},
"media": { "media": {
"autoLoad": True, "autoLoad": True,

View File

@ -4,6 +4,16 @@ import QtQuick 2.12
import QtQuick.Controls 2.12 import QtQuick.Controls 2.12
Flickable { Flickable {
id: flickable
interactive: contentWidth > width || contentHeight > height 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
}
} }

View File

@ -117,14 +117,8 @@ GridView {
} }
MouseArea { HTrackpadFix {
id: mouseArea id: mouseArea
anchors.fill: parent 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
}
} }
} }

View File

@ -118,10 +118,8 @@ ListView {
} }
HScrollMouseArea { HTrackpadFix {
id: mouseArea id: mouseArea
anchors.fill: parent anchors.fill: parent
acceptedButtons: Qt.NoButton
flickable: listView
} }
} }

View File

@ -1,44 +1,46 @@
// SPDX-License-Identifier: LGPL-3.0-or-later // SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Controls 2.12
// Mouse area model to fix scroll on trackpad // Mouse area model to fix scroll on trackpad
MouseArea { MouseArea {
id: mouseArea id: mouseArea
enabled: window.settings.useTrackpadFix
propagateComposedEvents: true propagateComposedEvents: true
z: flickable.z + 1 acceptedButtons: Qt.NoButton
onWheel: { onWheel: {
wheel.accepted = false // Disable wheel to avoid too much scroll 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.flick(0, 0)
flickable.contentY = pos 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: parent
property Flickable flickable
function getNewPosition(flickable, wheel) { function getNewPosition(flickable, wheel) {
// wheel.pixelDelta will be available on high resolution trackpads. // wheel.pixelDelta will be available on high resolution trackpads.
// Otherwise use wheel.angleDelta, which is available from mouses and // Otherwise use wheel.angleDelta, which is available from mouses and
// low resolution trackpads. // low resolution trackpads.
// When higher pixelDelta, more scroll will be applied // 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 // Return current position if there was not any movement
if (flickable.contentHeight < flickable.height || !pixelDelta) if (flickable.contentHeight < flickable.height || !pixelDelta)
return flickable.contentY return flickable.contentY
var maxScroll = ( const maxScroll =
flickable.contentHeight + flickable.contentHeight +
flickable.originY + flickable.originY +
flickable.bottomMargin flickable.bottomMargin -
) - flickable.height flickable.height
var minScroll = flickable.topMargin + flickable.originY const minScroll = flickable.topMargin + flickable.originY
// Avoid overscrolling // Avoid overscrolling
return Math.max( return Math.max(
@ -47,19 +49,11 @@ MouseArea {
) )
} }
Timer {
id: cancelFlickTimer
interval: 100 // Flick duration in ms
onTriggered: {
flickable.cancelFlick()
// print("aaa")
}
}
Rectangle { Binding {
anchors.fill: parent target: flickable
color: "transparent" property: "flickDeceleration"
border.color: "red" value: 8000
border.width: 5 when: mouseArea.enabled
} }
} }