From 6f6f96909e7ded6ae3a4e59d9d4b62fa593abb23 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周日, 25 2月 2024 02:33:03 +0800
Subject: [PATCH] Fix quick initialization failure

---
 src/core/windowitemdelegate.cpp             |    2 
 src/core/contexts/abstractwindowcontext.cpp |    6 +++
 src/widgets/widgetwindowagent_win.cpp       |   35 +++++++++++------
 src/core/contexts/abstractwindowcontext_p.h |    5 ++
 src/quick/quickwindowagent_win.cpp          |   11 ++++-
 src/core/kernel/winidchangeeventfilter.cpp  |    6 +++
 src/core/kernel/winidchangeeventfilter_p.h  |    5 --
 7 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index 2665ac4..1c11015 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -260,6 +260,12 @@
                 m_windowAttributes.insert(it.key(), it.value());
             }
         }
+
+        // Send to shared dispatchers
+        if (oldWinId != m_windowId) {
+            QEvent e(QEvent::WinIdChange);
+            sharedDispatch(m_host, &e);
+        }
     }
 
     QVariant AbstractWindowContext::windowAttribute(const QString &key) const {
diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h
index e7711d7..52eb5cc 100644
--- a/src/core/contexts/abstractwindowcontext_p.h
+++ b/src/core/contexts/abstractwindowcontext_p.h
@@ -43,6 +43,7 @@
 
         inline QObject *host() const;
         inline QWindow *window() const;
+        inline WId windowId() const;
         inline WindowItemDelegate *delegate() const;
 
         inline bool isHitTestVisible(const QObject *obj) const;
@@ -114,6 +115,10 @@
         return m_windowHandle;
     }
 
+    inline WId AbstractWindowContext::windowId() const {
+        return m_windowId;
+    }
+
     inline WindowItemDelegate *AbstractWindowContext::delegate() const {
         return m_delegate.get();
     }
diff --git a/src/core/kernel/winidchangeeventfilter.cpp b/src/core/kernel/winidchangeeventfilter.cpp
index 2a0f95c..4d60d74 100644
--- a/src/core/kernel/winidchangeeventfilter.cpp
+++ b/src/core/kernel/winidchangeeventfilter.cpp
@@ -10,6 +10,12 @@
 
 namespace QWK {
 
+    WindowWinIdChangeEventFilter::WindowWinIdChangeEventFilter(QWindow *host,
+                                                               AbstractWindowContext *context)
+        : WinIdChangeEventFilter(host, context), win(host), isAboutToBeDestroyed(false) {
+        host->installEventFilter(this);
+    }
+
     WId WindowWinIdChangeEventFilter::winId() const {
         auto win = static_cast<QWindow *>(host);
         if (isAboutToBeDestroyed)
diff --git a/src/core/kernel/winidchangeeventfilter_p.h b/src/core/kernel/winidchangeeventfilter_p.h
index 4e0ab36..fc1b732 100644
--- a/src/core/kernel/winidchangeeventfilter_p.h
+++ b/src/core/kernel/winidchangeeventfilter_p.h
@@ -37,10 +37,7 @@
 
     class QWK_CORE_EXPORT WindowWinIdChangeEventFilter : public WinIdChangeEventFilter {
     public:
-        WindowWinIdChangeEventFilter(QObject *host, AbstractWindowContext *context)
-            : WinIdChangeEventFilter(host, context), win(static_cast<QWindow *>(host)),
-              isAboutToBeDestroyed(false) {
-        }
+        WindowWinIdChangeEventFilter(QWindow *host, AbstractWindowContext *context);
 
         WId winId() const override;
 
diff --git a/src/core/windowitemdelegate.cpp b/src/core/windowitemdelegate.cpp
index 9285958..fd8a88e 100644
--- a/src/core/windowitemdelegate.cpp
+++ b/src/core/windowitemdelegate.cpp
@@ -17,7 +17,7 @@
     WinIdChangeEventFilter *
         WindowItemDelegate::createWinIdEventFilter(QObject *host,
                                                    AbstractWindowContext *context) const {
-        return new WindowWinIdChangeEventFilter(host, context);
+        return new WindowWinIdChangeEventFilter(static_cast<QWindow *>(host), context);
     }
 
 }
diff --git a/src/quick/quickwindowagent_win.cpp b/src/quick/quickwindowagent_win.cpp
index aea554a..a265bbb 100644
--- a/src/quick/quickwindowagent_win.cpp
+++ b/src/quick/quickwindowagent_win.cpp
@@ -56,7 +56,8 @@
         anchors->setLeft(parentPri->left());
         anchors->setRight(parentPri->right());
 
-        setZ(std::numeric_limits<qreal>::max()); // Make sure our fake border always above everything in the window.
+        setZ(std::numeric_limits<qreal>::max()); // Make sure our fake border always above
+                                                 // everything in the window.
 
         context->installNativeEventFilter(this);
         context->installSharedEventFilter(this);
@@ -130,8 +131,14 @@
 
     bool BorderItem::sharedEventFilter(QObject *obj, QEvent *event) {
         Q_UNUSED(obj)
-        
+
         switch (event->type()) {
+            case QEvent::WinIdChange: {
+                if (auto winId = context->windowId()) {
+                    updateGeometry();
+                }
+                break;
+            }
             case QEvent::WindowStateChange: {
                 updateGeometry();
                 break;
diff --git a/src/widgets/widgetwindowagent_win.cpp b/src/widgets/widgetwindowagent_win.cpp
index 9a4c6b3..791ed40 100644
--- a/src/widgets/widgetwindowagent_win.cpp
+++ b/src/widgets/widgetwindowagent_win.cpp
@@ -136,22 +136,31 @@
             Q_UNUSED(obj)
 
             auto window = widget->windowHandle();
+            switch (event->type()) {
+                case QEvent::Expose: {
+                    // Qt will absolutely send a QExposeEvent or QResizeEvent to the QWindow when it
+                    // receives a WM_PAINT message. When the control flow enters the expose handler,
+                    // Qt must have already called BeginPaint() and it's the best time for us to
+                    // draw the top border.
 
-            // Qt will absolutely send a QExposeEvent or QResizeEvent to the QWindow when it
-            // receives a WM_PAINT message. When the control flow enters the expose handler, Qt
-            // must have already called BeginPaint() and it's the best time for us to draw the
-            // top border.
-
-            // Since a QExposeEvent will be sent immediately after the QResizeEvent, we can simply
-            // ignore it.
-            if (event->type() == QEvent::Expose) {
-                auto ee = static_cast<QExposeEvent *>(event);
-                if (window->isExposed() && isNormalWindow() && !ee->region().isNull()) {
-                    resumeWindowEventAndDraw(window, event);
-                    return true;
+                    // Since a QExposeEvent will be sent immediately after the QResizeEvent, we can
+                    // simply ignore it.
+                    auto ee = static_cast<QExposeEvent *>(event);
+                    if (window->isExposed() && isNormalWindow() && !ee->region().isNull()) {
+                        resumeWindowEventAndDraw(window, event);
+                        return true;
+                    }
+                    break;
                 }
+                case QEvent::WinIdChange: {
+                    if (auto winId = ctx->windowId()) {
+                        updateGeometry();
+                    }
+                    break;
+                }
+                default:
+                    break;
             }
-
             return false;
         }
 

--
Gitblit v1.9.1