From 142cfea2ee8b50b5ef2e22b9de4fe4c461411ff2 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 14 Jul 2017 00:02:33 +0200 Subject: Use a ScrollHelper to fix scrolling on listviews Because the standard scrolling is so unusable depending on the input device we replace it by something custom that is fairly similar to what QQC1 ScrollView did. Using a ScrollView sucks in many ways, including that you have to wrap all sorts of things which is just code wise not great at all. The ScrollHelper can instead be attached to any existing flickable to override it's scrolling behaviour, so we can also silently drop it once the default flickable behaviour starts to make sense. --- framework/qml/ScrollHelper.qml | 96 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 framework/qml/ScrollHelper.qml (limited to 'framework/qml/ScrollHelper.qml') diff --git a/framework/qml/ScrollHelper.qml b/framework/qml/ScrollHelper.qml new file mode 100644 index 00000000..b2f91201 --- /dev/null +++ b/framework/qml/ScrollHelper.qml @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016 Michael Bohlender, + * Copyright (C) 2017 Christian Mollekopf, + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.7 +import QtQuick.Controls 2 +import org.kube.framework 1.0 as Kube + +/* +* 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 +*/ +MouseArea { + id: root + propagateComposedEvents: true + + property Flickable flickable + + //Place the mouse area under the flickable + z: -1 + onFlickableChanged: { + flickable.interactive = false + flickable.maximumFlickVelocity = 100000 + flickable.boundsBehavior = Flickable.StopAtBounds + root.parent = flickable + } + + function calculateNewPosition(flickableItem, wheel) { + //Nothing to scroll + if (flickableItem.contentHeight < flickableItem.height) { + return flickableItem.contentY; + } + //Ignore 0 events (happens at least with Christians trackpad) + if (wheel.pixelDelta.y == 0 && wheel.angleDelta.y == 0) { + return flickableItem.contentY; + } + //pixelDelta seems to be the same as angleDelta/8 + var pixelDelta = wheel.pixelDelta.y != 0 ? wheel.pixelDelta.y : wheel.angleDelta.y / 8 + + var y = pixelDelta + if (!y) { + return flickableItem.contentY; + } + + var minYExtent = flickableItem.originY + flickableItem.topMargin; + var maxYExtent = (flickableItem.contentHeight + flickableItem.bottomMargin + flickableItem.originY) - flickableItem.height; + + if (typeof(flickableItem.headerItem) !== "undefined" && flickableItem.headerItem) { + minYExtent += flickableItem.headerItem.height + } + + //Avoid overscrolling + return Math.max(minYExtent, Math.min(maxYExtent, flickableItem.contentY - y)); + } + + onWheel: { + var newPos = calculateNewPosition(flickable, wheel); + // console.warn("Delta: ", wheel.pixelDelta.y); + // console.warn("Old position: ", flickable.contentY); + // console.warn("New position: ", newPos); + + // Show the scrollbars + flickable.flick(0, 0); + flickable.contentY = newPos; + cancelFlickStateTimer.start() + } + + + Timer { + id: cancelFlickStateTimer + //How long the scrollbar will remain visible + interval: 500 + // Hide the scrollbars + onTriggered: listView.cancelFlick(); + } +} + -- cgit v1.2.3