1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
Copyright (C) 2014 Martin Klapetek <mklapetek@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "notificationshelper.h"
#include <QGuiApplication>
#include <qfontmetrics.h>
#include <QTimer>
#include <QQuickWindow>
#include <QQuickItem>
#include <QQmlEngine>
#include <QReadWriteLock>
#include <QDebug>
NotificationsHelper::NotificationsHelper(QObject *parent)
: QObject(parent),
m_popupLocation(NotificationsHelper::BottomRight),
m_busy(false)
{
m_mutex = new QReadWriteLock(QReadWriteLock::Recursive);
m_offset = QFontMetrics(QGuiApplication::font()).boundingRect(QStringLiteral("M")).height();
m_dispatchTimer = new QTimer(this);
m_dispatchTimer->setInterval(500);
m_dispatchTimer->setSingleShot(true);
connect(m_dispatchTimer, &QTimer::timeout, [this](){m_busy = false; processQueues();});
}
NotificationsHelper::~NotificationsHelper()
{
qDeleteAll(m_availablePopups);
qDeleteAll(m_popupsOnScreen);
delete m_mutex;
}
void NotificationsHelper::setPopupLocation(PositionOnScreen popupLocation)
{
if (m_popupLocation != popupLocation) {
m_popupLocation = popupLocation;
emit popupLocationChanged();
repositionPopups();
}
}
void NotificationsHelper::setPlasmoidScreenGeometry(const QRect &plasmoidScreenGeometry)
{
m_plasmoidScreen = plasmoidScreenGeometry;
repositionPopups();
}
void NotificationsHelper::addNotificationPopup(QObject *win)
{
QQuickWindow *popup = qobject_cast<QQuickWindow*>(win);
m_availablePopups.append(popup);
// Don't let QML ever delete this component
QQmlEngine::setObjectOwnership(win, QQmlEngine::CppOwnership);
connect(win, SIGNAL(notificationTimeout()),
this, SLOT(onPopupClosed()));
connect(popup, &QWindow::heightChanged, this, &NotificationsHelper::repositionPopups, Qt::UniqueConnection);
connect(popup, &QWindow::visibleChanged, this, &NotificationsHelper::onPopupShown, Qt::UniqueConnection);
popup->setProperty("initialPositionSet", false);
}
void NotificationsHelper::onPopupShown()
{
QWindow *popup = qobject_cast<QWindow*>(sender());
if (!popup || !popup->isVisible()) {
return;
}
// Make sure Dialog lays everything out and gets proper geometry
QMetaObject::invokeMethod(popup, "updateVisibility", Qt::DirectConnection, Q_ARG(bool, true));
// Now we can position the popups properly as the geometry is now known
repositionPopups();
}
void NotificationsHelper::processQueues()
{
if (m_busy) {
return;
}
m_mutex->lockForRead();
bool shouldProcessShow = !m_showQueue.isEmpty() && !m_availablePopups.isEmpty();
m_mutex->unlock();
if (shouldProcessShow) {
m_busy = true;
processShow();
// Return here, makes the movement more clear and easier to follow
return;
}
m_mutex->lockForRead();
bool shouldProcessHide = !m_hideQueue.isEmpty();
m_mutex->unlock();
if (shouldProcessHide) {
m_busy = true;
processHide();
}
}
void NotificationsHelper::processShow()
{
m_mutex->lockForWrite();
const QVariantMap notificationData = m_showQueue.takeFirst();
m_mutex->unlock();
QString sourceName = notificationData.value(QStringLiteral("source")).toString();
// Try getting existing popup for the given source
// (case of notification being just updated)
QQuickWindow *popup = m_sourceMap.value(sourceName);
if (!popup) {
// No existing notification for the given source,
// take one from the available popups
m_mutex->lockForWrite();
popup = m_availablePopups.takeFirst();
m_popupsOnScreen << popup;
m_sourceMap.insert(sourceName, popup);
m_mutex->unlock();
// Set the source name directly on the popup object too
// to avoid looking up the notificationProperties map as above
popup->setProperty("sourceName", sourceName);
}
// Populate the popup with data, this is the component's own QML method
QMetaObject::invokeMethod(popup, "populatePopup", Qt::DirectConnection, Q_ARG(QVariant, notificationData));
QTimer::singleShot(300, popup, &QWindow::show);
if (!m_dispatchTimer->isActive()) {
m_dispatchTimer->start();
}
}
void NotificationsHelper::processHide()
{
m_mutex->lockForWrite();
QQuickWindow *popup = m_hideQueue.takeFirst();
m_mutex->unlock();
if (popup) {
m_mutex->lockForWrite();
// Remove the popup from the active list and return it into the available list
m_popupsOnScreen.removeAll(popup);
m_sourceMap.remove(popup->property("sourceName").toString());
if (!m_availablePopups.contains(popup)) {
// make extra sure that pointers in here aren't doubled
m_availablePopups.append(popup);
}
m_mutex->unlock();
popup->hide();
// Make sure the popup gets placed correctly
// next time it's put on screen
popup->setProperty("initialPositionSet", false);
}
m_mutex->lockForRead();
bool shouldReposition = !m_popupsOnScreen.isEmpty();// && m_showQueue.isEmpty();
m_mutex->unlock();
if (shouldReposition) {
repositionPopups();
}
if (!m_dispatchTimer->isActive()) {
m_dispatchTimer->start();
}
}
void NotificationsHelper::displayNotification(const QVariantMap ¬ificationData)
{
if (notificationData.isEmpty()) {
return;
}
QVariant sourceName = notificationData.value(QStringLiteral("source"));
// first check if we don't already have data for the same source
// which would mean that the notification was just updated
// so remove the old one and append the newest data only
QMutableListIterator<QVariantMap> i(m_showQueue);
while (i.hasNext()) {
if (i.next().value(QStringLiteral("source")) == sourceName) {
m_mutex->lockForWrite();
i.remove();
m_mutex->unlock();
}
}
// ...also look into the hide queue, if it's already queued
// for hiding, we need to remove it from there otherwise
// it will get closed too soon
QMutableListIterator<QQuickWindow*> j(m_hideQueue);
while (j.hasNext()) {
if (j.next()->property("sourceName") == sourceName) {
m_mutex->lockForWrite();
j.remove();
m_mutex->unlock();
}
}
m_mutex->lockForWrite();
m_showQueue.append(notificationData);
m_mutex->unlock();
if (!m_dispatchTimer->isActive()) {
// If the dispatch timer is not already running, process
// the queues directly, that should cut the time between
// notification emitting the event and popup displaying
processQueues();
}
}
void NotificationsHelper::closePopup(const QString &sourceName)
{
QQuickWindow *popup = m_sourceMap.value(sourceName);
m_mutex->lockForRead();
bool shouldQueue = popup && !m_hideQueue.contains(popup);
m_mutex->unlock();
// Make sure the notification that was closed (programatically)
// is not in the show queue. This is important otherwise that
// notification will be shown and then never closed (because
// the close event arrives here, before it's even shown)
QMutableListIterator<QVariantMap> i(m_showQueue);
while (i.hasNext()) {
if (i.next().value(QStringLiteral("source")) == sourceName) {
m_mutex->lockForWrite();
i.remove();
m_mutex->unlock();
}
}
if (shouldQueue) {
m_mutex->lockForWrite();
m_hideQueue.append(popup);
m_mutex->unlock();
if (!m_dispatchTimer->isActive()) {
processQueues();
}
}
}
void NotificationsHelper::onPopupClosed()
{
QQuickWindow *popup = qobject_cast<QQuickWindow*>(sender());
m_mutex->lockForRead();
bool shouldQueue = popup && !m_hideQueue.contains(popup);
m_mutex->unlock();
if (shouldQueue) {
m_mutex->lockForWrite();
m_hideQueue << popup;
m_mutex->unlock();
if (!m_dispatchTimer->isActive()) {
processQueues();
}
}
}
void NotificationsHelper::repositionPopups()
{
int cumulativeHeight = m_offset;
m_mutex->lockForWrite();
for (int i = 0; i < m_popupsOnScreen.size(); ++i) {
if (m_popupLocation == NotificationsHelper::TopLeft
|| m_popupLocation == NotificationsHelper::TopCenter
|| m_popupLocation == NotificationsHelper::TopRight) {
int posY = m_plasmoidScreen.top() + cumulativeHeight;
if (m_popupsOnScreen[i]->isVisible() && m_popupsOnScreen[i]->property("initialPositionSet").toBool() == true && m_popupsOnScreen[i]->y() != 0) {
//if it's visible, go through setProperty which animates it
m_popupsOnScreen[i]->setProperty("y", posY);
} else {
// ...otherwise just set it directly
m_popupsOnScreen[i]->setY(posY);
m_popupsOnScreen[i]->setProperty("initialPositionSet", true);
}
} else {
int posY = m_plasmoidScreen.bottom() - cumulativeHeight - m_popupsOnScreen[i]->contentItem()->height();
if (m_popupsOnScreen[i]->isVisible() && m_popupsOnScreen[i]->property("initialPositionSet").toBool() == true && m_popupsOnScreen[i]->y() != 0) {
m_popupsOnScreen[i]->setProperty("y", posY);
} else {
m_popupsOnScreen[i]->setY(posY);
m_popupsOnScreen[i]->setProperty("initialPositionSet", true);
}
}
switch (m_popupLocation) {
case Default:
//This should not happen as the defualt handling is in NotificationApplet::onScreenPositionChanged
Q_ASSERT(false);
qWarning("Notication popupLocation is still \"default\". This should not happen");
//fall through to top right
case TopRight:
case BottomRight:
m_popupsOnScreen[i]->setX(m_plasmoidScreen.right() - m_popupsOnScreen[i]->contentItem()->width() - m_offset);
break;
case TopCenter:
case BottomCenter:
m_popupsOnScreen[i]->setX(m_plasmoidScreen.x() + (m_plasmoidScreen.width() / 2) - (m_popupsOnScreen[i]->contentItem()->width() / 2));
break;
case TopLeft:
case BottomLeft:
m_popupsOnScreen[i]->setX(m_plasmoidScreen.left() + m_offset);
break;
case Left:
case Center:
case Right:
// Fall-through to make the compiler happy
break;
}
cumulativeHeight += (m_popupsOnScreen[i]->contentItem()->height() + m_offset);
}
m_mutex->unlock();
}
|