minor refactor

This commit is contained in:
Yuhang Zhao 2023-04-23 13:42:27 +08:00
parent a203e2c3ca
commit b2561c16ba
6 changed files with 145 additions and 123 deletions

View File

@ -138,6 +138,14 @@
# define IsMaximized(hwnd) (IsZoomed(hwnd) != FALSE) # define IsMaximized(hwnd) (IsZoomed(hwnd) != FALSE)
#endif #endif
#ifndef RECT_WIDTH
# define RECT_WIDTH(rect) ((rect).right - (rect).left)
#endif
#ifndef RECT_HEIGHT
# define RECT_HEIGHT(rect) ((rect).bottom - (rect).top)
#endif
#ifndef MMSYSERR_NOERROR #ifndef MMSYSERR_NOERROR
# define MMSYSERR_NOERROR (0) # define MMSYSERR_NOERROR (0)
#endif #endif

View File

@ -136,7 +136,7 @@ FRAMELESSHELPER_CORE_API void fixupDialogsDpiScaling();
FRAMELESSHELPER_CORE_API void setDarkModeAllowedForApp(const bool allow = true); FRAMELESSHELPER_CORE_API void setDarkModeAllowedForApp(const bool allow = true);
FRAMELESSHELPER_CORE_API void bringWindowToFront(const WId windowId); FRAMELESSHELPER_CORE_API void bringWindowToFront(const WId windowId);
[[nodiscard]] FRAMELESSHELPER_CORE_API QPoint getWindowPlacementOffset(const WId windowId); [[nodiscard]] FRAMELESSHELPER_CORE_API QPoint getWindowPlacementOffset(const WId windowId);
[[nodiscard]] FRAMELESSHELPER_CORE_API QRect getWindowRestoreFrameGeometry(const WId windowId); [[nodiscard]] FRAMELESSHELPER_CORE_API QRect getWindowRestoreGeometry(const WId windowId);
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX

View File

