summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/inprocesssurfaceitem.cpp
blob: 4ccd3e58980964f1cb5f8c592fd37599e7844162 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "inprocesssurfaceitem.h"
#include "applicationmanagerwindow.h"
#include "qml-utilities.h"
#include <QQmlEngine>
#include <QSGSimpleRectNode>

QT_BEGIN_NAMESPACE_AM

static QByteArray nameToKey(const QString &name)
{
    return QByteArray("_am_") + name.toUtf8();
}

static QString keyToName(const QByteArray &key)
{
    return QString::fromUtf8(key.mid(4));
}

static bool isName(const QByteArray &key)
{
    return key.startsWith("_am_");
}

InProcessSurfaceItem::InProcessSurfaceItem(QQuickItem *parent)
    : QQuickFocusScope(parent)
{
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
    setClip(true);
    setFlag(ItemHasContents);
    m_windowProperties.installEventFilter(this);

    setFocusOnClick(true);
}

InProcessSurfaceItem::~InProcessSurfaceItem()
{
}

bool InProcessSurfaceItem::setWindowProperty(const QString &name, const QVariant &value)
{
    const QByteArray key = nameToKey(name);
    const QVariant v = convertFromJSVariant(value);
    QVariant oldValue = m_windowProperties.property(key);
    bool changed = !oldValue.isValid() || (oldValue != v);

    if (changed)
        m_windowProperties.setProperty(key, v);

    return true;
}

QVariant InProcessSurfaceItem::windowProperty(const QString &name) const
{
    QByteArray key = nameToKey(name);
    return m_windowProperties.property(key);
}

QVariantMap InProcessSurfaceItem::windowPropertiesAsVariantMap() const
{
    const QByteArrayList keys = m_windowProperties.dynamicPropertyNames();
    QVariantMap map;

    for (const QByteArray &key : keys) {
        if (!isName(key))
            continue;

        QString name = keyToName(key);
        map[name] = m_windowProperties.property(key);
    }

    return map;
}

bool InProcessSurfaceItem::eventFilter(QObject *o, QEvent *e)
{
    if ((o == &m_windowProperties) && (e->type() == QEvent::DynamicPropertyChange)) {
        QDynamicPropertyChangeEvent *dpce = static_cast<QDynamicPropertyChangeEvent *>(e);
        QByteArray key = dpce->propertyName();

        if (isName(key)) {
            QString name = keyToName(dpce->propertyName());
            emit windowPropertyChanged(name, m_windowProperties.property(key));
        }
    }

    return QQuickFocusScope::eventFilter(o, e);
}

bool InProcessSurfaceItem::focusOnClick() const
{
    return m_focusOnClick;
}

void InProcessSurfaceItem::setFocusOnClick(bool newFocusOnClick)
{
    if (m_focusOnClick != newFocusOnClick) {
        m_focusOnClick = newFocusOnClick;

        setAcceptTouchEvents(newFocusOnClick);
        setAcceptedMouseButtons(newFocusOnClick ? Qt::AllButtons : Qt::NoButton);
        setFiltersChildMouseEvents(newFocusOnClick);

        emit focusOnClickChanged();
    }
}

void InProcessSurfaceItem::mousePressEvent(QMouseEvent *e)
{
    Q_ASSERT(m_focusOnClick);

    QQuickFocusScope::mousePressEvent(e);
    delayedForceActiveFocus();
}

void InProcessSurfaceItem::touchEvent(QTouchEvent *e)
{
    Q_ASSERT(m_focusOnClick);

    QQuickFocusScope::touchEvent(e);
    if (e->type() == QEvent::TouchBegin)
        delayedForceActiveFocus();
}

bool InProcessSurfaceItem::childMouseEventFilter(QQuickItem *item, QEvent *e)
{
    Q_UNUSED(item)
    Q_ASSERT(m_focusOnClick);

    if ((e->type() == QEvent::MouseButtonPress) || (e->type() == QEvent::TouchBegin))
        delayedForceActiveFocus();
    return false;
}

void InProcessSurfaceItem::delayedForceActiveFocus()
{
    QMetaObject::invokeMethod(this, [this]() {
            if (m_amWindow && !m_amWindow->activeFocusItem())
                forceActiveFocus(Qt::ActiveWindowFocusReason);
        }, Qt::QueuedConnection);
}

void InProcessSurfaceItem::setVisibleClientSide(bool visible)
{
    if (visible)
        m_closed = false;

    if (visible != m_visibleClientSide) {
        m_visibleClientSide = visible;
        emit visibleClientSideChanged();
    }
}

bool InProcessSurfaceItem::visibleClientSide() const
{
    return m_visibleClientSide;
}

void InProcessSurfaceItem::setDeadClientSide()
{
    if (!m_deadClientSide) {
        m_deadClientSide = true;
        emit deadClientSideChanged();
    }
}

bool InProcessSurfaceItem::isDeadClientSide() const
{
    return m_deadClientSide;
}

QSGNode *InProcessSurfaceItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
{
    if (m_color.alpha() == 0)
        return oldNode;  // no need to render fully transparent node

    QSGSimpleRectNode *node = static_cast<QSGSimpleRectNode *>(oldNode);
    if (!node) {
        node = new QSGSimpleRectNode(clipRect(), m_color);
    } else {
        node->setRect(clipRect());
        node->setColor(m_color);
    }
    return node;
}

QColor InProcessSurfaceItem::color() const
{
    return m_color;
}

void InProcessSurfaceItem::setColor(const QColor &c)
{
    if (m_color != c) {
        m_color = c;
        update();
    }
}

void InProcessSurfaceItem::close()
{
    emit closeRequested();
}

void InProcessSurfaceItem::setClosed(bool closed)
{
    if (m_closed != closed) {
        m_closed = closed;
        emit closedChanged();
    }
}

bool InProcessSurfaceItem::isClosed() const
{
    return m_closed;
}

ApplicationManagerWindow *InProcessSurfaceItem::applicationManagerWindow()
{
    return m_amWindow.data();
}

void InProcessSurfaceItem::setApplicationManagerWindow(ApplicationManagerWindow *win)
{
    m_amWindow = win;
}

QT_END_NAMESPACE_AM

#include "moc_inprocesssurfaceitem.cpp"