win: minor tweaks

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-11-10 11:17:29 +08:00
parent 3842f4e353
commit b8a3ff604f
8 changed files with 51 additions and 50 deletions

View File

@ -25,7 +25,7 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
project(FramelessHelper project(FramelessHelper
VERSION 2.3.1.0 VERSION 2.3.2.0
DESCRIPTION "Cross-platform window customization framework for Qt Widgets and Qt Quick." DESCRIPTION "Cross-platform window customization framework for Qt Widgets and Qt Quick."
HOMEPAGE_URL "https://github.com/wangwenx190/framelesshelper/" HOMEPAGE_URL "https://github.com/wangwenx190/framelesshelper/"
LANGUAGES CXX LANGUAGES CXX

View File

@ -410,10 +410,10 @@ using IMMERSIVE_HC_CACHE_MODE = enum IMMERSIVE_HC_CACHE_MODE
using PREFERRED_APP_MODE = enum PREFERRED_APP_MODE using PREFERRED_APP_MODE = enum PREFERRED_APP_MODE
{ {
PAM_DEFAULT = 0, PAM_DEFAULT = 0, // Use default behavior.
PAM_ALLOW_DARK = 1, PAM_AUTO = 1, // Let system decide.
PAM_FORCE_DARK = 2, PAM_DARK = 2, // Force dark mode.
PAM_FORCE_LIGHT = 3, PAM_LIGHT = 3, // Force light mode.
PAM_MAX = 4 PAM_MAX = 4
}; };

View File

@ -120,6 +120,7 @@ FRAMELESSHELPER_CORE_API void enableNonClientAreaDpiScalingForWindow(const WId w
Global::DpiAwareness getDpiAwarenessForCurrentProcess(bool *highest = nullptr); Global::DpiAwareness getDpiAwarenessForCurrentProcess(bool *highest = nullptr);
FRAMELESSHELPER_CORE_API void fixupChildWindowsDpiMessage(const WId windowId); FRAMELESSHELPER_CORE_API void fixupChildWindowsDpiMessage(const WId windowId);
FRAMELESSHELPER_CORE_API void fixupDialogsDpiScaling(); FRAMELESSHELPER_CORE_API void fixupDialogsDpiScaling();
FRAMELESSHELPER_CORE_API void setDarkModeEnabledForApp(const bool enable = true);
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX

View File

@ -35,9 +35,9 @@
[[maybe_unused]] inline constexpr const int FRAMELESSHELPER_VERSION_MAJOR = 2; [[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_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 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_COMMIT_STR[] = "UNKNOWN\0";
[[maybe_unused]] inline constexpr const char FRAMELESSHELPER_COMPILE_DATETIME_STR[] = "UNKNOWN\0"; [[maybe_unused]] inline constexpr const char FRAMELESSHELPER_COMPILE_DATETIME_STR[] = "UNKNOWN\0";

View File

@ -521,9 +521,8 @@ void FramelessHelperWin::addWindow(const SystemParameters &params)
qApp->installNativeEventFilter(g_win32Helper()->nativeEventFilter.data()); qApp->installNativeEventFilter(g_win32Helper()->nativeEventFilter.data());
} }
g_win32Helper()->mutex.unlock(); g_win32Helper()->mutex.unlock();
DEBUG.noquote() << "The DPI of window" << hwnd2str(windowId) << "is: QDpi(" DEBUG.nospace().noquote() << "The DPI of window " << hwnd2str(windowId) << " is: QDpi("
<< Utils::getWindowDpi(windowId, true) << ',' << Utils::getWindowDpi(windowId, true) << ", " << Utils::getWindowDpi(windowId, false) << ").";
<< Utils::getWindowDpi(windowId, false) << ").";
// Some Qt internals have to be corrected. // Some Qt internals have to be corrected.
Utils::maybeFixupQtInternals(windowId); Utils::maybeFixupQtInternals(windowId);
// Qt maintains a frame margin internally, we need to update it accordingly // 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: { case WM_DPICHANGED: {
const UINT dpiX = LOWORD(wParam); const UINT dpiX = LOWORD(wParam);
const UINT dpiY = HIWORD(wParam); const UINT dpiY = HIWORD(wParam);
DEBUG.noquote() << "New DPI for window" << hwnd2str(hWnd) DEBUG.nospace().noquote() << "New DPI for window "
<< ": QDpi(" << dpiX << ',' << dpiY << ")."; << hwnd2str(hWnd) << ": QDpi(" << dpiX << ", " << dpiY << ").";
// Sync the internal window frame margins with the latest DPI. // Sync the internal window frame margins with the latest DPI.
Utils::updateInternalWindowFrameMargins(data.params.getWindowHandle(), true); Utils::updateInternalWindowFrameMargins(data.params.getWindowHandle(), true);
// For some unknown reason, Qt sometimes won't re-paint the window contents after // For some unknown reason, Qt sometimes won't re-paint the window contents after

View File

@ -339,8 +339,11 @@ void setApplicationOSThemeAware()
} }
set = true; 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); Utils::setQtDarkModeAwareEnabled(true);
# endif
#endif #endif
#if ((defined(Q_OS_LINUX) && (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))) || \ #if ((defined(Q_OS_LINUX) && (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))) || \

