1/*
2 *  SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3 *
4 *  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6import QtQuick 2.0
7import QtQuick.Layouts 1.1
8import QtQuick.Window 2.0
9
10import org.kde.plasma.core 2.0 as PlasmaCore
11import org.kde.plasma.components 2.0 as PlasmaComponents
12import org.kde.kquickcontrolsaddons 2.0
13
14PlasmaCore.ToolTipArea {
15    id: root
16    objectName: "org.kde.desktop-CompactApplet"
17    anchors.fill: parent
18
19    mainText: plasmoid.toolTipMainText
20    subText: plasmoid.toolTipSubText
21    location: plasmoid.location
22    active: !plasmoid.expanded
23    textFormat: plasmoid.toolTipTextFormat
24    mainItem: plasmoid.toolTipItem ? plasmoid.toolTipItem : null
25
26    property Item fullRepresentation
27    property Item compactRepresentation
28    property Item expandedFeedback: expandedItem
29
30    onCompactRepresentationChanged: {
31        if (compactRepresentation) {
32            compactRepresentation.parent = root;
33            compactRepresentation.anchors.fill = root;
34            compactRepresentation.visible = true;
35        }
36        root.visible = true;
37    }
38
39    onFullRepresentationChanged: {
40
41        if (!fullRepresentation) {
42            return;
43        }
44
45        //if the fullRepresentation size was restored to a stored size, or if is dragged from the desktop, restore popup size
46        if (fullRepresentation.Layout && fullRepresentation.Layout.preferredWidth > 0) {
47            popupWindow.mainItem.width = Qt.binding(function() {
48                return fullRepresentation.Layout.preferredWidth
49            })
50        } else if (fullRepresentation.implicitWidth > 0) {
51            popupWindow.mainItem.width = Qt.binding(function() {
52                return fullRepresentation.implicitWidth
53            })
54        } else if (fullRepresentation.width > 0) {
55            popupWindow.mainItem.width = Qt.binding(function() {
56                return fullRepresentation.width
57            })
58        } else {
59            popupWindow.mainItem.width = Qt.binding(function() {
60                return PlasmaCore.Theme.mSize(PlasmaCore.Theme.defaultFont).width * 35
61            })
62        }
63
64        if (fullRepresentation.Layout && fullRepresentation.Layout.preferredHeight > 0) {
65            popupWindow.mainItem.height = Qt.binding(function() {
66                return fullRepresentation.Layout.preferredHeight
67            })
68        } else if (fullRepresentation.implicitHeight > 0) {
69            popupWindow.mainItem.height = Qt.binding(function() {
70                return fullRepresentation.implicitHeight
71            })
72        } else if (fullRepresentation.height > 0) {
73            popupWindow.mainItem.height = Qt.binding(function() {
74                return fullRepresentation.height
75            })
76        } else {
77            popupWindow.mainItem.height = Qt.binding(function() {
78                return PlasmaCore.Theme.mSize(PlasmaCore.Theme.defaultFont).height * 25
79            })
80        }
81
82        fullRepresentation.parent = appletParent;
83        fullRepresentation.anchors.fill = fullRepresentation.parent;
84    }
85
86    PlasmaCore.FrameSvgItem {
87        id: expandedItem
88        anchors.fill: parent
89        imagePath: "widgets/tabbar"
90        visible: fromCurrentTheme && opacity > 0
91        prefix: {
92            var prefix;
93            switch (plasmoid.location) {
94                case PlasmaCore.Types.LeftEdge:
95                    prefix = "west-active-tab";
96                    break;
97                case PlasmaCore.Types.TopEdge:
98                    prefix = "north-active-tab";
99                    break;
100                case PlasmaCore.Types.RightEdge:
101                    prefix = "east-active-tab";
102                    break;
103                default:
104                    prefix = "south-active-tab";
105                }
106                if (!hasElementPrefix(prefix)) {
107                    prefix = "active-tab";
108                }
109                return prefix;
110            }
111        opacity: plasmoid.expanded ? 1 : 0
112        Behavior on opacity {
113            NumberAnimation {
114                duration: PlasmaCore.Units.shortDuration
115                easing.type: Easing.InOutQuad
116            }
117        }
118    }
119
120    Timer {
121        id: expandedSync
122        interval: 100
123        onTriggered: plasmoid.expanded = popupWindow.visible;
124    }
125
126    Connections {
127        target: plasmoid.action("configure")
128        function onTriggered() { plasmoid.expanded = false }
129    }
130
131    Connections {
132        target: plasmoid
133        function onContextualActionsAboutToShow() { root.hideToolTip() }
134    }
135
136    PlasmaCore.Dialog {
137        id: popupWindow
138        objectName: "popupWindow"
139        flags: Qt.WindowStaysOnTopHint
140        visible: plasmoid.expanded && fullRepresentation
141        visualParent: compactRepresentation ? compactRepresentation : null
142        location: plasmoid.location
143        hideOnWindowDeactivate: plasmoid.hideOnWindowDeactivate
144        backgroundHints: (plasmoid.containmentDisplayHints & PlasmaCore.Types.DesktopFullyCovered) ? PlasmaCore.Dialog.SolidBackground : PlasmaCore.Dialog.StandardBackground
145
146        property var oldStatus: PlasmaCore.Types.UnknownStatus
147
148        //It's a MouseEventListener to get all the events, so the eventfilter will be able to catch them
149        mainItem: MouseEventListener {
150            id: appletParent
151
152            focus: true
153
154            Keys.onEscapePressed: {
155                plasmoid.expanded = false;
156            }
157
158            LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
159            LayoutMirroring.childrenInherit: true
160
161            Layout.minimumWidth: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.minimumWidth : 0
162            Layout.minimumHeight: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.minimumHeight: 0
163
164            Layout.preferredWidth: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.preferredWidth : -1
165            Layout.preferredHeight: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.preferredHeight: -1
166
167            Layout.maximumWidth: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.maximumWidth : Infinity
168            Layout.maximumHeight: (fullRepresentation && fullRepresentation.Layout) ? fullRepresentation.Layout.maximumHeight: Infinity
169
170            onActiveFocusChanged: {
171                if (activeFocus && fullRepresentation) {
172                    fullRepresentation.forceActiveFocus()
173                }
174            }
175        }
176
177        onVisibleChanged: {
178            if (!visible) {
179                expandedSync.restart();
180                plasmoid.status = oldStatus;
181            } else {
182                oldStatus = plasmoid.status;
183                plasmoid.status = PlasmaCore.Types.RequiresAttentionStatus;
184                // This call currently fails and complains at runtime:
185                // QWindow::setWindowState: QWindow::setWindowState does not accept Qt::WindowActive
186                popupWindow.requestActivate();
187            }
188        }
189
190    }
191}
192