core/menu: add handle support to QsMenuOpener + add handle to tray

This commit is contained in:
outfoxxed 2024-07-25 02:51:17 -07:00
parent acdbe73c10
commit 54350277be
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
6 changed files with 101 additions and 51 deletions

View file

@ -20,7 +20,6 @@ using namespace qs::dbus;
using namespace qs::dbus::dbusmenu;
using namespace qs::service::sni;
using namespace qs::menu::platform;
using qs::menu::QsMenuHandle;
SystemTrayItem::SystemTrayItem(qs::service::sni::StatusNotifierItem* item, QObject* parent)
: QObject(parent)
@ -96,6 +95,11 @@ bool SystemTrayItem::hasMenu() const {
return !this->item->menuPath.get().path().isEmpty();
}
DBusMenuHandle* SystemTrayItem::menu() const {
if (this->item == nullptr) return nullptr;
return this->item->menuHandle();
}
bool SystemTrayItem::onlyMenu() const {
if (this->item == nullptr) return false;
return this->item->isMenu.get();
@ -120,7 +124,7 @@ void SystemTrayItem::display(QObject* parentWindow, qint32 relativeX, qint32 rel
QObject::disconnect(handle, nullptr, this, nullptr);
if (!handle->menu()) {
handle->unref();
handle->unrefHandle();
return;
}
@ -128,7 +132,7 @@ void SystemTrayItem::display(QObject* parentWindow, qint32 relativeX, qint32 rel
// clang-format off
QObject::connect(platform, &PlatformMenuEntry::closed, this, [=]() { platform->deleteLater(); });
QObject::connect(platform, &QObject::destroyed, this, [=]() { handle->unref(); });
QObject::connect(platform, &QObject::destroyed, this, [=]() { handle->unrefHandle(); });
// clang-format on
auto success = platform->display(parentWindow, relativeX, relativeY);
@ -140,10 +144,10 @@ void SystemTrayItem::display(QObject* parentWindow, qint32 relativeX, qint32 rel
if (handle->menu()) {
onMenuChanged();
} else {
QObject::connect(handle, &QsMenuHandle::menuChanged, this, onMenuChanged);
QObject::connect(handle, &DBusMenuHandle::menuChanged, this, onMenuChanged);
}
handle->ref();
handle->refHandle();
}
SystemTray::SystemTray(QObject* parent): QObject(parent) {
@ -179,7 +183,7 @@ SystemTrayItem* SystemTrayMenuWatcher::trayItem() const { return this->item; }
SystemTrayMenuWatcher::~SystemTrayMenuWatcher() {
if (this->item != nullptr) {
this->item->item->menuHandle()->unref();
this->item->item->menuHandle()->unrefHandle();
}
}
@ -187,14 +191,14 @@ void SystemTrayMenuWatcher::setTrayItem(SystemTrayItem* item) {
if (item == this->item) return;
if (this->item != nullptr) {
this->item->item->menuHandle()->unref();
this->item->item->menuHandle()->unrefHandle();
QObject::disconnect(this->item, nullptr, this, nullptr);
}
this->item = item;
if (item != nullptr) {
this->item->item->menuHandle()->ref();
this->item->item->menuHandle()->refHandle();
QObject::connect(item, &QObject::destroyed, this, &SystemTrayMenuWatcher::onItemDestroyed);

View file

@ -53,6 +53,9 @@ Q_ENUM_NS(Enum);
class SystemTrayItem: public QObject {
using DBusMenuItem = qs::dbus::dbusmenu::DBusMenuItem;
// intentionally wrongly aliased to temporarily hack around a docgen issue
using QsMenuHandle = qs::dbus::dbusmenu::DBusMenuHandle;
Q_OBJECT;
/// A name unique to the application, such as its name.
Q_PROPERTY(QString id READ id NOTIFY idChanged);
@ -64,9 +67,10 @@ class SystemTrayItem: public QObject {
Q_PROPERTY(QString icon READ icon NOTIFY iconChanged);
Q_PROPERTY(QString tooltipTitle READ tooltipTitle NOTIFY tooltipTitleChanged);
Q_PROPERTY(QString tooltipDescription READ tooltipDescription NOTIFY tooltipDescriptionChanged);
/// If this tray item has an associated menu accessible via @@display()
/// or a @@SystemTrayMenuWatcher.
/// If this tray item has an associated menu accessible via @@display() or @@menu.
Q_PROPERTY(bool hasMenu READ hasMenu NOTIFY hasMenuChanged);
/// A handle to the menu associated with this tray item, if any.
Q_PROPERTY(QsMenuHandle* menu READ menu NOTIFY hasMenuChanged);
/// If this tray item only offers a menu and activation will do nothing.
Q_PROPERTY(bool onlyMenu READ onlyMenu NOTIFY onlyMenuChanged);
QML_ELEMENT;
@ -95,6 +99,7 @@ public:
[[nodiscard]] QString tooltipTitle() const;
[[nodiscard]] QString tooltipDescription() const;
[[nodiscard]] bool hasMenu() const;
[[nodiscard]] QsMenuHandle* menu() const;
[[nodiscard]] bool onlyMenu() const;
qs::service::sni::StatusNotifierItem* item = nullptr;
@ -136,6 +141,9 @@ private:
};
///! Accessor for SystemTrayItem menus.
/// > [!ERROR] Deprecated in favor of @@Quickshell.QsMenuOpener.menu,
/// > which now supports directly accessing a tray menu via @@SystemTrayItem.menu.
///
/// SystemTrayMenuWatcher provides access to the associated
/// @@Quickshell.DBusMenu.DBusMenuItem for a tray item.
class SystemTrayMenuWatcher: public QObject {