From f35407ce5cd412b2f507e087b5f1d904a151a35b Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 12 Nov 2020 20:34:12 +0800 Subject: [PATCH] Minor tweaks. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- framelesshelper.cpp | 90 ++++++++++++++++++++++------------------ framelesshelper.h | 12 +++--- winnativeeventfilter.cpp | 14 +++---- 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/framelesshelper.cpp b/framelesshelper.cpp index 873e0cc..c94703e 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -180,30 +180,30 @@ void FramelessHelper::setTitleBarHeight(const int val) m_titleBarHeight = val; } -QList FramelessHelper::getIgnoreAreas(QObject *obj) const +QList FramelessHelper::getIgnoreAreas(QObject *obj) const { Q_ASSERT(obj); return m_ignoreAreas.value(obj); } -void FramelessHelper::addIgnoreArea(QObject *obj, const QRect &val) +void FramelessHelper::addIgnoreArea(QObject *obj, const QRectF &val) { Q_ASSERT(obj); - QList areas = m_ignoreAreas[obj]; + QList areas = m_ignoreAreas[obj]; areas.append(val); m_ignoreAreas[obj] = areas; } -QList FramelessHelper::getDraggableAreas(QObject *obj) const +QList FramelessHelper::getDraggableAreas(QObject *obj) const { Q_ASSERT(obj); return m_draggableAreas.value(obj); } -void FramelessHelper::addDraggableArea(QObject *obj, const QRect &val) +void FramelessHelper::addDraggableArea(QObject *obj, const QRectF &val) { Q_ASSERT(obj); - QList areas = m_draggableAreas[obj]; + QList areas = m_draggableAreas[obj]; areas.append(val); m_draggableAreas[obj] = areas; } @@ -387,60 +387,62 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) } return Qt::CursorShape::ArrowCursor; }; - const auto isInSpecificAreas = [](const int x, const int y, const QList &areas) -> bool { - if (!areas.isEmpty()) { - for (auto &&area : qAsConst(areas)) { - if (area.contains(x, y)) { - return true; - } + const auto isInSpecificAreas = [](const QPointF &mousePos, const QList &areas) -> bool { + if (areas.isEmpty()) { + return false; + } + for (auto &&area : qAsConst(areas)) { + if (area.contains(mousePos)) { + return true; } } return false; }; - const auto isInSpecificObjects = - [](const int x, const int y, const QList &objects) -> bool { - if (!objects.isEmpty()) { - for (auto &&obj : qAsConst(objects)) { - if (!obj) { - continue; - } + const auto isInSpecificObjects = [](const QPointF &mousePos, + const QList &objects) -> bool { + if (objects.isEmpty()) { + return false; + } + for (auto &&obj : qAsConst(objects)) { + if (!obj) { + continue; + } #ifdef QT_WIDGETS_LIB - const auto widget = qobject_cast(obj); - if (widget) { + const auto widget = qobject_cast(obj); + if (widget) { #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) - const QPointF pos = widget->mapToGlobal(QPointF{0, 0}); + const QPointF pos = widget->mapToGlobal(QPointF{0, 0}); #else - const QPoint pos = widget->mapToGlobal(QPoint{0, 0}); + const QPoint pos = widget->mapToGlobal(QPoint{0, 0}); #endif - if (QRect(pos.x(), pos.y(), widget->width(), widget->height()).contains(x, y)) { - return true; - } + if (QRectF(pos.x(), pos.y(), widget->width(), widget->height()).contains(mousePos)) { + return true; } + } #endif #ifdef QT_QUICK_LIB - const auto quickItem = qobject_cast(obj); - if (quickItem) { - const QPointF pos = quickItem->mapToGlobal({0, 0}); - if (QRectF(pos.x(), pos.y(), quickItem->width(), quickItem->height()) - .contains(x, y)) { - return true; - } + const auto quickItem = qobject_cast(obj); + if (quickItem) { + const QPointF pos = quickItem->mapToGlobal({0, 0}); + if (QRectF(pos.x(), pos.y(), quickItem->width(), quickItem->height()) + .contains(mousePos)) { + return true; } -#endif } +#endif } return false; }; const auto isInIgnoreAreas = [this, &isInSpecificAreas](const QPointF &point, QObject *window) -> bool { Q_ASSERT(window); - return isInSpecificAreas(point.x(), point.y(), getIgnoreAreas(window)); + return isInSpecificAreas(point, getIgnoreAreas(window)); }; const auto isInIgnoreObjects = [this, &isInSpecificObjects](const QPointF &point, QObject *window) -> bool { Q_ASSERT(window); #if defined(QT_WIDGETS_LIB) || defined(QT_QUICK_LIB) - return isInSpecificObjects(point.x(), point.y(), getIgnoreObjects(window)); + return isInSpecificObjects(point, getIgnoreObjects(window)); #else Q_UNUSED(point) Q_UNUSED(window) @@ -451,14 +453,14 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) QObject *window) -> bool { Q_ASSERT(window); const auto areas = getDraggableAreas(window); - return (areas.isEmpty() ? true : isInSpecificAreas(point.x(), point.y(), areas)); + return (areas.isEmpty() ? true : isInSpecificAreas(point, areas)); }; const auto isInDraggableObjects = [this, &isInSpecificObjects](const QPointF &point, QObject *window) -> bool { Q_ASSERT(window); #if defined(QT_WIDGETS_LIB) || defined(QT_QUICK_LIB) const auto objs = getDraggableObjects(window); - return (objs.isEmpty() ? true : isInSpecificObjects(point.x(), point.y(), objs)); + return (objs.isEmpty() ? true : isInSpecificObjects(point, objs)); #else Q_UNUSED(point) Q_UNUSED(window) @@ -498,7 +500,7 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) Q_ASSERT(object); QWindow *window = getWindowHandle(object); if (window) { - const QPointF deltaPoint = globalPoint - m_pOldMousePos; + //const QPointF deltaPoint = globalPoint - m_pOldMousePos; const Qt::Edges edges = getWindowEdges(point, window->width(), window->height()); if (edges == Qt::Edges{}) { if (isInTitlebarArea(globalPoint, point, object)) { @@ -605,8 +607,14 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) } } break; case QEvent::MouseButtonRelease: { - m_bIsMRBPressed = false; - m_pOldMousePos = {}; + const auto mouseEvent = static_cast(event); + if (mouseEvent) { + if (mouseEvent->button() != Qt::MouseButton::LeftButton) { + break; + } + m_bIsMRBPressed = false; + m_pOldMousePos = {}; + } } break; case QEvent::TouchBegin: case QEvent::TouchUpdate: { diff --git a/framelesshelper.h b/framelesshelper.h index dceb78b..637bd46 100644 --- a/framelesshelper.h +++ b/framelesshelper.h @@ -29,7 +29,7 @@ #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) #include #include -#include +#include QT_BEGIN_NAMESPACE QT_FORWARD_DECLARE_CLASS(QWindow) @@ -58,11 +58,11 @@ public: int getTitleBarHeight() const; void setTitleBarHeight(const int val); - void addIgnoreArea(QObject *obj, const QRect &val); - QList getIgnoreAreas(QObject *obj) const; + void addIgnoreArea(QObject *obj, const QRectF &val); + QList getIgnoreAreas(QObject *obj) const; - void addDraggableArea(QObject *obj, const QRect &val); - QList getDraggableAreas(QObject *obj) const; + void addDraggableArea(QObject *obj, const QRectF &val); + QList getDraggableAreas(QObject *obj) const; void addIgnoreObject(QObject *obj, QObject *val); QList getIgnoreObjects(QObject *obj) const; @@ -84,7 +84,7 @@ private: // the scale factor is 1.0. Don't know how to acquire these values on UNIX // platforms through native API. int m_borderWidth = 8, m_borderHeight = 8, m_titleBarHeight = 30; - QHash> m_ignoreAreas = {}, m_draggableAreas = {}; + QHash> m_ignoreAreas = {}, m_draggableAreas = {}; QHash> m_ignoreObjects = {}, m_draggableObjects = {}; QHash m_fixedSize = {}, m_disableTitleBar = {}; }; diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index 0ece571..03338c2 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -814,7 +814,7 @@ BOOL IsFullScreen(const HWND handle) return FALSE; } -BOOL IsTopLevel(const HWND handle) +[[maybe_unused]] BOOL IsTopLevel(const HWND handle) { Q_ASSERT(handle); if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, handle)) { @@ -971,7 +971,7 @@ qreal GetDevicePixelRatioForWindow(const HWND handle) return GetPreferedNumber(result); } -RECT GetFrameSizeForWindow(const HWND handle, const BOOL includingTitleBar = FALSE) +[[maybe_unused]] RECT GetFrameSizeForWindow(const HWND handle, const BOOL includingTitleBar = FALSE) { Q_ASSERT(handle); RECT rect = {0, 0, 0, 0}; @@ -1161,13 +1161,9 @@ bool displaySystemMenu_internal(const HWND handle, const bool isRtl, const LPARA const POINT globalMouse{GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)}; POINT localMouse = globalMouse; WNEF_EXECUTE_WINAPI(ScreenToClient, handle, &localMouse) - const int bh - = WinNativeEventFilter::getSystemMetric(handle, - WinNativeEventFilter::SystemMetric::BorderHeight, - true); const int tbh = WinNativeEventFilter::getSystemMetric( handle, WinNativeEventFilter::SystemMetric::TitleBarHeight, true); - const bool isTitleBar = localMouse.y <= (tbh + bh); + const bool isTitleBar = localMouse.y <= tbh; if (isTitleBar && !IsFullScreen(handle)) { return WinNativeEventFilter::displaySystemMenu(handle, isRtl, @@ -1895,7 +1891,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, const int bh = getSystemMetric(msg->hwnd, SystemMetric::BorderHeight, true); const int tbh = getSystemMetric(msg->hwnd, SystemMetric::TitleBarHeight, true); const bool isTitleBar = (customDrag ? (isInDraggableAreas && isInDraggableObjects) - : (localMouse.y() <= (tbh + bh))) + : (localMouse.y() <= tbh)) && isResizePermitted && !data->disableTitleBar; const bool isTop = (localMouse.y() <= bh) && isResizePermitted; if (shouldHaveWindowFrame()) { @@ -1948,7 +1944,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, const bool isRight = (localMouse.x() >= (ww - (bw * factor))); const bool fixedSize = data->fixedSize; const auto getBorderValue = [fixedSize](int value) -> int { - // HTBORDER: non-resizeable window border. + // HTBORDER: non-resizable window border. return fixedSize ? HTBORDER : value; }; if (isTop) {