From 2d2fc799bc698ebf7e7c8bcc394366d0d7bf071b Mon Sep 17 00:00:00 2001 From: Sine Striker <trueful@163.com> Date: 周二, 20 2月 2024 18:18:39 +0800 Subject: [PATCH] Partially finish the bug after window close and reshow --- src/core/style/styleagent_win.cpp | 80 ++++++++++++++++++--------------------- 1 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/core/style/styleagent_win.cpp b/src/core/style/styleagent_win.cpp index c4e4e96..8adf936 100644 --- a/src/core/style/styleagent_win.cpp +++ b/src/core/style/styleagent_win.cpp @@ -1,3 +1,7 @@ +// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware) +// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao) +// SPDX-License-Identifier: Apache-2.0 + #include "styleagent_p.h" #include <QtCore/QSet> @@ -12,6 +16,23 @@ using StyleAgentSet = QSet<StyleAgentPrivate *>; Q_GLOBAL_STATIC(StyleAgentSet, g_styleAgentSet) + static StyleAgent::SystemTheme getSystemTheme() { + if (isHighContrastModeEnabled()) { + return StyleAgent::HighContrast; + } else if (isDarkThemeActive()) { + return StyleAgent::Dark; + } else { + return StyleAgent::Light; + } + } + + static void notifyAllStyleAgents() { + auto theme = getSystemTheme(); + for (auto &&ap : std::as_const(*g_styleAgentSet())) { + ap->notifyThemeChanged(theme); + } + } + class SystemSettingEventFilter : public AppNativeEventFilter { public: bool nativeEventFilter(const QByteArray &eventType, void *message, @@ -21,48 +42,29 @@ 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; + const auto msg = static_cast<const MSG *>(message); + switch (msg->message) { + case WM_THEMECHANGED: + case WM_SYSCOLORCHANGE: + case WM_DWMCOLORIZATIONCOLORCHANGED: { + notifyAllStyleAgents(); + break; } - return false; - }(); - if (themeChanged) { - auto theme = []() -> StyleAgent::SystemTheme { - if (isHighContrastModeEnabled()) { - return StyleAgent::HighContrast; - } else if (isDarkThemeActive()) { - return StyleAgent::Dark; - } else { - return StyleAgent::Light; + case WM_SETTINGCHANGE: { + if (isImmersiveColorSetChange(msg->wParam, msg->lParam)) { + notifyAllStyleAgents(); } - }(); - for (auto &&ap : std::as_const(*g_styleAgentSet())) { - ap->notifyThemeChanged(theme); + break; } + + default: + break; } - return false; } - static SystemSettingEventFilter *instance; + static inline SystemSettingEventFilter *instance = nullptr; static inline void install() { if (instance) { @@ -80,16 +82,8 @@ } }; - SystemSettingEventFilter *SystemSettingEventFilter::instance = nullptr; - void StyleAgentPrivate::setupSystemThemeHook() { - if (isHighContrastModeEnabled()) { - systemTheme = StyleAgent::HighContrast; - } else if (isDarkThemeActive()) { - systemTheme = StyleAgent::Dark; - } else { - systemTheme = StyleAgent::Light; - } + systemTheme = getSystemTheme(); g_styleAgentSet->insert(this); SystemSettingEventFilter::install(); -- Gitblit v1.9.1