mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 19:08:58 +03:00
Incubate delegates asynchronously
This commit is contained in:
parent
c8f97216fa
commit
3d6f502bcc
3 changed files with 42 additions and 10 deletions
|
@ -29,7 +29,7 @@ Rectangle {
|
||||||
ListView {
|
ListView {
|
||||||
id: chat
|
id: chat
|
||||||
|
|
||||||
cacheBuffer: parent.height
|
cacheBuffer: 2000
|
||||||
|
|
||||||
visible: timelineManager.timeline != null
|
visible: timelineManager.timeline != null
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
|
|
|
@ -95,17 +95,11 @@ DelegateChooser::recalcChild()
|
||||||
auto choiceValue = choice->roleValue();
|
auto choiceValue = choice->roleValue();
|
||||||
if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) {
|
if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) {
|
||||||
if (child) {
|
if (child) {
|
||||||
// delete child;
|
child->setParentItem(nullptr);
|
||||||
child = nullptr;
|
child = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
child = dynamic_cast<QQuickItem *>(
|
choice->delegate()->create(incubator, QQmlEngine::contextForObject(this));
|
||||||
choice->delegate()->create(QQmlEngine::contextForObject(this)));
|
|
||||||
child->setParentItem(this);
|
|
||||||
connect(this->child, &QQuickItem::heightChanged, this, [this]() {
|
|
||||||
this->setHeight(this->child->height());
|
|
||||||
});
|
|
||||||
this->setHeight(this->child->height());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,3 +112,28 @@ DelegateChooser::componentComplete()
|
||||||
recalcChild();
|
recalcChild();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DelegateChooser::DelegateIncubator::statusChanged(QQmlIncubator::Status status)
|
||||||
|
{
|
||||||
|
if (status == QQmlIncubator::Ready) {
|
||||||
|
chooser.child = dynamic_cast<QQuickItem *>(object());
|
||||||
|
if (chooser.child == nullptr) {
|
||||||
|
nhlog::ui()->error("Delegate has to be derived of Item!");
|
||||||
|
delete chooser.child;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
chooser.child->setParentItem(&chooser);
|
||||||
|
connect(chooser.child, &QQuickItem::heightChanged, &chooser, [this]() {
|
||||||
|
chooser.setHeight(chooser.child->height());
|
||||||
|
});
|
||||||
|
chooser.setHeight(chooser.child->height());
|
||||||
|
QQmlEngine::setObjectOwnership(chooser.child,
|
||||||
|
QQmlEngine::ObjectOwnership::JavaScriptOwnership);
|
||||||
|
|
||||||
|
} else if (status == QQmlIncubator::Error) {
|
||||||
|
for (const auto &e : errors())
|
||||||
|
nhlog::ui()->error("Error instantiating delegate: {}",
|
||||||
|
e.toString().toStdString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QQmlComponent>
|
#include <QQmlComponent>
|
||||||
|
#include <QQmlIncubator>
|
||||||
#include <QQmlListProperty>
|
#include <QQmlListProperty>
|
||||||
#include <QQuickItem>
|
#include <QQuickItem>
|
||||||
#include <QtCore/QObject>
|
#include <QtCore/QObject>
|
||||||
|
@ -59,9 +60,21 @@ signals:
|
||||||
void roleValueChanged();
|
void roleValueChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct DelegateIncubator : public QQmlIncubator
|
||||||
|
{
|
||||||
|
DelegateIncubator(DelegateChooser &parent)
|
||||||
|
: QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
|
||||||
|
, chooser(parent)
|
||||||
|
{}
|
||||||
|
void statusChanged(QQmlIncubator::Status status) override;
|
||||||
|
|
||||||
|
DelegateChooser &chooser;
|
||||||
|
};
|
||||||
|
|
||||||
QVariant roleValue_;
|
QVariant roleValue_;
|
||||||
QList<DelegateChoice *> choices_;
|
QList<DelegateChoice *> choices_;
|
||||||
QQuickItem *child;
|
QQuickItem *child = nullptr;
|
||||||
|
DelegateIncubator incubator{*this};
|
||||||
|
|
||||||
static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *);
|
static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *);
|
||||||
static int choiceCount(QQmlListProperty<DelegateChoice> *);
|
static int choiceCount(QQmlListProperty<DelegateChoice> *);
|
||||||
|
|
Loading…
Reference in a new issue