Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
win: minor tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Yuhang Zhao <[email protected]>
  • Loading branch information
wangwenx190 committed Nov 10, 2022
1 parent 3842f4e commit b8a3ff6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions include/FramelessHelper/Core/framelesshelper_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
1 change: 1 addition & 0 deletions include/FramelessHelper/Core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions qmake/inc/core/framelesshelper.version
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
9 changes: 4 additions & 5 deletions src/core/framelesshelper_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/core/framelesshelpercore_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))) || \
Expand Down
18 changes: 16 additions & 2 deletions src/core/framelessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
<< '.';
}
}

Expand All @@ -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 << '.';
}
}

Expand Down Expand Up @@ -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);
Expand Down
54 changes: 19 additions & 35 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2375,23 +2375,13 @@ 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));
wcad.Attrib = WCA_USEDARKMODECOLORS;
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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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

0 comments on commit b8a3ff6

Please sign in to comment.