From 499afac952920595e6c3d4f94806427f894c7674 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周六, 02 12月 2023 19:05:24 +0800
Subject: [PATCH] optimize code

---
 src/core/windowitemdelegate.h               |    9 ++--
 src/quick/quickwindowagent.h                |    2 -
 src/widgets/widgetitemdelegate_p.h          |    2 -
 src/core/contexts/abstractwindowcontext.cpp |   18 ++++++++-
 src/core/corewindowagent.h                  |    2 -
 src/core/contexts/abstractwindowcontext_p.h |   18 ++++----
 src/quick/quickwindowagent_p.h              |    2 -
 src/widgets/widgetwindowagent_p.h           |    2 -
 src/core/contexts/qtwindowcontext_p.h       |    4 -
 src/core/contexts/win32windowcontext.cpp    |    6 ++-
 src/core/corewindowagent.cpp                |   15 ++++---
 src/core/corewindowagent_p.h                |    8 ++--
 src/core/contexts/win32windowcontext_p.h    |    4 -
 src/widgets/widgetwindowagent.cpp           |    2 
 src/quick/quickitemdelegate_p.h             |    2 -
 src/core/contexts/qtwindowcontext.cpp       |    2 
 src/widgets/widgetwindowagent.h             |    2 -
 src/quick/quickwindowagent.cpp              |    2 
 18 files changed, 52 insertions(+), 50 deletions(-)

diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index faac874..a6d4aeb 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -2,7 +2,9 @@
 
 namespace QWK {
 
-    AbstractWindowContext::~AbstractWindowContext() = default;
+    AbstractWindowContext::~AbstractWindowContext() {
+        delete m_delegate;
+    }
 
     void AbstractWindowContext::setupWindow(QWindow *window) {
         Q_ASSERT(window);
@@ -37,11 +39,12 @@
         } else {
             m_hitTestVisibleRects.removeAll(rect);
         }
+        hitTestVisibleShapeDirty = true;
         return true;
     }
 
     bool AbstractWindowContext::setSystemButton(CoreWindowAgent::SystemButton button,
-                                                     QObject *obj) {
+                                                QObject *obj) {
         Q_ASSERT(obj);
         Q_ASSERT(button != CoreWindowAgent::Unknown);
         if (!obj || (button == CoreWindowAgent::Unknown)) {
@@ -72,4 +75,15 @@
         // ?
     }
 
+    QRegion AbstractWindowContext::hitTestShape() const {
+        if (hitTestVisibleShapeDirty) {
+            hitTestVisibleShape = {};
+            for (const auto &rect : m_hitTestVisibleRects) {
+                hitTestVisibleShape += rect;
+            }
+            hitTestVisibleShapeDirty = false;
+        }
+        return hitTestVisibleShape;
+    }
+
 }
\ No newline at end of file
diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h
index a633b18..4c04734 100644
--- a/src/core/contexts/abstractwindowcontext_p.h
+++ b/src/core/contexts/abstractwindowcontext_p.h
@@ -1,6 +1,8 @@
 #ifndef ABSTRACTWINDOWCONTEXT_P_H
 #define ABSTRACTWINDOWCONTEXT_P_H
 
+#include <array>
+
 #include <QtCore/QSet>
 #include <QtGui/QWindow>
 #include <QtGui/QPolygon>
