Add the "HA Dashboard" tab with HA's web UI (DSH-1)
A second dashboard surface shows a WebEngineView at homeassistant.url, or a hint when none is set. HaWebUi (QML singleton owned by Dashboard) holds the URL and a persistent WebEngine profile under ~/.local/share/frameassistant/webengine so the HA login survives restarts. QtWebEngineQuick::initialize() runs in main() before the app object (D39). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include "ha/HaWebUi.h"
|
||||
|
||||
#include <QQmlEngine>
|
||||
#include <QQuickWebEngineProfile>
|
||||
#include <QStandardPaths>
|
||||
|
||||
namespace {
|
||||
|
||||
HaWebUi* g_instance = nullptr;
|
||||
|
||||
} // namespace
|
||||
|
||||
HaWebUi::HaWebUi(QObject* parent) : QObject(parent) {
|
||||
Q_ASSERT(!g_instance);
|
||||
g_instance = this;
|
||||
}
|
||||
|
||||
HaWebUi::~HaWebUi() { g_instance = nullptr; }
|
||||
|
||||
QQuickWebEngineProfile* HaWebUi::profile() {
|
||||
if (!m_profile) {
|
||||
m_profile = new QQuickWebEngineProfile(this);
|
||||
m_profile->setStorageName(QStringLiteral("frameassistant"));
|
||||
m_profile->setOffTheRecord(false);
|
||||
m_profile->setPersistentStoragePath(storagePath());
|
||||
m_profile->setCachePath(cachePath());
|
||||
m_profile->setPersistentCookiesPolicy(
|
||||
QQuickWebEngineProfile::AllowPersistentCookies);
|
||||
}
|
||||
return m_profile;
|
||||
}
|
||||
|
||||
QString HaWebUi::storagePath() {
|
||||
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) +
|
||||
QStringLiteral("/frameassistant/webengine");
|
||||
}
|
||||
|
||||
QString HaWebUi::cachePath() {
|
||||
return QStandardPaths::writableLocation(
|
||||
QStandardPaths::GenericCacheLocation) +
|
||||
QStringLiteral("/frameassistant/webengine");
|
||||
}
|
||||
|
||||
HaWebUi* HaWebUi::create(QQmlEngine*, QJSEngine*) {
|
||||
Q_ASSERT(g_instance);
|
||||
// Owned by the app, not by the QML engine.
|
||||
QJSEngine::setObjectOwnership(g_instance, QJSEngine::CppOwnership);
|
||||
return g_instance;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtQml/qqmlregistration.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QQuickWebEngineProfile>
|
||||
#include <QUrl>
|
||||
|
||||
class QJSEngine;
|
||||
class QQmlEngine;
|
||||
|
||||
// HA's web UI in the "HA Dashboard" tab (DSH-1): the URL from the settings
|
||||
// and a persistent WebEngine profile, so the HA login survives restarts
|
||||
// (docs/DESIGN.md, D39). A QML singleton owned by the app (Dashboard).
|
||||
class HaWebUi : public QObject {
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
// Empty: no URL configured (the tab shows a hint).
|
||||
Q_PROPERTY(QUrl url READ url CONSTANT)
|
||||
Q_PROPERTY(QQuickWebEngineProfile* profile READ profile CONSTANT)
|
||||
|
||||
public:
|
||||
// Not default-constructible, so QML uses create() instead of making its
|
||||
// own instance.
|
||||
explicit HaWebUi(QObject* parent);
|
||||
~HaWebUi() override;
|
||||
|
||||
// Call before the QML is loaded.
|
||||
void setUrl(const QUrl& url) { m_url = url; }
|
||||
|
||||
QUrl url() const { return m_url; }
|
||||
// Created on first use: only a tab that shows the web UI needs it.
|
||||
QQuickWebEngineProfile* profile();
|
||||
|
||||
// Where the profile keeps its data (cookies, local storage) and cache.
|
||||
static QString storagePath();
|
||||
static QString cachePath();
|
||||
|
||||
// QML singleton factory: the instance the app created.
|
||||
static HaWebUi* create(QQmlEngine*, QJSEngine*);
|
||||
|
||||
private:
|
||||
QUrl m_url;
|
||||
QQuickWebEngineProfile* m_profile = nullptr;
|
||||
};
|
||||
Reference in New Issue
Block a user