diff --git a/include/FramelessHelper/Core/framelesshelper_windows.h b/include/FramelessHelper/Core/framelesshelper_windows.h index d5db16af..2b9c5305 100644 --- a/include/FramelessHelper/Core/framelesshelper_windows.h +++ b/include/FramelessHelper/Core/framelesshelper_windows.h @@ -138,6 +138,14 @@ # define IsMaximized(hwnd) (IsZoomed(hwnd) != FALSE) #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 # define MMSYSERR_NOERROR (0) #endif diff --git a/include/FramelessHelper/Core/utils.h b/include/FramelessHelper/Core/utils.h index 8012c750..20d71c1c 100644 --- a/include/FramelessHelper/Core/utils.h +++ b/include/FramelessHelper/Core/utils.h @@ -136,7 +136,7 @@ FRAMELESSHELPER_CORE_API void fixupDialogsDpiScaling(); FRAMELESSHELPER_CORE_API void setDarkModeAllowedForApp(const bool allow = true); FRAMELESSHELPER_CORE_API void bringWindowToFront(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 #ifdef Q_OS_LINUX diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 892585ea..69c315fa 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -113,18 +113,16 @@ struct Win32Helper Q_GLOBAL_STATIC(Win32Helper, g_win32Helper) -[[nodiscard]] static inline 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]] extern bool operator==(const RECT &lhs, const RECT &rhs) noexcept; +[[nodiscard]] extern bool operator!=(const RECT &lhs, const RECT &rhs) noexcept; -[[nodiscard]] static inline QString hwnd2str(const HWND hwnd) -{ - // NULL handle is allowed here. - return hwnd2str(reinterpret_cast(hwnd)); -} +[[nodiscard]] extern QRect rect2qrect(const RECT &rect); +[[nodiscard]] extern RECT qrect2rect(const QRect &qrect); + +[[nodiscard]] extern QString hwnd2str(const WId windowId); +[[nodiscard]] extern QString hwnd2str(const HWND hwnd); + +[[nodiscard]] extern std::optional getMonitorForWindow(const HWND hwnd); [[nodiscard]] static inline LRESULT CALLBACK FallbackTitleBarWindowProc (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; if (data.params.isInsideSystemButtons(qtScenePos, &buttonType)) { switch (buttonType) { - case SystemButtonType::Unknown: - Q_UNREACHABLE_RETURN(HTNOWHERE); case SystemButtonType::WindowIcon: return HTSYSMENU; case SystemButtonType::Help: @@ -223,6 +219,8 @@ Q_GLOBAL_STATIC(Win32Helper, g_win32Helper) return HTZOOM; case SystemButtonType::Close: return HTCLOSE; + case SystemButtonType::Unknown: + Q_UNREACHABLE_RETURN(HTNOWHERE); } } // 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 // on Windows 7 or Windows 8. if (WindowsVersionHelper::isWin8Point1OrGreater()) { - MONITORINFOEXW monitorInfo; - SecureZeroMemory(&monitorInfo, sizeof(monitorInfo)); - monitorInfo.cbSize = sizeof(monitorInfo); - const HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); - if (!monitor) { - WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow); - break; - } - if (GetMonitorInfoW(monitor, &monitorInfo) == FALSE) { - WARNING << Utils::getSystemErrorMessage(kGetMonitorInfoW); + const std::optional monitorInfo = getMonitorForWindow(hWnd); + if (!monitorInfo.has_value()) { + WARNING << "Failed to retrieve the window's monitor."; break; } + const RECT monitorRect = monitorInfo.value().rcMonitor; // This helper can be used to determine if there's a // auto-hide taskbar on the given edge of the monitor // we're currently on. - const auto hasAutohideTaskbar = [&monitorInfo](const UINT edge) -> bool { - APPBARDATA _abd; - SecureZeroMemory(&_abd, sizeof(_abd)); - _abd.cbSize = sizeof(_abd); - _abd.uEdge = edge; - _abd.rc = monitorInfo.rcMonitor; - const auto hTaskbar = reinterpret_cast(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &_abd)); + const auto hasAutohideTaskbar = [monitorRect](const UINT edge) -> bool { + APPBARDATA abd; + SecureZeroMemory(&abd, sizeof(abd)); + abd.cbSize = sizeof(abd); + abd.uEdge = edge; + abd.rc = monitorRect; + const auto hTaskbar = reinterpret_cast(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &abd)); return (hTaskbar != nullptr); }; top = hasAutohideTaskbar(ABE_TOP); @@ -852,24 +844,24 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me right = hasAutohideTaskbar(ABE_RIGHT); } else { int edge = -1; - APPBARDATA _abd; - SecureZeroMemory(&_abd, sizeof(_abd)); - _abd.cbSize = sizeof(_abd); - _abd.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr); - if (_abd.hWnd) { + APPBARDATA abd; + SecureZeroMemory(&abd, sizeof(abd)); + abd.cbSize = sizeof(abd); + abd.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr); + if (abd.hWnd) { const HMONITOR windowMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); if (!windowMonitor) { WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow); break; } - const HMONITOR taskbarMonitor = MonitorFromWindow(_abd.hWnd, MONITOR_DEFAULTTOPRIMARY); + const HMONITOR taskbarMonitor = MonitorFromWindow(abd.hWnd, MONITOR_DEFAULTTOPRIMARY); if (!taskbarMonitor) { WARNING << Utils::getSystemErrorMessage(kMonitorFromWindow); break; } if (taskbarMonitor == windowMonitor) { - SHAppBarMessage(ABM_GETTASKBARPOS, &_abd); - edge = _abd.uEdge; + SHAppBarMessage(ABM_GETTASKBARPOS, &abd); + edge = abd.uEdge; } } else { 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. 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); // We need to round the scale factor according to Qt's rounding policy. 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)) { break; } - const QRect rect = Utils::getWindowRestoreFrameGeometry(windowId); + const QRect rect = Utils::getWindowRestoreGeometry(windowId); if (rect.isNull() || !rect.isValid()) { WARNING << "The calculated restore geometry is invalid."; break; @@ -1218,7 +1210,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me break; } if (data.restoreGeometry.isNull() || !data.restoreGeometry.isValid()) { - const QRect rect = Utils::getWindowRestoreFrameGeometry(windowId); + const QRect rect = Utils::getWindowRestoreGeometry(windowId); if (rect.isValid() && !rect.isNull()) { const QMutexLocker locker(&g_win32Helper()->mutex); g_win32Helper()->data[windowId].restoreGeometry = rect; @@ -1234,10 +1226,12 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me WARNING << Utils::getSystemErrorMessage(kGetWindowPlacement); break; } - wp.rcNormalPosition = { - data.restoreGeometry.left(), data.restoreGeometry.top(), - data.restoreGeometry.right(), data.restoreGeometry.bottom() - }; + // The restore geometry is correct, no need to bother. + if (rect2qrect(wp.rcNormalPosition) == data.restoreGeometry) { + break; + } + // OK, the restore geometry is wrong, let's correct it then :) + wp.rcNormalPosition = qrect2rect(data.restoreGeometry); if (SetWindowPlacement(hWnd, &wp) == FALSE) { WARNING << Utils::getSystemErrorMessage(kSetWindowPlacement); } diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 180166ff..5c41faf8 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -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}} }; +[[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(hwnd)); +} + +[[nodiscard]] std::optional 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() { 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); } -[[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()); if (function.isEmpty()) { @@ -321,7 +377,7 @@ struct SYSTEM_METRIC #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()); if (function.isEmpty()) { @@ -331,36 +387,9 @@ struct SYSTEM_METRIC return kSuccessMessageText; } const DWORD dwError = HRESULT_CODE(hr); - return __getSystemErrorMessage(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)); + return getSystemErrorMessageImpl(function, dwError); } -[[nodiscard]] static inline std::optional 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) { Q_ASSERT(hwnd); @@ -594,7 +623,7 @@ bool Utils::isDwmCompositionEnabled() BOOL enabled = FALSE; const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmIsCompositionEnabled, &enabled); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kDwmIsCompositionEnabled, hr); + WARNING << getSystemErrorMessageImpl(kDwmIsCompositionEnabled, hr); return resultFromRegistry(); } return (enabled != FALSE); @@ -649,7 +678,7 @@ void Utils::updateWindowFrameMargins(const WId windowId, const bool reset) const auto hwnd = reinterpret_cast(windowId); const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmExtendFrameIntoClientArea, hwnd, &margins); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kDwmExtendFrameIntoClientArea, hr); + WARNING << getSystemErrorMessageImpl(kDwmExtendFrameIntoClientArea, hr); return; } triggerFrameChange(windowId); @@ -712,7 +741,7 @@ QString Utils::getSystemErrorMessage(const QString &function) if (code == ERROR_SUCCESS) { return {}; } - return __getSystemErrorMessage(function, code); + return getSystemErrorMessageImpl(function, code); } QColor Utils::getDwmColorizationColor() @@ -735,7 +764,7 @@ QColor Utils::getDwmColorizationColor() BOOL opaque = FALSE; const HRESULT hr = API_CALL_FUNCTION(dwmapi, DwmGetColorizationColor, &color, &opaque); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kDwmGetColorizationColor, hr); + WARNING << getSystemErrorMessageImpl(kDwmGetColorizationColor, hr); return resultFromRegistry(); } return QColor::fromRgba(color); @@ -959,7 +988,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal) if (SUCCEEDED(hr) && (dpiX > 0) && (dpiY > 0)) { return (horizontal ? dpiX : dpiY); } else { - WARNING << __getSystemErrorMessage(kGetDpiForMonitor, hr); + WARNING << getSystemErrorMessageImpl(kGetDpiForMonitor, hr); } } // 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)) { return quint32(std::round(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100))); } else { - WARNING << __getSystemErrorMessage(kGetScaleFactorForMonitor, hr); + WARNING << getSystemErrorMessageImpl(kGetScaleFactorForMonitor, hr); } } // This solution is supported on Windows 2000 and onwards. @@ -1028,10 +1057,10 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal) WARNING << "GetDesktopDpi() failed."; } } else { - WARNING << __getSystemErrorMessage(kReloadSystemMetrics, hr); + WARNING << getSystemErrorMessageImpl(kReloadSystemMetrics, hr); } } else { - WARNING << __getSystemErrorMessage(kD2D1CreateFactory, hr); + WARNING << getSystemErrorMessageImpl(kD2D1CreateFactory, hr); } if (d2dFactory) { d2dFactory->Release(); @@ -1499,7 +1528,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel() DEBUG << kDpiNoAccessErrorMessage; return true; } - WARNING << __getSystemErrorMessage(kSetProcessDpiAwarenessContext, dwError); + WARNING << getSystemErrorMessageImpl(kSetProcessDpiAwarenessContext, dwError); return false; }; if (currentAwareness == DpiAwareness::PerMonitorVersion2) { @@ -1540,7 +1569,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel() DEBUG << kDpiNoAccessErrorMessage; return true; } - WARNING << __getSystemErrorMessage(kSetProcessDpiAwareness, hr); + WARNING << getSystemErrorMessageImpl(kSetProcessDpiAwareness, hr); return false; }; 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, (dark ? kSystemDarkThemeResourceName : kSystemLightThemeResourceName), nullptr); 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, hwnd, _DWMWA_WINDOW_CORNER_PREFERENCE, &wcp, sizeof(wcp)); 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)); if (FAILED(hr)) { result = false; - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } } else if (WindowsVersionHelper::isWin11OrGreater()) { const BOOL enable = FALSE; @@ -1758,7 +1787,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, hwnd, _DWMWA_MICA_EFFECT, &enable, sizeof(enable)); if (FAILED(hr)) { result = false; - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } } else { ACCENT_POLICY policy; @@ -1813,7 +1842,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, if (SUCCEEDED(hr)) { return true; } else { - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } } else { const BOOL enable = TRUE; @@ -1822,11 +1851,11 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, if (SUCCEEDED(hr)) { return true; } else { - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } } } else { - WARNING << __getSystemErrorMessage(kDwmExtendFrameIntoClientArea, hr); + WARNING << getSystemErrorMessageImpl(kDwmExtendFrameIntoClientArea, hr); } restoreWindowFrameMargins(); } else { @@ -1895,7 +1924,7 @@ bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, if (SUCCEEDED(hr)) { return true; } - WARNING << __getSystemErrorMessage(kDwmEnableBlurBehindWindow, hr); + WARNING << getSystemErrorMessageImpl(kDwmEnableBlurBehindWindow, hr); } return false; } @@ -1990,7 +2019,7 @@ void Utils::hideOriginalTitleBarElements(const WId windowId, const bool disable) const DWORD mask = (disable ? validBits : 0); const HRESULT hr = _SetWindowThemeNonClientAttributes(hwnd, mask, mask); 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)); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } SetLastError(ERROR_SUCCESS); _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)); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kDwmSetWindowAttribute, hr); + WARNING << getSystemErrorMessageImpl(kDwmSetWindowAttribute, hr); } SetLastError(ERROR_SUCCESS); _FlushMenuThemes(); @@ -2217,7 +2246,7 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest) _PROCESS_DPI_AWARENESS pda = _PROCESS_DPI_UNAWARE; const HRESULT hr = API_CALL_FUNCTION4(shcore, GetProcessDpiAwareness, nullptr, &pda); if (FAILED(hr)) { - WARNING << __getSystemErrorMessage(kGetProcessDpiAwareness, hr); + WARNING << getSystemErrorMessageImpl(kGetProcessDpiAwareness, hr); return DpiAwareness::Unknown; } auto result = DpiAwareness::Unknown; @@ -2407,22 +2436,17 @@ QPoint Utils::getWindowPlacementOffset(const WId windowId) if (exStyle & WS_EX_TOOLWINDOW) { return {}; } - const HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); - if (!mon) { - WARNING << getSystemErrorMessage(kMonitorFromWindow); - return {}; - } - MONITORINFOEXW mi; - SecureZeroMemory(&mi, sizeof(mi)); - mi.cbSize = sizeof(mi); - if (GetMonitorInfoW(mon, &mi) == FALSE) { - WARNING << getSystemErrorMessage(kGetMonitorInfoW); + const std::optional mi = getMonitorForWindow(hwnd); + if (!mi.has_value()) { + WARNING << "Failed to retrieve the window's monitor."; return {}; } - return {mi.rcWork.left - mi.rcMonitor.left, mi.rcWork.top - mi.rcMonitor.top}; + const RECT work = mi.value().rcWork; + const RECT total = mi.value().rcMonitor; + return {work.left - total.left, work.top - total.top}; } -QRect Utils::getWindowRestoreFrameGeometry(const WId windowId) +QRect Utils::getWindowRestoreGeometry(const WId windowId) { Q_ASSERT(windowId); if (!windowId) { @@ -2436,11 +2460,7 @@ QRect Utils::getWindowRestoreFrameGeometry(const WId windowId) WARNING << getSystemErrorMessage(kGetWindowPlacement); return {}; } - const RECT rawRect = wp.rcNormalPosition; - 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); + return rect2qrect(wp.rcNormalPosition).translated(getWindowPlacementOffset(windowId)); } FRAMELESSHELPER_END_NAMESPACE diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp index 99aded10..a152860e 100644 --- a/src/quick/framelessquickhelper.cpp +++ b/src/quick/framelessquickhelper.cpp @@ -278,8 +278,6 @@ void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickG return; } switch (buttonType) { - case QuickGlobal::SystemButtonType::Unknown: - Q_UNREACHABLE_RETURN(static_cast(0)); case QuickGlobal::SystemButtonType::WindowIcon: data->windowIconButton = item; break; @@ -296,6 +294,8 @@ void FramelessQuickHelperPrivate::setSystemButton(QQuickItem *item, const QuickG case QuickGlobal::SystemButtonType::Close: data->closeButton = item; break; + case QuickGlobal::SystemButtonType::Unknown: + Q_UNREACHABLE_RETURN(static_cast(0)); } } @@ -827,8 +827,6 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System const QuickHelperData data = getWindowData(); QQuickAbstractButton *quickButton = nullptr; switch (button) { - case QuickGlobal::SystemButtonType::Unknown: - Q_UNREACHABLE_RETURN(void(0)); case QuickGlobal::SystemButtonType::WindowIcon: if (data.windowIconButton) { if (const auto btn = qobject_cast(data.windowIconButton)) { @@ -865,6 +863,8 @@ void FramelessQuickHelperPrivate::setSystemButtonState(const QuickGlobal::System } } break; + case QuickGlobal::SystemButtonType::Unknown: + Q_UNREACHABLE_RETURN(void(0)); } if (!quickButton) { return; diff --git a/src/widgets/framelesswidgetshelper.cpp b/src/widgets/framelesswidgetshelper.cpp index eddc9522..287ed3d2 100644 --- a/src/widgets/framelesswidgetshelper.cpp +++ b/src/widgets/framelesswidgetshelper.cpp @@ -716,8 +716,6 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType const WidgetsHelperData data = getWindowData(); QWidget *widgetButton = nullptr; switch (button) { - case SystemButtonType::Unknown: - Q_UNREACHABLE_RETURN(void(0)); case SystemButtonType::WindowIcon: if (data.windowIconButton) { widgetButton = data.windowIconButton; @@ -744,6 +742,8 @@ void FramelessWidgetsHelperPrivate::setSystemButtonState(const SystemButtonType widgetButton = data.closeButton; } break; + case SystemButtonType::Unknown: + Q_UNREACHABLE_RETURN(void(0)); } if (!widgetButton) { return; @@ -863,8 +863,6 @@ void FramelessWidgetsHelperPrivate::setSystemButton(QWidget *widget, const Syste return; } switch (buttonType) { - case SystemButtonType::Unknown: - Q_UNREACHABLE_RETURN(void(0)); case SystemButtonType::WindowIcon: data->windowIconButton = widget; break; @@ -881,6 +879,8 @@ void FramelessWidgetsHelperPrivate::setSystemButton(QWidget *widget, const Syste case SystemButtonType::Close: data->closeButton = widget; break; + case SystemButtonType::Unknown: + Q_UNREACHABLE_RETURN(void(0)); } }