summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/qmlinprocapplicationmanagerwindowimpl.cpp
blob: b31846dd927e35587f9033fef446e3e9feb41199 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright (C) 2023 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 <memory>

#include <QQuickWindow>
#include <QScopedValueRollback>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickevents_p_p.h>

#include "logging.h"
#include "qml-utilities.h"
#include "applicationmanagerwindow.h"
#include "qmlinprocapplicationmanagerwindowimpl.h"
#include "inprocesssurfaceitem.h"
#include "qmlinprocruntime.h"
#include <private/qqmlcomponentattached_p.h>


QT_BEGIN_NAMESPACE_AM

QVector<QmlInProcApplicationManagerWindowImpl *> QmlInProcApplicationManagerWindowImpl::s_inCompleteWindows;

QmlInProcApplicationManagerWindowImpl::QmlInProcApplicationManagerWindowImpl(ApplicationManagerWindow *window)
    : ApplicationManagerWindowImpl(window)
    , m_surfaceItem(new InProcessSurfaceItem)
{
    m_surfaceItem->setApplicationManagerWindow(window);
    m_surfaceItem->setColor(QColorConstants::White);

    QObject::connect(m_surfaceItem.data(), &QQuickItem::widthChanged,
                     window, &ApplicationManagerWindow::widthChanged);

    QObject::connect(m_surfaceItem.data(), &QQuickItem::heightChanged,
                     window, &ApplicationManagerWindow::heightChanged);

    QObject::connect(m_surfaceItem.data(), &InProcessSurfaceItem::windowPropertyChanged,
                     window, &ApplicationManagerWindow::windowPropertyChanged);

    QObject::connect(m_surfaceItem.data(), &InProcessSurfaceItem::closeRequested,
                     window, &ApplicationManagerWindow::close);

    auto connectWindowSignals = [this, window](QQuickWindow *qwindow) {
        if (!qwindow)
            return;

        connectActiveFocusItem();

        // pass-through signals from the actual QQuickWindow
        QObject::connect(qwindow, &QQuickWindow::frameSwapped,
                         window, &ApplicationManagerWindow::frameSwapped);
        QObject::connect(qwindow, &QQuickWindow::afterAnimating,
                         window, &ApplicationManagerWindow::afterAnimating);
        QObject::connect(qwindow, &QQuickWindow::sceneGraphError,
                         window, &ApplicationManagerWindow::sceneGraphError);
    };

    connectWindowSignals(m_surfaceItem->window());

    QObject::connect(m_surfaceItem.data(), &InProcessSurfaceItem::windowChanged,
                     window, connectWindowSignals);
}
void QmlInProcApplicationManagerWindowImpl::connectActiveFocusItem()
{
    QObject::disconnect(m_activeFocusItemConnection);

    if (m_surfaceItem->window()) {
        // We can only track the AFI of our Window directly, but we keep a pointer to the AFI,
        // if it is a child of our InProcessSurfaceItem

        m_activeFocusItemConnection = QObject::connect(m_surfaceItem->window(), &QQuickWindow::activeFocusItemChanged,
                                                       amWindow(), [this]() {
            auto *afi = m_surfaceItem->window() ? m_surfaceItem->window()->activeFocusItem()
                                                : nullptr;
            if (afi) {
                bool found = false;
                QQuickItem *p = afi->parentItem();
                while (p) {
                    if (p == m_surfaceItem) {
                        found = true;
                        break;
                    }
                    p = p->parentItem();
                }
                if (!found)
                    afi = nullptr;
            }
            if (afi != m_activeFocusItem) {
                bool activeChanged = (bool(afi) != bool(m_activeFocusItem));
                m_activeFocusItem = afi;

                if (activeChanged)
                    emit amWindow()->activeChanged();
                emit amWindow()->activeFocusItemChanged();
            }
        });
    }
}

QmlInProcApplicationManagerWindowImpl::~QmlInProcApplicationManagerWindowImpl()
{
    m_surfaceItem->setDeadClientSide();
}

bool QmlInProcApplicationManagerWindowImpl::isSingleProcess() const
{
    return true;
}

QObject *QmlInProcApplicationManagerWindowImpl::backingObject() const
{
    return m_surfaceItem.get();
}

