win: minor refactor

This commit is contained in:
Yuhang Zhao 2023-04-28 09:14:06 +08:00
parent 4d063369fa
commit b039a9130a
3 changed files with 45 additions and 36 deletions

View File

@ -83,6 +83,7 @@ FRAMELESSHELPER_CORE_API void registerThemeChangeNotification();
[[nodiscard]] FRAMELESSHELPER_CORE_API int horizontalAdvance(const QFontMetrics &fm, const QString &str); [[nodiscard]] FRAMELESSHELPER_CORE_API int horizontalAdvance(const QFontMetrics &fm, const QString &str);
[[nodiscard]] FRAMELESSHELPER_CORE_API qreal getRelativeScaleFactor(const quint32 oldDpi, const quint32 newDpi); [[nodiscard]] FRAMELESSHELPER_CORE_API qreal getRelativeScaleFactor(const quint32 oldDpi, const quint32 newDpi);
[[nodiscard]] FRAMELESSHELPER_CORE_API QSize rescaleSize(const QSize &oldSize, const quint32 oldDpi, const quint32 newDpi); [[nodiscard]] FRAMELESSHELPER_CORE_API QSize rescaleSize(const QSize &oldSize, const quint32 oldDpi, const quint32 newDpi);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isValidGeometry(const QRect &rect);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version);

View File

@ -656,6 +656,23 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
const bool frameBorderVisible = Utils::isWindowFrameBorderVisible(); const bool frameBorderVisible = Utils::isWindowFrameBorderVisible();
const WPARAM wParam = msg->wParam; const WPARAM wParam = msg->wParam;
const LPARAM lParam = msg->lParam; const LPARAM lParam = msg->lParam;
const auto updateRestoreGeometry = [windowId, &data](const bool ignoreWindowState) -> void {
if (!ignoreWindowState && !Utils::isWindowNoState(windowId)) {
return;
}
const QRect rect = Utils::getWindowRestoreGeometry(windowId);
if (!Utils::isValidGeometry(rect)) {
WARNING << "The calculated restore geometry is invalid.";
return;
}
if (Utils::isValidGeometry(data.restoreGeometry) && (data.restoreGeometry == rect)) {
return;
}
const QMutexLocker locker(&g_win32Helper()->mutex);
g_win32Helper()->data[windowId].restoreGeometry = rect;
};
switch (uMsg) { switch (uMsg) {
#if (QT_VERSION < QT_VERSION_CHECK(5, 9, 0)) // Qt has done this for us since 5.9.0 #if (QT_VERSION < QT_VERSION_CHECK(5, 9, 0)) // Qt has done this for us since 5.9.0
case WM_NCCREATE: { case WM_NCCREATE: {
@ -830,12 +847,12 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// 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 = [monitorRect](const UINT edge) -> bool { const auto hasAutohideTaskbar = [monitorRect](const UINT edge) -> bool {
APPBARDATA abd; APPBARDATA abd2;
SecureZeroMemory(&abd, sizeof(abd)); SecureZeroMemory(&abd2, sizeof(abd2));
abd.cbSize = sizeof(abd); abd2.cbSize = sizeof(abd2);
abd.uEdge = edge; abd2.uEdge = edge;
abd.rc = monitorRect; abd2.rc = monitorRect;
const auto hTaskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &abd)); const auto hTaskbar = reinterpret_cast<HWND>(SHAppBarMessage(ABM_GETAUTOHIDEBAREX, &abd2));
return (hTaskbar != nullptr); return (hTaskbar != nullptr);
}; };
top = hasAutohideTaskbar(ABE_TOP); top = hasAutohideTaskbar(ABE_TOP);
@ -844,24 +861,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 abd2;
SecureZeroMemory(&abd, sizeof(abd)); SecureZeroMemory(&abd2, sizeof(abd2));
abd.cbSize = sizeof(abd); abd2.cbSize = sizeof(abd2);
abd.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr); abd2.hWnd = FindWindowW(L"Shell_TrayWnd", nullptr);
if (abd.hWnd) { if (abd2.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(abd2.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, &abd2);
edge = abd.uEdge; edge = abd2.uEdge;
} }
} else { } else {
WARNING << Utils::getSystemErrorMessage(kFindWindowW); WARNING << Utils::getSystemErrorMessage(kFindWindowW);
@ -1177,7 +1194,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
<< "is" << newDpi << "(was" << oldDpi << ")."; << "is" << newDpi << "(was" << oldDpi << ").";
g_win32Helper()->mutex.lock(); g_win32Helper()->mutex.lock();
g_win32Helper()->data[windowId].dpi = newDpi; g_win32Helper()->data[windowId].dpi = newDpi;
if (data.restoreGeometry.isValid() && !data.restoreGeometry.isNull()) { if (Utils::isValidGeometry(data.restoreGeometry)) {
// Update the window size only. The position should not be changed. // Update the window size only. The position should not be changed.
g_win32Helper()->data[windowId].restoreGeometry.setSize( g_win32Helper()->data[windowId].restoreGeometry.setSize(
Utils::rescaleSize(data.restoreGeometry.size(), oldDpi.x, newDpi.x)); Utils::rescaleSize(data.restoreGeometry.size(), oldDpi.x, newDpi.x));
@ -1199,31 +1216,16 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
Utils::updateWindowFrameMargins(windowId, false); Utils::updateWindowFrameMargins(windowId, false);
} break; } break;
#if (QT_VERSION < QT_VERSION_CHECK(6, 5, 1)) #if (QT_VERSION < QT_VERSION_CHECK(6, 5, 1))
case WM_ENTERSIZEMOVE: case WM_ENTERSIZEMOVE: // Sent to a window when the user drags the title bar or the resize border.
case WM_EXITSIZEMOVE: { case WM_EXITSIZEMOVE: // Sent to a window when the user releases the mouse button (from dragging the title bar or the resize border).
if (!Utils::isWindowNoState(windowId)) { updateRestoreGeometry(false);
break; break;
}
const QRect rect = Utils::getWindowRestoreGeometry(windowId);
if (rect.isNull() || !rect.isValid()) {
WARNING << "The calculated restore geometry is invalid.";
break;
}
const QMutexLocker locker(&g_win32Helper()->mutex);
g_win32Helper()->data[windowId].restoreGeometry = rect;
} break;
case WM_SIZE: { case WM_SIZE: {
if (wParam != SIZE_MAXIMIZED) { if (wParam != SIZE_MAXIMIZED) {
break; break;
} }
if (data.restoreGeometry.isNull() || !data.restoreGeometry.isValid()) { if (!Utils::isValidGeometry(data.restoreGeometry)) {
const QRect rect = Utils::getWindowRestoreGeometry(windowId); updateRestoreGeometry(true);
if (rect.isValid() && !rect.isNull()) {
const QMutexLocker locker(&g_win32Helper()->mutex);
g_win32Helper()->data[windowId].restoreGeometry = rect;
} else {
WARNING << "The calculated restore geometry is invalid.";
}
break; break;
} }
WINDOWPLACEMENT wp; WINDOWPLACEMENT wp;

View File

@ -560,4 +560,10 @@ QSize Utils::rescaleSize(const QSize &oldSize, const quint32 oldDpi, const quint
return newSize.toSize(); // The numbers will be rounded to the nearest integer. return newSize.toSize(); // The numbers will be rounded to the nearest integer.
} }
bool Utils::isValidGeometry(const QRect &rect)
{
// The position of the rectangle is not relevant.
return ((rect.right() > rect.left()) && (rect.bottom() > rect.top()));
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE