summaryrefslogtreecommitdiffstats
path: root/src/shared-main-lib/applicationmanagerwindow.cpp
blob: 1d37abebe1470022f03d13d6120dc87b6f2e821e (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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "applicationmanagerwindow.h"
#include "applicationmanagerwindowimpl.h"

#include <QtQuick/private/qquickitem_p.h>

QT_BEGIN_NAMESPACE_AM

/*!
    \qmltype ApplicationManagerWindow
    \inqmlmodule QtApplicationManager.Application
    \ingroup app-instantiatable
    \inherits Window
    \brief The window root element of an application.

    This QML item can be used as the root item in your QML application. In doing so, you enable
    your application to be usable in both single-process (EGL fullscreen, desktop) and
    multi-process (Wayland) mode. It inherits from \l Window in multi-process and from \l QtObject
    in single-process mode. In contrast to a \l Window it is visible by default. This documentation
    reflects the Window inheritance. Note that only a subset of the Window type members have been
    added to ApplicationManagerWindow when derived from QtObject. Additional details can be found
    in the section about \l {The Root Element}{the root element} and \l {Application Windows}
    {application windows}.

    The QML import for this item is

    \c{import QtApplicationManager.Application}

    After importing, you can instantiate the QML item like so:

    \qml
    import QtQuick
    import QtApplicationManager.Application

    ApplicationManagerWindow {
        Text {
            text: ApplicationInterface.applicationId
        }
    }
    \endqml

    In order to make your applications easily runnable outside of the application manager, even
    though you are using an ApplicationManagerWindow as a root item, you can simply provide this
    little dummy import to your application.

    \list 1
    \li Pick a base dir and create a \c{QtApplicationManager.Application} directory in it
    \li Add a file named \c qmldir there, consisting of the single line
        \c{ApplicationManagerWindow 2.0 ApplicationManagerWindow.qml}
    \li Add a second file named \c ApplicationManagerWindow.qml, with the following content

    \qml
    import QtQuick

    Window {
        signal windowPropertyChanged
        function setWindowProperty(name, value) {}
        // ... add additional dummy members that are used by your implementation

        width: 1280   // use your screen width here
        height: 600   // use your screen height here
        visible: true
    }
    \endqml

    \endlist

    Now you can run your application for instance with: \c{qml -I <path to base dir>}
*/


ApplicationManagerWindow::ApplicationManagerWindow(QObject *parent)
    : QObject(parent)
    , m_impl(ApplicationManagerWindowImpl::create(this))
{ }

ApplicationManagerWindow::~ApplicationManagerWindow()
{ }

ApplicationManagerWindowAttached *ApplicationManagerWindow::qmlAttachedProperties(QObject *object)
{
    return new ApplicationManagerWindowAttached(object);
}

QQmlListProperty<QObject> ApplicationManagerWindow::data()
{
    return QQmlListProperty<QObject>(this, nullptr,
                                     ApplicationManagerWindow::data_append,
                                     ApplicationManagerWindow::data_count,
                                     ApplicationManagerWindow::data_at,
                                     ApplicationManagerWindow::data_clear);
}

void ApplicationManagerWindow::data_append(QQmlListProperty<QObject> *property, QObject *object)
{
    auto *that = static_cast<ApplicationManagerWindow*>(property->object);
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    itemProperty.append(&itemProperty, object);
    emit that->dataChanged();
}

qsizetype ApplicationManagerWindow::data_count(QQmlListProperty<QObject> *property)
{
    auto *that = static_cast<ApplicationManagerWindow*>(property->object);
    if (!QQuickItemPrivate::get(that->contentItem())->data().count)
        return 0;
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    return itemProperty.count(&itemProperty);
}

QObject *ApplicationManagerWindow::data_at(QQmlListProperty<QObject> *property, qsizetype index)
{
    auto *that = static_cast<ApplicationManagerWindow*>(property->object);
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    return itemProperty.at(&itemProperty, index);
}

void ApplicationManagerWindow::data_clear(QQmlListProperty<QObject> *property)
{
    auto *that = static_cast<ApplicationManagerWindow*>(property->object);
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    itemProperty.clear(&itemProperty);
    emit that->dataChanged();
}

void ApplicationManagerWindow::classBegin()
{
    m_impl->classBegin();
}

void ApplicationManagerWindow::componentComplete()
{
    m_impl->componentComplete();
}

/*!
    \qmlproperty Item ApplicationManagerWindow::contentItem
    \readonly

    The invisible root item of the scene.
*/
QQuickItem *ApplicationManagerWindow::contentItem()
{
    return m_impl->contentItem();
}

/*! \qmlproperty bool ApplicationManagerWindow::singleProcess
    \readonly

    This property is \c true when the application is running in single-process mode and \c false
    when it is running in multi-process mode.

    \sa ApplicationManager::singleProcess
*/
bool ApplicationManagerWindow::isSingleProcess() const
{
    return m_impl->isSingleProcess();
}

/*! \qmlproperty QtObject ApplicationManagerWindow::backingObject
    \readonly

    This property holds the backing object of this window: in single-process mode, this is an
    an \l Item acting as a container for \l contentItem, but in multi-process mode, it is the
    actual \l Window instance.
*/
QObject *ApplicationManagerWindow::backingObject() const
{
    return m_impl->backingObject();
}

/*!
    \qmlmethod void ApplicationManagerWindow::setWindowProperty(string name, var &value)
    Sets this application window's shared property identified by \a name to the given \a value.

    These properties are shared between the System UI and the client applications: in single-process
    mode simply via a QVariantMap; in multi-process mode via Qt's extended surface Wayland extension.
    Changes from the client side are signalled via windowPropertyChanged.

    See WindowManager for the server side API.

    \note When listening to property changes of Wayland clients on the System UI side, be aware of
          the \l{Multi-process Wayland caveats}{asynchronous nature} of the underlying Wayland
          protocol.

    \sa windowProperty, windowProperties, windowPropertyChanged
*/
bool ApplicationManagerWindow::setWindowProperty(const QString &name, const QVariant &value)
{
    return m_impl->setWindowProperty(name, value);
}

/*!
    \qmlmethod var ApplicationManagerWindow::windowProperty(string name)

    Returns the value of this application window's shared property identified by \a name.

    \sa setWindowProperty
*/
QVariant ApplicationManagerWindow::windowProperty(const QString &name) const
{
    return m_impl->windowProperty(name);
}

/*!
    \qmlmethod object ApplicationManagerWindow::windowProperties()

    Returns an object containing all shared properties of this application window.

    \sa setWindowProperty
*/
QVariantMap ApplicationManagerWindow::windowProperties() const
{
    return m_impl->windowProperties();
}

/*!
    \qmlsignal ApplicationManagerWindow::windowPropertyChanged(string name, var value)

    Reports a change of this application window's property identified by \a name to the given
    \a value.

    \sa setWindowProperty
*/

/*!
    \qmlmethod ApplicationManagerWindow::close()

    Closes the window.

    When this method is called, the \l closing signal will be emitted. If there is no handler, or
    the handler does not revoke permission to close, the window will subsequently close. If the
    QGuiApplication::quitOnLastWindowClosed  property is \c true, and if there are no other windows
    open, the application will quit.
*/
void ApplicationManagerWindow::close()
{
    m_impl->close();
}

/*!
    \qmlsignal ApplicationManagerWindow::closing(CloseEvent close)

    This signal is emitted when the user tries to close the window.

    This signal includes a \a close parameter. The \c {close.accepted}
    property is true by default so that the window is allowed to close, but you
    can implement an \c onClosing handler and set \c {close.accepted = false} if
    you need to do something else before the window can be closed.
*/

/*!
    \qmlmethod void ApplicationManagerWindow::hide()

    Hides the window.

    Equivalent to setting \l visible to \c false or \l visibility to \l {QWindow::}{Hidden}.

    \sa show()
*/
void ApplicationManagerWindow::hide()
{
    m_impl->hide();
}

/*!
    \qmlmethod void ApplicationManagerWindow::show()

    Shows the window.

    Equivalent to setting \l visible to \c true.

    \sa hide()
*/
void ApplicationManagerWindow::show()
{
    m_impl->show();
}

/*!
    \qmlmethod void ApplicationManagerWindow::showFullScreen()

    This call is passed through to the underlying QWindow object in multi-process mode. The
    single-process mode does not support window states, so this call gets mapped to a simple show().

    \sa QWindow::showFullScreen()
*/
void ApplicationManagerWindow::showFullScreen()
{
    m_impl->showFullScreen();
}

/*!
    \qmlmethod void ApplicationManagerWindow::showMinimized()

    This call is passed through to the underlying QWindow object in multi-process mode. The
    single-process mode does not support window states, so this call gets mapped to a simple show().

    \sa QWindow::showMinimized()
*/
void ApplicationManagerWindow::showMinimized()
{
    m_impl->showMinimized();
}

/*!
    \qmlmethod void ApplicationManagerWindow::showMaximized()

    This call is passed through to the underlying QWindow object in multi-process mode. The
    single-process mode does not support window states, so this call gets mapped to a simple show().

    \sa QWindow::showMaximized()
*/
void ApplicationManagerWindow::showMaximized()
{
    m_impl->showMaximized();
}

/*!
    \qmlmethod void ApplicationManagerWindow::showNormal()

    This call is passed through to the underlying QWindow object in multi-process mode. The
    single-process mode does not support window states, so this call gets mapped to a simple show().

    \sa QWindow::showNormal()
*/
void ApplicationManagerWindow::showNormal()
{
    m_impl->showNormal();
}

/*!
    \qmlsignal void ApplicationManagerWindow::frameSwapped()

    This signal is forwarded from the underlying QQuickWindow object: either the corresponding
    QQuickWindow for this object in multi-process mode, or the System-UI's QQuickWindow in
    single-process mode.

    \sa QQuickWindow::frameSwapped()
*/

/*!
    \qmlsignal void ApplicationManagerWindow::sceneGraphError(QQuickWindow::SceneGraphError error, string message)

    This signal with its parameters \a error and \a message is forwarded from the underlying
    QQuickWindow object: either the corresponding QQuickWindow for this object in multi-process
    mode, or the System-UI's QQuickWindow in single-process mode.

    \sa QQuickWindow::sceneGraphError()
*/

/*!
    \qmlsignal void ApplicationManagerWindow::afterAnimating()

    This signal is forwarded from the underlying QQuickWindow object: either the corresponding
    QQuickWindow for this object in multi-process mode, or the System-UI's QQuickWindow in
    single-process mode.
*/

ApplicationManagerWindowImpl *ApplicationManagerWindow::implementation()
{
    return m_impl.get();
}

/*!
    \qmlproperty string ApplicationManagerWindow::title

    The window's title in the windowing system.
*/
QString ApplicationManagerWindow::title() const
{
    return m_impl->title();
}

void ApplicationManagerWindow::setTitle(const QString &title)
{
    m_impl->setTitle(title);
}

/*!
    \qmlproperty int ApplicationManagerWindow::x
    \qmlproperty int ApplicationManagerWindow::y
    \qmlproperty int ApplicationManagerWindow::width
    \qmlproperty int ApplicationManagerWindow::height

    Defines the window's position and size.

    The (x,y) position is relative to the compositors screens.
*/
int ApplicationManagerWindow::x() const
{
    return m_impl->x();
}

void ApplicationManagerWindow::setX(int x)
{
    m_impl->setX(x);
}

int ApplicationManagerWindow::y() const
{
    return m_impl->y();
}

void ApplicationManagerWindow::setY(int y)
{
    m_impl->setY(y);
}

int ApplicationManagerWindow::width() const
{
    return m_impl->width();
}

void ApplicationManagerWindow::setWidth(int w)
{
    m_impl->setWidth(w);
}

int ApplicationManagerWindow::height() const
{
    return m_impl->height();
}

void ApplicationManagerWindow::setHeight(int h)
{
    m_impl->setHeight(h);
}

/*!
    \qmlproperty int ApplicationManagerWindow::minimumWidth
    \qmlproperty int ApplicationManagerWindow::minimumHeight

    Defines the window's minimum size.

    This is a hint to the compositor to prevent resizing below the specified
    width and height.
*/
int ApplicationManagerWindow::minimumWidth() const
{
    return m_impl->minimumWidth();
}

void ApplicationManagerWindow::setMinimumWidth(int minw)
{
    m_impl->setMinimumWidth(minw);
}

int ApplicationManagerWindow::minimumHeight() const
{
    return m_impl->minimumHeight();
}

void ApplicationManagerWindow::setMinimumHeight(int minh)
{
    m_impl->setMinimumHeight(minh);
}

/*!
    \qmlproperty int ApplicationManagerWindow::maximumWidth
    \qmlproperty int ApplicationManagerWindow::maximumHeight

    Defines the window's maximum size.

    This is a hint to the compositor to prevent resizing above the specified
    width and height.
*/
int ApplicationManagerWindow::maximumWidth() const
{
    return m_impl->maximumWidth();
}

void ApplicationManagerWindow::setMaximumWidth(int maxw)
{
    m_impl->setMaximumWidth(maxw);
}

int ApplicationManagerWindow::maximumHeight() const
{
    return m_impl->maximumHeight();
}

void ApplicationManagerWindow::setMaximumHeight(int maxh)
{
    m_impl->setMaximumHeight(maxh);
}

/*! \qmlproperty bool ApplicationManagerWindow::visible

    Whether the window is visible on the screen.

    Setting visible to false is the same as setting \l visibility to \l {QWindow::}{Hidden}.

    \sa visibility
*/
bool ApplicationManagerWindow::isVisible() const
{
    return m_impl->isVisible();
}

void ApplicationManagerWindow::setVisible(bool visible)
{
    m_impl->setVisible(visible);
}

/*!
    \qmlproperty real ApplicationManagerWindow::opacity

    The opacity of the window.

    A value of 1.0 or above is treated as fully opaque, whereas a value of 0.0 or below
    is treated as fully transparent. Values in between represent varying levels of
    translucency between the two extremes.

    The default value is 1.0.
*/
qreal ApplicationManagerWindow::opacity() const
{
    return m_impl->opacity();
}

void ApplicationManagerWindow::setOpacity(qreal opacity)
{
    m_impl->setOpacity(opacity);
}

/*!
    \qmlproperty color ApplicationManagerWindow::color

    The background color for the window.

    Setting this property is more efficient than using a separate Rectangle.

    \note Since version 6.7, if an alpha channel is used, it needs to be straight alpha. Pre 6.7
          it needed to be pre-multiplied alpha (which is what the \l{Window} QML type still expects)
          in multi-process and straight alpha in single-process mode.
*/
QColor ApplicationManagerWindow::color() const
{
    return m_impl->color();
}

void ApplicationManagerWindow::setColor(const QColor &c)
{
    m_impl->setColor(c);
}

/*!
    \qmlproperty bool ApplicationManagerWindow::active

    The active status of the window.
 */
bool ApplicationManagerWindow::isActive() const
{
    return m_impl->isActive();
}

/*!
    \qmlproperty Item ApplicationManagerWindow::activeFocusItem

    The item which currently has active focus or \c null if there is no item with active focus.
*/
QQuickItem *ApplicationManagerWindow::activeFocusItem() const
{
    return m_impl->activeFocusItem();
}

/*!
    \keyword qml-amwindow-visibility-prop
    \qmlproperty QWindow::Visibility ApplicationManagerWindow::visibility

    The screen-occupation state of the window.

    Visibility is whether the window should appear in the windowing system as
    normal, minimized, maximized, fullscreen or hidden.

    See Window::visibility for more details. In single-process mode, the minimized, maximized and
    fullscreen states are not supported and simply map to normal.
*/
QWindow::Visibility ApplicationManagerWindow::visibility() const
{
    return m_impl->visibility();
}

void ApplicationManagerWindow::setVisibility(QWindow::Visibility newVisibility)
{
    m_impl->setVisibility(newVisibility);
}

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


ApplicationManagerWindowAttached::ApplicationManagerWindowAttached(QObject *attachee)
    : QObject(attachee)
{
    if (auto *attacheeItem = qobject_cast<QQuickItem*>(attachee)) {
        m_impl.reset(ApplicationManagerWindowAttachedImpl::create(this, attacheeItem));

        if (auto *amwindow = m_impl->findApplicationManagerWindow())
            reconnect(amwindow);
    }
}


void ApplicationManagerWindowAttached::reconnect(ApplicationManagerWindow *newWin)
{
    if (newWin == m_amwindow)
        return;

    if (m_amwindow)
        QObject::disconnect(m_amwindow, nullptr, this, nullptr);

    m_amwindow = newWin;
    emit windowChanged();
    emit backingObjectChanged();
    emit contentItemChanged();

    if (m_amwindow) {
        QObject::connect(m_amwindow, &ApplicationManagerWindow::activeChanged,
                         this, &ApplicationManagerWindowAttached::activeChanged);
        QObject::connect(m_amwindow, &ApplicationManagerWindow::activeFocusItemChanged,
                         this, &ApplicationManagerWindowAttached::activeFocusItemChanged);
        QObject::connect(m_amwindow, &ApplicationManagerWindow::widthChanged,
                         this, &ApplicationManagerWindowAttached::widthChanged);
        QObject::connect(m_amwindow, &ApplicationManagerWindow::heightChanged,
                         this, &ApplicationManagerWindowAttached::heightChanged);
        QObject::connect(m_amwindow, &ApplicationManagerWindow::visibilityChanged,
                         this, &ApplicationManagerWindowAttached::visibilityChanged);
    }
}

/*!
    \qmlattachedproperty ApplicationManagerWindow ApplicationManagerWindow::window
    \readonly

    This attached property holds the item's ApplicationManagerWindow.
    The ApplicationManagerWindow attached property can be attached to any Item.
*/
ApplicationManagerWindow *ApplicationManagerWindowAttached::window() const
{
    return m_amwindow.get();
}

/*!
    \qmlattachedproperty QtObject ApplicationManagerWindow::backingObject
    \readonly

    This attached property holds backingObject of the ApplicationManagerWindow.

    \sa ApplicationManagerWindow::backingObject
*/
QObject *ApplicationManagerWindowAttached::backingObject() const
{
    return m_amwindow ? m_amwindow->backingObject() : nullptr;
}

/*!
    \qmlattachedproperty QWindow::Visibility ApplicationManagerWindow::visibility
    \readonly

    This attached property holds whether the window is currently shown
    in the windowing system as normal, minimized, maximized, fullscreen or
    hidden. The \c ApplicationManagerWindow attached property can be attached to any Item. If the
    item is not shown in any ApplicationManagerWindow, the value will be \l {QWindow::}{Hidden}.

    \sa visible, {qml-amwindow-visibility-prop}{visibility}
*/
QWindow::Visibility ApplicationManagerWindowAttached::visibility() const
{
    return m_amwindow ? m_amwindow->visibility() : QWindow::Hidden;
}

/*!
    \qmlattachedproperty bool ApplicationManagerWindow::active

    This attached property tells whether the window is active. The ApplicationManagerWindow
    attached property can be attached to any Item.
*/
bool ApplicationManagerWindowAttached::isActive() const
{
    return m_amwindow ? m_amwindow->isActive() : false;
}

/*!
    \qmlattachedproperty Item ApplicationManagerWindow::activeFocusItem
    \readonly

    This attached property holds the item which currently has active focus or
    \c null if there is no item with active focus. The ApplicationManagerWindow attached property
    can be attached to any Item.
*/
QQuickItem *ApplicationManagerWindowAttached::activeFocusItem() const
{
    return m_amwindow ? m_amwindow->activeFocusItem() : nullptr;
}

/*!
    \qmlattachedproperty Item ApplicationManagerWindow::contentItem
    \readonly

    This attached property holds the invisible root item of the scene or
    \c null if the item is not in a window. The ApplicationManagerWindow attached property
    can be attached to any Item.
*/
QQuickItem *ApplicationManagerWindowAttached::contentItem() const
{
    return m_amwindow ? m_amwindow->contentItem() : nullptr;
}

/*!
    \qmlattachedproperty int ApplicationManagerWindow::width
    \qmlattachedproperty int ApplicationManagerWindow::height
    \readonly

    These attached properties hold the size of the item's window.
    The ApplicationManagerWindow attached property can be attached to any Item.
*/
int ApplicationManagerWindowAttached::width() const
{
    return m_amwindow ? m_amwindow->height() : 0;
}

int ApplicationManagerWindowAttached::height() const
{
    return m_amwindow ? m_amwindow->height() : 0;
}


QT_END_NAMESPACE_AM

#include "moc_applicationmanagerwindow.cpp"