Sine Striker
2023-12-13 44c08f3cc456155b960ca5a115df93109d2202ce
src/quick/quickwindowagent.cpp
@@ -1,83 +1,12 @@
#include "quickwindowagent.h"
#include "quickwindowagent_p.h"
#include "quickitemdelegate_p.h"
#include <QtQuick/QQuickWindow>
#include <QtQuick/QQuickPaintedItem>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickanchors_p.h>
#include "quickitemdelegate_p.h"
namespace QWK {
    class BorderItem : public QQuickPaintedItem {
        Q_OBJECT
    public:
        explicit BorderItem(AbstractWindowContext *ctx, QQuickItem *parent = nullptr);
        ~BorderItem() override;
        void updateHeight();
        void paint(QPainter *painter) override;
        void itemChange(ItemChange change, const ItemChangeData &data) override;
    private:
        AbstractWindowContext *context;
    };
    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(std::numeric_limits<qreal>::max());
    }
    BorderItem::~BorderItem() = default;
    void BorderItem::updateHeight() {
        bool native = false;
        quint32 thickness = 0;
        void *args[] = { &native, &thickness };
        context->virtual_hook(AbstractWindowContext::QueryBorderThicknessHook, &args);
        setHeight(thickness);
    }
    void BorderItem::paint(QPainter *painter) {
        auto rect = QRect{ QPoint{ 0, 0}, size().toSize() };
        auto region = QRegion{ 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, [this](){ update(); });
                }
                Q_FALLTHROUGH();
            case ItemVisibleHasChanged:
            case ItemDevicePixelRatioHasChanged:
                updateHeight();
                break;
            default:
                break;
        }
    }
    QuickWindowAgentPrivate::QuickWindowAgentPrivate() {
    }
@@ -86,7 +15,6 @@
    }
    void QuickWindowAgentPrivate::init() {
        borderItem = std::make_unique<BorderItem>(context.get(), hostWindow->contentItem());
    }
    QuickWindowAgent::QuickWindowAgent(QObject *parent)
@@ -111,6 +39,10 @@
            return false;
        }
        d->hostWindow = window;
#ifdef Q_OS_WINDOWS
        d->setupWindows10BorderWorkaround();
#endif
        return true;
    }
@@ -160,6 +92,4 @@
        d.init();
    }
}
#include "quickwindowagent.moc"
}