@@ -45,6 +45,8 @@ Q_ENUM_NS(Enum);
4545// / A system tray item, roughly conforming to the [kde/freedesktop spec]
4646// / (there is no real spec, we just implemented whatever seemed to actually be used).
4747// /
48+ // / The associated context menu can be retrieved using a [SystemTrayMenuWatcher](../systemtraymenuwatcher).
49+ // /
4850// / [kde/freedesktop spec]: https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem/
4951class SystemTrayItem : public QObject {
5052 using DBusMenuItem = qs::dbus::dbusmenu::DBusMenuItem;
@@ -60,8 +62,6 @@ class SystemTrayItem: public QObject {
6062 Q_PROPERTY (QString icon READ icon NOTIFY iconChanged);
6163 Q_PROPERTY (QString tooltipTitle READ tooltipTitle NOTIFY tooltipTitleChanged);
6264 Q_PROPERTY (QString tooltipDescription READ tooltipDescription NOTIFY tooltipDescriptionChanged);
63- // The context menu provided by the application, generally displayed via a right click.
64- Q_PROPERTY (DBusMenuItem* menu READ menu NOTIFY menuChanged);
6565 // / If this tray item only offers a menu and activation will do nothing.
6666 Q_PROPERTY (bool onlyMenu READ onlyMenu NOTIFY onlyMenuChanged);
6767 QML_ELEMENT;
@@ -71,13 +71,13 @@ class SystemTrayItem: public QObject {
7171 explicit SystemTrayItem (qs::service::sni::StatusNotifierItem* item, QObject* parent = nullptr );
7272
7373 // / Primary activation action, generally triggered via a left click.
74- Q_INVOKABLE void activate ();
74+ Q_INVOKABLE void activate () const ;
7575
7676 // / Secondary activation action, generally triggered via a middle click.
77- Q_INVOKABLE void secondaryActivate ();
77+ Q_INVOKABLE void secondaryActivate () const ;
7878
7979 // / Scroll action, such as changing volume on a mixer.
80- Q_INVOKABLE void scroll (qint32 delta, bool horizontal);
80+ Q_INVOKABLE void scroll (qint32 delta, bool horizontal) const ;
8181
8282 [[nodiscard]] QString id () const ;
8383 [[nodiscard]] QString title () const ;
@@ -86,9 +86,10 @@ class SystemTrayItem: public QObject {
8686 [[nodiscard]] QString icon () const ;
8787 [[nodiscard]] QString tooltipTitle () const ;
8888 [[nodiscard]] QString tooltipDescription () const ;
89- [[nodiscard]] DBusMenuItem* menu () const ;
9089 [[nodiscard]] bool onlyMenu () const ;
9190
91+ qs::service::sni::StatusNotifierItem* item = nullptr ;
92+
9293signals:
9394 void idChanged ();
9495 void titleChanged ();
@@ -97,17 +98,7 @@ class SystemTrayItem: public QObject {
9798 void iconChanged ();
9899 void tooltipTitleChanged ();
99100 void tooltipDescriptionChanged ();
100- void menuChanged ();
101101 void onlyMenuChanged ();
102-
103- private slots:
104- void onMenuPathChanged ();
105-
106- private:
107- qs::service::sni::StatusNotifierItem* item = nullptr ;
108- qs::dbus::dbusmenu::DBusMenu* mMenu = nullptr ;
109-
110- friend class SystemTray ;
111102};
112103
113104// /! System tray
@@ -139,3 +130,39 @@ private slots:
139130
140131 QList<SystemTrayItem*> mItems ;
141132};
133+
134+ // /! Accessor for SystemTrayItem menus.
135+ // / SystemTrayMenuWatcher provides access to the associated
136+ // / [DBusMenuItem](../../quickshell.dbusmenu/dbusmenuitem) for a tray item.
137+ class SystemTrayMenuWatcher : public QObject {
138+ using DBusMenu = qs::dbus::dbusmenu::DBusMenu;
139+ using DBusMenuItem = qs::dbus::dbusmenu::DBusMenuItem;
140+
141+ Q_OBJECT;
142+ // / The tray item to watch.
143+ Q_PROPERTY (SystemTrayItem* trayItem READ trayItem WRITE setTrayItem NOTIFY trayItemChanged);
144+ // / The menu associated with the tray item. Will be null if `trayItem` is null
145+ // / or has no associated menu.
146+ Q_PROPERTY (DBusMenuItem* menu READ menu NOTIFY menuChanged);
147+ QML_ELEMENT;
148+
149+ public:
150+ explicit SystemTrayMenuWatcher (QObject* parent = nullptr ): QObject(parent) {}
151+
152+ [[nodiscard]] SystemTrayItem* trayItem () const ;
153+ void setTrayItem (SystemTrayItem* item);
154+
155+ [[nodiscard]] DBusMenuItem* menu () const ;
156+
157+ signals:
158+ void menuChanged ();
159+ void trayItemChanged ();
160+
161+ private slots:
162+ void onItemDestroyed ();
163+ void onMenuPathChanged ();
164+
165+ private:
166+ SystemTrayItem* item = nullptr ;
167+ DBusMenu* mMenu = nullptr ;
168+ };
0 commit comments