From 8c403d5dd03418febd67d50c6d9094a61c036f9b Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周一, 11 12月 2023 14:43:40 +0800
Subject: [PATCH] Add virtual hook

---
 /dev/null                                   |   51 -----------------
 src/core/contexts/abstractwindowcontext.cpp |   17 ++++-
 src/core/contexts/win32windowcontext_p.h    |    3 
 src/core/kernel/nativeeventfilter.cpp       |   25 ++------
 qmsetup                                     |    2 
 src/core/contexts/abstractwindowcontext_p.h |   15 +++-
 src/core/CMakeLists.txt                     |    2 
 src/core/contexts/qtwindowcontext.cpp       |    7 ++
 src/core/contexts/qtwindowcontext_p.h       |    3 +
 src/core/contexts/win32windowcontext.cpp    |   24 ++++++-
 10 files changed, 62 insertions(+), 87 deletions(-)

diff --git a/qmsetup b/qmsetup
index 35836d6..c8d69a4 160000
--- a/qmsetup
+++ b/qmsetup
@@ -1 +1 @@
-Subproject commit 35836d6e80f4395f187e58d94e2030b615d757e9
+Subproject commit c8d69a4895784220061c3cf8be5f092da2ac013e
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 1464e23..837baa0 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -14,8 +14,6 @@
     windowitemdelegate.cpp
     kernel/nativeeventfilter.h
     kernel/nativeeventfilter.cpp
-    kernel/sharedeventfilter.h
-    kernel/sharedeventfilter.cpp
     contexts/abstractwindowcontext_p.h
     contexts/abstractwindowcontext.cpp
 )
diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index 71431ff..d60a6c7 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -88,7 +88,9 @@
         return true;
     }
 
