matrixion/resources/qml/ScrollHelper.qml

95 lines
3.3 KiB
QML
Raw Normal View History

2021-03-05 02:35:15 +03:00
// Copyright (C) 2016 Michael Bohlender, <michael.bohlender@kdemail.net>
// Copyright (C) 2017 Christian Mollekopf, <mollekopf@kolabsystems.com>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Shamelessly stolen from:
2020-04-29 12:14:43 +03:00
* https://cgit.kde.org/kube.git/tree/framework/qml/ScrollHelper.qml
*
* The MouseArea + interactive: false + maximumFlickVelocity are required
* to fix scrolling for desktop systems where we don't want flicking behaviour.
*
* See also:
* ScrollView.qml in qtquickcontrols
* qquickwheelarea.cpp in qtquickcontrols
*/
2020-10-08 22:11:21 +03:00
import QtQuick 2.9
2020-10-08 22:11:21 +03:00
import QtQuick.Controls 2.3
MouseArea {
2020-10-08 22:11:21 +03:00
// console.warn("Delta: ", wheel.pixelDelta.y);
// console.warn("Old position: ", flickable.contentY);
// console.warn("New position: ", newPos);
id: root
property Flickable flickable
property alias enabled: root.enabled
function calculateNewPosition(flickableItem, wheel) {
//Nothing to scroll
2020-10-08 22:11:21 +03:00
if (flickableItem.contentHeight < flickableItem.height)
return flickableItem.contentY;
2020-10-08 22:11:21 +03:00
//Ignore 0 events (happens at least with Christians trackpad)
2020-10-08 22:11:21 +03:00
if (wheel.pixelDelta.y == 0 && wheel.angleDelta.y == 0)
return flickableItem.contentY;
2020-10-08 22:11:21 +03:00
//pixelDelta seems to be the same as angleDelta/8
2020-10-08 22:11:21 +03:00
var pixelDelta = 0;
//The pixelDelta is a smaller number if both are provided, so pixelDelta can be 0 while angleDelta is still something. So we check the angleDelta
if (wheel.angleDelta.y) {
2020-10-08 22:11:21 +03:00
var wheelScrollLines = 3; //Default value of QApplication wheelScrollLines property
var pixelPerLine = 20; //Default value in Qt, originally comes from QTextEdit
var ticks = (wheel.angleDelta.y / 8) / 15; //Divide by 8 gives us pixels typically come in 15pixel steps.
pixelDelta = ticks * pixelPerLine * wheelScrollLines;
} else {
2020-10-08 22:11:21 +03:00
pixelDelta = wheel.pixelDelta.y;
}
2020-10-08 22:11:21 +03:00
pixelDelta = Math.round(pixelDelta);
if (!pixelDelta)
return flickableItem.contentY;
var minYExtent = flickableItem.originY + flickableItem.topMargin;
var maxYExtent = (flickableItem.contentHeight + flickableItem.bottomMargin + flickableItem.originY) - flickableItem.height;
2020-10-08 22:11:21 +03:00
if (typeof (flickableItem.headerItem) !== "undefined" && flickableItem.headerItem)
minYExtent += flickableItem.headerItem.height;
//Avoid overscrolling
return Math.max(minYExtent, Math.min(maxYExtent, flickableItem.contentY - pixelDelta));
}
2020-10-08 22:11:21 +03:00
propagateComposedEvents: true
//Place the mouse area under the flickable
z: -1
onFlickableChanged: {
if (enabled) {
flickable.maximumFlickVelocity = 100000;
flickable.boundsBehavior = Flickable.StopAtBounds;
root.parent = flickable;
}
}
acceptedButtons: Qt.NoButton
onWheel: {
var newPos = calculateNewPosition(flickable, wheel);
// Show the scrollbars
flickable.flick(0, 0);
flickable.contentY = newPos;
2021-03-03 23:34:24 +03:00
cancelFlickStateTimer.restart();
}
Timer {
id: cancelFlickStateTimer
2020-10-08 22:11:21 +03:00
//How long the scrollbar will remain visible
interval: 500
// Hide the scrollbars
2020-10-08 22:11:21 +03:00
onTriggered: {
flickable.cancelFlick();
flickable.movementEnded();
}
}
2020-10-08 22:11:21 +03:00
}