From 2f57263c413021716397841ca9d0bfc9628c8703 Mon Sep 17 00:00:00 2001
From: SineStriker <trueful@163.com>
Date: 周三, 06 12月 2023 18:28:52 +0800
Subject: [PATCH] Revert "update pointer usage"

---
 src/core/corewindowagent.cpp                |   17 +++++------------
 src/core/corewindowagent_p.h                |    5 ++---
 src/core/contexts/abstractwindowcontext.cpp |    2 +-
 src/core/contexts/win32windowcontext_p.h    |    2 +-
 src/core/contexts/abstractwindowcontext_p.h |   10 +++++-----
 src/core/contexts/win32windowcontext.cpp    |   12 ++++--------
 6 files changed, 18 insertions(+), 30 deletions(-)

diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index d0d6ebb..1c0e739 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -2,7 +2,7 @@
 
 namespace QWK {
 
-    AbstractWindowContext::AbstractWindowContext(const QObject *host, const WindowItemDelegate *delegate)
+    AbstractWindowContext::AbstractWindowContext(QObject *host, WindowItemDelegate *delegate)
         : m_host(host), m_delegate(delegate), m_windowHandle(delegate->hostWindow(host)) {
     }
 
diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h
index 127b33d..b1202ea 100644
--- a/src/core/contexts/abstractwindowcontext_p.h
+++ b/src/core/contexts/abstractwindowcontext_p.h
@@ -16,13 +16,13 @@
     class QWK_CORE_EXPORT AbstractWindowContext : public QObject {
         Q_OBJECT
     public:
-        AbstractWindowContext(const QObject *host, const WindowItemDelegate *delegate);
+        AbstractWindowContext(QObject *host, WindowItemDelegate *delegate);
         ~AbstractWindowContext() override;
 
     public:
         virtual bool setup() = 0;
 
-        inline const QObject *host() const;
+        inline QObject *host() const;
         inline QWindow *window() const;
 
         inline bool isHitTestVisible(const QObject *obj) const;
@@ -42,8 +42,8 @@
         bool isInTitleBarDraggableArea(const QPoint &pos) const;
 
     protected:
-        const QObject *m_host;
-        const WindowItemDelegate *m_delegate;
+        QObject *m_host;
+        std::unique_ptr<WindowItemDelegate> m_delegate;
         QWindow *m_windowHandle;
 
         QSet<const QObject *> m_hitTestVisibleItems;
@@ -57,7 +57,7 @@
         mutable QRegion hitTestVisibleShape;
     };
 
-    inline const QObject *AbstractWindowContext::host() const {
+    inline QObject *AbstractWindowContext::host() const {
         return m_host;
     }
 
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index a843e90..4a99a18 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -443,17 +443,11 @@
         static WindowsNativeEventFilter *instance;
 
         static inline void install() {
-            if (instance) {
-                return;
-            }
             instance = new WindowsNativeEventFilter();
             installNativeEventFilter(instance);
         }
 
         static inline void uninstall() {
-            if (!instance) {
-                return;
-            }
             removeNativeEventFilter(instance);
             delete instance;
             instance = nullptr;
@@ -545,7 +539,7 @@
         return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam);
     }
 
-    Win32WindowContext::Win32WindowContext(const QObject *host, const WindowItemDelegate *delegate)
+    Win32WindowContext::Win32WindowContext(QObject *host, WindowItemDelegate *delegate)
         : AbstractWindowContext(host, delegate) {
     }
 
@@ -582,7 +576,9 @@
         ::SetWindowLongPtrW(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(QWKHookedWndProc));
 
         // Install global native event filter
-        WindowsNativeEventFilter::install();
+        if (!WindowsNativeEventFilter::instance) {
+            WindowsNativeEventFilter::install();
+        }
 
         // Cache window ID
         windowId = winId;
diff --git a/src/core/contexts/win32windowcontext_p.h b/src/core/contexts/win32windowcontext_p.h
index 672a033..b8b8fea 100644
--- a/src/core/contexts/win32windowcontext_p.h
+++ b/src/core/contexts/win32windowcontext_p.h
@@ -9,7 +9,7 @@
     class QWK_CORE_EXPORT Win32WindowContext : public AbstractWindowContext {
         Q_OBJECT
     public:
-        Win32WindowContext(const QObject *host, const WindowItemDelegate *delegate);
+        Win32WindowContext(QObject *host, WindowItemDelegate *delegate);
         ~Win32WindowContext() override;
 
         enum WindowPart {
diff --git a/src/core/corewindowagent.cpp b/src/core/corewindowagent.cpp
index 5d1f74e..bea2239 100644
--- a/src/core/corewindowagent.cpp
+++ b/src/core/corewindowagent.cpp
@@ -16,30 +16,23 @@
     CoreWindowAgentPrivate::CoreWindowAgentPrivate() : q_ptr(nullptr), context(nullptr) {
     }
 
-    CoreWindowAgentPrivate::~CoreWindowAgentPrivate() {
-        if (context) {
-            delete context;
-            context = nullptr;
-        }
-    }
+    CoreWindowAgentPrivate::~CoreWindowAgentPrivate() = default;
 
     void CoreWindowAgentPrivate::init() {
     }
 
-    bool CoreWindowAgentPrivate::setup(const QObject *host, const WindowItemDelegate *delegate) {
+    bool CoreWindowAgentPrivate::setup(QObject *host, WindowItemDelegate *delegate) {
         auto ctx =
 #ifdef Q_OS_WINDOWS
-            new Win32WindowContext(host, delegate)
+            std::make_unique<Win32WindowContext>(host, delegate)
 #else
-            new QtWindowContext(host, window, delegate)
+            std::make_unique<QtWindowContext>(host, window, delegate)
 #endif
             ;
         if (!ctx->setup()) {
-            delete ctx;
-            ctx = nullptr;
             return false;
         }
-        context = ctx;
+        context = std::move(ctx);
         return true;
     }
 
diff --git a/src/core/corewindowagent_p.h b/src/core/corewindowagent_p.h
index 6b1912d..faa61f8 100644
--- a/src/core/corewindowagent_p.h
+++ b/src/core/corewindowagent_p.h
@@ -16,11 +16,10 @@
 
         CoreWindowAgent *q_ptr; // no need to initialize
 
-        bool setup(const QObject *host, const WindowItemDelegate *delegate);
+        bool setup(QObject *host, WindowItemDelegate *delegate);
 
-        AbstractWindowContext *context;
+        std::unique_ptr<AbstractWindowContext> context;
 
-    private:
         Q_DISABLE_COPY_MOVE(CoreWindowAgentPrivate)
     };
 

--
Gitblit v1.9.1