From abdad66bae077da6c3480db457beb47df5eda813 Mon Sep 17 00:00:00 2001
From: SineStriker <trueful@163.com>
Date: 周六, 16 12月 2023 19:27:16 +0800
Subject: [PATCH] Fix mac window mistakes

---
 src/widgets/widgetwindowagent_win.cpp   |    3 ---
 src/core/contexts/qtwindowcontext.cpp   |    6 +-----
 src/widgets/widgetwindowagent_cocoa.cpp |   26 ++++++++++++++++----------
 3 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/core/contexts/qtwindowcontext.cpp b/src/core/contexts/qtwindowcontext.cpp
index 6887cfd..edf73f3 100644
--- a/src/core/contexts/qtwindowcontext.cpp
+++ b/src/core/contexts/qtwindowcontext.cpp
@@ -74,8 +74,6 @@
     }
 
     class WindowEventFilter : public QObject {
-        Q_OBJECT
-
     public:
         explicit WindowEventFilter(AbstractWindowContext *context, QObject *parent = nullptr);
         ~WindowEventFilter() override;
@@ -201,6 +199,4 @@
         return true;
     }
 
-}
-
-#include "qtwindowcontext.moc"
+}
\ No newline at end of file
diff --git a/src/widgets/widgetwindowagent_cocoa.cpp b/src/widgets/widgetwindowagent_cocoa.cpp
index 815aadd..cccb5fe 100644
--- a/src/widgets/widgetwindowagent_cocoa.cpp
+++ b/src/widgets/widgetwindowagent_cocoa.cpp
@@ -1,10 +1,10 @@
 #include "widgetwindowagent_p.h"
 
+#include <QtCore/QDebug>
+
 namespace QWK {
 
     class TitleBarEventFilter : public QObject {
-        Q_OBJECT
-
     public:
         explicit TitleBarEventFilter(AbstractWindowContext *context, QObject *parent = nullptr);
         ~TitleBarEventFilter() override;
@@ -15,9 +15,12 @@
     private:
         AbstractWindowContext *m_context;
         bool m_leftButtonPressed;
-    }
+        bool m_moving;
+    };
 
-    TitleBarEventFilter::TitleBarEventFilter(QObject *parent) : QObject(parent), m_context(context), m_leftButtonPressed(false) {}
+    TitleBarEventFilter::TitleBarEventFilter(AbstractWindowContext *context, QObject *parent)
+        : QObject(parent), m_context(context), m_leftButtonPressed(false), m_moving(false) {
+    }
 
     TitleBarEventFilter::~TitleBarEventFilter() = default;
 
@@ -31,8 +34,8 @@
         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();
+        const QPoint scenePos = me->windowPos().toPoint();
+        const QPoint globalPos = me->screenPos().toPoint();
 #endif
         if (!m_context->isInTitleBarDraggableArea(scenePos)) {
             return false;
@@ -41,6 +44,7 @@
             case QEvent::MouseButtonPress: {
                 if (me->button() == Qt::LeftButton) {
                     m_leftButtonPressed = true;
+                    m_moving = false;
                     event->accept();
                     return true;
                 }
@@ -49,6 +53,7 @@
             case QEvent::MouseButtonRelease: {
                 if (me->button() == Qt::LeftButton) {
                     m_leftButtonPressed = false;
+                    m_moving = false;
                     event->accept();
                     return true;
                 }
@@ -56,7 +61,10 @@
             }
             case QEvent::MouseMove: {
                 if (m_leftButtonPressed) {
-                    static_cast<QWidget *>(object)->windowHandle()->startSystemMove();
+                    if (!m_moving) {
+                        m_moving = true;
+                        m_context->window()->startSystemMove();
+                    }
                     event->accept();
                     return true;
                 }
@@ -64,7 +72,7 @@
             }
             case QEvent::MouseButtonDblClick: {
                 if (me->button() == Qt::LeftButton) {
-                    QWidget *window = static_cast<QWidget *>(object)->window();
+                    auto window = static_cast<QWidget *>(object)->window();
                     if (!window->isFullScreen()) {
                         if (window->isMaximized()) {
                             window->showNormal();
@@ -90,5 +98,3 @@
     }
 
 }
-
-#include "widgetwindowagent_cocoa.moc"
diff --git a/src/widgets/widgetwindowagent_win.cpp b/src/widgets/widgetwindowagent_win.cpp
index aa7e9c0..3eb9f1e 100644
--- a/src/widgets/widgetwindowagent_win.cpp
+++ b/src/widgets/widgetwindowagent_win.cpp
@@ -8,7 +8,6 @@
 namespace QWK {
 
     class WidgetBorderHandler : public QObject, public NativeEventFilter {
-        Q_OBJECT
     public:
         explicit WidgetBorderHandler(QWidget *widget, AbstractWindowContext *ctx)
             : QObject(ctx), widget(widget), ctx(ctx) {
@@ -114,5 +113,3 @@
     }
 
 }
-
-#include "widgetwindowagent_win.moc"

--
Gitblit v1.9.1