From b8a3ff604ff01f50c067d37965152baf30ef7733 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 10 Nov 2022 11:17:29 +0800 Subject: [PATCH] win: minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- CMakeLists.txt | 2 +- .../Core/framelesshelper_windows.h | 8 +-- include/FramelessHelper/Core/utils.h | 1 + qmake/inc/core/framelesshelper.version | 4 +- src/core/framelesshelper_win.cpp | 9 ++-- src/core/framelesshelpercore_global.cpp | 5 +- src/core/framelessmanager.cpp | 18 ++++++- src/core/utils_win.cpp | 54 +++++++------------ 8 files changed, 51 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60e88ade..962a3b88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ cmake_minimum_required(VERSION 3.20) project(FramelessHelper - VERSION 2.3.1.0 + VERSION 2.3.2.0 DESCRIPTION "Cross-platform window customization framework for Qt Widgets and Qt Quick." HOMEPAGE_URL "https://github.com/wangwenx190/framelesshelper/" LANGUAGES CXX diff --git a/include/FramelessHelper/Core/framelesshelper_windows.h b/include/FramelessHelper/Core/framelesshelper_windows.h index d1926456..0db2c799 100644 --- a/include/FramelessHelper/Core/framelesshelper_windows.h +++ b/include/FramelessHelper/Core/framelesshelper_windows.h @@ -410,10 +410,10 @@ using IMMERSIVE_HC_CACHE_MODE = enum IMMERSIVE_HC_CACHE_MODE using PREFERRED_APP_MODE = enum PREFERRED_APP_MODE { - PAM_DEFAULT = 0, - PAM_ALLOW_DARK = 1, - PAM_FORCE_DARK = 2, - PAM_FORCE_LIGHT = 3, + PAM_DEFAULT = 0, // Use default behavior. + PAM_AUTO = 1, // Let system decide. + PAM_DARK = 2, // Force dark mode. + PAM_LIGHT = 3, // Force light mode. PAM_MAX = 4 }; diff --git a/include/FramelessHelper/Core/utils.h b/include/FramelessHelper/Core/utils.h index a7aad0a0..4deb69ea 100644 --- a/include/FramelessHelper/Core/utils.h +++ b/include/FramelessHelper/Core/utils.h @@ -120,6 +120,7 @@ FRAMELESSHELPER_CORE_API void enableNonClientAreaDpiScalingForWindow(const WId w Global::DpiAwareness getDpiAwarenessForCurrentProcess(bool *highest = nullptr); FRAMELESSHELPER_CORE_API void fixupChildWindowsDpiMessage(const WId windowId); FRAMELESSHELPER_CORE_API void fixupDialogsDpiScaling(); +FRAMELESSHELPER_CORE_API void setDarkModeEnabledForApp(const bool enable = true); #endif // Q_OS_WINDOWS #ifdef Q_OS_LINUX diff --git a/qmake/inc/core/framelesshelper.version b/qmake/inc/core/framelesshelper.version index 2e91eabd..4012e5d8 100644 --- a/qmake/inc/core/framelesshelper.version +++ b/qmake/inc/core/framelesshelper.version @@ -35,9 +35,9 @@ [[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_MAJOR = 2; [[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_MINOR = 3; -[[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_PATCH = 1; +[[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_PATCH = 2; [[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_TWEAK = 0; -[[maybe_unused]] inline constexpr const char FRAMELESSHELPER_VERSION_STR[] = "2.3.1.0\0"; +[[maybe_unused]] inline constexpr const char FRAMELESSHELPER_VERSION_STR[] = "2.3.2.0\0"; [[maybe_unused]] inline constexpr const char FRAMELESSHELPER_COMMIT_STR[] = "UNKNOWN\0"; [[maybe_unused]] inline constexpr const char FRAMELESSHELPER_COMPILE_DATETIME_STR[] = "UNKNOWN\0"; diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 1dc9ab66..e9282773 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -521,9 +521,8 @@ void FramelessHelperWin::addWindow(const SystemParameters ¶ms) qApp->installNativeEventFilter(g_win32Helper()->nativeEventFilter.data()); } g_win32Helper()->mutex.unlock(); - DEBUG.noquote() << "The DPI of window" << hwnd2str(windowId) << "is: QDpi(" - << Utils::getWindowDpi(windowId, true) << ',' - << Utils::getWindowDpi(windowId, false) << ")."; + DEBUG.nospace().noquote() << "The DPI of window " << hwnd2str(windowId) << " is: QDpi(" + << Utils::getWindowDpi(windowId, true) << ", " << Utils::getWindowDpi(windowId, false) << ")."; // Some Qt internals have to be corrected. Utils::maybeFixupQtInternals(windowId); // Qt maintains a frame margin internally, we need to update it accordingly @@ -1118,8 +1117,8 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me case WM_DPICHANGED: { const UINT dpiX = LOWORD(wParam); const UINT dpiY = HIWORD(wParam); - DEBUG.noquote() << "New DPI for window" << hwnd2str(hWnd) - << ": QDpi(" << dpiX << ',' << dpiY << ")."; + DEBUG.nospace().noquote() << "New DPI for window " + << hwnd2str(hWnd) << ": QDpi(" << dpiX << ", " << dpiY << ")."; // Sync the internal window frame margins with the latest DPI. Utils::updateInternalWindowFrameMargins(data.params.getWindowHandle(), true); // For some unknown reason, Qt sometimes won't re-paint the window contents after diff --git a/src/core/framelesshelpercore_global.cpp b/src/core/framelesshelpercore_global.cpp index 559008ae..f7c71a92 100644 --- a/src/core/framelesshelpercore_global.cpp +++ b/src/core/framelesshelpercore_global.cpp @@ -339,8 +339,11 @@ void setApplicationOSThemeAware() } set = true; -#if (defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))) +#ifdef Q_OS_WINDOWS + Utils::setDarkModeEnabledForApp(true); +# if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) Utils::setQtDarkModeAwareEnabled(true); +# endif #endif #if ((defined(Q_OS_LINUX) && (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))) || \ diff --git a/src/core/framelessmanager.cpp b/src/core/framelessmanager.cpp index 1515982a..d88675b1 100644 --- a/src/core/framelessmanager.cpp +++ b/src/core/framelessmanager.cpp @@ -297,7 +297,12 @@ void FramelessManagerPrivate::notifySystemThemeHasChangedOrNot() if (notify) { Q_Q(FramelessManager); Q_EMIT q->systemThemeChanged(); - DEBUG << "Detected system theme changed."; + DEBUG.nospace() << "System theme changed. Current theme: " << m_systemTheme + << ", accent color: " << m_accentColor.name(QColor::HexArgb).toUpper() +#ifdef Q_OS_WINDOWS + << ", colorization area: " << m_colorizationArea +#endif + << '.'; } } @@ -318,7 +323,8 @@ void FramelessManagerPrivate::notifyWallpaperHasChangedOrNot() if (notify) { Q_Q(FramelessManager); Q_EMIT q->wallpaperChanged(); - DEBUG << "Detected wallpaper changed."; + DEBUG.nospace() << "Wallpaper changed. Current wallpaper: " << m_wallpaper + << ", aspect style: " << m_wallpaperAspectStyle << '.'; } } @@ -350,6 +356,14 @@ void FramelessManagerPrivate::initialize() #endif m_wallpaper = Utils::getWallpaperFilePath(); m_wallpaperAspectStyle = Utils::getWallpaperAspectStyle(); + DEBUG.nospace() << "Current system theme: " << m_systemTheme + << ", accent color: " << m_accentColor.name(QColor::HexArgb).toUpper() +#ifdef Q_OS_WINDOWS + << ", colorization area: " << m_colorizationArea +#endif + << ", wallpaper: " << m_wallpaper + << ", aspect style: " << m_wallpaperAspectStyle + << '.'; #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) QStyleHints * const styleHints = QGuiApplication::styleHints(); Q_ASSERT(styleHints); diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 3f4ba0d0..5437b3ae 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -2375,7 +2375,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark) const auto hWnd = reinterpret_cast(windowId); const DWORD borderFlag = (WindowsVersionHelper::isWin1020H1OrGreater() ? _DWMWA_USE_IMMERSIVE_DARK_MODE : _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1); - const PREFERRED_APP_MODE appMode = (dark ? PAM_ALLOW_DARK : PAM_DEFAULT); const BOOL darkFlag = (dark ? TRUE : FALSE); WINDOWCOMPOSITIONATTRIBDATA wcad; SecureZeroMemory(&wcad, sizeof(wcad)); @@ -2383,15 +2382,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark) wcad.pvData = const_cast(&darkFlag); wcad.cbData = sizeof(darkFlag); if (dark) { - if (WindowsVersionHelper::isWin1019H1OrGreater()) { - if (SetPreferredAppMode(appMode) == PAM_MAX) { - WARNING << getSystemErrorMessage(kSetPreferredAppMode); - } - } else { - if (AllowDarkModeForApp(darkFlag) == FALSE) { - WARNING << getSystemErrorMessage(kAllowDarkModeForApp); - } - } if (AllowDarkModeForWindow(hWnd, darkFlag) == FALSE) { WARNING << getSystemErrorMessage(kAllowDarkModeForWindow); } @@ -2419,11 +2409,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark) if (GetLastError() != ERROR_SUCCESS) { WARNING << getSystemErrorMessage(kRefreshImmersiveColorPolicyState); } - SetLastError(ERROR_SUCCESS); - Q_UNUSED(GetIsImmersiveColorUsingHighContrast(IHCM_REFRESH)); - if (GetLastError() != ERROR_SUCCESS) { - WARNING << getSystemErrorMessage(kGetIsImmersiveColorUsingHighContrast); - } } else { if (AllowDarkModeForWindow(hWnd, darkFlag) == FALSE) { WARNING << getSystemErrorMessage(kAllowDarkModeForWindow); @@ -2452,20 +2437,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark) if (GetLastError() != ERROR_SUCCESS) { WARNING << getSystemErrorMessage(kRefreshImmersiveColorPolicyState); } - SetLastError(ERROR_SUCCESS); - Q_UNUSED(GetIsImmersiveColorUsingHighContrast(IHCM_REFRESH)); - if (GetLastError() != ERROR_SUCCESS) { - WARNING << getSystemErrorMessage(kGetIsImmersiveColorUsingHighContrast); - } - if (WindowsVersionHelper::isWin1019H1OrGreater()) { - if (SetPreferredAppMode(appMode) == PAM_MAX) { - WARNING << getSystemErrorMessage(kSetPreferredAppMode); - } - } else { - if (AllowDarkModeForApp(darkFlag) == FALSE) { - WARNING << getSystemErrorMessage(kAllowDarkModeForApp); - } - } } } @@ -2601,10 +2572,10 @@ void Utils::fixupChildWindowsDpiMessage(const WId windowId) return; } // This hack is only available on Windows 10 and newer, and starting from - // Win10 1607 it become useless due to the PMv2 DPI awareness mode already - // takes care of it for us. + // Win10 build 14986 it become useless due to the PMv2 DPI awareness mode + // already takes care of it for us. if (!WindowsVersionHelper::isWin10OrGreater() - || (WindowsVersionHelper::isWin10RS1OrGreater() + || (WindowsVersionHelper::isWin10RS2OrGreater() && (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) { return; } @@ -2622,10 +2593,10 @@ void Utils::fixupChildWindowsDpiMessage(const WId windowId) void Utils::fixupDialogsDpiScaling() { // This hack is only available on Windows 10 and newer, and starting from - // Win10 1607 it become useless due to the PMv2 DPI awareness mode already - // takes care of it for us. + // Win10 build 14986 it become useless due to the PMv2 DPI awareness mode + // already takes care of it for us. if (!WindowsVersionHelper::isWin10OrGreater() - || (WindowsVersionHelper::isWin10RS1OrGreater() + || (WindowsVersionHelper::isWin10RS2OrGreater() && (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) { return; } @@ -2639,4 +2610,17 @@ void Utils::fixupDialogsDpiScaling() WARNING << getSystemErrorMessage(kEnablePerMonitorDialogScaling); } +void Utils::setDarkModeEnabledForApp(const bool enable) +{ + if (WindowsVersionHelper::isWin1019H1OrGreater()) { + if (SetPreferredAppMode(enable ? PAM_AUTO : PAM_DEFAULT) == PAM_MAX) { + WARNING << getSystemErrorMessage(kSetPreferredAppMode); + } + } else if (WindowsVersionHelper::isWin10RS5OrGreater()) { + if (AllowDarkModeForApp(enable ? TRUE : FALSE) == FALSE) { + WARNING << getSystemErrorMessage(kAllowDarkModeForApp); + } + } +} + FRAMELESSHELPER_END_NAMESPACE