View File

@ -297,7 +297,12 @@ void FramelessManagerPrivate::notifySystemThemeHasChangedOrNot()
if (notify) { if (notify) {
Q_Q(FramelessManager); Q_Q(FramelessManager);
Q_EMIT q->systemThemeChanged(); 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) { if (notify) {
Q_Q(FramelessManager); Q_Q(FramelessManager);
Q_EMIT q->wallpaperChanged(); 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 #endif
m_wallpaper = Utils::getWallpaperFilePath(); m_wallpaper = Utils::getWallpaperFilePath();
m_wallpaperAspectStyle = Utils::getWallpaperAspectStyle(); 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)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
QStyleHints * const styleHints = QGuiApplication::styleHints(); QStyleHints * const styleHints = QGuiApplication::styleHints();
Q_ASSERT(styleHints); Q_ASSERT(styleHints);

View File

@ -2375,7 +2375,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
const auto hWnd = reinterpret_cast<HWND>(windowId); const auto hWnd = reinterpret_cast<HWND>(windowId);
const DWORD borderFlag = (WindowsVersionHelper::isWin1020H1OrGreater() const DWORD borderFlag = (WindowsVersionHelper::isWin1020H1OrGreater()
? _DWMWA_USE_IMMERSIVE_DARK_MODE : _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1); ? _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); const BOOL darkFlag = (dark ? TRUE : FALSE);
WINDOWCOMPOSITIONATTRIBDATA wcad; WINDOWCOMPOSITIONATTRIBDATA wcad;
SecureZeroMemory(&wcad, sizeof(wcad)); SecureZeroMemory(&wcad, sizeof(wcad));
@ -2383,15 +2382,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
wcad.pvData = const_cast<BOOL *>(&darkFlag); wcad.pvData = const_cast<BOOL *>(&darkFlag);
wcad.cbData = sizeof(darkFlag); wcad.cbData = sizeof(darkFlag);
if (dark) { 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) { if (AllowDarkModeForWindow(hWnd, darkFlag) == FALSE) {
WARNING << getSystemErrorMessage(kAllowDarkModeForWindow); WARNING << getSystemErrorMessage(kAllowDarkModeForWindow);
} }
@ -2419,11 +2409,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
if (GetLastError() != ERROR_SUCCESS) { if (GetLastError() != ERROR_SUCCESS) {
WARNING << getSystemErrorMessage(kRefreshImmersiveColorPolicyState); WARNING << getSystemErrorMessage(kRefreshImmersiveColorPolicyState);
} }
SetLastError(ERROR_SUCCESS);
Q_UNUSED(GetIsImmersiveColorUsingHighContrast(IHCM_REFRESH));
if (GetLastError() != ERROR_SUCCESS) {
WARNING << getSystemErrorMessage(kGetIsImmersiveColorUsingHighContrast);
}
} else { } else {
if (AllowDarkModeForWindow(hWnd, darkFlag) == FALSE) { if (AllowDarkModeForWindow(hWnd, darkFlag) == FALSE) {
WARNING << getSystemErrorMessage(kAllowDarkModeForWindow); WARNING << getSystemErrorMessage(kAllowDarkModeForWindow);
@ -2452,20 +2437,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
if (GetLastError() != ERROR_SUCCESS) { if (GetLastError() != ERROR_SUCCESS) {
WARNING << getSystemErrorMessage(kRefreshImmersiveColorPolicyState); 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; return;
} }
// This hack is only available on Windows 10 and newer, and starting from // 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 // Win10 build 14986 it become useless due to the PMv2 DPI awareness mode
// takes care of it for us. // already takes care of it for us.
if (!WindowsVersionHelper::isWin10OrGreater() if (!WindowsVersionHelper::isWin10OrGreater()
|| (WindowsVersionHelper::isWin10RS1OrGreater() || (WindowsVersionHelper::isWin10RS2OrGreater()
&& (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) { && (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) {
return; return;
} }
@ -2622,10 +2593,10 @@ void Utils::fixupChildWindowsDpiMessage(const WId windowId)
void Utils::fixupDialogsDpiScaling() void Utils::fixupDialogsDpiScaling()
{ {
// This hack is only available on Windows 10 and newer, and starting from // 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 // Win10 build 14986 it become useless due to the PMv2 DPI awareness mode
// takes care of it for us. // already takes care of it for us.
if (!WindowsVersionHelper::isWin10OrGreater() if (!WindowsVersionHelper::isWin10OrGreater()
|| (WindowsVersionHelper::isWin10RS1OrGreater() || (WindowsVersionHelper::isWin10RS2OrGreater()
&& (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) { && (getDpiAwarenessForCurrentProcess() == DpiAwareness::PerMonitorVersion2))) {
return; return;
} }
@ -2639,4 +2610,17 @@ void Utils::fixupDialogsDpiScaling()
WARNING << getSystemErrorMessage(kEnablePerMonitorDialogScaling); 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 FRAMELESSHELPER_END_NAMESPACE