Warning, /maui/nomad-style/ScrollView.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * Copyright 2017 Marco Martin <mart@kde.org>
0003  * Copyright 2017 The Qt Company Ltd.
0004  *
0005  * GNU Lesser General Public License Usage
0006  * Alternatively, this file may be used under the terms of the GNU Lesser
0007  * General Public License version 3 as published by the Free Software
0008  * Foundation and appearing in the file LICENSE.LGPLv3 included in the
0009  * packaging of this file. Please review the following information to
0010  * ensure the GNU Lesser General Public License version 3 requirements
0011  * will be met: https://www.gnu.org/licenses/lgpl.html.
0012  *
0013  * GNU General Public License Usage
0014  * Alternatively, this file may be used under the terms of the GNU
0015  * General Public License version 2.0 or later as published by the Free
0016  * Software Foundation and appearing in the file LICENSE.GPL included in
0017  * the packaging of this file. Please review the following information to
0018  * ensure the GNU General Public License version 2.0 requirements will be
0019  * met: http://www.gnu.org/licenses/gpl-2.0.html.
0020  */
0021 
0022 
0023 import QtQuick 2.9
0024 import QtQuick.Controls 2.3
0025 import QtQuick.Templates 2.3 as T
0026 import org.kde.kirigami 2.2 as Kirigami
0027 import org.kde.qqc2desktopstyle.private 1.0 as StylePrivate
0028 
0029 T.ScrollView {
0030     id: controlRoot
0031 
0032     clip: true
0033 
0034     implicitWidth: Math.max(background ? background.implicitWidth : 0, contentWidth + leftPadding + rightPadding)
0035     implicitHeight: Math.max(background ? background.implicitHeight : 0, contentHeight + topPadding + bottomPadding)
0036 
0037     contentWidth: scrollHelper.flickableItem ? scrollHelper.flickableItem.contentWidth : 0
0038     contentHeight: scrollHelper.flickableItem ? scrollHelper.flickableItem.contentHeight : 0
0039 
0040     Kirigami.Theme.colorSet: Kirigami.Theme.View
0041     Kirigami.Theme.inherit: !background || !background.visible
0042 
0043     //create a background only after Component.onCompleted, see on the component creation below for explanation
0044     Component.onCompleted: {
0045         if (!controlRoot.background) {
0046             controlRoot.background = backgroundComponent.createObject(controlRoot);
0047             print(controlRoot.background.width);
0048         }
0049     }
0050 
0051     onChildrenChanged: {
0052         if (controlRoot.children[controlRoot.children.length - 1].hasOwnProperty("contentY")) {
0053             scrollHelper.flickableItem = controlRoot.children[controlRoot.children.length - 1];
0054         }            
0055     }
0056 
0057     children: [
0058         MouseArea {
0059             id: scrollHelper
0060             anchors.fill: parent
0061             drag.filterChildren: !Kirigami.Settings.isMobile
0062             property bool isMobile: !verticalScrollBar.interactive
0063             onIsMobileChanged: {
0064                 flickableItem.boundsBehavior = scrollHelper.isMobile ? Flickable.DragAndOvershootBounds : Flickable.StopAtBounds;
0065             }
0066             property Flickable flickableItem
0067             onFlickableItemChanged: {
0068                 flickableItem.parent = scrollHelper;
0069                 flickableItem.boundsBehavior = scrollHelper.isMobile ? Flickable.DragAndOvershootBounds : Flickable.StopAtBounds;
0070 
0071                 flickableItem.anchors.fill = scrollHelper;
0072                 //don't make the scrolling items overlap the background borders.
0073                 flickableItem.anchors.margins = Qt.binding(function() { return controlRoot.background && controlRoot.background.visible ? 2 : 0; });
0074                 flickableItem.clip = true;
0075                 flickableItem.interactive = Kirigami.Settings.isMobile
0076             }
0077             onPressed: {
0078                 mouse.accepted = false;
0079                 flickableItem.interactive = true;
0080             }
0081             onPositionChanged: {
0082                 mouse.accepted = false;
0083             }
0084             onReleased:  {
0085                 mouse.accepted = false;
0086                 flickableItem.interactive = false;
0087             }
0088             onWheel: {
0089                 if (isMobile || flickableItem.contentHeight < flickableItem.height) {
0090                     return;
0091                 }
0092 
0093                 flickableItem.interactive = false;
0094                 var y = wheel.pixelDelta.y != 0 ? wheel.pixelDelta.y : wheel.angleDelta.y / 8
0095 
0096                 //if we don't have a pixeldelta, apply the configured mouse wheel lines
0097                 if (!wheel.pixelDelta.y) {
0098                     y *= Kirigami.Settings.mouseWheelScrollLines;
0099                 }
0100 
0101                 var minYExtent = flickableItem.topMargin - flickableItem.originY;
0102                 var maxYExtent = flickableItem.height - (flickableItem.contentHeight + flickableItem.bottomMargin + flickableItem.originY);
0103 
0104                 flickableItem.contentY = Math.min(-maxYExtent, Math.max(-minYExtent, flickableItem.contentY - y));
0105 
0106                 //this is just for making the scrollbar appear
0107                 flickableItem.flick(0, 0);
0108                 flickableItem.cancelFlick();
0109             }
0110 
0111             Connections {
0112                 target: scrollHelper.flickableItem
0113                 onFlickEnded: scrollHelper.flickableItem.interactive = false;
0114                 onMovementEnded: scrollHelper.flickableItem.interactive = false;
0115             }
0116 
0117              /*create a background only after Component.onCompleted because:
0118               * implementations can set their own background in a declarative way
0119               * ScrollView {background.visible: true} must *not* work, becasue all  upstream styles don't have a background so applications using this would break with other styles
0120               * This is child of scrollHelper as it would break native scrollview finding of the flickable if it was a direct child
0121               */
0122             Component {
0123                 id: backgroundComponent
0124                 StylePrivate.StyleItem {
0125                     control: controlRoot
0126                     elementType: "edit"
0127                     visible: false
0128                     sunken: true
0129                     hasFocus: controlRoot.activeFocus || scrollHelper.flickableItem.activeFocus
0130                     hover: controlRoot.hovered
0131                 }
0132             }
0133         }
0134     ]
0135     ScrollBar.vertical: ScrollBar {
0136         id: verticalScrollBar
0137         parent: controlRoot
0138         x: controlRoot.mirrored ? 0 : controlRoot.width - width
0139         y: controlRoot.topPadding
0140         height: controlRoot.availableHeight
0141         active: controlRoot.ScrollBar.horizontal || controlRoot.ScrollBar.horizontal.active
0142     }
0143 
0144     ScrollBar.horizontal: ScrollBar {
0145         parent: controlRoot
0146         x: controlRoot.leftPadding
0147         y: controlRoot.height - height
0148         width: controlRoot.availableWidth
0149         active: controlRoot.ScrollBar.vertical || controlRoot.ScrollBar.vertical.active
0150     }
0151 }