main.qml 8.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  Copyright 2015 Marco Martin <mart@kde.org>
 *
 *  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  2.010-1301, USA.
 */

import QtQuick 2.4
import QtQuick.Layouts 1.1
Marco Martin's avatar
Marco Martin committed
21
import QtQuick.Window 2.2
Marco Martin's avatar
Marco Martin committed
22
import QtGraphicalEffects 1.12
23

Marco Martin's avatar
Marco Martin committed
24
import org.kde.taskmanager 0.1 as TaskManager
25 26 27 28 29
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.kquickcontrolsaddons 2.0

30 31
import org.kde.plasma.private.nanoshell 2.0 as NanoShell

Marco Martin's avatar
Marco Martin committed
32 33
import org.kde.plasma.private.mobileshell 1.0 as MobileShell

Marco Martin's avatar
Marco Martin committed
34 35
PlasmaCore.ColorScope {
    id: root
36
    width: 600
Marco Martin's avatar
Marco Martin committed
37
    height: 480
Marco Martin's avatar
Marco Martin committed
38
    colorGroup: showingApp ? PlasmaCore.Theme.NormalColorGroup : PlasmaCore.Theme.ComplementaryColorGroup
Marco Martin's avatar
Marco Martin committed
39

40 41
    Plasmoid.backgroundHints: PlasmaCore.Types.NoBackground

42 43
    readonly property color backgroundColor: NanoShell.StartupFeedback.visible ? NanoShell.StartupFeedback.backgroundColor : PlasmaCore.ColorScope.backgroundColor
    readonly property bool showingApp: !plasmoid.nativeInterface.allMinimized
44 45

    readonly property bool hasTasks: tasksModel.count > 0
46

47 48 49
    property QtObject taskSwitcher: taskSwitcherLoader.item ? taskSwitcherLoader.item : null
    Loader {
        id: taskSwitcherLoader
Marco Martin's avatar
Marco Martin committed
50
    }
51
    //FIXME: why it crashes on startup if TaskSwitcher is loaded immediately?
Marco Martin's avatar
Marco Martin committed
52 53
    Connections {
        target: plasmoid.nativeInterface
54 55
        function onAllMinimizedChanged() {
            MobileShell.HomeScreenControls.homeScreenVisible = plasmoid.nativeInterface.allMinimized
Marco Martin's avatar
Marco Martin committed
56 57
        }
    }
58 59 60
    Timer {
        running: true
        interval: 200
Marco Martin's avatar
Marco Martin committed
61 62 63
        onTriggered: {
            taskSwitcherLoader.setSource(Qt.resolvedUrl("TaskSwitcher.qml"), {"model": tasksModel});
        }
64 65
    }

Marco Martin's avatar
Marco Martin committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    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);
            }
        }
    }

84 85 86 87 88
    TaskManager.TasksModel {
        id: tasksModel
        groupMode: TaskManager.TasksModel.GroupDisabled

        screenGeometry: plasmoid.screenGeometry
89 90 91 92 93 94 95 96 97 98 99
        sortMode: TaskManager.TasksModel.SortAlpha

        virtualDesktop: virtualDesktopInfo.currentDesktop
        activity: activityInfo.currentActivity
        //FIXME: workaround
        Component.onCompleted: tasksModel.countChanged();
    }

    TaskManager.VirtualDesktopInfo {
        id: virtualDesktopInfo
    }
100

101 102
    TaskManager.ActivityInfo {
        id: activityInfo
103 104
    }

