Zhao Yuhang
2023-12-22 61cb997e4e9ebf50bcff835841fbd1ecc9499e49
add themechange notify on win
6个文件已修改
80 ■■■■ 已修改文件
examples/mainwindow/main.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/qml/main.cpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/shared/qwkwindowsextra_p.h 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/style/styleagent.cpp 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/style/styleagent_p.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/style/styleagent_win.cpp 64 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/main.cpp
@@ -3,7 +3,7 @@
#include "mainwindow.h"
int main(int argc, char *argv[]) {
    qputenv("QT_WIN_DEBUG_CONSOLE", "1");
    qputenv("QT_WIN_DEBUG_CONSOLE", "attach");
    qputenv("QSG_INFO", "1");
#if 0
    qputenv("QT_WIDGETS_RHI", "1");
examples/qml/main.cpp
@@ -4,7 +4,7 @@
#include <QWKQuick/qwkquickglobal.h>
int main(int argc, char *argv[]) {
    qputenv("QT_WIN_DEBUG_CONSOLE", "1");
    qputenv("QT_WIN_DEBUG_CONSOLE", "attach");
    qputenv("QSG_INFO", "1");
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    qputenv("QT_QUICK_CONTROLS_STYLE", "Basic");
src/core/shared/qwkwindowsextra_p.h
@@ -327,6 +327,13 @@
        return value.first;
    }
    static inline bool isHighContrastModeEnabled() {
        HIGHCONTRASTW hc{};
        hc.cbSize = sizeof(hc);
        ::SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, FALSE);
        return (hc.dwFlags & HCF_HIGHCONTRASTON);
    }
    static inline bool isDarkThemeActive() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
        return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark;
src/core/style/styleagent.cpp
@@ -10,9 +10,6 @@
    StyleAgentPrivate::~StyleAgentPrivate() = default;
    void StyleAgentPrivate::init() {
    }
    void StyleAgentPrivate::notifyThemeChanged(StyleAgent::SystemTheme theme) {
        if (theme == systemTheme)
            return;
src/core/style/styleagent_p.h
@@ -26,7 +26,7 @@
        StyleAgent *q_ptr;
        StyleAgent::SystemTheme systemTheme = StyleAgent::Dark;
        StyleAgent::SystemTheme systemTheme = StyleAgent::Unknown;
        virtual void setupSystemThemeHook();
        virtual void removeSystemThemeHook();
src/core/style/styleagent_win.cpp
@@ -21,27 +21,47 @@
                return false;
            }
            const auto msg = static_cast<const MSG *>(message);
            switch (msg->message) {
                case WM_THEMECHANGED:
                case WM_SYSCOLORCHANGE:
                case WM_DWMCOLORIZATIONCOLORCHANGED: {
                    // TODO: walk through `g_styleAgentSet`
                    break;
                }
            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") ==
                    case WM_SETTINGCHANGE: {
                        if (!msg->wParam && msg->lParam &&
                            std::wcscmp(reinterpret_cast<LPCWSTR>(msg->lParam), L"ImmersiveColorSet") ==
                            0) {
                        // TODO: walk through `g_styleAgentSet`
                            return true;
                        }
                        break;
                    }
                    break;
                }
                default:
                    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;
        }
@@ -65,11 +85,19 @@
    SystemSettingEventFilter *SystemSettingEventFilter::instance = nullptr;
    void StyleAgentPrivate::init() {
        if (isHighContrastModeEnabled()) {
            systemTheme = StyleAgent::HighContrast;
        } else if (isDarkThemeActive()) {
            systemTheme = StyleAgent::Dark;
        } else {
            systemTheme = StyleAgent::Light;
        }
    }
    void StyleAgentPrivate::setupSystemThemeHook() {
        g_styleAgentSet->insert(this);
        SystemSettingEventFilter::install();
        // Initialize `systemTheme` variable
    }
    void StyleAgentPrivate::removeSystemThemeHook() {