Zhao Yuhang
2023-12-22 adb46e1e151b73e56a7e0066a896b48b92a7230e
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
#include "styleagent_p.h"
 
#include <QtCore/QSet>
#include <QtCore/QVariant>
#include <QtGui/QColor>
 
#include <QWKCore/private/qwkwindowsextra_p.h>
#include <QWKCore/private/nativeeventfilter_p.h>
 
namespace QWK {
 
    using StyleAgentSet = QSet<StyleAgentPrivate *>;
    Q_GLOBAL_STATIC(StyleAgentSet, g_styleAgentSet)
 
    class SystemSettingEventFilter : public AppNativeEventFilter {
    public:
        bool nativeEventFilter(const QByteArray &eventType, void *message,
                               QT_NATIVE_EVENT_RESULT_TYPE *result) override {
            Q_UNUSED(eventType)
            if (!result) {
                return false;
            }
 
            auto themeChanged = [message]() -> bool {
                const auto msg = static_cast<const MSG *>(message);
                switch (msg->message) {
                    case WM_THEMECHANGED:
                    case WM_SYSCOLORCHANGE:
                    case WM_DWMCOLORIZATIONCOLORCHANGED:
                        return true;
 
                    case WM_SETTINGCHANGE: {
                        if (!msg->wParam && msg->lParam &&
                            std::wcscmp(reinterpret_cast<LPCWSTR>(msg->lParam), L"ImmersiveColorSet") ==
                            0) {
                            return true;
                        }
                        break;
                    }
 
                    default:
                        break;
                }
                return false;
            }();
 
            if (themeChanged) {
                auto theme = []() -> StyleAgent::SystemTheme {
                    if (isHighContrastModeEnabled()) {
                        return StyleAgent::HighContrast;
                    } else if (isDarkThemeActive()) {
                        return StyleAgent::Dark;
                    } else {
                        return StyleAgent::Light;
                    }
                }();
                for (auto &&ap : std::as_const(*g_styleAgentSet())) {
                    if (ap->systemTheme != theme) {
                        ap->systemTheme = theme;
                        ap->notifyThemeChanged(theme);
                    }
                }
            }
 
            return false;
        }
 
        static SystemSettingEventFilter *instance;
 
        static inline void install() {
            if (instance) {
                return;
            }
            instance = new SystemSettingEventFilter();
        }
 
        static inline void uninstall() {
            if (!instance) {
                return;
            }
            delete instance;
            instance = nullptr;
        }
    };
 
    SystemSettingEventFilter *SystemSettingEventFilter::instance = nullptr;
 
    void StyleAgentPrivate::setupSystemThemeHook() {
        if (isHighContrastModeEnabled()) {
            systemTheme = StyleAgent::HighContrast;
        } else if (isDarkThemeActive()) {
            systemTheme = StyleAgent::Dark;
        } else {
            systemTheme = StyleAgent::Light;
        }
 
        g_styleAgentSet->insert(this);
        SystemSettingEventFilter::install();
    }
 
    void StyleAgentPrivate::removeSystemThemeHook() {
        if (!g_styleAgentSet->remove(this))
            return;
 
        if (g_styleAgentSet->isEmpty()) {
            SystemSettingEventFilter::uninstall();
        }
    }
 
}