moment/src/gui/Base/HTrackpadFix.qml

62 lines
1.7 KiB
QML
Raw Normal View History

2020-05-16 07:48:31 +10:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
// MouseArea component to fix scroll on trackpad
2020-05-16 07:48:31 +10:00
MouseArea {
id: mouseArea
enabled: window.settings.useTrackpadFix
2020-05-16 07:48:31 +10:00
propagateComposedEvents: true
acceptedButtons: Qt.NoButton
2020-05-16 07:48:31 +10:00
onWheel: {
wheel.accepted = false // Disable wheel to avoid too much scroll
const pos = getNewPosition(flickable, wheel)
2020-05-16 07:48:31 +10:00
flickable.flick(0, 0)
flickable.contentY = pos
}
property Flickable flickable: parent
// Used to get default flickDeceleration value
readonly property Flickable dummy: Flickable {}
2020-05-16 07:48:31 +10:00
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
const pixelDelta =
wheel.pixelDelta.y ||
wheel.angleDelta.y / 24 * Qt.styleHints.wheelScrollLines
2020-05-16 07:48:31 +10:00
// Return current position if there was not any movement
if (flickable.contentHeight < flickable.height || !pixelDelta)
return flickable.contentY
const maxScroll =
2020-05-16 07:48:31 +10:00
flickable.contentHeight +
flickable.originY +
flickable.bottomMargin -
flickable.height
const minScroll = flickable.topMargin + flickable.originY
2020-05-16 07:48:31 +10:00
// Avoid overscrolling
return Math.max(
minScroll,
Math.min(maxScroll, flickable.contentY - pixelDelta)
)
}
Binding {
target: flickable
property: "flickDeceleration"
value: mouseArea.enabled ? 8000.0 : dummy.flickDeceleration
2020-05-16 07:48:31 +10:00
}
}