void QmlInProcApplicationManagerWindowImpl::close()
{
    if (m_closing)
        return;
    QScopedValueRollback<bool> rollback(m_closing, true);

    for (const auto &child: std::as_const(m_childWindows))
        child->close();

    auto ce = std::make_unique<QQuickCloseEvent>();
    emit amWindow()->closing(ce.get());

    if (ce->isAccepted()) {
        setVisible(false);
        m_surfaceItem->setClosed(true);
        if (m_runtime) {
            // Queued because the runtime might end up deleting this object
            QMetaObject::invokeMethod(m_runtime, &QmlInProcRuntime::stopIfLastWindowClosed, Qt::QueuedConnection);
        }
    }
}

QWindow::Visibility QmlInProcApplicationManagerWindowImpl::visibility() const
{
    return m_visibility;
}

void QmlInProcApplicationManagerWindowImpl::setVisibility(QWindow::Visibility newVisibility)
{
    // copied from qwindow.cpp:
    switch (newVisibility) {
    case QWindow::Hidden:
        hide();
        break;
    case QWindow::AutomaticVisibility:
        show();
        break;
    case QWindow::Windowed:
        showNormal();
        break;
    case QWindow::Minimized:
        showMinimized();
        break;
    case QWindow::Maximized:
        showMaximized();
        break;
    case QWindow::FullScreen:
        showFullScreen();
        break;
    default:
        Q_ASSERT(false);
    }
}

void QmlInProcApplicationManagerWindowImpl::updateVisibility(QWindow::Visibility newVisibility)
{
    if (m_visibility != newVisibility) {
        m_visibility = newVisibility;
        emit amWindow()->visibilityChanged(m_visibility);
    }
}

void QmlInProcApplicationManagerWindowImpl::hide()
{
    amWindow()->setVisible(false);
    updateVisibility(QWindow::Hidden);
}

void QmlInProcApplicationManagerWindowImpl::show()
{
    showNormal();
}

void QmlInProcApplicationManagerWindowImpl::showFullScreen()
{
    amWindow()->setVisible(true);
    updateVisibility(QWindow::FullScreen);
}

void QmlInProcApplicationManagerWindowImpl::showMinimized()
{
    amWindow()->setVisible(true);
    updateVisibility(QWindow::Minimized);
}

void QmlInProcApplicationManagerWindowImpl::showMaximized()
{
    amWindow()->setVisible(true);
    updateVisibility(QWindow::Maximized);
}

void QmlInProcApplicationManagerWindowImpl::showNormal()
{
    amWindow()->setVisible(true);
    updateVisibility(QWindow::Windowed);
}

bool QmlInProcApplicationManagerWindowImpl::setWindowProperty(const QString &name, const QVariant &value)
{
    return m_surfaceItem->setWindowProperty(name, value);
}

QVariant QmlInProcApplicationManagerWindowImpl::windowProperty(const QString &name) const
{
    return m_surfaceItem->windowProperty(name);
}

QVariantMap QmlInProcApplicationManagerWindowImpl::windowProperties() const
{
    return m_surfaceItem->windowPropertiesAsVariantMap();
}

void QmlInProcApplicationManagerWindowImpl::classBegin()
{ }

void QmlInProcApplicationManagerWindowImpl::componentComplete()
{
    ensureCurrentContextIsInProcessApplication(amWindow());

    if (!m_runtime)
        m_runtime = QmlInProcRuntime::determineRuntime(amWindow());

    findParentWindow(amWindow()->parent());

    // This part is scary, but we need to make sure that all Component.onComplete: handlers on
    // the QML side have been run, before we hand this window over to the WindowManager to emit the
    // windowAdded signal. The problem here is that the C++ componentComplete() handler (this
    // function) is called *before* the QML side, plus, to make matters worse, the QML incubator
    // could switch back to the event loop a couple of times before finally calling the QML
    // onCompleted handler(s).
    // The workaround is to setup watchers for all Component.onCompleted handlers for this object
    // and wait until the last of them has been dealt with, to finally call our addSurfaceItem
    // function (we are also relying on the signal emission order, so that our lambda is called
    // after the actual QML handler).
    // We also make sure that onWindowAdded is called in the same order, as this function is
    // called, so we don't depend on the existence of onCompleted handlers (and conform to multi-
    // process mode; there is no guarantee though, since multi-process is inherently asynchronous).

    for (auto a = QQmlComponent::qmlAttachedProperties(amWindow()); a; a = a->next()) {
        auto appWindow = qobject_cast<ApplicationManagerWindow *>(a->parent());
        if (!appWindow || appWindow != amWindow())
            continue;

        m_attachedCompleteHandlers << a;

        QObject::connect(a, &QQmlComponentAttached::completed, amWindow(), [this, a]() {
            m_attachedCompleteHandlers.removeAll(a);

            if (m_attachedCompleteHandlers.isEmpty()) {
                for (auto it = s_inCompleteWindows.cbegin(); it != s_inCompleteWindows.cend(); ) {
                    QmlInProcApplicationManagerWindowImpl *win = *it;
                    if (win->m_attachedCompleteHandlers.isEmpty()) {
                        it = s_inCompleteWindows.erase(it); // clazy:exclude=strict-iterators
                        win->notifyRuntimeAboutSurface();
                    } else {
                        break;
                    }

                }
            }
        });
    }

    // If we do not even have a single Component.onCompleted handler on the QML side and no
    // previous window is waiting for onCompleted, we need to show the window immediately.
    // Otherwise we save this window and process it later in the lambda above, in the same order as
    // this function is called.
    if (m_attachedCompleteHandlers.isEmpty() && s_inCompleteWindows.isEmpty())
        notifyRuntimeAboutSurface();
    else
        s_inCompleteWindows << this;
}

