summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/toaster.hpp
blob: 22da2122b1a00e19d7a11f1ab93ca85c208ce662 (plain)
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
#pragma once

#include <qobject.h>
#include <qqmlintegration.h>
#include <qset.h>

namespace caelestia {

class Toast : public QObject {
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("Toast instances can only be retrieved from a Toaster")

    Q_PROPERTY(bool closed READ closed NOTIFY closedChanged)
    Q_PROPERTY(QString title READ title CONSTANT)
    Q_PROPERTY(QString message READ message CONSTANT)
    Q_PROPERTY(QString icon READ icon CONSTANT)
    Q_PROPERTY(int timeout READ timeout CONSTANT)
    Q_PROPERTY(Type type READ type CONSTANT)

public:
    enum class Type {
        Info = 0,
        Success,
        Warning,
        Error
    };
    Q_ENUM(Type)

    explicit Toast(const QString& title, const QString& message, const QString& icon, Type type, int timeout,
        QObject* parent = nullptr);

    [[nodiscard]] bool closed() const;
    [[nodiscard]] QString title() const;
    [[nodiscard]] QString message() const;
    [[nodiscard]] QString icon() const;
    [[nodiscard]] int timeout() const;
    [[nodiscard]] Type type() const;

    Q_INVOKABLE void close();
    Q_INVOKABLE void lock(QObject* sender);
    Q_INVOKABLE void unlock(QObject* sender);

signals:
    void closedChanged();
    void finishedClose();

private:
    QSet<QObject*> m_locks;

    bool m_closed;
    QString m_title;
    QString m_message;
    QString m_icon;
    Type m_type;
    int m_timeout;
};

class Toaster : public QObject {
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON

    Q_PROPERTY(QList<Toast*> toasts READ toasts NOTIFY toastsChanged)

public:
    explicit Toaster(QObject* parent = nullptr);

    [[nodiscard]] QList<Toast*> toasts() const;

    Q_INVOKABLE void toast(const QString& title, const QString& message, const QString& icon = QString(),
        Toast::Type type = Toast::Type::Info, int timeout = 5000);

signals:
    void toastsChanged();

private:
    QList<Toast*> m_toasts;
};

} // namespace caelestia