1/*
2    SPDX-FileCopyrightText: 2015 Sebastian Kügler <sebas@kde.org>
3    SPDX-FileCopyrightText: 2016 Anthony Fieroni <bvbfan@abv.bg>
4    SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
5
6    SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9import QtQuick 2.7
10import QtQuick.Layouts 1.1
11
12import org.kde.plasma.plasmoid 2.0
13import org.kde.plasma.core 2.0 as PlasmaCore
14
15import org.kde.taskmanager 0.1 as TaskManager
16
17Item {
18    id: root
19
20    readonly property bool inPanel: (plasmoid.location === PlasmaCore.Types.TopEdge
21        || plasmoid.location === PlasmaCore.Types.RightEdge
22        || plasmoid.location === PlasmaCore.Types.BottomEdge
23        || plasmoid.location === PlasmaCore.Types.LeftEdge)
24
25    Layout.minimumWidth: PlasmaCore.Units.gridUnit
26    Layout.minimumHeight: PlasmaCore.Units.gridUnit
27
28    Layout.maximumWidth: inPanel ? PlasmaCore.Units.iconSizeHints.panel : -1
29    Layout.maximumHeight: inPanel ? PlasmaCore.Units.iconSizeHints.panel : -1
30
31    Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation
32    Plasmoid.backgroundHints: PlasmaCore.Types.NoBackground
33
34    Plasmoid.onActivated: toggleActive()
35
36    property bool active: false
37    property var minimizedClients: [] //list of persistentmodelindexes from task manager model of clients minimised by us
38
39    function activate() {
40        var clients = []
41        for (var i = 0 ; i < tasksModel.count; i++) {
42            var idx = tasksModel.makeModelIndex(i);
43            if (!tasksModel.data(idx, TaskManager.AbstractTasksModel.IsHidden)) {
44                tasksModel.requestToggleMinimized(idx);
45                clients.push(tasksModel.makePersistentModelIndex(i));
46            }
47        }
48        root.minimizedClients = clients;
49        root.active = true;
50    }
51
52    function deactivate() {
53        root.active = false;
54        for (var i = 0 ; i < root.minimizedClients.length; i++) {
55            var idx = root.minimizedClients[i]
56            //client deleted, do nothing
57            if (!idx.valid) {
58                continue;
59            }
60            //if the user has restored it already, do nothing
61            if (!tasksModel.data(idx, TaskManager.AbstractTasksModel.IsHidden)) {
62                continue;
63            }
64            tasksModel.requestToggleMinimized(idx);
65        }
66        root.minimizedClients = [];
67    }
68
69    function toggleActive() {
70        if (root.active) {
71            deactivate();
72        } else {
73            activate();
74        }
75    }
76
77    TaskManager.TasksModel {
78        id: tasksModel
79        sortMode: TaskManager.TasksModel.SortDisabled
80        groupMode: TaskManager.TasksModel.GroupDisabled
81    }
82
83    Connections {
84        target: tasksModel
85        enabled: root.active
86
87        function onActiveTaskChanged() {
88            if (tasksModel.activeTask.valid) { //to suppress changing focus to non windows, such as the desktop
89                root.active = false;
90                root.minimizedClients = [];
91            }
92        }
93        function onVirtualDesktopChanged() {deactivate()}
94        function onActivityChanged() {deactivate()}
95    }
96
97    PlasmaCore.FrameSvgItem {
98        id: expandedItem
99        anchors.fill: parent
100        imagePath: "widgets/tabbar"
101        prefix: {
102            var prefix;
103            switch (plasmoid.location) {
104                case PlasmaCore.Types.LeftEdge:
105                    prefix = "west-active-tab";
106                    break;
107                case PlasmaCore.Types.TopEdge:
108                    prefix = "north-active-tab";
109                    break;
110                case PlasmaCore.Types.RightEdge:
111                    prefix = "east-active-tab";
112                    break;
113                default:
114                    prefix = "south-active-tab";
115            }
116            if (!hasElementPrefix(prefix)) {
117                prefix = "active-tab";
118            }
119            return prefix;
120        }
121        opacity: root.active ? 1 : 0
122        Behavior on opacity {
123            NumberAnimation {
124                duration: PlasmaCore.Units.shortDuration
125                easing.type: Easing.InOutQuad
126            }
127        }
128    }
129
130    PlasmaCore.IconItem {
131        id:icon
132        source: plasmoid.configuration.icon
133        active: tooltip.containsMouse
134        anchors.fill: parent
135    }
136
137    PlasmaCore.ToolTipArea {
138        id: tooltip
139        anchors.fill: parent
140        mainText : i18n("Minimize all Windows")
141        subText : i18n("Show the desktop by minimizing all windows")
142
143        MouseArea {
144            id: mouseArea
145            anchors.fill: parent
146            onClicked: root.toggleActive()
147        }
148        //also activate when dragging an item over the plasmoid so a user can easily drag data to the desktop
149        DropArea {
150            anchors.fill: parent
151            onEntered: activateTimer.start()
152            onExited: activateTimer.stop()
153            Timer {
154                id: activateTimer
155                interval: 250 //to match TaskManager
156                onTriggered: toggleActive()
157            }
158        }
159    }
160}
161