shift-shell/components/mobileshell/qml/taskswitcher/TaskSwitcher.qml

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

285 lines
10 KiB
QML
Raw Normal View History

2015-06-18 23:31:26 +00:00
/*
2021-03-01 20:03:25 +00:00
* SPDX-FileCopyrightText: 2015 Marco Martin <notmart@gmail.com>
2021-10-18 03:50:59 +00:00
* SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
2015-06-18 23:31:26 +00:00
*
2021-03-01 20:03:25 +00:00
* SPDX-License-Identifier: LGPL-2.0-or-later
2015-06-18 23:31:26 +00:00
*/
2020-07-17 12:13:35 +00:00
import QtQuick 2.12
2015-06-18 23:31:26 +00:00
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
import org.kde.taskmanager 0.1 as TaskManager
2015-06-24 20:47:07 +00:00
import org.kde.plasma.core 2.1 as PlasmaCore
2019-10-11 10:06:12 +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
import org.kde.plasma.private.mobileshell 1.0 as MobileShell
2015-06-18 23:31:26 +00:00
Item {
id: root
2015-06-18 23:31:26 +00:00
visible: false
2021-10-18 03:50:59 +00:00
readonly property real taskPanelHeight: MobileShell.TaskPanelControls.panelHeight
readonly property real taskPanelWidth: MobileShell.TaskPanelControls.panelWidth
readonly property bool isPortrait: MobileShell.TaskPanelControls.isPortrait
2021-10-18 03:50:59 +00:00
// dimensions of a window on the screen
readonly property real windowHeight: root.height - (root.isPortrait ? root.taskPanelHeight : 0) - MobileShell.TopPanelControls.panelHeight
readonly property real windowWidth: root.width - (root.isPortrait ? 0 : root.taskPanelWidth)
readonly property int tasksCount: root.model.count
readonly property int currentTaskIndex: tasksView.currentIndex
2020-07-17 12:13:35 +00:00
property TaskManager.TasksModel model
2021-10-18 03:50:59 +00:00
// offset constants
readonly property real targetYOffsetDist: root.height - tasksView.height // offset distance to perfect opening
readonly property real dismissYOffsetDist: root.height
// properties controlled from NavigationPanel (swipe to open gesture)
2021-10-24 04:16:38 +00:00
property real oldYOffset: 0
property real yOffset: 0
2021-10-18 18:47:17 +00:00
// set from NavigationPanel in taskpanel containment
2021-10-18 18:47:17 +00:00
property bool wasInActiveTask: false // whether we were in an app before opening the task switcher
2021-10-24 04:16:38 +00:00
property bool currentlyDragging: false // whether we are in a swipe up gesture
2015-06-18 23:31:26 +00:00
property var displaysModel: MobileShell.DisplaysModel {}
2020-07-17 12:13:35 +00:00
enum MovementDirection {
None = 0,
2021-10-18 03:50:59 +00:00
Left,
Right
}
onVisibleChanged: MobileShell.HomeScreenControls.taskSwitcherVisible = visible;
2020-07-17 12:13:35 +00:00
onTasksCountChanged: {
if (tasksCount == 0) {
hide();
}
}
2021-10-18 03:50:59 +00:00
2020-07-31 10:06:46 +00:00
Rectangle {
2021-10-18 03:50:59 +00:00
id: backgroundRect
2020-07-31 10:06:46 +00:00
anchors.fill: parent
color: Qt.rgba(0, 0, 0, 0.6 * (root.wasInActiveTask ? 1 : Math.min(1, root.yOffset / root.targetYOffsetDist)))
2021-10-18 03:50:59 +00:00
MouseArea {
anchors.fill: parent
onClicked: root.hide()
2021-10-18 03:50:59 +00:00
}
2020-07-31 10:06:46 +00:00
}
2015-06-18 23:31:26 +00:00
2021-10-18 18:47:17 +00:00
//BEGIN functions
2021-10-18 18:47:17 +00:00
function show(animation) {
root.yOffset = 0;
root.wasInActiveTask = root.model.activeTask.row >= 0;
2021-10-18 18:47:17 +00:00
2021-10-18 03:50:59 +00:00
// skip to first active task
if (root.wasInActiveTask) {
tasksView.currentIndex = root.model.activeTask.row;
tasksView.positionViewAtIndex(root.model.activeTask.row, ListView.SnapPosition);
}
2021-10-18 03:50:59 +00:00
root.visible = true;
minimizeAll();
2021-10-18 18:47:17 +00:00
// animate app shrink
if (animation) {
offsetAnimator.to = root.targetYOffsetDist;
2021-10-18 18:47:17 +00:00
offsetAnimator.restart();
}
2015-06-18 23:31:26 +00:00
}
function hide() {
if (!root.visible) return;
root.visible = false;
2015-06-18 23:31:26 +00:00
}
2021-10-18 18:47:17 +00:00
function snapOffset() {
let movingUp = root.yOffset > root.oldYOffset;
2021-10-24 04:16:38 +00:00
if (movingUp || root.yOffset >= root.targetYOffsetDist) { // open task switcher and stay
offsetAnimator.to = root.targetYOffsetDist;
2021-10-18 18:47:17 +00:00
offsetAnimator.restart();
2021-10-24 04:16:38 +00:00
} else { // close task switcher and return to app
if (!root.wasInActiveTask) { // if pulled up from homescreen, don't activate app
2021-10-18 18:47:17 +00:00
offsetAnimator.activateApp = false;
}
offsetAnimator.to = 0;
offsetAnimator.restart();
}
}
// scroll to delegate index, and activate it
function activateWindow(id) {
offsetAnimator.to = 0;
offsetAnimator.restart();
}
2020-07-30 15:53:55 +00:00
function setSingleActiveWindow(id, delegate) {
if (id < 0) {
return;
}
var newActiveIdx = root.model.index(id, 0)
var newActiveGeo = tasksModel.data(newActiveIdx, TaskManager.AbstractTasksModel.ScreenGeometry)
for (var i = 0 ; i < tasksModel.count; i++) {
var idx = root.model.index(i, 0)
if (i == id) {
root.model.requestActivate(idx);
} else if (!tasksModel.data(idx, TaskManager.AbstractTasksModel.IsMinimized)) {
var geo = tasksModel.data(idx, TaskManager.AbstractTasksModel.ScreenGeometry)
// Only minimize the other windows in the same screen
if (geo === newActiveGeo) {
tasksModel.requestToggleMinimized(idx);
}
}
}
2021-10-18 03:50:59 +00:00
root.visible = false;
}
function minimizeAll() {
for (var i = 0 ; i < tasksModel.count; i++) {
var idx = tasksModel.makeModelIndex(i);
if (!tasksModel.data(idx, TaskManager.AbstractTasksModel.IsMinimized)) {
tasksModel.requestToggleMinimized(idx);
}
}
}
function restoreAll() {
for (var i = 0 ; i < tasksModel.count; i++) {
var idx = tasksModel.makeModelIndex(i);
if (tasksModel.data(idx, TaskManager.AbstractTasksModel.IsMinimized)) {
tasksModel.requestToggleMinimized(idx);
}
}
2020-07-30 15:53:55 +00:00
}
2021-10-18 18:47:17 +00:00
//END functions
2020-07-30 15:53:55 +00:00
2021-10-18 18:47:17 +00:00
// animate app grow and shrink
2021-10-24 04:16:38 +00:00
NumberAnimation on yOffset {
2021-10-18 18:47:17 +00:00
id: offsetAnimator
duration: PlasmaCore.Units.longDuration
easing.type: Easing.InOutQuad
property bool activateApp: true
// states of to:
// 0 - open/resume app (zoom up the thumbnail)
// root.targetYOffsetDist - animate shrinking of thumbnail, to listview (open task switcher)
2021-10-18 18:47:17 +00:00
to: 0
onFinished: {
if (to === 0) { // close task switcher, and switch to current app
if (!root.visible) return;
root.visible = false;
2021-10-18 18:47:17 +00:00
if (activateApp) {
setSingleActiveWindow(root.currentTaskIndex);
2021-10-18 18:47:17 +00:00
}
activateApp = true;
} else if (to == root.dismissYOffsetDist) {
root.hide();
2021-10-18 18:47:17 +00:00
}
}
}
2021-10-18 03:50:59 +00:00
2021-10-31 19:21:02 +00:00
Item {
id: container
2021-10-18 03:50:59 +00:00
2021-10-31 19:21:02 +00:00
// provide shell margins
anchors.fill: parent
anchors.rightMargin: root.isPortrait ? 0 : root.taskPanelWidth
anchors.bottomMargin: root.isPortrait ? root.taskPanelHeight : 0
2021-10-31 19:21:02 +00:00
anchors.topMargin: MobileShell.TopPanelControls.panelHeight
2021-10-18 03:50:59 +00:00
2021-10-31 19:21:02 +00:00
// applications list
ListView {
id: tasksView
opacity: root.wasInActiveTask ? 1 : Math.min(1, root.yOffset / root.targetYOffsetDist)
2021-10-31 19:21:02 +00:00
anchors.centerIn: parent
readonly property real sizeFactor: 0.75
readonly property real taskHeaderHeight: PlasmaCore.Units.gridUnit * 2 + PlasmaCore.Units.smallSpacing * 2
width: root.windowWidth * sizeFactor
height: root.windowHeight * sizeFactor + taskHeaderHeight
2021-10-31 19:21:02 +00:00
model: root.model
2021-10-31 19:21:02 +00:00
orientation: ListView.Horizontal
2021-10-18 03:50:59 +00:00
2021-10-31 19:21:02 +00:00
highlightRangeMode: ListView.StrictlyEnforceRange // ensures currentIndex is updated
snapMode: ListView.SnapToItem
spacing: PlasmaCore.Units.largeSpacing
displayMarginBeginning: 2 * (width + spacing)
displayMarginEnd: 2 * (width + spacing)
displaced: Transition {
NumberAnimation { properties: "x,y"; duration: PlasmaCore.Units.longDuration; easing.type: Easing.InOutQuad }
2015-06-21 19:26:14 +00:00
}
2021-10-31 04:11:10 +00:00
2021-10-31 19:21:02 +00:00
// ensure that window previews are exactly to the scale of the device screen
property real scalingFactor: {
let candidateHeight = (tasksView.width / root.windowWidth) * root.windowHeight;
2021-10-31 19:21:02 +00:00
if (candidateHeight > tasksView.height) {
return tasksView.height / root.windowHeight;
2021-10-31 19:21:02 +00:00
} else {
return tasksView.width / root.windowWidth;
2021-10-31 19:21:02 +00:00
}
}
2021-10-31 04:11:10 +00:00
2021-10-31 19:21:02 +00:00
delegate: Task {
id: task
property int curIndex: model.index
z: root.currentTaskIndex === curIndex ? 1 : 0
2021-10-31 19:21:02 +00:00
width: tasksView.width
height: tasksView.height
taskSwitcher: root
displaysModel: root.displaysModel
2021-10-31 19:21:02 +00:00
// account for header offset (center the preview)
y: -tasksView.taskHeaderHeight / 2
2021-10-31 04:11:10 +00:00
2021-10-31 19:21:02 +00:00
// scale gesture
scale: {
let maxScale = 1 / tasksView.scalingFactor;
let subtract = (maxScale - 1) * (root.yOffset / root.targetYOffsetDist);
2021-10-31 19:21:02 +00:00
let finalScale = Math.max(0, Math.min(maxScale, maxScale - subtract));
if ((root.wasInActiveTask || !taskSwitcher.currentlyDragging) && root.currentTaskIndex === task.curIndex) {
2021-10-31 19:21:02 +00:00
return finalScale;
}
return 1;
2021-10-31 04:11:10 +00:00
}
2021-10-31 19:21:02 +00:00
// ensure that window previews are exactly to the scale of the device screen
previewWidth: tasksView.scalingFactor * root.windowWidth
previewHeight: tasksView.scalingFactor * root.windowHeight
2021-10-31 04:11:10 +00:00
}
2020-02-04 19:10:01 +00:00
}
2015-06-18 23:31:26 +00:00
}
2021-10-18 03:50:59 +00:00
2021-10-31 04:11:10 +00:00
// top panel swipe down gesture
MouseArea {
anchors.top: parent.top
2021-10-18 03:50:59 +00:00
anchors.left: parent.left
anchors.right: parent.right
2021-10-31 04:11:10 +00:00
height: MobileShell.TopPanelControls.panelHeight
property int oldMouseY: 0
onPositionChanged: {
MobileShell.TopPanelControls.requestRelativeScroll(mouse.y - oldMouseY);
oldMouseY = mouse.y;
}
onPressed: {
oldMouseY = mouse.y;
MobileShell.TopPanelControls.startSwipe();
}
onReleased: MobileShell.TopPanelControls.endSwipe();
}
2015-06-18 23:31:26 +00:00
}