-    void AbstractWindowContext::showSystemMenu(const QPoint &pos){Q_UNUSED(pos)}
+    void AbstractWindowContext::showSystemMenu(const QPoint &pos) {
+        virtual_hook(ShowSystemMenuHook, &const_cast<QPoint &>(pos));
+    }
 
     QRegion AbstractWindowContext::hitTestShape() const {
         if (hitTestVisibleShapeDirty) {
@@ -162,8 +164,17 @@
         return true;
     }
 
-    QObject *AbstractWindowContext::target() const {
-        return m_host;
+    QString AbstractWindowContext::key() const {
+        return {};
+    }
+
+    void AbstractWindowContext::virtual_hook(int id, void *data) {
+        switch (id) {
+            case CentralizeHook:
+                break;
+            default:
+                break;
+        }
     }
 
 }
\ No newline at end of file
diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h
index 28ff0de..fde76b0 100644
--- a/src/core/contexts/abstractwindowcontext_p.h
+++ b/src/core/contexts/abstractwindowcontext_p.h
@@ -9,12 +9,11 @@
 #include <QtGui/QPolygon>
 
 #include <QWKCore/windowagentbase.h>
-#include <QWKCore/sharedeventfilter.h>
 #include <QWKCore/private/windowitemdelegate_p.h>
 
 namespace QWK {
 
-    class QWK_CORE_EXPORT AbstractWindowContext : public QObject, public SharedEventDispatcher {
+    class QWK_CORE_EXPORT AbstractWindowContext : public QObject {
         Q_OBJECT
     public:
         AbstractWindowContext();
@@ -36,16 +35,22 @@
         inline const QObject *titleBar() const;
         bool setTitleBar(const QObject *obj);
 
-        virtual void showSystemMenu(const QPoint &pos);
+        void showSystemMenu(const QPoint &pos);
 
         QRegion hitTestShape() const;
         bool isInSystemButtons(const QPoint &pos, WindowAgentBase::SystemButton *button) const;
         bool isInTitleBarDraggableArea(const QPoint &pos) const;
 
+        virtual QString key() const;
+
+        enum WindowContextHook {
+            CentralizeHook = 1,
+            ShowSystemMenuHook,
+        };
+        virtual void virtual_hook(int id, void *data);
+
     protected:
         virtual bool setupHost() = 0;
-
-        QObject *target() const override;
 
     protected:
         QObject *m_host;
diff --git a/src/core/contexts/qtwindowcontext.cpp b/src/core/contexts/qtwindowcontext.cpp
index 0eaf7c8..631c7e2 100644
--- a/src/core/contexts/qtwindowcontext.cpp
+++ b/src/core/contexts/qtwindowcontext.cpp
@@ -46,6 +46,13 @@
     QtWindowContext::~QtWindowContext() {
     }
 
+    QString QtWindowContext::key() const {
+        return "qt";
+    }
+
+    void QtWindowContext::virtual_hook(int id, void *data) {
+    }
+
     bool QtWindowContext::setupHost() {
         return false;
     }
diff --git a/src/core/contexts/qtwindowcontext_p.h b/src/core/contexts/qtwindowcontext_p.h
index 7a31987..6c264d2 100644
--- a/src/core/contexts/qtwindowcontext_p.h
+++ b/src/core/contexts/qtwindowcontext_p.h
@@ -11,6 +11,9 @@
         QtWindowContext();
         ~QtWindowContext();
 
+        QString key() const override;
+        void virtual_hook(int id, void *data) override;
+
     protected:
         bool setupHost() override;
     };
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 9cf1ecc..c1f3dbd 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -669,10 +669,24 @@
         }
     }
 
-    void Win32WindowContext::showSystemMenu(const QPoint &pos) {
-        auto winId = m_windowHandle->winId();
-        auto hWnd = reinterpret_cast<HWND>(winId);
-        showSystemMenu2(hWnd, {pos.x(), pos.y()}, false, m_delegate->isHostSizeFixed(m_host));
+    QString Win32WindowContext::key() const {
+        return "win32";
+    }
+
+    void Win32WindowContext::virtual_hook(int id, void *data) {
+        switch (id) {
+            case ShowSystemMenuHook: {
+                const auto &pos = *reinterpret_cast<const QPoint *>(data);
+                auto winId = m_windowHandle->winId();
+                auto hWnd = reinterpret_cast<HWND>(winId);
+                showSystemMenu2(hWnd, {pos.x(), pos.y()}, false,
+                                m_delegate->isHostSizeFixed(m_host));
+                return;
+            }
+            default:
+                break;
+        }
+        AbstractWindowContext::virtual_hook(id, data);
     }
 
     bool Win32WindowContext::setupHost() {
@@ -742,7 +756,7 @@
         return false; // Not handled
     }
 
-    static constexpr const auto kMessageTag = WPARAM(0x97CCEA99);
+    static constexpr const auto kMessageTag = WPARAM(0xF1C9ADD4);
 
     static inline constexpr bool isTaggedMessage(WPARAM wParam) {
         return (wParam == kMessageTag);
diff --git a/src/core/contexts/win32windowcontext_p.h b/src/core/contexts/win32windowcontext_p.h
index 758633f..a56aac8 100644
--- a/src/core/contexts/win32windowcontext_p.h
+++ b/src/core/contexts/win32windowcontext_p.h
@@ -21,7 +21,8 @@
             TitleBar,
         };
 
-        void showSystemMenu(const QPoint &pos) override;
+        QString key() const override;
+        void virtual_hook(int id, void *data) override;
 
     protected:
         bool setupHost() override;
diff --git a/src/core/kernel/nativeeventfilter.cpp b/src/core/kernel/nativeeventfilter.cpp
index a3b8c1b..44a1337 100644
--- a/src/core/kernel/nativeeventfilter.cpp
+++ b/src/core/kernel/nativeeventfilter.cpp
@@ -1,7 +1,7 @@
 #include "nativeeventfilter.h"
 
 #include <QtCore/QAbstractNativeEventFilter>
-#include <QtGui/QGuiApplication>
+#include <QtCore/QCoreApplication>
 
 namespace QWK {
 
@@ -19,7 +19,7 @@
 
         bool nativeEventFilter(const QByteArray &eventType, void *message,
                                QT_NATIVE_EVENT_RESULT_TYPE *result) override {
-            for (const auto &child : qAsConst(m_children)) {
+            for (const auto &child : qAsConst(children)) {
                 if (child->nativeEventFilter(eventType, message, result)) {
                     return true;
                 }
@@ -27,22 +27,9 @@
             return false;
         }
 
-        inline int count() const {
-            return m_children.size();
-        }
-
-        inline void addChild(NativeEventFilter *child) {
-            m_children.append(child);
-        }
-
-        inline void removeChild(NativeEventFilter *child) {
-            m_children.removeOne(child);
-        }
+        QVector<NativeEventFilter *> children;
 
         static MasterNativeEventFilter *instance;
-
-    protected:
-        QVector<NativeEventFilter *> m_children;
     };
 
     MasterNativeEventFilter *MasterNativeEventFilter::instance = nullptr;
@@ -51,12 +38,12 @@
         if (!MasterNativeEventFilter::instance) {
             MasterNativeEventFilter::instance = new MasterNativeEventFilter();
         }
-        MasterNativeEventFilter::instance->addChild(this);
+        MasterNativeEventFilter::instance->children.append(this);
     }
 
     NativeEventFilter::~NativeEventFilter() {
-        MasterNativeEventFilter::instance->removeChild(this);
-        if (MasterNativeEventFilter::instance->count() == 0) {
+        MasterNativeEventFilter::instance->children.removeOne(this);
+        if (MasterNativeEventFilter::instance->children.isEmpty()) {
             delete MasterNativeEventFilter::instance;
             MasterNativeEventFilter::instance = nullptr;
         }
diff --git a/src/core/kernel/sharedeventfilter.cpp b/src/core/kernel/sharedeventfilter.cpp
deleted file mode 100644
index 26cb4ac..0000000
--- a/src/core/kernel/sharedeventfilter.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-#include "sharedeventfilter.h"
-
-namespace QWK {
-
-    class EventFilterForwarder : public QObject {
-    public:
-        explicit EventFilterForwarder(SharedEventDispatcherPrivate *master,
-                                      QObject *parent = nullptr)
-            : QObject(parent), master(master) {
-        }
-
-        bool eventFilter(QObject *obj, QEvent *event) override;
-
-    protected:
-        SharedEventDispatcherPrivate *master;
-    };
-
-    class SharedEventDispatcherPrivate {
-    public:
-        explicit SharedEventDispatcherPrivate(SharedEventDispatcher *q)
-            : q(q), forwarder(new EventFilterForwarder(this)) {
-        }
-        ~SharedEventDispatcherPrivate() = default;
-
-        bool dispatch(QObject *obj, QEvent *event) {
-            for (const auto &ef : qAsConst(eventFilters)) {
-                if (ef->eventFilter(obj, event)) {
-                    return true;
-                }
-            }
-            return false;
-        }
-
-        inline void install(SharedEventFilter *eventFilter) {
-            bool empty = eventFilters.isEmpty();
-
-            eventFilters.append(eventFilter);
-            eventFilter->m_dispatcher = q;
-
-            if (empty) {
-                q->target()->installEventFilter(forwarder.get());
-            }
-        }
-
-        inline void uninstall(SharedEventFilter *eventFilter) {
-            if (!eventFilters.removeOne(eventFilter)) {
-                return;
-            }
-            eventFilter->m_dispatcher = nullptr;
-
-            if (eventFilters.isEmpty()) {
-                q->target()->removeEventFilter(forwarder.get());
-            }
-        }
-
-        SharedEventDispatcher *q;
-
-        std::unique_ptr<EventFilterForwarder> forwarder;
-        QVector<SharedEventFilter *> eventFilters;
-    };
-
-    bool EventFilterForwarder::eventFilter(QObject *obj, QEvent *event) {
-        return master->dispatch(obj, event);
-    }
-
-    SharedEventFilter::SharedEventFilter() : m_dispatcher(nullptr) {
-    }
-
-    SharedEventFilter::~SharedEventFilter() {
-        if (m_dispatcher)
-            m_dispatcher->removeSharedEventFilter(this);
-    }
-
-    SharedEventDispatcher::SharedEventDispatcher() : d(new SharedEventDispatcherPrivate(this)) {
-    }
-
-    SharedEventDispatcher::~SharedEventDispatcher() {
-        delete d;
-    }
-
-    void SharedEventDispatcher::installSharedEventFilter(SharedEventFilter *eventFilter) {
-        d->install(eventFilter);
-    }
-
-    void SharedEventDispatcher::removeSharedEventFilter(SharedEventFilter *eventFilter) {
-        d->uninstall(eventFilter);
-    }
-
-
-}
\ No newline at end of file
diff --git a/src/core/kernel/sharedeventfilter.h b/src/core/kernel/sharedeventfilter.h
deleted file mode 100644
index de74f3b..0000000
--- a/src/core/kernel/sharedeventfilter.h
+++ /dev/null
@@ -1,51 +0,0 @@
-#ifndef SHAREDEVENTFILTER_H
-#define SHAREDEVENTFILTER_H
-
-#include <QtCore/QObject>
-#include <QtCore/QVector>
-
-#include <QWKCore/qwkcoreglobal.h>
-
-namespace QWK {
-
-    class SharedEventDispatcherPrivate;
-
-    class SharedEventDispatcher;
-
-    class QWK_CORE_EXPORT SharedEventFilter {
-    public:
-        SharedEventFilter();
-        virtual ~SharedEventFilter();
-
-    public:
-        virtual bool eventFilter(QObject *obj, QEvent *event) = 0;
-
-    private:
-        SharedEventDispatcher *m_dispatcher;
-
-        Q_DISABLE_COPY(SharedEventFilter)
-
-        friend class SharedEventDispatcher;
-        friend class SharedEventDispatcherPrivate;
-    };
-
-    class QWK_CORE_EXPORT SharedEventDispatcher {
-    public:
-        SharedEventDispatcher();
-        virtual ~SharedEventDispatcher();
-
-    public:
-        virtual QObject *target() const = 0;
-
-        void installSharedEventFilter(SharedEventFilter *eventFilter);
-        void removeSharedEventFilter(SharedEventFilter *eventFilter);
-
-    protected:
-        SharedEventDispatcherPrivate *d;
-
-        Q_DISABLE_COPY(SharedEventDispatcher)
-    };
-
-}
-
-#endif // SHAREDEVENTFILTER_H

--
Gitblit v1.9.1