Sine Striker
2023-12-11 5dbf2d71aa0aa6b8f0cb8f1334d061e0d759caf8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#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>
 
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(10);
    }
 
    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() {
    }
 
    QuickWindowAgentPrivate::~QuickWindowAgentPrivate() {
    }
 
    void QuickWindowAgentPrivate::init() {
    }
 
    QuickWindowAgent::QuickWindowAgent(QObject *parent)
        : QuickWindowAgent(*new QuickWindowAgentPrivate(), parent) {
    }
 
    QuickWindowAgent::~QuickWindowAgent() {
    }
 
    bool QuickWindowAgent::setup(QQuickWindow *window) {
        Q_ASSERT(window);
        if (!window) {
            return false;
        }
 
        Q_D(QuickWindowAgent);
        if (d->hostWindow) {
            return false;
        }
 
        if (!d->setup(window, new QuickItemDelegate())) {
            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;
    }
 
    QQuickItem *QuickWindowAgent::titleBar() const {
        Q_D(const QuickWindowAgent);
        return static_cast<QQuickItem *>(d->context->titleBar());
    }
 
    void QuickWindowAgent::setTitleBar(QQuickItem *item) {
        Q_D(QuickWindowAgent);
        if (!d->context->setTitleBar(item)) {
            return;
        }
        Q_EMIT titleBarWidgetChanged(item);
    }
 
    QQuickItem *QuickWindowAgent::systemButton(SystemButton button) const {
        Q_D(const QuickWindowAgent);
        return static_cast<QQuickItem *>(d->context->systemButton(button));
    }
 
    void QuickWindowAgent::setSystemButton(SystemButton button, QQuickItem *item) {
        Q_D(QuickWindowAgent);
        if (!d->context->setSystemButton(button, item)) {
            return;
        }
        Q_EMIT systemButtonChanged(button, item);
    }
 
    bool QuickWindowAgent::isHitTestVisible(const QQuickItem *item) const {
        Q_D(const QuickWindowAgent);
        return d->context->isHitTestVisible(item);
    }
 
    void QuickWindowAgent::setHitTestVisible_item(const QQuickItem *item, bool visible) {
        Q_D(QuickWindowAgent);
        d->context->setHitTestVisible(item, visible);
    }
 
    void QuickWindowAgent::setHitTestVisible_rect(const QRect &rect, bool visible) {
        Q_D(QuickWindowAgent);
        d->context->setHitTestVisible(rect, visible);
    }
 
    QuickWindowAgent::QuickWindowAgent(QuickWindowAgentPrivate &d, QObject *parent)
        : WindowAgentBase(d, parent) {
        d.init();
    }
 
}
 
#include "quickwindowagent.moc"