shift-shell/containments/panel/package/contents/ui/SlidingPanel.qml

238 lines
7.2 KiB
QML
Raw Normal View History

/*
* Copyright 2014 Marco Martin <notmart@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, 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 Library General Public License for more details
*
* You should have received a copy of the GNU Library 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.0
2015-06-12 04:26:00 +00:00
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
2014-10-23 14:29:27 +00:00
import org.kde.plasma.core 2.0 as PlasmaCore
2019-10-04 14:02:26 +00:00
import org.kde.plasma.components 3.0 as PlasmaComponents
2019-09-18 13:31:04 +00:00
import org.kde.plasma.private.nanoshell 2.0 as NanoShell
2019-09-18 14:49:44 +00:00
NanoShell.FullScreenOverlay {
id: window
property int offset: 0
2019-10-04 14:02:26 +00:00
property int openThreshold
2017-09-05 11:59:20 +00:00
property bool userInteracting: false
2020-08-20 10:28:03 +00:00
readonly property bool wideScreen: width > height || width > units.gridUnit * 45
readonly property int drawerWidth: wideScreen ? contentItem.implicitWidth : width
2019-09-18 14:42:33 +00:00
property int drawerX: 0
2019-10-08 16:21:39 +00:00
property alias fixedArea: mainScope
property alias flickable: mainFlickable
2019-10-04 14:27:29 +00:00
color: "transparent"//Qt.rgba(0, 0, 0, 0.6 * Math.min(1, offset/contentArea.height))
2019-10-04 14:02:26 +00:00
property alias contentItem: contentArea.contentItem
2017-09-05 11:14:15 +00:00
property int headerHeight
2020-02-04 20:26:26 +00:00
signal closed
2020-07-23 11:47:40 +00:00
//width: Screen.width
//height: Screen.height
2020-08-20 09:44:56 +00:00
enum MovementDirection {
None = 0,
Up,
Down
}
property int direction: SlidingPanel.MovementDirection.None
function cancelAnimations() {
closeAnim.stop();
openAnim.stop();
}
function open() {
cancelAnimations();
window.showFullScreen();
2020-08-20 09:44:56 +00:00
openAnim.restart();
}
function close() {
cancelAnimations();
2020-08-20 09:44:56 +00:00
closeAnim.restart();
}
2014-10-23 15:00:24 +00:00
function updateState() {
cancelAnimations();
if (window.offset <= -headerHeight) {
// close immediately, so that we don't have to wait units.longDuration
window.visible = false;
window.closed();
} else if (window.direction === SlidingPanel.MovementDirection.None) {
2020-08-20 09:44:56 +00:00
if (offset < openThreshold) {
close();
} else {
openAnim.restart();
}
} else if (offset > openThreshold && window.direction === SlidingPanel.MovementDirection.Down) {
openAnim.restart();
} else if (mainFlickable.contentY > openThreshold) {
close();
2019-10-04 14:02:26 +00:00
} else {
2020-08-20 09:44:56 +00:00
openAnim.restart();
2014-10-23 14:29:27 +00:00
}
}
2017-09-05 11:14:15 +00:00
Timer {
id: updateStateTimer
interval: 0
onTriggered: updateState()
}
2019-10-08 16:21:39 +00:00
onActiveChanged: {
if (!active) {
close();
}
}
2020-07-23 11:47:40 +00:00
/*onVisibleChanged: {
if (visible) {
window.width = Screen.width;
window.height = Screen.height;
window.requestActivate();
}
2020-07-23 11:47:40 +00:00
}*/
2019-10-04 14:27:29 +00:00
SequentialAnimation {
id: closeAnim
PropertyAnimation {
target: window
duration: units.longDuration
easing.type: Easing.InOutQuad
properties: "offset"
from: window.offset
2019-10-04 14:37:51 +00:00
to: -headerHeight
}
ScriptAction {
script: {
2019-10-04 14:02:26 +00:00
window.visible = false;
2020-02-04 20:26:26 +00:00
window.closed();
2015-03-13 16:17:24 +00:00
}
}
}
2017-09-01 16:49:07 +00:00
PropertyAnimation {
id: openAnim
target: window
duration: units.longDuration
easing.type: Easing.InOutQuad
properties: "offset"
from: window.offset
2019-10-04 14:27:29 +00:00
to: contentArea.height
2017-09-01 16:49:07 +00:00
}
2019-10-04 14:27:29 +00:00
Rectangle {
anchors {
2020-08-29 23:06:37 +00:00
left: parent.left
right: parent.right
bottom: parent.bottom
2019-10-04 14:27:29 +00:00
}
2020-09-12 02:01:28 +00:00
height: parent.height - headerHeight // don't layer on top panel indicators (area is darkened separately)
2019-10-04 14:27:29 +00:00
color: "black"
opacity: 0.6 * Math.min(1, offset/contentArea.height)
Rectangle {
height: headerHeight
anchors {
left: parent.left
right: parent.right
bottom: parent.top
}
color: "black"
opacity: 0.2
}
Rectangle {
height: units.smallSpacing
anchors {
left: parent.left
right: parent.right
top: parent.top
}
gradient: Gradient {
GradientStop {
position: 1.0
color: Qt.rgba(0, 0, 0, 0.0)
}
GradientStop {
position: 0.5
color: Qt.rgba(0, 0, 0, 0.4)
}
GradientStop {
position: 1.0
color: "transparent"
}
}
}
}
PlasmaCore.ColorScope {
2019-10-08 15:15:17 +00:00
id: mainScope
anchors.fill: parent
2015-03-13 16:17:24 +00:00
Flickable {
id: mainFlickable
2019-10-04 14:27:29 +00:00
anchors {
fill: parent
topMargin: headerHeight
}
Binding {
target: mainFlickable
property: "contentY"
2019-10-04 14:27:29 +00:00
value: -window.offset + contentArea.height
when: !mainFlickable.moving && !mainFlickable.dragging && !mainFlickable.flicking
}
//no loop as those 2 values compute to exactly the same
onContentYChanged: {
2020-08-20 09:44:56 +00:00
if (contentY === oldContentY) {
window.direction = SlidingPanel.MovementDirection.None;
} else {
window.direction = contentY > oldContentY ? SlidingPanel.MovementDirection.Up : SlidingPanel.MovementDirection.Down;
}
2019-10-04 14:27:29 +00:00
window.offset = -contentY + contentArea.height
2020-08-20 09:44:56 +00:00
oldContentY = contentY;
}
2020-08-20 09:44:56 +00:00
property real oldContentY
2019-10-11 13:21:24 +00:00
boundsBehavior: Flickable.StopAtBounds
contentWidth: window.width
contentHeight: window.height*2
bottomMargin: window.height
onMovementStarted: {
window.cancelAnimations();
window.userInteracting = true;
}
2017-09-05 11:59:20 +00:00
onFlickStarted: window.userInteracting = true;
onMovementEnded: {
window.userInteracting = false;
window.updateState();
}
onFlickEnded: {
window.userInteracting = true;
window.updateState();
}
2019-09-18 14:42:33 +00:00
MouseArea {
2019-10-04 14:02:26 +00:00
id: dismissArea
z: 2
2019-09-18 14:42:33 +00:00
width: parent.width
2019-10-04 14:02:26 +00:00
height: mainFlickable.contentHeight
2019-09-18 14:42:33 +00:00
onClicked: window.close();
2019-10-04 14:02:26 +00:00
PlasmaComponents.Control {
id: contentArea
2019-10-08 16:21:39 +00:00
z: 1
y: 0
2019-09-18 14:42:33 +00:00
x: drawerX
width: drawerWidth
2014-10-23 14:29:27 +00:00
}
}
2015-06-08 19:28:42 +00:00
}
}
}