2121#include < qvariant.h>
2222
2323#include " ../../core/iconimageprovider.hpp"
24+ #include " ../../core/model.hpp"
2425#include " ../../core/qsmenu.hpp"
2526#include " ../../dbus/properties.hpp"
2627#include " dbus_menu.h"
@@ -95,22 +96,8 @@ void DBusMenuItem::updateLayout() const {
9596
9697bool DBusMenuItem::hasChildren () const { return this ->displayChildren || this ->id == 0 ; }
9798
98- QQmlListProperty<QsMenuEntry> DBusMenuItem::children () {
99- return QQmlListProperty<QsMenuEntry>(
100- this ,
101- nullptr ,
102- &DBusMenuItem::childrenCount,
103- &DBusMenuItem::childAt
104- );
105- }
106-
107- qsizetype DBusMenuItem::childrenCount (QQmlListProperty<QsMenuEntry>* property) {
108- return reinterpret_cast <DBusMenuItem*>(property->object )->enabledChildren .count ();
109- }
110-
111- QsMenuEntry* DBusMenuItem::childAt (QQmlListProperty<QsMenuEntry>* property, qsizetype index) {
112- auto * item = reinterpret_cast <DBusMenuItem*>(property->object );
113- return item->menu ->items .value (item->enabledChildren .at (index));
99+ ObjectModel<QsMenuEntry>* DBusMenuItem::children () {
100+ return reinterpret_cast <ObjectModel<QsMenuEntry>*>(&this ->enabledChildren );
114101}
115102
116103void DBusMenuItem::updateProperties (const QVariantMap& properties, const QStringList& removed) {
@@ -270,14 +257,13 @@ void DBusMenuItem::updateProperties(const QVariantMap& properties, const QString
270257}
271258
272259void DBusMenuItem::onChildrenUpdated () {
273- this ->enabledChildren .clear ();
274-
260+ QVector<DBusMenuItem*> children;
275261 for (auto child: this ->mChildren ) {
276262 auto * item = this ->menu ->items .value (child);
277- if (item->visible ) this -> enabledChildren . push_back (child );
263+ if (item->visible ) children. append (item );
278264 }
279265
280- emit this ->childrenChanged ( );
266+ this ->enabledChildren . diffUpdate (children );
281267}
282268
283269QDebug operator <<(QDebug debug, DBusMenuItem* item) {
@@ -388,7 +374,7 @@ void DBusMenu::updateLayoutRecursive(
388374 [&](const DBusMenuLayout& layout) { return layout.id == *iter; }
389375 );
390376
391- if (existing == layout.children .end ()) {
377+ if (!item-> mShowChildren || existing == layout.children .end ()) {
392378 qCDebug (logDbusMenu) << " Removing missing layout item" << this ->items .value (*iter) << " from"
393379 << item;
394380 this ->removeRecursive (*iter);
@@ -402,15 +388,23 @@ void DBusMenu::updateLayoutRecursive(
402388 for (const auto & child: layout.children ) {
403389 if (item->mShowChildren && !item->mChildren .contains (child.id )) {
404390 qCDebug (logDbusMenu) << " Creating new layout item" << child.id << " in" << item;
405- item->mChildren .push_back (child.id );
391+ // item->mChildren.push_back(child.id);
406392 this ->items .insert (child.id , nullptr );
407393 childrenChanged = true ;
408394 }
409395
410396 this ->updateLayoutRecursive (child, item, depth - 1 );
411397 }
412398
413- if (childrenChanged) item->onChildrenUpdated ();
399+ if (childrenChanged) {
400+ // reset to preserve order
401+ item->mChildren .clear ();
402+ for (const auto & child: layout.children ) {
403+ item->mChildren .push_back (child.id );
404+ }
405+
406+ item->onChildrenUpdated ();
407+ }
414408 }
415409
416410 if (item->mShowChildren && !item->childrenLoaded ) {
@@ -554,6 +548,7 @@ void DBusMenuHandle::onMenuPathChanged() {
554548 this ->mMenu ->setParent (this );
555549
556550 QObject::connect (&this ->mMenu ->rootItem , &DBusMenuItem::layoutUpdated, this , [this ]() {
551+ QObject::disconnect (&this ->mMenu ->rootItem , &DBusMenuItem::layoutUpdated, this , nullptr );
557552 this ->loaded = true ;
558553 emit this ->menuChanged ();
559554 });
0 commit comments