aboutsummaryrefslogtreecommitdiff
path: root/src/notificationpopup.h
blob: ebb587d31a266c4d4bfb5f333a2e4c01b0f14b7c (plain) (blame)
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
#ifndef NOTIFICATIONPOPUP_H
#define NOTIFICATIONPOPUP_H

#pragma once

#include "widgets/scrolltext/scrolltext.h"
#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QSettings>
#include <QSpacerItem>
#include <QTimer>
#include <QVBoxLayout>
#include <QWebEngineNotification>

#include <memory>

class NotificationPopup : public QWidget {
  Q_OBJECT

  QLabel m_icon, m_title;
  ScrollText m_message;
  std::unique_ptr<QWebEngineNotification> notification;
  QSettings settings;

public:
  NotificationPopup(QWidget *parent) : QWidget(parent) {
    setWindowFlags(Qt::ToolTip);
    auto rootLayout = new QHBoxLayout(this);

    rootLayout->addWidget(&m_icon);

    auto bodyLayout = new QVBoxLayout;
    rootLayout->addLayout(bodyLayout);

    auto titleLayout = new QHBoxLayout;
    bodyLayout->addLayout(titleLayout);

    titleLayout->addWidget(&m_title);
    titleLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));

    auto close = new QPushButton(tr("Close"));
    titleLayout->addWidget(close);
    connect(close, &QPushButton::clicked, this, &NotificationPopup::onClosed);

    bodyLayout->addWidget(&m_message);
    adjustSize();
  }

  void present(QString title, QString message, const QPixmap image) {
    m_title.setText("<b>" + title + "</b>");
    m_message.setText(message);
    m_icon.setPixmap(
        image.scaledToHeight(m_icon.height(), Qt::SmoothTransformation));

    this->adjustSize();
    qApp->processEvents();

    int x = QApplication::desktop()->geometry().width() - (this->width() + 10);
    int y = 40;

    this->update();

    QTimer::singleShot(settings.value("notificationTimeOut", 9000).toInt(),
                       this, [=]() { onClosed(); });

    QPropertyAnimation *a = new QPropertyAnimation(this, "pos");
    a->setDuration(200);
    a->setStartValue(
        QApplication::desktop()->mapToGlobal(QPoint(x + this->width(), y)));
    a->setEndValue(QApplication::desktop()->mapToGlobal(QPoint(x, y)));
    a->setEasingCurve(QEasingCurve::Linear);
    a->start(QPropertyAnimation::DeleteWhenStopped);

    this->show();
  }

  void present(std::unique_ptr<QWebEngineNotification> &newNotification) {
    if (notification) {
      notification->close();
      notification.reset();
    }

    notification.swap(newNotification);

    m_title.setText("<b>" + notification->title() + "</b>");
    m_message.setText(notification->message());
    m_icon.setPixmap(QPixmap::fromImage(notification->icon())
                         .scaledToHeight(m_icon.height()));

    notification->show();

    connect(notification.get(), &QWebEngineNotification::closed, this,
            &NotificationPopup::onClosed);
    QTimer::singleShot(settings.value("notificationTimeOut", 9000).toInt(),
                       notification.get(), [&]() { onClosed(); });

    this->adjustSize();
    qApp->processEvents();

    int x = QApplication::desktop()->geometry().width() - (this->width() + 10);
    int y = 40;

    this->update();

    QPropertyAnimation *a = new QPropertyAnimation(this, "pos");
    a->setDuration(200);
    a->setStartValue(
        QApplication::desktop()->mapToGlobal(QPoint(x + this->width(), y)));
    a->setEndValue(QApplication::desktop()->mapToGlobal(QPoint(x, y)));
    a->setEasingCurve(QEasingCurve::Linear);
    a->start(QPropertyAnimation::DeleteWhenStopped);
    this->show();
  }

protected slots:
  void onClosed() {
    hide();
    if (notification) {
      notification->close();
      notification.reset();
    } else {
      this->deleteLater();
    }
  }

protected:
  void mouseReleaseEvent(QMouseEvent *event) override {
    QWidget::mouseReleaseEvent(event);
    if (event->button() == Qt::LeftButton) {
      qDebug() << "noti clicked";
      emit notification_clicked();
      if (notification)
        notification->click();
      onClosed();
    }
  }
signals:
  void notification_clicked();
};

#endif // NOTIFICATIONPOPUP_H