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)
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

View File

@ -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
};

View File

@ -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

View File

@ -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";

View File

@ -521,9 +521,8 @@ void FramelessHelperWin::addWindow(const SystemParameters &params)
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

View File

@ -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))) || \

View File

@ -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);

View File

@ -2375,7 +2375,6 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
const auto hWnd = reinterpret_cast<HWND>(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<BOOL *>(&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