Skip to content

Commit

Permalink
core/qsmenu!: improve menu layout change UX
Browse files Browse the repository at this point in the history
Exposes QsMenuOpener.children as an ObjectModel instead of a list to
allow smoother layout change handling in custom menu renderers.

Fixes QsMenuAnchor/platform menus closing whenever menu content changes.
  • Loading branch information
outfoxxed committed Dec 13, 2024
1 parent 3fc1c91 commit a053373
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 67 deletions.
16 changes: 16 additions & 0 deletions src/core/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ bool UntypedObjectModel::removeObject(const QObject* object) {
return true;
}

void UntypedObjectModel::diffUpdate(const QVector<QObject*>& newValues) {
for (qsizetype i = 0; i < this->valuesList.length();) {
if (newValues.contains(this->valuesList.at(i))) i++;
else this->removeAt(i);
}

qsizetype oi = 0;
for (auto* object: newValues) {
if (this->valuesList.length() == oi || this->valuesList.at(oi) != object) {
this->insertObject(object, oi);
}

oi++;
}
}

qsizetype UntypedObjectModel::indexOf(QObject* object) { return this->valuesList.indexOf(object); }

UntypedObjectModel* UntypedObjectModel::emptyInstance() {
Expand Down
8 changes: 8 additions & 0 deletions src/core/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class UntypedObjectModel: public QAbstractListModel {
void insertObject(QObject* object, qsizetype index = -1);
bool removeObject(const QObject* object);

// Assumes only one instance of a specific value
void diffUpdate(const QVector<QObject*>& newValues);

QVector<QObject*> valuesList;

private:
Expand All @@ -97,6 +100,11 @@ class ObjectModel: public UntypedObjectModel {

void removeObject(const T* object) { this->UntypedObjectModel::removeObject(object); }

// Assumes only one instance of a specific value
void diffUpdate(const QVector<T*>& newValues) {
this->UntypedObjectModel::diffUpdate(*std::bit_cast<const QVector<QObject*>*>(&newValues));
}

static ObjectModel<T>* emptyInstance() {
return static_cast<ObjectModel<T>*>(UntypedObjectModel::emptyInstance());
}
Expand Down
8 changes: 5 additions & 3 deletions src/core/platformmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../window/proxywindow.hpp"
#include "../window/windowinterface.hpp"
#include "iconprovider.hpp"
#include "model.hpp"
#include "platformmenu_p.hpp"
#include "popupanchor.hpp"
#include "qsmenu.hpp"
Expand Down Expand Up @@ -61,6 +62,7 @@ PlatformMenuEntry::PlatformMenuEntry(QsMenuEntry* menu): QObject(menu), menu(men
QObject::connect(menu, &QsMenuEntry::buttonTypeChanged, this, &PlatformMenuEntry::onButtonTypeChanged);
QObject::connect(menu, &QsMenuEntry::checkStateChanged, this, &PlatformMenuEntry::onCheckStateChanged);
QObject::connect(menu, &QsMenuEntry::hasChildrenChanged, this, &PlatformMenuEntry::relayoutParent);
QObject::connect(menu->children(), &UntypedObjectModel::valuesChanged, this, &PlatformMenuEntry::relayout);
// clang-format on
}

Expand Down Expand Up @@ -178,10 +180,10 @@ void PlatformMenuEntry::relayout() {
this->qmenu->setIcon(getCurrentEngineImageAsIcon(icon));
}

auto children = this->menu->children();
auto len = children.count(&children);
const auto& children = this->menu->children()->valueList();
auto len = children.count();
for (auto i = 0; i < len; i++) {
auto* child = children.at(&children, i);
auto* child = children.at(i);

auto* instance = new PlatformMenuEntry(child);
QObject::connect(instance, &QObject::destroyed, this, &PlatformMenuEntry::onChildDestroyed);
Expand Down
33 changes: 6 additions & 27 deletions src/core/qsmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <qobject.h>
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qtypes.h>

#include "model.hpp"
#include "platformmenu.hpp"

using namespace qs::menu::platform;
Expand Down Expand Up @@ -34,15 +34,6 @@ void QsMenuEntry::display(QObject* parentWindow, int relativeX, int relativeY) {
if (!success) delete platform;
}

QQmlListProperty<QsMenuEntry> QsMenuEntry::emptyChildren(QObject* parent) {
return QQmlListProperty<QsMenuEntry>(
parent,
nullptr,
&QsMenuEntry::childCount,
&QsMenuEntry::childAt
);
}

void QsMenuEntry::ref() {
this->refcount++;
if (this->refcount == 1) emit this->opened();
Expand All @@ -53,7 +44,9 @@ void QsMenuEntry::unref() {
if (this->refcount == 0) emit this->closed();
}

QQmlListProperty<QsMenuEntry> QsMenuEntry::children() { return QsMenuEntry::emptyChildren(this); }
ObjectModel<QsMenuEntry>* QsMenuEntry::children() {
return ObjectModel<QsMenuEntry>::emptyInstance();
}

QsMenuOpener::~QsMenuOpener() {
if (this->mMenu) {
Expand Down Expand Up @@ -83,13 +76,6 @@ void QsMenuOpener::setMenu(QsMenuHandle* menu) {
if (menu != nullptr) {
auto onMenuChanged = [this, menu]() {
if (menu->menu()) {
QObject::connect(
menu->menu(),
&QsMenuEntry::childrenChanged,
this,
&QsMenuOpener::childrenChanged
);

menu->menu()->ref();
}

Expand All @@ -113,19 +99,12 @@ void QsMenuOpener::onMenuDestroyed() {
emit this->childrenChanged();
}

QQmlListProperty<QsMenuEntry> QsMenuOpener::children() {
ObjectModel<QsMenuEntry>* QsMenuOpener::children() {
if (this->mMenu && this->mMenu->menu()) {
return this->mMenu->menu()->children();
} else {
return QsMenuEntry::emptyChildren(this);
return ObjectModel<QsMenuEntry>::emptyInstance();
}
}

qsizetype QsMenuEntry::childCount(QQmlListProperty<QsMenuEntry>* /*property*/) { return 0; }

QsMenuEntry*
QsMenuEntry::childAt(QQmlListProperty<QsMenuEntry>* /*property*/, qsizetype /*index*/) {
return nullptr;
}

} // namespace qs::menu
14 changes: 5 additions & 9 deletions src/core/qsmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <qtypes.h>

#include "doc.hpp"
#include "model.hpp"

namespace qs::menu {

Expand Down Expand Up @@ -107,9 +108,7 @@ class QsMenuEntry: public QsMenuHandle {
void ref();
void unref();

[[nodiscard]] virtual QQmlListProperty<QsMenuEntry> children();

static QQmlListProperty<QsMenuEntry> emptyChildren(QObject* parent);
[[nodiscard]] virtual ObjectModel<QsMenuEntry>* children();

signals:
/// Send a trigger/click signal to the menu entry.
Expand All @@ -125,12 +124,8 @@ class QsMenuEntry: public QsMenuHandle {
void buttonTypeChanged();
void checkStateChanged();
void hasChildrenChanged();
QSDOC_HIDE void childrenChanged();

private:
static qsizetype childCount(QQmlListProperty<QsMenuEntry>* property);
static QsMenuEntry* childAt(QQmlListProperty<QsMenuEntry>* property, qsizetype index);

qsizetype refcount = 0;
};

Expand All @@ -140,7 +135,8 @@ class QsMenuOpener: public QObject {
/// The menu to retrieve children from.
Q_PROPERTY(qs::menu::QsMenuHandle* menu READ menu WRITE setMenu NOTIFY menuChanged);
/// The children of the given menu.
Q_PROPERTY(QQmlListProperty<qs::menu::QsMenuEntry> children READ children NOTIFY childrenChanged);
QSDOC_TYPE_OVERRIDE(ObjectModel<qs::menu::QsMenuEntry>*);
Q_PROPERTY(UntypedObjectModel* children READ children NOTIFY childrenChanged);
QML_ELEMENT;

public:
Expand All @@ -151,7 +147,7 @@ class QsMenuOpener: public QObject {
[[nodiscard]] QsMenuHandle* menu() const;
void setMenu(QsMenuHandle* menu);

[[nodiscard]] QQmlListProperty<QsMenuEntry> children();
[[nodiscard]] ObjectModel<QsMenuEntry>* children();

signals:
void menuChanged();
Expand Down
41 changes: 18 additions & 23 deletions src/dbus/dbusmenu/dbusmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <qvariant.h>

#include "../../core/iconimageprovider.hpp"
#include "../../core/model.hpp"
#include "../../core/qsmenu.hpp"
#include "../../dbus/properties.hpp"
#include "dbus_menu.h"
Expand Down Expand Up @@ -95,22 +96,8 @@ void DBusMenuItem::updateLayout() const {

bool DBusMenuItem::hasChildren() const { return this->displayChildren || this->id == 0; }

QQmlListProperty<QsMenuEntry> DBusMenuItem::children() {
return QQmlListProperty<QsMenuEntry>(
this,
nullptr,
&DBusMenuItem::childrenCount,
&DBusMenuItem::childAt
);
}

qsizetype DBusMenuItem::childrenCount(QQmlListProperty<QsMenuEntry>* property) {
return reinterpret_cast<DBusMenuItem*>(property->object)->enabledChildren.count();
}

QsMenuEntry* DBusMenuItem::childAt(QQmlListProperty<QsMenuEntry>* property, qsizetype index) {
auto* item = reinterpret_cast<DBusMenuItem*>(property->object);
return item->menu->items.value(item->enabledChildren.at(index));
ObjectModel<QsMenuEntry>* DBusMenuItem::children() {
return reinterpret_cast<ObjectModel<QsMenuEntry>*>(&this->enabledChildren);
}

void DBusMenuItem::updateProperties(const QVariantMap& properties, const QStringList& removed) {
Expand Down Expand Up @@ -270,14 +257,13 @@ void DBusMenuItem::updateProperties(const QVariantMap& properties, const QString
}

void DBusMenuItem::onChildrenUpdated() {
this->enabledChildren.clear();

QVector<DBusMenuItem*> children;
for (auto child: this->mChildren) {
auto* item = this->menu->items.value(child);
if (item->visible) this->enabledChildren.push_back(child);
if (item->visible) children.append(item);
}

emit this->childrenChanged();
this->enabledChildren.diffUpdate(children);
}

QDebug operator<<(QDebug debug, DBusMenuItem* item) {
Expand Down Expand Up @@ -388,7 +374,7 @@ void DBusMenu::updateLayoutRecursive(
[&](const DBusMenuLayout& layout) { return layout.id == *iter; }
);

if (existing == layout.children.end()) {
if (!item->mShowChildren || existing == layout.children.end()) {
qCDebug(logDbusMenu) << "Removing missing layout item" << this->items.value(*iter) << "from"
<< item;
this->removeRecursive(*iter);
Expand All @@ -402,15 +388,23 @@ void DBusMenu::updateLayoutRecursive(
for (const auto& child: layout.children) {
if (item->mShowChildren && !item->mChildren.contains(child.id)) {
qCDebug(logDbusMenu) << "Creating new layout item" << child.id << "in" << item;
item->mChildren.push_back(child.id);
// item->mChildren.push_back(child.id);
this->items.insert(child.id, nullptr);
childrenChanged = true;
}

this->updateLayoutRecursive(child, item, depth - 1);
}

if (childrenChanged) item->onChildrenUpdated();
if (childrenChanged) {
// reset to preserve order
item->mChildren.clear();
for (const auto& child: layout.children) {
item->mChildren.push_back(child.id);
}

item->onChildrenUpdated();
}
}

if (item->mShowChildren && !item->childrenLoaded) {
Expand Down Expand Up @@ -554,6 +548,7 @@ void DBusMenuHandle::onMenuPathChanged() {
this->mMenu->setParent(this);

QObject::connect(&this->mMenu->rootItem, &DBusMenuItem::layoutUpdated, this, [this]() {
QObject::disconnect(&this->mMenu->rootItem, &DBusMenuItem::layoutUpdated, this, nullptr);
this->loaded = true;
emit this->menuChanged();
});
Expand Down
8 changes: 3 additions & 5 deletions src/dbus/dbusmenu/dbusmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "../../core/doc.hpp"
#include "../../core/imageprovider.hpp"
#include "../../core/model.hpp"
#include "../../core/qsmenu.hpp"
#include "../properties.hpp"
#include "dbus_menu_types.hpp"
Expand Down Expand Up @@ -65,7 +66,7 @@ class DBusMenuItem: public QsMenuEntry {
[[nodiscard]] bool isShowingChildren() const;
void setShowChildrenRecursive(bool showChildren);

[[nodiscard]] QQmlListProperty<menu::QsMenuEntry> children() override;
[[nodiscard]] ObjectModel<QsMenuEntry>* children() override;

void updateProperties(const QVariantMap& properties, const QStringList& removed = {});
void onChildrenUpdated();
Expand Down Expand Up @@ -96,11 +97,8 @@ private slots:
menu::QsMenuButtonType::Enum mButtonType = menu::QsMenuButtonType::None;
Qt::CheckState mCheckState = Qt::Unchecked;
bool displayChildren = false;
QVector<qint32> enabledChildren;
ObjectModel<DBusMenuItem> enabledChildren {this};
DBusMenuItem* parentMenu = nullptr;

static qsizetype childrenCount(QQmlListProperty<menu::QsMenuEntry>* property);
static menu::QsMenuEntry* childAt(QQmlListProperty<menu::QsMenuEntry>* property, qsizetype index);
};

QDebug operator<<(QDebug debug, DBusMenuItem* item);
Expand Down

0 comments on commit a053373

Please sign in to comment.