@@ -12,12 +14,9 @@
 
     class QWK_CORE_EXPORT AbstractWindowContext : public QObject {
         Q_OBJECT
-        Q_DISABLE_COPY(AbstractWindowContext)
-
     public:
-        inline AbstractWindowContext(QWindow *window, WindowItemDelegatePtr delegate)
-            : m_windowHandle(window), m_delegate(std::move(delegate))
-        {
+        inline AbstractWindowContext(QWindow *window, WindowItemDelegate *delegate)
+            : m_windowHandle(window), m_delegate(delegate) {
         }
         ~AbstractWindowContext() override;
 
@@ -39,9 +38,11 @@
 
         void showSystemMenu(const QPoint &pos);
 
+        QRegion hitTestShape() const;
+
     protected:
         QWindow *m_windowHandle;
-        WindowItemDelegatePtr m_delegate;
+        WindowItemDelegate *m_delegate;
 
         QSet<QObject *> m_hitTestVisibleItems;
         QList<QRect> m_hitTestVisibleRects;
@@ -49,8 +50,9 @@
         QObject *m_titleBar{};
         std::array<QObject *, CoreWindowAgent::NumSystemButton> m_systemButtons{};
 
+        // Cached shape
         mutable bool hitTestVisibleShapeDirty{};
-        mutable QPolygon hitTestVisibleShape;
+        mutable QRegion hitTestVisibleShape;
     };
 
     inline QWindow *AbstractWindowContext::window() const {
@@ -69,8 +71,6 @@
     inline QObject *AbstractWindowContext::titleBar() const {
         return m_titleBar;
     }
-
-    using WindowContextPtr = std::shared_ptr<AbstractWindowContext>;
 
 }
 
diff --git a/src/core/contexts/qtwindowcontext.cpp b/src/core/contexts/qtwindowcontext.cpp
index b0e8e33..a9740f1 100644
--- a/src/core/contexts/qtwindowcontext.cpp
+++ b/src/core/contexts/qtwindowcontext.cpp
@@ -40,7 +40,7 @@
 #endif
     }
 
-    QtWindowContext::QtWindowContext(QWindow *window, WindowItemDelegatePtr delegate)
+    QtWindowContext::QtWindowContext(QWindow *window, WindowItemDelegate *delegate)
         : AbstractWindowContext(window, delegate) {
     }
 
diff --git a/src/core/contexts/qtwindowcontext_p.h b/src/core/contexts/qtwindowcontext_p.h
index da3d640..6daccb8 100644
--- a/src/core/contexts/qtwindowcontext_p.h
+++ b/src/core/contexts/qtwindowcontext_p.h
@@ -7,10 +7,8 @@
 
     class QWK_CORE_EXPORT QtWindowContext : public AbstractWindowContext {
         Q_OBJECT
-        Q_DISABLE_COPY(QtWindowContext)
-
     public:
-        QtWindowContext(QWindow *window, WindowItemDelegatePtr delegate);
+        QtWindowContext(QWindow *window, WindowItemDelegate *delegate);
         ~QtWindowContext();
 
     public:
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 5154308..a0d5779 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -32,8 +32,8 @@
         return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam);
     }
 
