Changeset View
Changeset View
Standalone View
Standalone View
applets/notifications/package/contents/ui/NotificationPopup.qml
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright 2014 Martin Klapetek <mklapetek@kde.org> | 2 | * Copyright 2019 Kai Uwe Broulik <kde@privat.broulik.de> | ||
3 | * | 3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | 4 | * This program is free software; you can redistribute it and/or | ||
5 | * it under the terms of the GNU Library General Public License as | 5 | * modify it under the terms of the GNU General Public License as | ||
6 | * published by the Free Software Foundation; either version 2, or | 6 | * published by the Free Software Foundation; either version 2 of | ||
7 | * (at your option) any later version. | 7 | * the License or (at your option) version 3 or any later version | ||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
8 | * | 11 | * | ||
9 | * This program is distributed in the hope that it will be useful, | 12 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU Library General Public License for more details | 15 | * GNU General Public License for more details. | ||
13 | * | 16 | * | ||
14 | * You should have received a copy of the GNU Library General Public | 17 | * You should have received a copy of the GNU General Public License | ||
15 | * License along with this program; if not, write to the | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
16 | * Free Software Foundation, Inc., | | |||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | | |||
18 | */ | 19 | */ | ||
19 | 20 | | |||
20 | import QtQuick 2.0 | 21 | import QtQuick 2.8 | ||
21 | import QtQuick.Controls.Private 1.0 | | |||
22 | import QtQuick.Layouts 1.1 | 22 | import QtQuick.Layouts 1.1 | ||
23 | 23 | | |||
24 | import org.kde.plasma.core 2.0 as PlasmaCore | 24 | import org.kde.plasma.core 2.0 as PlasmaCore | ||
25 | import org.kde.plasma.components 2.0 as PlasmaComponents | 25 | import org.kde.plasma.components 2.0 as Components | ||
26 | import org.kde.plasma.extras 2.0 as PlasmaExtras | | |||
27 | import org.kde.kquickcontrolsaddons 2.0 | | |||
28 | 26 | | |||
29 | import org.kde.kquickcontrolsaddons 2.0 as KQuickControlsAddons | 27 | import org.kde.notificationmanager 1.0 as NotificationManager | ||
28 | | ||||
29 | import ".." | ||||
30 | 30 | | |||
31 | PlasmaCore.Dialog { | 31 | PlasmaCore.Dialog { | ||
32 | id: notificationPopup | 32 | id: notificationPopup | ||
33 | 33 | | |||
34 | location: PlasmaCore.Types.Floating | 34 | property int popupWidth | ||
35 | type: PlasmaCore.Dialog.Notification | | |||
36 | flags: Qt.WindowDoesNotAcceptFocus | | |||
37 | 35 | | |||
38 | property var notificationProperties: ({}) | 36 | property alias notificationType: notificationItem.notificationType | ||
39 | signal notificationTimeout() | | |||
40 | 37 | | |||
41 | onVisibleChanged: { | 38 | property alias applicationName: notificationItem.applicationName | ||
42 | if (!visible) { | 39 | property alias applicationIconSource: notificationItem.applicationIconSource | ||
43 | notificationTimer.stop(); | 40 | property alias deviceName: notificationItem.deviceName | ||
44 | } | 41 | | ||
45 | } | 42 | property alias time: notificationItem.time | ||
43 | | ||||
44 | property alias summary: notificationItem.summary | ||||
45 | property alias body: notificationItem.body | ||||
46 | property alias icon: notificationItem.icon | ||||
47 | property alias urls: notificationItem.urls | ||||
48 | | ||||
49 | property int urgency | ||||
50 | property int timeout | ||||
51 | property int dismissTimeout | ||||
52 | | ||||
53 | property alias jobState: notificationItem.jobState | ||||
54 | property alias percentage: notificationItem.percentage | ||||
55 | property alias jobError: notificationItem.jobError | ||||
56 | property alias suspendable: notificationItem.suspendable | ||||
57 | property alias killable: notificationItem.killable | ||||
58 | property alias jobDetails: notificationItem.jobDetails | ||||
46 | 59 | | |||
47 | onYChanged: { | 60 | property alias configureActionLabel: notificationItem.configureActionLabel | ||
48 | if (visible && !notificationItem.dragging) { | 61 | property alias configurable: notificationItem.configurable | ||
49 | notificationTimer.restart(); | 62 | property alias dismissable: notificationItem.dismissable | ||
63 | property alias closable: notificationItem.closable | ||||
64 | | ||||
65 | property bool hasDefaultAction | ||||
66 | property alias actionNames: notificationItem.actionNames | ||||
67 | property alias actionLabels: notificationItem.actionLabels | ||||
68 | | ||||
69 | signal configureClicked | ||||
70 | signal dismissClicked | ||||
71 | signal closeClicked | ||||
72 | | ||||
73 | signal defaultActionInvoked | ||||
74 | signal actionInvoked(string actionName) | ||||
75 | signal openUrl(string url) | ||||
76 | signal fileActionInvoked | ||||
77 | | ||||
78 | signal expired | ||||
79 | | ||||
80 | signal suspendJobClicked | ||||
81 | signal resumeJobClicked | ||||
82 | signal killJobClicked | ||||
83 | | ||||
84 | property int defaultTimeout: 5000 | ||||
85 | readonly property int effectiveTimeout: { | ||||
86 | if (timeout === -1) { | ||||
87 | return defaultTimeout; | ||||
88 | } | ||||
89 | if (dismissTimeout) { | ||||
90 | return dismissTimeout; | ||||
50 | } | 91 | } | ||
92 | return timeout; | ||||
51 | } | 93 | } | ||
52 | 94 | | |||
53 | function populatePopup(notification) { | 95 | location: PlasmaCore.Types.Floating | ||
54 | notificationProperties = notification | 96 | | ||
55 | notificationTimer.interval = notification.expireTimeout | 97 | type: PlasmaCore.Dialog.Notification | ||
56 | notificationTimer.restart(); | 98 | flags: { | ||
57 | //temporarly disable height binding, avoids an useless window resize when removing the old actions | 99 | var flags = Qt.WindowDoesNotAcceptFocus; | ||
58 | heightBinding.when = false; | 100 | // FIXME this needs support in KWin somehow... | ||
59 | // notification.actions is a JS array, but we can easily append that to our model | 101 | if (urgency === NotificationManager.Notifications.CriticalUrgency) { | ||
60 | notificationItem.actions.clear(); | 102 | flags |= Qt.WindowStaysOnTopHint; | ||
61 | // Workaround a crash in Qt when appending an empty list (https://codereview.qt-project.org/#/c/223985/) | | |||
62 | if (notificationProperties.actions.length > 0) { | | |||
63 | notificationItem.actions.append(notificationProperties.actions); | | |||
64 | } | 103 | } | ||
65 | //enable height binding again, finally do the resize | 104 | return flags; | ||
66 | heightBinding.when = true; | | |||
67 | } | 105 | } | ||
68 | 106 | | |||
69 | function clearPopup() { | 107 | visible: false | ||
70 | notificationProperties = {} | 108 | | ||
71 | notificationItem.actions.clear() | 109 | // When notification is updated, restart hide timer | ||
110 | onTimeChanged: { | ||||
111 | if (timer.running) { | ||||
112 | timer.restart(); | ||||
113 | } | ||||
72 | } | 114 | } | ||
73 | 115 | | |||
74 | mainItem: NotificationItem { | 116 | mainItem: MouseArea { | ||
75 | id: notificationItem | 117 | id: area | ||
118 | width: notificationPopup.popupWidth | ||||
119 | height: notificationItem.implicitHeight + notificationItem.y | ||||
76 | hoverEnabled: true | 120 | hoverEnabled: true | ||
77 | 121 | | |||
122 | cursorShape: hasDefaultAction ? Qt.PointingHandCursor : Qt.ArrowCursor | ||||
123 | acceptedButtons: hasDefaultAction ? Qt.LeftButton : Qt.NoButton | ||||
124 | | ||||
125 | onClicked: notificationPopup.defaultActionInvoked() | ||||
126 | | ||||
78 | LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft | 127 | LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft | ||
79 | LayoutMirroring.childrenInherit: true | 128 | LayoutMirroring.childrenInherit: true | ||
80 | 129 | | |||
81 | //the binding needs to be disabled when re-populating actions, to minimize resizes | | |||
82 | Binding on height { | | |||
83 | id: heightBinding | | |||
84 | value: notificationItem.implicitHeight | | |||
85 | when: true | | |||
86 | } | | |||
87 | | ||||
88 | Timer { | 130 | Timer { | ||
89 | id: notificationTimer | 131 | id: timer | ||
132 | interval: notificationPopup.effectiveTimeout | ||||
133 | running: notificationPopup.visible && !area.containsMouse && interval > 0 | ||||
134 | && !notificationItem.dragging && !notificationItem.menuOpen | ||||
90 | onTriggered: { | 135 | onTriggered: { | ||
91 | if (!notificationProperties.isPersistent) { | 136 | if (notificationPopup.dismissTimeout) { | ||
92 | expireNotification(notificationProperties.source) | 137 | notificationPopup.dismissClicked(); | ||
93 | } | 138 | } else { | ||
94 | notificationPopup.notificationTimeout(); | 139 | notificationPopup.expired(); | ||
95 | } | 140 | } | ||
96 | } | 141 | } | ||
97 | onContainsMouseChanged: { | | |||
98 | if (containsMouse) { | | |||
99 | notificationTimer.stop() | | |||
100 | } else if (!containsMouse && !dragging && visible) { | | |||
101 | notificationTimer.restart() | | |||
102 | } | 142 | } | ||
103 | } | 143 | | ||
104 | onDraggingChanged: { | 144 | Timer { | ||
105 | if (dragging) { | 145 | id: timeoutIndicatorDelayTimer | ||
106 | notificationTimer.stop() | 146 | // only show indicator for the last ten seconds of timeout | ||
107 | } else if (!containsMouse && !dragging && visible) { | 147 | readonly property int remainingTimeout: 10000 | ||
108 | notificationTimer.restart() | 148 | interval: Math.max(0, timer.interval - remainingTimeout) | ||
149 | running: interval > 0 && timer.running | ||||
150 | } | ||||
151 | | ||||
152 | Rectangle { | ||||
153 | id: timeoutIndicatorRect | ||||
154 | anchors { | ||||
155 | right: parent.right | ||||
156 | rightMargin: -notificationPopup.margins.right | ||||
157 | bottom: parent.bottom | ||||
158 | bottomMargin: -notificationPopup.margins.bottom | ||||
159 | } | ||||
160 | width: units.devicePixelRatio * 3 | ||||
161 | radius: width | ||||
162 | color: theme.highlightColor | ||||
163 | opacity: timeoutIndicatorAnimation.running ? 0.6 : 0 | ||||
164 | visible: units.longDuration > 1 | ||||
165 | Behavior on opacity { | ||||
166 | NumberAnimation { | ||||
167 | duration: units.longDuration | ||||
109 | } | 168 | } | ||
110 | } | 169 | } | ||
111 | 170 | | |||
112 | summary: notificationProperties.summary || "" | 171 | NumberAnimation { | ||
113 | body: notificationProperties.body || "" | 172 | id: timeoutIndicatorAnimation | ||
114 | icon: notificationProperties.appIcon || "" | 173 | target: timeoutIndicatorRect | ||
115 | image: notificationProperties.image | 174 | property: "height" | ||
116 | // explicit true/false or else it complains about assigning undefined to bool | 175 | from: area.height + notificationPopup.margins.top + notificationPopup.margins.bottom | ||
117 | configurable: notificationProperties.configurable && !Settings.isMobile ? true : false | 176 | to: 0 | ||
118 | urls: notificationProperties.urls || [] | 177 | duration: Math.min(timer.interval, timeoutIndicatorDelayTimer.remainingTimeout) | ||
119 | hasDefaultAction: notificationProperties.hasDefaultAction || false | 178 | running: timer.running && !timeoutIndicatorDelayTimer.running && units.longDuration > 1 | ||
120 | hasConfigureAction: notificationProperties.hasConfigureAction || false | | |||
121 | | ||||
122 | width: Math.round(23 * units.gridUnit) | | |||
123 | maximumTextHeight: theme.mSize(theme.defaultFont).height * 10 | | |||
124 | | ||||
125 | onClose: { | | |||
126 | closeNotification(notificationProperties.source) | | |||
127 | // the popup will be closed in response to sourceRemoved | | |||
128 | } | | |||
129 | onConfigure: { | | |||
130 | configureNotification(notificationProperties.appRealName, notificationProperties.eventId) | | |||
131 | notificationPositioner.closePopup(notificationProperties.source); | | |||
132 | } | 179 | } | ||
133 | onAction: { | | |||
134 | executeAction(notificationProperties.source, actionId) | | |||
135 | actions.clear() | | |||
136 | } | 180 | } | ||
137 | onOpenUrl: { | 181 | | ||
138 | Qt.openUrlExternally(url) | 182 | NotificationItem { | ||
139 | notificationPositioner.closePopup(notificationProperties.source); | 183 | id: notificationItem | ||
184 | // let the item bleed into the dialog margins so the close button margins cancel out | ||||
185 | y: closable || dismissable || configurable ? -notificationPopup.margins.top : 0 | ||||
186 | headingRightPadding: -notificationPopup.margins.right | ||||
187 | width: parent.width | ||||
188 | hovered: area.containsMouse | ||||
189 | maximumLineCount: 8 | ||||
190 | bodyCursorShape: notificationPopup.hasDefaultAction ? Qt.PointingHandCursor : 0 | ||||
191 | | ||||
192 | thumbnailLeftPadding: -notificationPopup.margins.left | ||||
193 | thumbnailRightPadding: -notificationPopup.margins.right | ||||
194 | thumbnailTopPadding: -notificationPopup.margins.top | ||||
195 | thumbnailBottomPadding: -notificationPopup.margins.bottom | ||||
196 | | ||||
197 | closable: true | ||||
198 | onBodyClicked: { | ||||
199 | if (area.acceptedButtons & mouse.button) { | ||||
200 | area.clicked(null /*mouse*/); | ||||
201 | } | ||||
202 | } | ||||
203 | onCloseClicked: notificationPopup.closeClicked() | ||||
204 | onDismissClicked: notificationPopup.dismissClicked() | ||||
205 | onConfigureClicked: notificationPopup.configureClicked() | ||||
206 | onActionInvoked: notificationPopup.actionInvoked(actionName) | ||||
207 | onOpenUrl: notificationPopup.openUrl(url) | ||||
208 | onFileActionInvoked: notificationPopup.fileActionInvoked() | ||||
209 | | ||||
210 | onSuspendJobClicked: notificationPopup.suspendJobClicked() | ||||
211 | onResumeJobClicked: notificationPopup.resumeJobClicked() | ||||
212 | onKillJobClicked: notificationPopup.killJobClicked() | ||||
140 | } | 213 | } | ||
141 | } | 214 | } | ||
142 | } | 215 | } |