styleagent_win.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
  2. // Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
  3. // SPDX-License-Identifier: Apache-2.0
  4. #include "styleagent_p.h"
  5. #include <QtCore/QSet>
  6. #include <QtCore/QVariant>
  7. #include <QtGui/QColor>
  8. #include <core/kernel/nativeeventfilter_p.h>
  9. #include <core/shared/qwkwindowsextra_p.h>
  10. namespace QWK {
  11. using StyleAgentSet = QSet<StyleAgentPrivate *>;
  12. Q_GLOBAL_STATIC(StyleAgentSet, g_styleAgentSet)
  13. static StyleAgent::SystemTheme getSystemTheme() {
  14. if (isHighContrastModeEnabled()) {
  15. return StyleAgent::HighContrast;
  16. } else if (isDarkThemeActive()) {
  17. return StyleAgent::Dark;
  18. } else {
  19. return StyleAgent::Light;
  20. }
  21. }
  22. static void notifyAllStyleAgents() {
  23. auto theme = getSystemTheme();
  24. for (auto &&ap : std::as_const(*g_styleAgentSet())) {
  25. ap->notifyThemeChanged(theme);
  26. }
  27. }
  28. class SystemSettingEventFilter : public AppNativeEventFilter {
  29. public:
  30. bool nativeEventFilter(const QByteArray &eventType, void *message,
  31. QT_NATIVE_EVENT_RESULT_TYPE *result) override {
  32. Q_UNUSED(eventType)
  33. if (!result) {
  34. return false;
  35. }
  36. const auto msg = static_cast<const MSG *>(message);
  37. switch (msg->message) {
  38. case WM_THEMECHANGED:
  39. case WM_SYSCOLORCHANGE:
  40. case WM_DWMCOLORIZATIONCOLORCHANGED: {
  41. notifyAllStyleAgents();
  42. break;
  43. }
  44. case WM_SETTINGCHANGE: {
  45. if (isImmersiveColorSetChange(msg->wParam, msg->lParam)) {
  46. notifyAllStyleAgents();
  47. }
  48. break;
  49. }
  50. default:
  51. break;
  52. }
  53. return false;
  54. }
  55. static inline SystemSettingEventFilter *instance = nullptr;
  56. static inline void install() {
  57. if (instance) {
  58. return;
  59. }
  60. instance = new SystemSettingEventFilter();
  61. }
  62. static inline void uninstall() {
  63. if (!instance) {
  64. return;
  65. }
  66. delete instance;
  67. instance = nullptr;
  68. }
  69. };
  70. void StyleAgentPrivate::setupSystemThemeHook() {
  71. systemTheme = getSystemTheme();
  72. g_styleAgentSet->insert(this);
  73. SystemSettingEventFilter::install();
  74. }
  75. void StyleAgentPrivate::removeSystemThemeHook() {
  76. if (!g_styleAgentSet->remove(this))
  77. return;
  78. if (g_styleAgentSet->isEmpty()) {
  79. SystemSettingEventFilter::uninstall();
  80. }
  81. }
  82. }