Sine Striker
2023-12-21 77f10f7c5eb8716c06eb7e0447c3fd8dbcb48d79
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
#include "styleagent.h"
#include "styleagent_p.h"
 
#include <QtCore/QVariant>
 
namespace QWK {
 
    StyleAgentPrivate::StyleAgentPrivate() {
    }
 
    StyleAgentPrivate::~StyleAgentPrivate() = default;
 
    void StyleAgentPrivate::init() {
    }
 
    void StyleAgentPrivate::notifyThemeChanged(StyleAgent::SystemTheme theme) {
        if (theme == systemTheme)
            return;
        systemTheme = theme;
 
        Q_Q(StyleAgent);
        Q_EMIT q->systemThemeChanged();
    }
 
    void StyleAgentPrivate::_q_windowDestroyed() {
        windowAttributes.remove(static_cast<QWindow *>(sender()));
    }
 
    StyleAgent::StyleAgent(QObject *parent) : StyleAgent(*new StyleAgentPrivate(), parent) {
        Q_D(StyleAgent);
        d->setupSystemThemeHook();
    }
 
    StyleAgent::~StyleAgent() {
        Q_D(StyleAgent);
        d->removeSystemThemeHook();
    }
 
    StyleAgent::SystemTheme StyleAgent::systemTheme() const {
        Q_D(const StyleAgent);
        return d->systemTheme;
    }
 
    QVariant StyleAgent::windowAttribute(QWindow *window, const QString &key) const {
        Q_D(const StyleAgent);
        return d->windowAttributes.value(window).value(key);
    }
 
    bool StyleAgent::setWindowAttribute(QWindow *window, const QString &key,
                                        const QVariant &attribute) {
        Q_D(StyleAgent);
        if (!window)
            return false;
 
        auto it = d->windowAttributes.find(window);
        if (it == d->windowAttributes.end()) {
            if (!attribute.isValid())
                return true;
            if (!d->updateWindowAttribute(window, key, attribute, {}))
                return false;
            connect(window, &QWindow::destroyed, d, &StyleAgentPrivate::_q_windowDestroyed);
            d->windowAttributes.insert(window, QVariantHash{
                                                   {key, attribute}
            });
        } else {
            auto &attributes = it.value();
            auto oldAttribute = attributes.value(key);
            if (oldAttribute == attribute)
                return true;
            if (!d->updateWindowAttribute(window, key, attribute, oldAttribute))
                return false;
            attributes.insert(key, attribute);
        }
        return true;
    }
 
    StyleAgent::StyleAgent(StyleAgentPrivate &d, QObject *parent) : QObject(parent), d_ptr(&d) {
        d.q_ptr = this;
 
        d.init();
    }
 
}