Marco Martin's avatar
Marco Martin committed
105 106
    MouseArea {
        id: mainMouseArea
Marco Martin's avatar
Marco Martin committed
107
        anchors.fill: parent
Marco Martin's avatar
Marco Martin committed
108
        property int oldMouseY: 0
Marco Martin's avatar
Marco Martin committed
109 110
        property int startMouseY: 0
        property bool isDragging: false
Marco Martin's avatar
Marco Martin committed
111
        property bool opening: false
Marco Martin's avatar
Marco Martin committed
112
        drag.filterChildren: true
Marco Martin's avatar
Marco Martin committed
113 114 115
        property Button activeButton

        onPressed: {
Marco Martin's avatar
Marco Martin committed
116
            startMouseY = oldMouseY = mouse.y;
Marco Martin's avatar
Marco Martin committed
117 118
            taskSwitcher.offset = -taskSwitcher.height;
            activeButton = icons.childAt(mouse.x, mouse.y);
Marco Martin's avatar
Marco Martin committed
119 120
        }
        onPositionChanged: {
Marco Martin's avatar
Marco Martin committed
121 122 123 124
            let newButton = icons.childAt(mouse.x, mouse.y);
            if (newButton != activeButton) {
                activeButton = null;
            }
Marco Martin's avatar
Marco Martin committed
125 126 127 128 129 130 131
            if (!isDragging && Math.abs(startMouseY - oldMouseY) < root.height) {
                oldMouseY = mouse.y;
                return;
            } else {
                isDragging = true;
            }

Marco Martin's avatar
Marco Martin committed
132
            taskSwitcher.offset = taskSwitcher.offset - (mouse.y - oldMouseY);
Marco Martin's avatar
Marco Martin committed
133
            opening = oldMouseY > mouse.y;
134

Marco Martin's avatar
Marco Martin committed
135
            if (taskSwitcher.visibility == Window.Hidden && taskSwitcher.offset > -taskSwitcher.height + units.gridUnit && taskSwitcher.tasksCount) {
Marco Martin's avatar
Marco Martin committed
136
                activeButton = null;
137
                taskSwitcher.showFullScreen();
138 139 140
            //no tasks, let's scroll up the homescreen instead
            } else if (taskSwitcher.tasksCount === 0) {
                MobileShell.HomeScreenControls.requestHomeScreenPosition(MobileShell.HomeScreenControls.homeScreenPosition - (mouse.y - oldMouseY));
Marco Martin's avatar
Marco Martin committed
141
            }
142
            oldMouseY = mouse.y;
Marco Martin's avatar
Marco Martin committed
143 144 145
        }
        onReleased: {
            if (taskSwitcher.visibility == Window.Hidden) {
146 147 148
                if (taskSwitcher.tasksCount === 0) {
                    MobileShell.HomeScreenControls.snapHomeScreenPosition();
                }
Marco Martin's avatar
Marco Martin committed
149 150 151 152

                if (activeButton) {
                    activeButton.clicked();
                }
Marco Martin's avatar
Marco Martin committed
153 154
                return;
            }
Marco Martin's avatar
Marco Martin committed
155 156 157 158 159

            if (!isDragging) {
                return;
            }

Marco Martin's avatar
Marco Martin committed
160
            if (opening) {
Marco Martin's avatar
Marco Martin committed
161 162 163 164 165
                taskSwitcher.show();
            } else {
                taskSwitcher.hide();
            }
        }
Marco Martin's avatar
Marco Martin committed
166

Marco Martin's avatar
Marco Martin committed
167 168
        DropShadow {
            anchors.fill: icons
Jonah Brüchert's avatar
Jonah Brüchert committed
169
            visible: !showingApp
Marco Martin's avatar
Marco Martin committed
170
            cached: true
Marco Martin's avatar
Marco Martin committed
171 172 173 174 175 176 177
            horizontalOffset: 0
            verticalOffset: 1
            radius: 4.0
            samples: 17
            color: Qt.rgba(0,0,0,0.8)
            source: icons
        }
178
        Item {
Marco Martin's avatar
Marco Martin committed
179
            id: icons
Marco Martin's avatar
Marco Martin committed
180
            anchors.fill: parent
Marco Martin's avatar
Marco Martin committed
181

182
            visible: plasmoid.configuration.PanelButtonsVisible
183

Marco Martin's avatar
Marco Martin committed
184 185 186 187 188
            Rectangle {
                anchors.fill: parent
                gradient: Gradient {
                    GradientStop {
                        position: 0
189
                        color: showingApp ? root.backgroundColor : "transparent"
Marco Martin's avatar
Marco Martin committed
190 191 192
                    }
                    GradientStop {
                        position: 1
193
                        color: showingApp ? root.backgroundColor : Qt.rgba(0, 0, 0, 0.1)
Marco Martin's avatar
Marco Martin committed
194 195
                    }
                }
196
            }
Marco Martin's avatar
Marco Martin committed
197

Marco Martin's avatar
Marco Martin committed
198 199 200 201
            Button {
                anchors.left: parent.left
                height: parent.height
                width: parent.width/3
Marco Martin's avatar
Marco Martin committed
202
                mouseArea: mainMouseArea
203
                enabled: root.hasTasks
Ilya Bizyaev's avatar
Ilya Bizyaev committed
204
                iconSource: "box"
Marco Martin's avatar
Marco Martin committed
205
                onClicked: {
Marco Martin's avatar
Marco Martin committed
206
                    if (!enabled) {
207 208
                        return;
                    }
Marco Martin's avatar
Marco Martin committed
209
                    plasmoid.nativeInterface.showDesktop = false;
Marco Martin's avatar
Marco Martin committed
210 211
                    taskSwitcher.visible ? taskSwitcher.hide() : taskSwitcher.show();
                }
Marco Martin's avatar
Marco Martin committed
212
            }
Marco Martin's avatar
Marco Martin committed
213 214 215 216 217 218

            Button {
                id: showDesktopButton
                height: parent.height
                width: parent.width/3
                anchors.horizontalCenter: parent.horizontalCenter
Marco Martin's avatar
Marco Martin committed
219
                mouseArea: mainMouseArea
Ilya Bizyaev's avatar
Ilya Bizyaev committed
220
                iconSource: "start-here-kde"
Marco Martin's avatar
Marco Martin committed
221
                enabled: !taskSwitcher.visible && (root.showingApp || MobileShell.HomeScreenControls.homeScreenPosition != 0)
Marco Martin's avatar
Marco Martin committed
222
                onClicked: {
Marco Martin's avatar
Marco Martin committed
223
                    if (!enabled) {
224 225
                        return;
                    }
Marco Martin's avatar
Marco Martin committed
226
                    root.minimizeAll();
227 228
                    MobileShell.HomeScreenControls.resetHomeScreenPosition();
                    plasmoid.nativeInterface.allMinimizedChanged();
Marco Martin's avatar
Marco Martin committed
229
                }
230
            }
Marco Martin's avatar
Marco Martin committed
231

Marco Martin's avatar
Marco Martin committed
232 233 234 235
            Button {
                height: parent.height
                width: parent.width/3
                anchors.right: parent.right
Marco Martin's avatar
Marco Martin committed
236
                mouseArea: mainMouseArea
Ilya Bizyaev's avatar
Ilya Bizyaev committed
237
                iconSource: "paint-none"
Marco Martin's avatar
Marco Martin committed
238
                enabled: plasmoid.nativeInterface.hasCloseableActiveWindow && !taskSwitcher.visible
Marco Martin's avatar
Marco Martin committed
239
                onClicked: {
Marco Martin's avatar
Marco Martin committed
240
                    if (!enabled) {
241 242 243 244 245
                        return;
                    }
                    if (!plasmoid.nativeInterface.hasCloseableActiveWindow) {
                        return;
                    }
Marco Martin's avatar
Marco Martin committed
246 247 248 249 250
                    var index = taskSwitcher.model.activeTask;
                    if (index) {
                        taskSwitcher.model.requestClose(index);
                    }
                }
Marco Martin's avatar
Marco Martin committed
251
            }
Marco Martin's avatar
Marco Martin committed
252
        }
253
    }
254
}