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:
		@@ -241,6 +241,7 @@ class UISettings(JSONDataFile):
 | 
			
		||||
            "maxMessageCharactersPerLine": 65,
 | 
			
		||||
            "ownMessagesOnLeftAboveWidth": 895,
 | 
			
		||||
            "theme": "Midnight.qpl",
 | 
			
		||||
            "useTrackpadFix": False,
 | 
			
		||||
            "writeAliases": {},
 | 
			
		||||
            "media": {
 | 
			
		||||
                "autoLoad": True,
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -118,10 +118,8 @@ ListView {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    HScrollMouseArea {
 | 
			
		||||
    HTrackpadFix {
 | 
			
		||||
        id: mouseArea
 | 
			
		||||
        anchors.fill: parent
 | 
			
		||||
        acceptedButtons: Qt.NoButton
 | 
			
		||||
        flickable: listView
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user