From 3fbcf1aa8f91a804af50fad4e2b122f698259c94 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 摹曛, 09 5月 2024 23:45:28 +0800
Subject: [PATCH] Remove `isHostSizeFixed` in delegate

---
 src/core/contexts/qtwindowcontext.cpp |  194 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 130 insertions(+), 64 deletions(-)

diff --git a/src/core/contexts/qtwindowcontext.cpp b/src/core/contexts/qtwindowcontext.cpp
index edf73f3..644ce64 100644
--- a/src/core/contexts/qtwindowcontext.cpp
+++ b/src/core/contexts/qtwindowcontext.cpp
@@ -1,4 +1,13 @@
+// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
+// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
+// SPDX-License-Identifier: Apache-2.0
+
 #include "qtwindowcontext_p.h"
+
+#include <QtCore/QDebug>
+
+#include "qwkglobal_p.h"
+#include "systemwindow_p.h"
 
 namespace QWK {
 
@@ -40,8 +49,7 @@
 #endif
     }
 
-    static inline Qt::Edges calculateWindowEdges(const QWindow *window, const QPoint &pos)
-    {
+    static inline Qt::Edges calculateWindowEdges(const QWindow *window, const QPoint &pos) {
 #ifdef Q_OS_MACOS
         Q_UNUSED(window);
         Q_UNUSED(pos);
@@ -73,94 +81,145 @@
 #endif
     }
 
-    class WindowEventFilter : public QObject {
+    class QtWindowEventFilter : public SharedEventFilter {
     public:
-        explicit WindowEventFilter(AbstractWindowContext *context, QObject *parent = nullptr);
-        ~WindowEventFilter() override;
+        explicit QtWindowEventFilter(AbstractWindowContext *context);
+        ~QtWindowEventFilter() override;
+
+        enum WindowStatus {
+            Idle,
+            WaitingRelease,
+            PreparingMove,
+            Moving,
+            Resizing,
+        };
 
     protected:
-        bool eventFilter(QObject *object, QEvent *event) override;
+        bool sharedEventFilter(QObject *object, QEvent *event) override;
 
     private:
         AbstractWindowContext *m_context;
-        bool m_leftButtonPressed;
         bool m_cursorShapeChanged;
+        WindowStatus m_windowStatus;
     };
 
-    WindowEventFilter::WindowEventFilter(AbstractWindowContext *context, QObject *parent) : QObject(parent), m_context(context), m_leftButtonPressed(false), m_cursorShapeChanged(false) {}
+    QtWindowEventFilter::QtWindowEventFilter(AbstractWindowContext *context)
+        : m_context(context), m_cursorShapeChanged(false), m_windowStatus(Idle) {
+        m_context->installSharedEventFilter(this);
+    }
 
-    WindowEventFilter::~WindowEventFilter() = default;
+    QtWindowEventFilter::~QtWindowEventFilter() = default;
 
-    bool WindowEventFilter::eventFilter(QObject *object, QEvent *event) {
-        const auto type = event->type();
+    bool QtWindowEventFilter::sharedEventFilter(QObject *obj, QEvent *event) {
+        Q_UNUSED(obj)
+
+        auto type = event->type();
         if (type < QEvent::MouseButtonPress || type > QEvent::MouseMove) {
             return false;
         }
-        QObject *host = m_context->host();
-        QWindow *window = m_context->window();
-        WindowItemDelegate *delegate = m_context->delegate();
-        const bool fixedSize = delegate->isHostSizeFixed(host);
-        const auto mouseEvent = static_cast<const QMouseEvent *>(event);
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
-        const QPoint scenePos = mouseEvent->scenePosition().toPoint();
-        const QPoint globalPos = mouseEvent->globalPosition().toPoint();
-#else
-        const QPoint scenePos = mouseEvent->windowPos().toPoint();
-        const QPoint globalPos = mouseEvent->screenPos().toPoint();
-#endif
-        const bool inTitleBar = m_context->isInTitleBarDraggableArea(scenePos);
+        auto host = m_context->host();
+        auto window = m_context->window();
+        auto delegate = m_context->delegate();
+        auto me = static_cast<const QMouseEvent *>(event);
+        bool fixedSize = m_context->isHostSizeFixed();
+
+        QPoint scenePos = getMouseEventScenePos(me);
+        QPoint globalPos = getMouseEventGlobalPos(me);
+
+        bool inTitleBar = m_context->isInTitleBarDraggableArea(scenePos);
         switch (type) {
             case QEvent::MouseButtonPress: {
-                if (mouseEvent->button() == Qt::LeftButton) {
-                    m_leftButtonPressed = true;
-                    if (!fixedSize) {
-                        const Qt::Edges edges = calculateWindowEdges(window, scenePos);
-                        if (edges != Qt::Edges{}) {
-                            window->startSystemResize(edges);
+                switch (me->button()) {
+                    case Qt::LeftButton: {
+                        if (!fixedSize) {
+                            Qt::Edges edges = calculateWindowEdges(window, scenePos);
+                            if (edges != Qt::Edges()) {
+                                m_windowStatus = Resizing;
+                                startSystemResize(window, edges);
+                                event->accept();
+                                return true;
+                            }
+                        }
+                        if (inTitleBar) {
+                            // If we call startSystemMove() now but release the mouse without actual
+                            // movement, there will be no MouseReleaseEvent, so we defer it when the
+                            // mouse is actually moving for the first time
+                            m_windowStatus = PreparingMove;
                             event->accept();
                             return true;
                         }
+                        break;
                     }
-                    if (inTitleBar) {
-                        event->accept();
-                        return true;
+                    case Qt::RightButton: {
+                        m_context->showSystemMenu(globalPos);
+                        break;
                     }
+                    default:
+                        break;
                 }
+                m_windowStatus = WaitingRelease;
                 break;
             }
+
             case QEvent::MouseButtonRelease: {
-                if (mouseEvent->button() == Qt::LeftButton) {
-                    m_leftButtonPressed = false;
-                    if (inTitleBar) {
+                switch (m_windowStatus) {
+                    case PreparingMove:
+                    case Moving:
+                    case Resizing: {
+                        m_windowStatus = Idle;
                         event->accept();
                         return true;
                     }
-                }
-                break;
-            }
-            case QEvent::MouseMove: {
-                if (!fixedSize) {
-                    const Qt::CursorShape shape = calculateCursorShape(window, scenePos);
-                    if (shape == Qt::ArrowCursor) {
-                        if (m_cursorShapeChanged) {
-                            delegate->restoreCursorShape(host);
-                            m_cursorShapeChanged = false;
+                    case WaitingRelease: {
+                        m_windowStatus = Idle;
+                        break;
+                    }
+                    default: {
+                        if (inTitleBar) {
+                            event->accept();
+                            return true;
                         }
-                    } else {
-                        delegate->setCursorShape(host, shape);
-                        m_cursorShapeChanged = true;
+                        break;
                     }
                 }
-                if (m_leftButtonPressed && inTitleBar) {
-                    window->startSystemMove();
-                    event->accept();
-                    return true;
+                break;
+            }
+
+            case QEvent::MouseMove: {
+                switch (m_windowStatus) {
+                    case Moving: {
+                        return true;
+                    }
+                    case PreparingMove: {
+                        m_windowStatus = Moving;
+                        startSystemMove(window);
+                        event->accept();
+                        return true;
+                    }
+                    case Idle: {
+                        if (!fixedSize) {
+                            const Qt::CursorShape shape = calculateCursorShape(window, scenePos);
+                            if (shape == Qt::ArrowCursor) {
+                                if (m_cursorShapeChanged) {
+                                    delegate->restoreCursorShape(host);
+                                    m_cursorShapeChanged = false;
+                                }
+                            } else {
+                                delegate->setCursorShape(host, shape);
+                                m_cursorShapeChanged = true;
+                            }
+                        }
+                        break;
+                    }
+                    default:
+                        break;
                 }
                 break;
             }
+
             case QEvent::MouseButtonDblClick: {
-                if (mouseEvent->button() == Qt::LeftButton && inTitleBar && !fixedSize) {
-                    const Qt::WindowStates windowState = delegate->getWindowState(host);
+                if (me->button() == Qt::LeftButton && inTitleBar && !fixedSize) {
+                    Qt::WindowStates windowState = delegate->getWindowState(host);
                     if (!(windowState & Qt::WindowFullScreen)) {
                         if (windowState & Qt::WindowMaximized) {
                             delegate->setWindowState(host, windowState & ~Qt::WindowMaximized);
@@ -173,15 +232,15 @@
                 }
                 break;
             }
-            default: {
-                Q_UNREACHABLE();
-                return false;
-            }
+
+            default:
+                break;
         }
         return false;
     }
 
     QtWindowContext::QtWindowContext() : AbstractWindowContext() {
+        qtWindowEventFilter = std::make_unique<QtWindowEventFilter>(this);
     }
 
     QtWindowContext::~QtWindowContext() = default;
@@ -191,12 +250,19 @@
     }
 
     void QtWindowContext::virtual_hook(int id, void *data) {
+        AbstractWindowContext::virtual_hook(id, data);
     }
 
-    bool QtWindowContext::setupHost() {
-        m_delegate->setWindowFlags(m_host, Qt::FramelessWindowHint);
-        m_windowHandle->installEventFilter(new WindowEventFilter(this, m_windowHandle));
-        return true;
+    void QtWindowContext::winIdChanged(WId winId, WId oldWinId) {
+        if (!m_windowHandle) {
+            m_delegate->setWindowFlags(m_host, m_delegate->getWindowFlags(m_host) &
+                                                   ~Qt::FramelessWindowHint);
+            return;
+        }
+
+        // Allocate new resources
+        m_delegate->setWindowFlags(m_host,
+                                   m_delegate->getWindowFlags(m_host) | Qt::FramelessWindowHint);
     }
 
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1