core/proxywindow: improve QsWindowAttached robustness

Can now track window parent window changes.
Added tests.
This commit is contained in:
outfoxxed 2024-11-27 23:30:38 -08:00
parent 539692bc11
commit b6a79fe99c
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
7 changed files with 145 additions and 36 deletions

View file

@ -2,33 +2,19 @@
#include <qobject.h>
#include <qquickitem.h>
#include <qvariant.h>
#include "proxywindow.hpp"
QsWindowAttached* WindowInterface::qmlAttachedProperties(QObject* object) {
auto* visualRoot = qobject_cast<QQuickItem*>(object);
ProxyWindowBase* proxy = nullptr;
while (visualRoot != nullptr) {
proxy = visualRoot->property("__qs_proxywindow").value<ProxyWindowBase*>();
if (proxy) break;
visualRoot = visualRoot->parentItem();
};
if (!proxy) return nullptr;
auto v = proxy->property("__qs_window_attached");
if (auto* attached = v.value<QsWindowAttached*>()) {
return attached;
}
auto* attached = new ProxyWindowAttached(proxy);
if (attached) {
proxy->setProperty("__qs_window_attached", QVariant::fromValue(attached));
}
return attached;
QsWindowAttached::QsWindowAttached(QQuickItem* parent): QObject(parent) {
QObject::connect(parent, &QQuickItem::windowChanged, this, &QsWindowAttached::updateWindow);
}
QsWindowAttached* WindowInterface::qmlAttachedProperties(QObject* object) {
while (object && !qobject_cast<QQuickItem*>(object)) {
object = object->parent();
}
if (!object) return nullptr;
auto* item = static_cast<QQuickItem*>(object); // NOLINT
return new ProxyWindowAttached(item);
}