-    Win32WindowContext::Win32WindowContext(QWindow *window, WindowItemDelegatePtr delegate)
-        : AbstractWindowContext(window, std::move(delegate)), windowId(0) {
+    Win32WindowContext::Win32WindowContext(QWindow *window, WindowItemDelegate *delegate)
+        : AbstractWindowContext(window, delegate), windowId(0) {
     }
 
     Win32WindowContext::~Win32WindowContext() {
@@ -70,6 +70,8 @@
         // TODO: Implement
         // ...
 
+        Q_UNUSED(windowId)
+
         return false; // Not handled
     }
 
diff --git a/src/core/contexts/win32windowcontext_p.h b/src/core/contexts/win32windowcontext_p.h
index ff6fea5..b218497 100644
--- a/src/core/contexts/win32windowcontext_p.h
+++ b/src/core/contexts/win32windowcontext_p.h
@@ -8,10 +8,8 @@
 
     class QWK_CORE_EXPORT Win32WindowContext : public AbstractWindowContext {
         Q_OBJECT
-        Q_DISABLE_COPY(Win32WindowContext)
-
     public:
-        Win32WindowContext(QWindow *window, WindowItemDelegatePtr delegate);
+        Win32WindowContext(QWindow *window, WindowItemDelegate *delegate);
         ~Win32WindowContext() override;
 
     public:
diff --git a/src/core/corewindowagent.cpp b/src/core/corewindowagent.cpp
index b705937..c843273 100644
--- a/src/core/corewindowagent.cpp
+++ b/src/core/corewindowagent.cpp
@@ -11,28 +11,31 @@
 
 namespace QWK {
 
-    CoreWindowAgentPrivate::CoreWindowAgentPrivate() : q_ptr(nullptr), eventHandler(nullptr) {
+    CoreWindowAgentPrivate::CoreWindowAgentPrivate() : eventHandler(nullptr) {
     }
 
-    CoreWindowAgentPrivate::~CoreWindowAgentPrivate() = default;
+    CoreWindowAgentPrivate::~CoreWindowAgentPrivate() {
+        delete eventHandler;
+    }
 
     void CoreWindowAgentPrivate::init() {
     }
 
-    bool CoreWindowAgentPrivate::setup(QWindow *window, const WindowItemDelegatePtr &delegate) {
+    bool CoreWindowAgentPrivate::setup(QWindow *window, WindowItemDelegate *delegate) {
         Q_ASSERT(window);
         if (!window) {
             return false;
         }
+
         auto handler =
 #ifdef Q_OS_WINDOWS
-            std::make_shared<Win32WindowContext>(window, delegate)
+            new Win32WindowContext(window, delegate)
 #else
-            std::make_shared<QtWindowContext>(window, delegate)
+            new QtWindowContext(window, delegate)
 #endif
             ;
-
         if (!handler->setup()) {
+            delete handler;
             return false;
         }
         eventHandler = handler;
diff --git a/src/core/corewindowagent.h b/src/core/corewindowagent.h
index 05a60d2..9cfae3c 100644
--- a/src/core/corewindowagent.h
+++ b/src/core/corewindowagent.h
@@ -11,9 +11,7 @@
 
     class QWK_CORE_EXPORT CoreWindowAgent : public QObject {
         Q_OBJECT
-        Q_DISABLE_COPY(CoreWindowAgent)
         Q_DECLARE_PRIVATE(CoreWindowAgent)
-
     public:
         ~CoreWindowAgent() override;
 
diff --git a/src/core/corewindowagent_p.h b/src/core/corewindowagent_p.h
index 4c31d01..0c9be23 100644
--- a/src/core/corewindowagent_p.h
+++ b/src/core/corewindowagent_p.h
@@ -7,9 +7,7 @@
 namespace QWK {
 
     class QWK_CORE_EXPORT CoreWindowAgentPrivate {
-        Q_DISABLE_COPY(CoreWindowAgentPrivate)
         Q_DECLARE_PUBLIC(CoreWindowAgent)
-
     public:
         CoreWindowAgentPrivate();
         virtual ~CoreWindowAgentPrivate();
@@ -18,9 +16,11 @@
 
         CoreWindowAgent *q_ptr; // no need to initialize
 
-        bool setup(QWindow *window, const WindowItemDelegatePtr &delegate);
+        bool setup(QWindow *window, WindowItemDelegate *delegate);
 
-        WindowContextPtr eventHandler;
+        AbstractWindowContext *eventHandler;
+
+        Q_DISABLE_COPY_MOVE(CoreWindowAgentPrivate)
     };
 
 }
diff --git a/src/core/windowitemdelegate.h b/src/core/windowitemdelegate.h
index 9886fa5..985dedc 100644
--- a/src/core/windowitemdelegate.h
+++ b/src/core/windowitemdelegate.h
@@ -1,6 +1,8 @@
 #ifndef WINDOWITEMDELEGATE_H
 #define WINDOWITEMDELEGATE_H
 
+#include <memory>
+
 #include <QtCore/QObject>
 #include <QtGui/QWindow>
 
@@ -9,8 +11,6 @@
 namespace QWK {
 
     class WindowItemDelegate {
-        Q_DISABLE_COPY(WindowItemDelegate)
-
     public:
         WindowItemDelegate() = default;
         virtual ~WindowItemDelegate() = default;
@@ -20,9 +20,10 @@
 
         virtual bool isEnabled(QObject *obj) const = 0;
         virtual bool isVisible(QObject *obj) const = 0;
-    };
 
-    using WindowItemDelegatePtr = std::shared_ptr<WindowItemDelegate>;
+    private:
+        Q_DISABLE_COPY_MOVE(WindowItemDelegate)
+    };
 
 }
 
diff --git a/src/quick/quickitemdelegate_p.h b/src/quick/quickitemdelegate_p.h
index 658752f..7cd3473 100644
--- a/src/quick/quickitemdelegate_p.h
+++ b/src/quick/quickitemdelegate_p.h
@@ -10,8 +10,6 @@
 namespace QWK {
 
     class QWK_QUICK_EXPORT QuickItemDelegate : public WindowItemDelegate {
-        Q_DISABLE_COPY(QuickItemDelegate)
-
     public:
         QuickItemDelegate();
         ~QuickItemDelegate() override;
diff --git a/src/quick/quickwindowagent.cpp b/src/quick/quickwindowagent.cpp
index 1b653bb..3e22883 100644
--- a/src/quick/quickwindowagent.cpp
+++ b/src/quick/quickwindowagent.cpp
@@ -34,7 +34,7 @@
             return false;
         }
 
-        if (!d->setup(window, std::make_shared<QuickItemDelegate>())) {
+        if (!d->setup(window, new QuickItemDelegate())) {
             return true;
         }
         d->host = window;
diff --git a/src/quick/quickwindowagent.h b/src/quick/quickwindowagent.h
index ef26eea..78357ec 100644
--- a/src/quick/quickwindowagent.h
+++ b/src/quick/quickwindowagent.h
@@ -13,9 +13,7 @@
 
     class QWK_QUICK_EXPORT QuickWindowAgent : public CoreWindowAgent {
         Q_OBJECT
-        Q_DISABLE_COPY(QuickWindowAgent)
         Q_DECLARE_PRIVATE(QuickWindowAgent)
-
     public:
         explicit QuickWindowAgent(QObject *parent = nullptr);
         ~QuickWindowAgent() override;
diff --git a/src/quick/quickwindowagent_p.h b/src/quick/quickwindowagent_p.h
index daafb07..4f56140 100644
--- a/src/quick/quickwindowagent_p.h
+++ b/src/quick/quickwindowagent_p.h
@@ -7,9 +7,7 @@
 namespace QWK {
 
     class QuickWindowAgentPrivate : public CoreWindowAgentPrivate {
-        Q_DISABLE_COPY(QuickWindowAgentPrivate)
         Q_DECLARE_PUBLIC(QuickWindowAgent)
-
     public:
         QuickWindowAgentPrivate();
         ~QuickWindowAgentPrivate() override;
diff --git a/src/widgets/widgetitemdelegate_p.h b/src/widgets/widgetitemdelegate_p.h
index 0235325..a993975 100644
--- a/src/widgets/widgetitemdelegate_p.h
+++ b/src/widgets/widgetitemdelegate_p.h
@@ -10,8 +10,6 @@
 namespace QWK {
 
     class QWK_WIDGETS_EXPORT WidgetItemDelegate : public WindowItemDelegate {
-        Q_DISABLE_COPY(WidgetItemDelegate)
-
     public:
         WidgetItemDelegate();
         ~WidgetItemDelegate() override;
diff --git a/src/widgets/widgetwindowagent.cpp b/src/widgets/widgetwindowagent.cpp
index f6255c3..59ee504 100644
--- a/src/widgets/widgetwindowagent.cpp
+++ b/src/widgets/widgetwindowagent.cpp
@@ -33,7 +33,7 @@
         }
 
         std::ignore = w->winId(); // Make sure the window handle is created
-        if (!d->setup(w->windowHandle(), std::make_shared<WidgetItemDelegate>())) {
+        if (!d->setup(w->windowHandle(), new WidgetItemDelegate())) {
             return false;
         }
         d->host = w;
diff --git a/src/widgets/widgetwindowagent.h b/src/widgets/widgetwindowagent.h
index 7c627c2..e1411f8 100644
--- a/src/widgets/widgetwindowagent.h
+++ b/src/widgets/widgetwindowagent.h
@@ -12,9 +12,7 @@
 
     class QWK_WIDGETS_EXPORT WidgetWindowAgent : public CoreWindowAgent {
         Q_OBJECT
-        Q_DISABLE_COPY(WidgetWindowAgent)
         Q_DECLARE_PRIVATE(WidgetWindowAgent)
-
     public:
         explicit WidgetWindowAgent(QObject *parent = nullptr);
         ~WidgetWindowAgent() override;
diff --git a/src/widgets/widgetwindowagent_p.h b/src/widgets/widgetwindowagent_p.h
index eb1d96d..4d8f2a5 100644
--- a/src/widgets/widgetwindowagent_p.h
+++ b/src/widgets/widgetwindowagent_p.h
@@ -7,9 +7,7 @@
 namespace QWK {
 
     class WidgetWindowAgentPrivate : public CoreWindowAgentPrivate {
-        Q_DISABLE_COPY(WidgetWindowAgentPrivate)
         Q_DECLARE_PUBLIC(WidgetWindowAgent)
-
     public:
         WidgetWindowAgentPrivate();
         ~WidgetWindowAgentPrivate();

--
Gitblit v1.9.1