void QmlInProcApplicationManagerWindowImpl::notifyRuntimeAboutSurface()
{
    if (!m_runtime)
        return;

    if (isVisible() && m_attachedCompleteHandlers.isEmpty() && (!m_parentWindow || m_parentWindow->isVisible()))
        m_runtime->addSurfaceItem(m_surfaceItem);
}

QQuickItem *QmlInProcApplicationManagerWindowImpl::contentItem()
{
    return m_surfaceItem.data();
}

void QmlInProcApplicationManagerWindowImpl::findParentWindow(QObject *object)
{
    if (!object)
        return;

    auto surfaceItem = qobject_cast<InProcessSurfaceItem *>(object);
    if (surfaceItem) {
        setRelations(surfaceItem->applicationManagerWindow());
    } else {
        if (auto window = qobject_cast<ApplicationManagerWindow *>(object))
            setRelations(window);
        else
            findParentWindow(object->parent());
    }
}

void QmlInProcApplicationManagerWindowImpl::setRelations(ApplicationManagerWindow *newParentWindow)
{
    // Only static window hieararchy supported
    Q_ASSERT(!m_parentWindow && !m_parentVisibleConnection);

    m_parentWindow = newParentWindow;

    if (m_parentWindow) {
        static_cast<QmlInProcApplicationManagerWindowImpl*>
            (m_parentWindow->implementation())->m_childWindows.append(amWindow());
        m_parentVisibleConnection = QObject::connect(
            m_parentWindow, &ApplicationManagerWindow::visibleChanged,
            amWindow(), [this]() { notifyRuntimeAboutSurface(); });
    }
}

void QmlInProcApplicationManagerWindowImpl::setTitle(const QString &title)
{
    if (m_title != title) {
        m_title = title;
        emit amWindow()->titleChanged();
    }
}

void QmlInProcApplicationManagerWindowImpl::setX(int x)
{
    if (m_x != x) {
        m_x = x;
        emit amWindow()->xChanged();
    }
}

void QmlInProcApplicationManagerWindowImpl::setY(int y)
{
    if (m_y != y) {
        m_y = y;
        emit amWindow()->yChanged();
    }
}

int QmlInProcApplicationManagerWindowImpl::width() const
{
    return int(m_surfaceItem->width());
}

void QmlInProcApplicationManagerWindowImpl::setWidth(int w)
{
    m_surfaceItem->setWidth(qBound(m_minimumWidth, w, m_maximumWidth));
}

int QmlInProcApplicationManagerWindowImpl::height() const
{
    return int(m_surfaceItem->height());
}

void QmlInProcApplicationManagerWindowImpl::setHeight(int h)
{
    m_surfaceItem->setHeight(qBound(m_minimumHeight, h, m_maximumHeight));
}

void QmlInProcApplicationManagerWindowImpl::setMinimumWidth(int minw)
{
    if ((minw < 0) || (minw > WindowSizeMax))
        return;

    if (m_minimumWidth != minw) {
        m_minimumWidth = minw;
        emit amWindow()->minimumWidthChanged();
        if (width() < minw)
            setWidth(minw);
    }
}

