| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "NhekoMenuVisibilityFilter.h" |
| 6 | |
| 7 | #include <QQmlListReference> |
| 8 | #include <QQuickItem> |
| 9 | #include <QTimer> |
| 10 | |
| 11 | #include "Logging.h" |
| 12 | |
| 13 | QQmlListProperty<QQmlComponent> |
| 14 | NhekoMenuVisibilityFilter::items() |
| 15 | { |
| 16 | return QQmlListProperty<QQmlComponent>(this, |
| 17 | this, |
| 18 | &NhekoMenuVisibilityFilter::appendItem, |
| 19 | &NhekoMenuVisibilityFilter::itemCount, |
| 20 | &NhekoMenuVisibilityFilter::getItem, |
| 21 | &NhekoMenuVisibilityFilter::clearItems, |
| 22 | &NhekoMenuVisibilityFilter::replaceItem, |
| 23 | &NhekoMenuVisibilityFilter::removeLast); |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | NhekoMenuVisibilityFilter::appendItem(QQmlListProperty<QQmlComponent> *p, QQmlComponent *c) |
| 28 | { |
| 29 | NhekoMenuVisibilityFilter *dc = static_cast<NhekoMenuVisibilityFilter *>(p->object); |
| 30 | dc->items_.append(c); |
| 31 | // dc->updateTarget(); |
| 32 | |
| 33 | // QQmlProperty prop(c, "visible"); |
| 34 | // prop.connectNotifySignal(dc, SLOT(updateTarget())); |
| 35 | } |
| 36 | qsizetype |
| 37 | NhekoMenuVisibilityFilter::itemCount(QQmlListProperty<QQmlComponent> *p) |
| 38 | { |
| 39 | return static_cast<NhekoMenuVisibilityFilter *>(p->object)->items_.count(); |
| 40 | } |
| 41 | QQmlComponent * |
| 42 | NhekoMenuVisibilityFilter::getItem(QQmlListProperty<QQmlComponent> *p, qsizetype index) |
| 43 | { |
| 44 | return static_cast<NhekoMenuVisibilityFilter *>(p->object)->items_.at(index); |
| 45 | } |
| 46 | void |
| 47 | NhekoMenuVisibilityFilter::clearItems(QQmlListProperty<QQmlComponent> *p) |
| 48 | { |
| 49 | static_cast<NhekoMenuVisibilityFilter *>(p->object)->items_.clear(); |
| 50 | // static_cast<NhekoMenuVisibilityFilter *>(p->object)->updateTarget(); |
| 51 | } |
| 52 | void |
| 53 | NhekoMenuVisibilityFilter::replaceItem(QQmlListProperty<QQmlComponent> *p, |
| 54 | qsizetype index, |
| 55 | QQmlComponent *c) |
| 56 | { |
| 57 | static_cast<NhekoMenuVisibilityFilter *>(p->object)->items_.assign(index, c); |
| 58 | // static_cast<NhekoMenuVisibilityFilter *>(p->object)->updateTarget(); |
| 59 | } |
| 60 | void |
| 61 | NhekoMenuVisibilityFilter::removeLast(QQmlListProperty<QQmlComponent> *p) |
| 62 | { |
| 63 | static_cast<NhekoMenuVisibilityFilter *>(p->object)->items_.pop_back(); |
| 64 | // static_cast<NhekoMenuVisibilityFilter *>(p->object)->updateTarget(); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | NhekoMenuVisibilityFilter::setTarget(const QQmlProperty &prop) |
| 69 | { |
| 70 | if (prop.propertyTypeCategory() != QQmlProperty::List) { |
| 71 | nhlog::ui()->warn("Target prop of NhekoMenuVisibilityFilter set to non list property"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | targetProperty = prop; |
| 76 | // updateTarget(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | NhekoMenuVisibilityFilter::updateTarget() |
| 81 | { |
| 82 | if (!targetProperty.isValid()) |
| 83 | return; |
| 84 | |
| 85 | auto newItems = qvariant_cast<QQmlListReference>(targetProperty.read()); |
| 86 | // newItems.clear(); <- does not remove the visual items |
| 87 | |
| 88 | for (qsizetype i = newItems.size(); i > 0; i--) { |
| 89 | // only remove items, not other random stuff in there! |
| 90 | if (auto item = qobject_cast<QQuickItem *>(newItems.at(i - 1))) { |
| 91 | // emit removeItem(item); <- easier to "automagic" this by using invokeMethod |
| 92 | QMetaObject::invokeMethod( |
| 93 | targetProperty.object(), "removeItem", Qt::DirectConnection, item); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | for (const auto &item : std::as_const(items_)) { |
| 98 | auto newItem = item->create(QQmlEngine::contextForObject(this)); |
| 99 | |
| 100 | if (auto prop = newItem->property("visible"); !prop.isValid() || prop.toBool()) { |
| 101 | // targetProperty.write(QVariant::fromValue(newItem)); <- appends but breaks removal |
| 102 | newItems.append(newItem); |
| 103 | // You might think this should be JS Ownership, but no! The menu deletes stuff |
| 104 | // explicitly! |
| 105 | // Inb4 this causes a leak... |
| 106 | // |
| 107 | // QQmlEngine::setObjectOwnership(newItem, QQmlEngine::JavaScriptOwnership); |
| 108 | // emit addItem(newItem); <- would work, but manual code, ew |
| 109 | } else { |
| 110 | newItem->deleteLater(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (delegate) { |
| 115 | for (const auto &modelData : std::as_const(model)) { |
| 116 | QVariantMap initial; |
| 117 | initial["modelData"] = modelData; |
| 118 | |
| 119 | auto newItem = |
| 120 | delegate->createWithInitialProperties(initial, QQmlEngine::contextForObject(this)); |
| 121 | |
| 122 | // targetProperty.write(QVariant::fromValue(newItem)); <- appends but breaks |
| 123 | // removal |
| 124 | newItems.append(newItem); |
| 125 | // You might think this should be JS Ownership, but no! The menu deletes stuff |
| 126 | // explicitly! |
| 127 | // Inb4 this causes a leak... |
| 128 | // |
| 129 | // QQmlEngine::setObjectOwnership(newItem, QQmlEngine::JavaScriptOwnership); |
| 130 | // emit addItem(newItem); <- would work, but manual code, ew |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | QTimer::singleShot(0, this, [this] { |
| 135 | auto createdItems = qvariant_cast<QQmlListReference>(targetProperty.read()); |
| 136 | // newItems.clear(); <- does not remove the visual items |
| 137 | |
| 138 | for (qsizetype i = createdItems.size(); i > 0; i--) { |
| 139 | // only remove items, not other random stuff in there! |
| 140 | if (auto item = qobject_cast<QQuickItem *>(createdItems.at(i - 1))) { |
| 141 | item->enabledChanged(); |
| 142 | } |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | // targetProperty.write(QVariant::fromValue(std::move(newItems))); |
| 147 | } |
| 148 | |
| 149 | #include "moc_NhekoMenuVisibilityFilter.cpp" |