From 5078f898257a53295167f22c67005ad1b30d4bf8 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周二, 12 12月 2023 02:53:57 +0800
Subject: [PATCH] Tested MinGW 13.2.0

---
 src/quick/quickwindowagent.cpp |   99 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/src/quick/quickwindowagent.cpp b/src/quick/quickwindowagent.cpp
index 77ccc9e..1b29b42 100644
--- a/src/quick/quickwindowagent.cpp
+++ b/src/quick/quickwindowagent.cpp
@@ -1,11 +1,94 @@
 #include "quickwindowagent.h"
 #include "quickwindowagent_p.h"
-
-#include <QtQuick/QQuickWindow>
-
 #include "quickitemdelegate_p.h"
 
+#include <QtQuick/QQuickWindow>
+#include <QtQuick/QQuickPaintedItem>
+#include <QtQuick/private/qquickitem_p.h>
+#include <QtQuick/private/qquickanchors_p.h>
+
 namespace QWK {
+
+    class BorderItem : public QQuickPaintedItem {
+        Q_OBJECT
+    public:
+        explicit BorderItem(AbstractWindowContext *ctx, QQuickItem *parent = nullptr);
+        ~BorderItem() override;
+
+        void updateHeight();
+
+    public:
+        void paint(QPainter *painter) override;
+        void itemChange(ItemChange change, const ItemChangeData &data) override;
+
+    private:
+        AbstractWindowContext *context;
+
+        void _q_windowActivityChanged();
+    };
+
+    BorderItem::BorderItem(AbstractWindowContext *ctx, QQuickItem *parent)
+        : QQuickPaintedItem(parent), context(ctx) {
+        setAntialiasing(true);   // ### FIXME: do we need to enable or disable this?
+        setMipmap(true);         // ### FIXME: do we need to enable or disable this?
+        setFillColor({});        // Will improve the performance a little bit.
+        setOpaquePainting(true); // Will also improve the performance, we don't draw
+                                 // semi-transparent borders of course.
+
+        auto parentPri = QQuickItemPrivate::get(parent);
+        auto anchors = QQuickItemPrivate::get(this)->anchors();
+        anchors->setTop(parentPri->top());
+        anchors->setLeft(parentPri->left());
+        anchors->setRight(parentPri->right());
+
+        setZ(10);
+    }
+
+    BorderItem::~BorderItem() = default;
+
+    void BorderItem::updateHeight() {
+        bool native = false;
+        quint32 thickness = 0;
+        void *args[] = {
+            &native,
+            &thickness,
+        };
+        context->virtual_hook(AbstractWindowContext::BorderThicknessHook, &args);
+        setHeight(thickness);
+    }
+
+    void BorderItem::paint(QPainter *painter) {
+        QRect rect(QPoint(0, 0), size().toSize());
+        QRegion region(rect);
+        void *args[] = {
+            painter,
+            &rect,
+            &region,
+        };
+        context->virtual_hook(AbstractWindowContext::DrawBordersHook, args);
+    }
+
+    void BorderItem::itemChange(ItemChange change, const ItemChangeData &data) {
+        QQuickPaintedItem::itemChange(change, data);
+        switch (change) {
+            case ItemSceneChange:
+                if (data.window) {
+                    connect(data.window, &QQuickWindow::activeChanged, this,
+                            &BorderItem::_q_windowActivityChanged);
+                }
+                Q_FALLTHROUGH();
+            case ItemVisibleHasChanged:
+            case ItemDevicePixelRatioHasChanged:
+                updateHeight();
+                break;
+            default:
+                break;
+        }
+    }
+
+    void BorderItem::_q_windowActivityChanged() {
+        update();
+    }
 
     QuickWindowAgentPrivate::QuickWindowAgentPrivate() {
     }
@@ -35,9 +118,15 @@
         }
 
         if (!d->setup(window, new QuickItemDelegate())) {
-            return true;
+            return false;
         }
         d->hostWindow = window;
+
+        if (bool needPaintBorder = false;
+            d->context->virtual_hook(AbstractWindowContext::NeedsDrawBordersHook, &needPaintBorder),
+            needPaintBorder) {
+            d->borderItem = std::make_unique<BorderItem>(d->context.get(), window->contentItem());
+        }
         return true;
     }
 
@@ -88,3 +177,5 @@
     }
 
 }
+
+#include "quickwindowagent.moc"
\ No newline at end of file

--
Gitblit v1.9.1