moment/src/gui/Base/HKineticScrollingDisabler.qml

99 lines
2.9 KiB
QML
Raw Normal View History

// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
2020-05-16 07:48:31 +10:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
MouseArea {
id: mouseArea
property Flickable flickable: parent
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 speedMultiply =
Qt.styleHints.wheelScrollLines *
window.settings.nonKineticScrollingSpeed
const pixelDelta = {
x: wheel.pixelDelta.x || wheel.angleDelta.x / 8 * speedMultiply,
y: wheel.pixelDelta.y || wheel.angleDelta.y / 8 * speedMultiply,
}
2020-05-16 07:48:31 +10:00
// Return current position if there was not any movement
if (
flickable.contentHeight < flickable.height ||
(! pixelDelta.x && ! pixelDelta.y)
)
return {x: flickable.contentX, y: flickable.contentY}
// Rotate the direction if shift is pressed
if (wheel.modifiers === Qt.ShiftModifier)
[pixelDelta.x, pixelDelta.y] = [pixelDelta.y, pixelDelta.x]
const maxScroll = {
x: flickable.contentWidth +
flickable.originX - // Why subtract?
flickable.rightMargin -
flickable.width,
y: flickable.contentHeight +
flickable.originY +
flickable.bottomMargin -
flickable.height,
}
2020-05-16 07:48:31 +10:00
const minScroll = {
x: flickable.originX - flickable.leftMargin,
y: flickable.originY - flickable.topMargin,
}
2020-05-16 07:48:31 +10:00
// Avoid overscrolling
return {
x: Math.max(
minScroll.x,
Math.min(maxScroll.x, flickable.contentX - pixelDelta.x)
),
y: Math.max(
minScroll.y,
Math.min(maxScroll.y, flickable.contentY - pixelDelta.y)
),
}
2020-05-16 07:48:31 +10:00
}
enabled: ! window.settings.enableKineticScrolling
propagateComposedEvents: true
acceptedButtons: Qt.NoButton
onWheel: {
// Make components below the stack notice the wheel event
wheel.accepted = false
if (wheel.modifiers === Qt.ControlModifier)
return
const pos = getNewPosition(flickable, wheel)
flickable.flick(0, 0)
flickable.contentX = pos.x
flickable.contentY = pos.y
}
Binding {
target: flickable
property: "maximumFlickVelocity"
2020-06-27 22:56:50 +10:00
value: mouseArea.enabled ? 0 : window.settings.kineticScrollingMaxSpeed
}
Binding {
target: flickable
property: "flickDeceleration"
value:
mouseArea.enabled ?
0 :
window.settings.kineticScrollingDeceleration
2020-05-16 07:48:31 +10:00
}
}