From 0287f19b3eabf6b6632d51c0288e6cf0be2c5e69 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <zhaoyuhang@rankyee.com>
Date: 周三, 06 12月 2023 18:04:31 +0800
Subject: [PATCH] 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, 30 insertions(+), 18 deletions(-)

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

--
Gitblit v1.9.1