void QmlInProcApplicationManagerWindowImpl::setMinimumHeight(int minh)
{
    if ((minh < 0) || (minh > WindowSizeMax))
        return;

    if (m_minimumHeight != minh) {
        m_minimumHeight = minh;
        emit amWindow()->minimumHeightChanged();
        if (height() < minh)
            setHeight(minh);
    }
}

void QmlInProcApplicationManagerWindowImpl::setMaximumWidth(int maxw)
{
    if ((maxw < 0) || (maxw > WindowSizeMax))
        return;

    if (m_maximumWidth != maxw) {
        m_maximumWidth = maxw;
        emit amWindow()->maximumWidthChanged();
        if (width() > maxw)
            setWidth(maxw);
    }
}

void QmlInProcApplicationManagerWindowImpl::setMaximumHeight(int maxh)
{
    if ((maxh < 0) || (maxh > WindowSizeMax))
        return;

    if (m_maximumHeight != maxh) {
        m_maximumHeight = maxh;
        emit amWindow()->maximumHeightChanged();
        if (height() > maxh)
            setHeight(maxh);
    }
}

bool QmlInProcApplicationManagerWindowImpl::isVisible() const
{
    return m_surfaceItem->visibleClientSide();
}

void QmlInProcApplicationManagerWindowImpl::setVisible(bool visible)
{
    if (visible != m_surfaceItem->visibleClientSide()) {
        m_surfaceItem->setVisibleClientSide(visible);
        emit amWindow()->visibleChanged();
        notifyRuntimeAboutSurface();
    }
}

void QmlInProcApplicationManagerWindowImpl::setOpacity(qreal opacity)
{
    if (!qFuzzyCompare(m_opacity, opacity)) {
        m_opacity = opacity;
        emit amWindow()->opacityChanged();
    }
}

QColor QmlInProcApplicationManagerWindowImpl::color() const
{
    return m_surfaceItem->color();
}

void QmlInProcApplicationManagerWindowImpl::setColor(const QColor &c)
{
    if (color() != c) {
        m_surfaceItem->setColor(c);
        emit amWindow()->colorChanged();
    }
}

bool QmlInProcApplicationManagerWindowImpl::isActive() const
{
    return (m_activeFocusItem);
}

QQuickItem *QmlInProcApplicationManagerWindowImpl::activeFocusItem() const
{
    return m_activeFocusItem;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// ApplicationManagerWindowAttached
///////////////////////////////////////////////////////////////////////////////////////////////////


QmlInProcApplicationManagerWindowAttachedImpl::QmlInProcApplicationManagerWindowAttachedImpl(ApplicationManagerWindowAttached *windowAttached, QQuickItem *attacheeItem)
    : ApplicationManagerWindowAttachedImpl(windowAttached, attacheeItem)
{ }

ApplicationManagerWindow *QmlInProcApplicationManagerWindowAttachedImpl::findApplicationManagerWindow()
{
    if (!attacheeItem())
        return nullptr;

    auto ipsurface = findInProcessSurfaceItem();
    return ipsurface ? ipsurface->applicationManagerWindow() : nullptr;
}

InProcessSurfaceItem *QmlInProcApplicationManagerWindowAttachedImpl::findInProcessSurfaceItem()
{
    // Connect to all parent items' parentChanged() signals, up to root or an InProcessSurfaceItem.
    // If one of them changes, disconnect all parentChanged connections, then find the new
    // InProcessSurfaceItem and re-connect.

    for (const auto &connection : std::as_const(m_parentChangeConnections))
        QObject::disconnect(connection);

    InProcessSurfaceItem *ipsurface = nullptr;
    QQuickItem *p = attacheeItem();
    while (p && !ipsurface) {
        ipsurface = qobject_cast<InProcessSurfaceItem *>(p);
        if (!ipsurface) {
            m_parentChangeConnections << QObject::connect(p, &QQuickItem::parentChanged,
                                                          amWindowAttached(), [this]() { onParentChanged(); });
        }
        p = p->parentItem();
    }
    return ipsurface;
}

void QmlInProcApplicationManagerWindowAttachedImpl::onParentChanged()
{
    InProcessSurfaceItem *ipsurface = findInProcessSurfaceItem();

    if (!amWindowAttached()->window()
            || (ipsurface != amWindowAttached()->window()->backingObject())) {
        onWindowChanged(ipsurface ? ipsurface->applicationManagerWindow() : nullptr);
    }
}

QT_END_NAMESPACE_AM