Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
LazyLoader.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorShell/phosphorshell_export.h>
7
8#include <QPointer>
9#include <QQuickItem>
10#include <QUrl>
11
12QT_BEGIN_NAMESPACE
13class QQmlComponent;
14class QQmlIncubator;
15QT_END_NAMESPACE
16
17namespace PhosphorShell {
18
19class PHOSPHORSHELL_EXPORT LazyLoader : public QQuickItem
20{
21 Q_OBJECT
22
23 Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
24 Q_PROPERTY(
25 QQmlComponent* sourceComponent READ sourceComponent WRITE setSourceComponent NOTIFY sourceComponentChanged)
26 Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
27 Q_PROPERTY(QQuickItem* item READ item NOTIFY itemChanged)
28 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
29
30public:
31 enum Status {
35 Error
36 };
37 Q_ENUM(Status)
38
39 explicit LazyLoader(QQuickItem* parent = nullptr);
40 ~LazyLoader() override;
41
42 [[nodiscard]] bool active() const;
43 void setActive(bool active);
44
45 [[nodiscard]] QQmlComponent* sourceComponent() const;
46 void setSourceComponent(QQmlComponent* component);
47
48 [[nodiscard]] QUrl source() const;
49 void setSource(const QUrl& source);
50
51 [[nodiscard]] QQuickItem* item() const;
52 [[nodiscard]] Status status() const;
53
54Q_SIGNALS:
55 void activeChanged();
56 void sourceComponentChanged();
57 void sourceChanged();
58 void itemChanged();
59 void statusChanged();
60 void loaded();
61
62private:
63 friend class LazyIncubator;
64
65 void startLoading();
66 void unload();
67 void onIncubatorReady();
68
69 bool m_active = false;
70 // QPointer because the component is externally owned by the QML
71 // scene — a parent reload that destroys the source component must
72 // not leave us with a dangling pointer that startLoading would
73 // dereference on the next active toggle.
74 QPointer<QQmlComponent> m_sourceComponent;
75 QQmlComponent* m_ownedComponent = nullptr;
76 QUrl m_source;
77 QPointer<QQuickItem> m_item;
78 QQmlIncubator* m_incubator = nullptr;
79 Status m_status = Null;
80};
81
82} // namespace PhosphorShell
Definition LazyLoader.h:20
Status
Definition LazyLoader.h:31
@ Null
Definition LazyLoader.h:32
@ Loading
Definition LazyLoader.h:33
@ Ready
Definition LazyLoader.h:34
Definition Environment.h:11