@ -113,18 +113,16 @@ struct Win32Helper
Q_GLOBAL_STATIC(Win32Helper, g_win32Helper) Q_GLOBAL_STATIC(Win32Helper, g_win32Helper)
[[nodiscard]] static inline QString hwnd2str(const WId windowId) [[nodiscard]] extern bool operator==(const RECT &lhs, const RECT &rhs) noexcept;
{ [[nodiscard]] extern bool operator!=(const RECT &lhs, const RECT &rhs) noexcept;
// NULL handle is allowed here.
return FRAMELESSHELPER_STRING_LITERAL("0x")
+ QString::number(windowId, 16).toUpper().rightJustified(8, u'0');
}
[[nodiscard]] static inline QString hwnd2str(const HWND hwnd) [[nodiscard]] extern QRect rect2qrect(const RECT &rect);
{ [[nodiscard]] extern RECT qrect2rect(const QRect &qrect);
// NULL handle is allowed here.
return hwnd2str(reinterpret_cast<WId>(hwnd)); [[nodiscard]] extern QString hwnd2str(const WId windowId);
} [[nodiscard]] extern QString hwnd2str(const HWND hwnd);
[[nodiscard]] extern std::optional<MONITORINFOEXW> getMonitorForWindow(const HWND hwnd);
[[nodiscard]] static inline LRESULT CALLBACK FallbackTitleBarWindowProc [[nodiscard]] static inline LRESULT CALLBACK FallbackTitleBarWindowProc
(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam) (const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam)
@ -210,8 +208,6 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper)
SystemButtonType buttonType = SystemButtonType::Unknown; SystemButtonType buttonType = SystemButtonType::Unknown;
if (data.params.isInsideSystemButtons(qtScenePos, &buttonType)) { if (data.params.isInsideSystemButtons(qtScenePos, &buttonType)) {
switch (buttonType) { switch (buttonType) {
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(HTNOWHERE);
case SystemButtonType::WindowIcon: case SystemButtonType::WindowIcon:
return HTSYSMENU; return HTSYSMENU;
case SystemButtonType::Help: case SystemButtonType::Help:
@ -223,6 +219,8 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper)
return HTZOOM; return HTZOOM;
case SystemButtonType::Close: case SystemButtonType::Close:
return HTCLOSE; return HTCLOSE;
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(HTNOWHERE);
} }
} }
// Returns "HTTRANSPARENT" to let the mouse event pass through this invisible // Returns "HTTRANSPARENT" to let the mouse event pass through this invisible
@ -822,28 +820,22 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// we have to use another way to judge this if we are running // we have to use another way to judge this if we are running
// on Windows 7 or Windows 8. // on Windows 7 or Windows 8.
if (WindowsVersionHelper::isWin8Point1OrGreater()) { if (WindowsVersionHelper::isWin8Point1OrGreater()) {
MONITORINFOEXW monitorInfo; const std::optional<MONITORINFOEXW> monitorInfo = getMonitorForWindow(hWnd);
SecureZeroMemory(&monitorInfo, sizeof(monitorInfo)); if (!monitorInfo.has_value()) {
monitorInfo.cbSize = sizeof(monitorInfo); WARNING << "Failed to retrieve the window's monitor.";
const HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
if (!monitor) {
WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow);
break;
}
if (GetMonitorInfoW(monitor, &monitorInfo) == FALSE) {
WARNING << Utils::getSystemErrorMessage(kGetMonitorInfoW);
break; break;
} }
const RECT monitorRect = monitorInfo.value().rcMonitor;
// This helper can be used to determine if there's a // This helper can be used to determine if there's a
// auto-hide taskbar on the given edge of the monitor // auto-hide taskbar on the given edge of the monitor
// we're currently on. // we're currently on.
const auto hasAutohideTaskbar = [&monitorInfo](const UINT edge) -> bool { const auto hasAutohideTaskbar = [monitorRect](const UINT edge) -> bool {
APPBARDATA _abd; APPBARDATA abd;
SecureZeroMemory(&_abd, sizeof(_abd)); SecureZeroMemory(&abd, sizeof(abd));
_abd.cbSize = sizeof(_abd); abd.cbSize = sizeof(abd);
_abd.uEdge = edge; abd.uEdge = edge;
_abd.rc = monitorInfo.rcMonitor; abd.rc = monitorRect;
const auto hTaskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &_abd)); const auto hTaskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &abd));
return (hTaskbar != nullptr); return (hTaskbar != nullptr);
}; };
top = hasAutohideTaskbar(ABE_TOP); top = hasAutohideTaskbar(ABE_TOP);
@ -852,24 +844,24 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
right = hasAutohideTaskbar(ABE_RIGHT); right = hasAutohideTaskbar(ABE_RIGHT);
} else { } else {
int edge = -1; int edge = -1;
APPBARDATA _abd; APPBARDATA abd;
SecureZeroMemory(&_abd, sizeof(_abd)); SecureZeroMemory(&abd, sizeof(abd));
_abd.cbSize = sizeof(_abd); abd.cbSize = sizeof(abd);
_abd.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr); abd.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr);
if (_abd.hWnd) { if (abd.hWnd) {
const HMONITOR windowMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); const HMONITOR windowMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
if (!windowMonitor) { if (!windowMonitor) {
WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow); WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow);
break; break;
} }
const HMONITOR taskbarMonitor = MonitorFromWindow(_abd.hWnd, MONITOR_DEFAULTTOPRIMARY); const HMONITOR taskbarMonitor = MonitorFromWindow(abd.hWnd, MONITOR_DEFAULTTOPRIMARY);
if (!taskbarMonitor) { if (!taskbarMonitor) {
WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow); WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow);
break; break;
} }
if (taskbarMonitor == windowMonitor) { if (taskbarMonitor == windowMonitor) {
SHAppBarMessage(ABM_GETTASKBARPOS, &_abd); SHAppBarMessage(ABM_GETTASKBARPOS, &abd);
edge = _abd.uEdge; edge = abd.uEdge;
} }
} else { } else {
WARNING << Utils::getSystemErrorMessage(kFindWindowW); WARNING << Utils::getSystemErrorMessage(kFindWindowW);
@ -1152,7 +1144,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
*result = FALSE; // Use the default linear DPI scaling provided by Windows. *result = FALSE; // Use the default linear DPI scaling provided by Windows.
return true; // Jump over Qt's wrong handling logic. return true; // Jump over Qt's wrong handling logic.
} }
const QSizeF oldSize = {qreal(clientRect.right - clientRect.left), qreal(clientRect.bottom - clientRect.top)}; const QSizeF oldSize = {qreal(RECT_WIDTH(clientRect)), qreal(RECT_HEIGHT(clientRect))};
static constexpr const auto defaultDpi = qreal(USER_DEFAULT_SCREEN_DPI); static constexpr const auto defaultDpi = qreal(USER_DEFAULT_SCREEN_DPI);
// We need to round the scale factor according to Qt's rounding policy. // We need to round the scale factor according to Qt's rounding policy.
const qreal oldDpr = Utils::roundScaleFactor(qreal(data.dpi.x) / defaultDpi); const qreal oldDpr = Utils::roundScaleFactor(qreal(data.dpi.x) / defaultDpi);
@ -1205,7 +1197,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
if (!Utils::isWindowNoState(windowId)) { if (!Utils::isWindowNoState(windowId)) {
break; break;
} }
const QRect rect = Utils::getWindowRestoreFrameGeometry(windowId); const QRect rect = Utils::getWindowRestoreGeometry(windowId);
if (rect.isNull() || !rect.isValid()) { if (rect.isNull() || !rect.isValid()) {
WARNING << "The calculated restore geometry is invalid."; WARNING << "The calculated restore geometry is invalid.";
break; break;
@ -1218,7 +1210,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
break; break;
} }
if (data.restoreGeometry.isNull() || !data.restoreGeometry.isValid()) { if (data.restoreGeometry.isNull() || !data.restoreGeometry.isValid()) {
const QRect rect = Utils::getWindowRestoreFrameGeometry(windowId); const QRect rect = Utils::getWindowRestoreGeometry(windowId);
if (rect.isValid() && !rect.isNull()) { if (rect.isValid() && !rect.isNull()) {
const QMutexLocker locker(&g_win32Helper()->mutex); const QMutexLocker locker(&g_win32Helper()->mutex);
g_win32Helper()->data[windowId].restoreGeometry = rect; g_win32Helper()->data[windowId].restoreGeometry = rect;
@ -1234,10 +1226,12 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
WARNING << Utils::getSystemErrorMessage(kGetWindowPlacement); WARNING << Utils::getSystemErrorMessage(kGetWindowPlacement);
break; break;
} }
wp.rcNormalPosition = { // The restore geometry is correct, no need to bother.
data.restoreGeometry.left(), data.restoreGeometry.top(), if (rect2qrect(wp.rcNormalPosition) == data.restoreGeometry) {
data.restoreGeometry.right(), data.restoreGeometry.bottom() break;
}; }
// OK, the restore geometry is wrong, let's correct it then :)
wp.rcNormalPosition = qrect2rect(data.restoreGeometry);
if (SetWindowPlacement(hWnd, &wp) == FALSE) { if (SetWindowPlacement(hWnd, &wp) == FALSE) {
WARNING << Utils::getSystemErrorMessage(kSetWindowPlacement); WARNING << Utils::getSystemErrorMessage(kSetWindowPlacement);
} }

View File

@ -236,6 +236,62 @@ struct SYSTEM_METRIC
{SM_CXPADDEDBORDER, { 4, 5, 5, 6, 6, 6, 7, 7, 8, 9, 10, 12, 14, 16, 18, 20}} {SM_CXPADDEDBORDER, { 4, 5, 5, 6, 6, 6, 7, 7, 8, 9, 10, 12, 14, 16, 18, 20}}
}; };
[[nodiscard]] bool operator==(const RECT &lhs, const RECT &rhs) noexcept
{
return ((lhs.left == rhs.left) && (lhs.top == rhs.top)
&& (lhs.right == rhs.right) && (lhs.bottom == rhs.bottom));
}
[[nodiscard]] bool operator!=(const RECT &lhs, const RECT &rhs) noexcept
{
return !operator==(lhs, rhs);
}
[[nodiscard]] QRect rect2qrect(const RECT &rect)
{
return QRect{QPoint{rect.left, rect.top}, QSize{RECT_WIDTH(rect), RECT_HEIGHT(rect)}};
}
[[nodiscard]] RECT qrect2rect(const QRect &qrect)
{
return {qrect.left(), qrect.top(), qrect.right(), qrect.bottom()};
}
[[nodiscard]] QString hwnd2str(const WId windowId)
{
// NULL handle is allowed here.
return FRAMELESSHELPER_STRING_LITERAL("0x") + QString::number(windowId, 16).toUpper().rightJustified(8, u'0');
}
[[nodiscard]] QString hwnd2str(const HWND hwnd)
{
// NULL handle is allowed here.
return hwnd2str(reinterpret_cast<WId>(hwnd));
}
[[nodiscard]] std::optional<MONITORINFOEXW> getMonitorForWindow(const HWND hwnd)
{
Q_ASSERT(hwnd);
if (!hwnd) {
return std::nullopt;
}
// Use "MONITOR_DEFAULTTONEAREST" here so that we can still get the correct
// monitor even if the window is minimized.
const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (!monitor) {
WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow);
return std::nullopt;
}
MONITORINFOEXW monitorInfo;
SecureZeroMemory(&monitorInfo, sizeof(monitorInfo));
monitorInfo.cbSize = sizeof(monitorInfo);
if (GetMonitorInfoW(monitor, &monitorInfo) == FALSE) {
WARNING << Utils::getSystemErrorMessage(kGetMonitorInfoW);
return std::nullopt;
}
return monitorInfo;
};
[[nodiscard]] static inline QString dwmRegistryKey() [[nodiscard]] static inline QString dwmRegistryKey()
{ {
static const QString key = QString::fromWCharArray(kDwmRegistryKey); static const QString key = QString::fromWCharArray(kDwmRegistryKey);
@ -296,7 +352,7 @@ struct SYSTEM_METRIC
return (VerifyVersionInfoW(&osvi, (VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER), dwlConditionMask) != FALSE); return (VerifyVersionInfoW(&osvi, (VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER), dwlConditionMask) != FALSE);
} }
[[nodiscard]] static inline QString __getSystemErrorMessage(const QString &function, const DWORD code) [[nodiscard]] static inline QString getSystemErrorMessageImpl(const QString &function, const DWORD code)
{ {
Q_ASSERT(!function.isEmpty()); Q_ASSERT(!function.isEmpty());
if (function.isEmpty()) { if (function.isEmpty()) {
@ -321,7 +377,7 @@ struct SYSTEM_METRIC
#endif // FRAMELESSHELPER_CORE_NO_PRIVATE #endif // FRAMELESSHELPER_CORE_NO_PRIVATE
} }
[[nodiscard]] static inline QString __getSystemErrorMessage(const QString &function, const HRESULT hr) [[nodiscard]] static inline QString getSystemErrorMessageImpl(const QString &function, const HRESULT hr)
{ {
Q_ASSERT(!function.isEmpty()); Q_ASSERT(!function.isEmpty());
if (function.isEmpty()) { if (function.isEmpty()) {
@ -331,36 +387,9 @@ struct SYSTEM_METRIC
return kSuccessMessageText; return kSuccessMessageText;
} }
const DWORD dwError = HRESULT_CODE(hr); const DWORD dwError = HRESULT_CODE(hr);
return __getSystemErrorMessage(function, dwError); return getSystemErrorMessageImpl(function, dwError);
} }
[[nodiscard]] static inline bool operator==(const RECT &lhs, const RECT &rhs) noexcept
{
return ((lhs.left == rhs.left) && (lhs.top == rhs.top)
&& (lhs.right == rhs.right) && (lhs.bottom == rhs.bottom));
}
[[nodiscard]] static inline std::optional<MONITORINFOEXW> getMonitorForWindow(const HWND hwnd)
{
Q_ASSERT(hwnd);
if (!hwnd) {
return std::nullopt;
}
const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (!monitor) {
WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow);
return std::nullopt;
}
MONITORINFOEXW monitorInfo;
SecureZeroMemory(&monitorInfo, sizeof(monitorInfo));
monitorInfo.cbSize = sizeof(monitorInfo);
if (GetMonitorInfoW(monitor, &monitorInfo) == FALSE) {
WARNING << Utils::getSystemErrorMessage(kGetMonitorInfoW);
return std::nullopt;
}
return monitorInfo;
};
static inline void moveWindowToMonitor(const HWND hwnd, const MONITORINFOEXW &activeMonitor) static inline void moveWindowToMonitor(const HWND hwnd, const MONITORINFOEXW &activeMonitor)
{ {
Q_ASSERT(hwnd); Q_ASSERT(hwnd);
@ -594,7 +623,7 @@ bool Utils::isDwmCompositionEnabled()
BOOL enabled = FALSE; BOOL enabled = FALSE;
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmIsCompositionEnabled, &enabled); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmIsCompositionEnabled, &enabled);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmIsCompositionEnabled, hr); WARNING << getSystemErrorMessageImpl(kDwmIsCompositionEnabled, hr);
return resultFromRegistry(); return resultFromRegistry();
} }
return (enabled != FALSE); return (enabled != FALSE);
@ -649,7 +678,7 @@ void Utils::updateWindowFrameMargins(const WId windowId, const bool reset)
const auto hwnd = reinterpret_cast<HWND>(windowId); const auto hwnd = reinterpret_cast<HWND>(windowId);
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmExtendFrameIntoClientArea, hwnd, &margins); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmExtendFrameIntoClientArea, hwnd, &margins);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmExtendFrameIntoClientArea, hr); WARNING << getSystemErrorMessageImpl(kDwmExtendFrameIntoClientArea, hr);
return; return;
} }
triggerFrameChange(windowId); triggerFrameChange(windowId);
@ -712,7 +741,7 @@ QString Utils::getSystemErrorMessage(const QString &function)
if (code == ERROR_SUCCESS) { if (code == ERROR_SUCCESS) {
return {}; return {};
} }
return __getSystemErrorMessage(function, code); return getSystemErrorMessageImpl(function, code);
} }
QColor Utils::getDwmColorizationColor() QColor Utils::getDwmColorizationColor()
@ -735,7 +764,7 @@ QColor Utils::getDwmColorizationColor()
BOOL opaque = FALSE; BOOL opaque = FALSE;
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmGetColorizationColor, &color, &opaque); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmGetColorizationColor, &color, &opaque);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmGetColorizationColor, hr); WARNING << getSystemErrorMessageImpl(kDwmGetColorizationColor, hr);
return resultFromRegistry(); return resultFromRegistry();
} }
return QColor::fromRgba(color); return QColor::fromRgba(color);
@ -959,7 +988,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
if (SUCCEEDED(hr) && (dpiX > 0) && (dpiY > 0)) { if (SUCCEEDED(hr) && (dpiX > 0) && (dpiY > 0)) {
return (horizontal ? dpiX : dpiY); return (horizontal ? dpiX : dpiY);
} else { } else {
WARNING << __getSystemErrorMessage(kGetDpiForMonitor, hr); WARNING << getSystemErrorMessageImpl(kGetDpiForMonitor, hr);
} }
} }
// GetScaleFactorForMonitor() is only available on Windows 8 and onwards. // GetScaleFactorForMonitor() is only available on Windows 8 and onwards.
@ -969,7 +998,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
if (SUCCEEDED(hr) && (factor != _DEVICE_SCALE_FACTOR_INVALID)) { if (SUCCEEDED(hr) && (factor != _DEVICE_SCALE_FACTOR_INVALID)) {
return quint32(std::round(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100))); return quint32(std::round(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100)));
} else { } else {
WARNING << __getSystemErrorMessage(kGetScaleFactorForMonitor, hr); WARNING << getSystemErrorMessageImpl(kGetScaleFactorForMonitor, hr);
} }
} }
// This solution is supported on Windows 2000 and onwards. // This solution is supported on Windows 2000 and onwards.
@ -1028,10 +1057,10 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
WARNING << "GetDesktopDpi() failed."; WARNING << "GetDesktopDpi() failed.";
} }
} else { } else {
WARNING << __getSystemErrorMessage(kReloadSystemMetrics, hr); WARNING << getSystemErrorMessageImpl(kReloadSystemMetrics, hr);
} }
} else { } else {
WARNING << __getSystemErrorMessage(kD2D1CreateFactory, hr); WARNING << getSystemErrorMessageImpl(kD2D1CreateFactory, hr);
} }
if (d2dFactory) { if (d2dFactory) {
d2dFactory->Release(); d2dFactory->Release();
@ -1499,7 +1528,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel()
DEBUG << kDpiNoAccessErrorMessage; DEBUG << kDpiNoAccessErrorMessage;
return true; return true;
} }
WARNING << __getSystemErrorMessage(kSetProcessDpiAwarenessContext, dwError); WARNING << getSystemErrorMessageImpl(kSetProcessDpiAwarenessContext, dwError);
return false; return false;
}; };
if (currentAwareness == DpiAwareness::PerMonitorVersion2) { if (currentAwareness == DpiAwareness::PerMonitorVersion2) {
@ -1540,7 +1569,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel()
DEBUG << kDpiNoAccessErrorMessage; DEBUG << kDpiNoAccessErrorMessage;
return true; return true;
} }
WARNING << __getSystemErrorMessage(kSetProcessDpiAwareness, hr); WARNING << getSystemErrorMessageImpl(kSetProcessDpiAwareness, hr);
return false; return false;
}; };
if (currentAwareness == DpiAwareness::PerMonitorVersion2) { if (currentAwareness == DpiAwareness::PerMonitorVersion2) {
@ -1608,7 +1637,7 @@ void Utils::updateGlobalWin32ControlsTheme(const WId windowId, const bool dark)
const HRESULT hr = API_CALL_FUNCTION(uxtheme, SetWindowTheme, hwnd, const HRESULT hr = API_CALL_FUNCTION(uxtheme, SetWindowTheme, hwnd,
(dark ? kSystemDarkThemeResourceName : kSystemLightThemeResourceName), nullptr); (dark ? kSystemDarkThemeResourceName : kSystemLightThemeResourceName), nullptr);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kSetWindowTheme, hr); WARNING << getSystemErrorMessageImpl(kSetWindowTheme, hr);
} }
} }
@ -1688,7 +1717,7 @@ void Utils::setCornerStyleForWindow(const WId windowId, const WindowCornerStyle
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute, const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute,
hwnd, _DWMWA_WINDOW_CORNER_PREFERENCE, &wcp, sizeof(wcp)); hwnd, _DWMWA_WINDOW_CORNER_PREFERENCE, &wcp, sizeof(wcp));
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
} }
@ -1750,7 +1779,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode,
hwnd, _DWMWA_SYSTEMBACKDROP_TYPE, &dwmsbt, sizeof(dwmsbt)); hwnd, _DWMWA_SYSTEMBACKDROP_TYPE, &dwmsbt, sizeof(dwmsbt));
if (FAILED(hr)) { if (FAILED(hr)) {
result = false; result = false;
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
} else if (WindowsVersionHelper::isWin11OrGreater()) { } else if (WindowsVersionHelper::isWin11OrGreater()) {
const BOOL enable = FALSE; const BOOL enable = FALSE;
@ -1758,7 +1787,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode,
hwnd, _DWMWA_MICA_EFFECT, &enable, sizeof(enable)); hwnd, _DWMWA_MICA_EFFECT, &enable, sizeof(enable));
if (FAILED(hr)) { if (FAILED(hr)) {
result = false; result = false;
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
} else { } else {
ACCENT_POLICY policy; ACCENT_POLICY policy;
@ -1813,7 +1842,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode,
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
return true; return true;
} else { } else {
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
} else { } else {
const BOOL enable = TRUE; const BOOL enable = TRUE;
@ -1822,11 +1851,11 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode,
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
return true; return true;
} else { } else {
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
} }
} else { } else {
WARNING << __getSystemErrorMessage(kDwmExtendFrameIntoClientArea, hr); WARNING << getSystemErrorMessageImpl(kDwmExtendFrameIntoClientArea, hr);
} }
restoreWindowFrameMargins(); restoreWindowFrameMargins();
} else { } else {
@ -1895,7 +1924,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode,
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
return true; return true;
} }
WARNING << __getSystemErrorMessage(kDwmEnableBlurBehindWindow, hr); WARNING << getSystemErrorMessageImpl(kDwmEnableBlurBehindWindow, hr);
} }
return false; return false;
} }
@ -1990,7 +2019,7 @@ void Utils::hideOriginalTitleBarElements(const WId windowId, const bool disable)
const DWORD mask = (disable ? validBits : 0); const DWORD mask = (disable ? validBits : 0);
const HRESULT hr = _SetWindowThemeNonClientAttributes(hwnd, mask, mask); const HRESULT hr = _SetWindowThemeNonClientAttributes(hwnd, mask, mask);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kSetWindowThemeAttribute, hr); WARNING << getSystemErrorMessageImpl(kSetWindowThemeAttribute, hr);
} }
} }
@ -2086,7 +2115,7 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
} }
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute, hWnd, borderFlag, &darkFlag, sizeof(darkFlag)); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute, hWnd, borderFlag, &darkFlag, sizeof(darkFlag));
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
SetLastError(ERROR_SUCCESS); SetLastError(ERROR_SUCCESS);
_FlushMenuThemes(); _FlushMenuThemes();
@ -2114,7 +2143,7 @@ void Utils::refreshWin32ThemeResources(const WId windowId, const bool dark)
} }
const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute, hWnd, borderFlag, &darkFlag, sizeof(darkFlag)); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmSetWindowAttribute, hWnd, borderFlag, &darkFlag, sizeof(darkFlag));
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr);
} }
SetLastError(ERROR_SUCCESS); SetLastError(ERROR_SUCCESS);
_FlushMenuThemes(); _FlushMenuThemes();
@ -2217,7 +2246,7 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
_PROCESS_DPI_AWARENESS pda = _PROCESS_DPI_UNAWARE; _PROCESS_DPI_AWARENESS pda = _PROCESS_DPI_UNAWARE;
const HRESULT hr = API_CALL_FUNCTION4(shcore, GetProcessDpiAwareness, nullptr, &pda); const HRESULT hr = API_CALL_FUNCTION4(shcore, GetProcessDpiAwareness, nullptr, &pda);
if (FAILED(hr)) { if (FAILED(hr)) {
WARNING << __getSystemErrorMessage(kGetProcessDpiAwareness, hr); WARNING << getSystemErrorMessageImpl(kGetProcessDpiAwareness, hr);
return DpiAwareness::Unknown; return DpiAwareness::Unknown;
} }
auto result = DpiAwareness::Unknown; auto result = DpiAwareness::Unknown;
@ -2407,22 +2436,17 @@ QPoint Utils::getWindowPlacementOffset(const WId windowId)
if (exStyle & WS_EX_TOOLWINDOW) { if (exStyle & WS_EX_TOOLWINDOW) {
return {}; return {};
} }
const HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); const std::optional<MONITORINFOEXW> mi = getMonitorForWindow(hwnd);
if (!mon) { if (!mi.has_value()) {
WARNING << getSystemErrorMessage(kMonitorFromWindow); WARNING << "Failed to retrieve the window's monitor.";
return {}; return {};
} }
MONITORINFOEXW mi; const RECT work = mi.value().rcWork;
SecureZeroMemory(&mi, sizeof(mi)); const RECT total = mi.value().rcMonitor;
mi.cbSize = sizeof(mi); return {work.left - total.left, work.top - total.top};
if (GetMonitorInfoW(mon, &mi) == FALSE) {
WARNING << getSystemErrorMessage(kGetMonitorInfoW);
return {};
}
return {mi.rcWork.left - mi.rcMonitor.left, mi.rcWork.top - mi.rcMonitor.top};
} }
QRect Utils::getWindowRestoreFrameGeometry(const WId windowId) QRect Utils::getWindowRestoreGeometry(const WId windowId)
{ {
Q_ASSERT(windowId); Q_ASSERT(windowId);
if (!windowId) { if (!windowId) {
@ -2436,11 +2460,7 @@ QRect Utils::getWindowRestoreFrameGeometry(const WId windowId)
WARNING << getSystemErrorMessage(kGetWindowPlacement); WARNING << getSystemErrorMessage(kGetWindowPlacement);
return {}; return {};
} }
const RECT rawRect = wp.rcNormalPosition; return rect2qrect(wp.rcNormalPosition).translated(getWindowPlacementOffset(windowId));
const QPoint topLeft = {rawRect.left, rawRect.top};
const QSize size = {rawRect.right - rawRect.left, rawRect.bottom - rawRect.top};
const QPoint offset = getWindowPlacementOffset(windowId);
return QRect{topLeft, size}.translated(offset);
} }
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -278,8 +278,6 @@ void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickG
return; return;
} }
switch (buttonType) { switch (buttonType) {
case QuickGlobal::SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(static_cast<void>(0));
case QuickGlobal::SystemButtonType::WindowIcon: case QuickGlobal::SystemButtonType::WindowIcon:
data->windowIconButton = item; data->windowIconButton = item;
break; break;
@ -296,6 +294,8 @@ void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickG
case QuickGlobal::SystemButtonType::Close: case QuickGlobal::SystemButtonType::Close:
data->closeButton = item; data->closeButton = item;
break; break;
case QuickGlobal::SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(static_cast<void>(0));
} }
} }
@ -827,8 +827,6 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System
const QuickHelperData data = getWindowData(); const QuickHelperData data = getWindowData();
QQuickAbstractButton *quickButton = nullptr; QQuickAbstractButton *quickButton = nullptr;
switch (button) { switch (button) {
case QuickGlobal::SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
case QuickGlobal::SystemButtonType::WindowIcon: case QuickGlobal::SystemButtonType::WindowIcon:
if (data.windowIconButton) { if (data.windowIconButton) {
if (const auto btn = qobject_cast<QQuickAbstractButton *>(data.windowIconButton)) { if (const auto btn = qobject_cast<QQuickAbstractButton *>(data.windowIconButton)) {
@ -865,6 +863,8 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System
} }
} }
break; break;
case QuickGlobal::SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
} }
if (!quickButton) { if (!quickButton) {
return; return;

View File

@ -716,8 +716,6 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType
const WidgetsHelperData data = getWindowData(); const WidgetsHelperData data = getWindowData();
QWidget *widgetButton = nullptr; QWidget *widgetButton = nullptr;
switch (button) { switch (button) {
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
case SystemButtonType::WindowIcon: case SystemButtonType::WindowIcon:
if (data.windowIconButton) { if (data.windowIconButton) {
widgetButton = data.windowIconButton; widgetButton = data.windowIconButton;
@ -744,6 +742,8 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType
widgetButton = data.closeButton; widgetButton = data.closeButton;
} }
break; break;
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
} }
if (!widgetButton) { if (!widgetButton) {
return; return;
@ -863,8 +863,6 @@ void FramelessWidgetsHelperPrivate::setSystemButton(QWidget *widget, const Syste
return; return;
} }
switch (buttonType) { switch (buttonType) {
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
case SystemButtonType::WindowIcon: case SystemButtonType::WindowIcon:
data->windowIconButton = widget; data->windowIconButton = widget;
break; break;
@ -881,6 +879,8 @@ void FramelessWidgetsHelperPrivate::setSystemButton(QWidget *widget, const Syste
case SystemButtonType::Close: case SystemButtonType::Close:
data->closeButton = widget; data->closeButton = widget;
break; break;
case SystemButtonType::Unknown:
Q_UNREACHABLE_RETURN(void(0));
} }
} }