summary refs log tree commit diff
path: root/src/ui/UIA.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/UIA.h')
-rw-r--r--src/ui/UIA.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/ui/UIA.h b/src/ui/UIA.h
index 7d23d88e..414cb804 100644
--- a/src/ui/UIA.h
+++ b/src/ui/UIA.h
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <QObject>
+#include <QQmlEngine>
 
 #include <mtxclient/http/client.hpp>
 
@@ -12,10 +13,31 @@ class UIA final : public QObject
 {
     Q_OBJECT
 
+    QML_ELEMENT
+    QML_SINGLETON
+
     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
 
 public:
     static UIA *instance();
+    static UIA *create(QQmlEngine *qmlEngine, QJSEngine *)
+    {
+        // The instance has to exist before it is used. We cannot replace it.
+        Q_ASSERT(instance());
+
+        // The engine has to have the same thread affinity as the singleton.
+        Q_ASSERT(qmlEngine->thread() == instance()->thread());
+
+        // There can only be one engine accessing the singleton.
+        static QJSEngine *s_engine = nullptr;
+        if (s_engine)
+            Q_ASSERT(qmlEngine == s_engine);
+        else
+            s_engine = qmlEngine;
+
+        QJSEngine::setObjectOwnership(instance(), QJSEngine::CppOwnership);
+        return instance();
+    }
 
     UIA(QObject *parent = nullptr)
       : QObject(parent)