Fix tray callback issues

This commit is contained in:
ReenigneArcher committed 2026-03-28 23:22:54 -04:00
1 parent 437250060b
commit 214ea00120
5 files changed
+197 -26

No files matched your search

+16 -3
View File
@@ -81,6 +81,17 @@ extern "C" {
*/
void tray_simulate_notification_click(void);
/**
* @brief Simulate clicking a top-level menu item by index (for testing purposes).
*
* On Linux (Qt): triggers the QAction associated with the given top-level menu
* index (separators and submenus are ignored).
* On other platforms: no-op.
*
* @param index Zero-based index in the top-level tray menu.
*/
void tray_simulate_menu_item_click(int index);
/**
* @brief Terminate UI loop.
*/
@@ -105,9 +116,11 @@ extern "C" {
* name, display name, and desktop file name used for D-Bus registration. On
* other platforms this function is a no-op.
*
* @param app_name Application name. NULL uses the default ("tray").
* @param app_display_name Display name shown in notifications. NULL derives
* from the tray tooltip or falls back to app_name.
* @param app_name Application name used as a technical identifier (e.g., for
* D-Bus registration). Converted to lowercase automatically. NULL uses the
* default ("tray").
* @param app_display_name Human-readable name shown in notifications and UI.
* NULL derives from the tray tooltip or falls back to app_name.
* @param desktop_name Desktop file name for D-Bus. NULL appends ".desktop"
* to app_name.
*/
+5
View File
@@ -135,6 +135,11 @@ void tray_simulate_notification_click(void) {
// Simulation is not supported here.
}
void tray_simulate_menu_item_click(int index) {
// Programmatic menu-item simulation is not supported here.
(void) index;
}
void tray_set_log_callback(void (*cb)(int level, const char *msg)) {
// Qt is not used on macOS; log routing is not applicable.
(void) cb;
+133 -23
View File
@@ -12,12 +12,15 @@
#include "tray.h"
// Qt includes
#include <QAction>
#include <QApplication>
#include <QCoreApplication>
#include <QCursor>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QFileInfo>
#include <QGuiApplication>
#include <QIcon>
@@ -303,15 +306,16 @@ namespace {
if (g_notification_id == 0) {
return;
}
const uint id_to_close = g_notification_id;
g_notification_id = 0;
QDBusInterface iface(
QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
QStringLiteral("org.freedesktop.Notifications")
);
if (iface.isValid()) {
iface.call(QStringLiteral("CloseNotification"), g_notification_id);
iface.asyncCall(QStringLiteral("CloseNotification"), id_to_close);
}
g_notification_id = 0;
}
QMenu *build_menu(struct tray_menu *m, QWidget *parent) {
@@ -322,7 +326,8 @@ namespace {
} else if (m->submenu != nullptr) {
QMenu *sub = build_menu(m->submenu, menu);
sub->setTitle(QString::fromUtf8(m->text));
menu->addMenu(sub);
QAction *sub_action = menu->addMenu(sub);
sub_action->setEnabled(m->disabled == 0);
} else {
auto *action = menu->addAction(QString::fromUtf8(m->text));
action->setEnabled(m->disabled == 0);
@@ -330,17 +335,79 @@ namespace {
action->setCheckable(true);
action->setChecked(m->checked != 0);
}
if (m->cb != nullptr) {
struct tray_menu *item = m;
QObject::connect(action, &QAction::triggered, [item]() {
action->setData(QVariant::fromValue<quintptr>(reinterpret_cast<quintptr>(m)));
QObject::connect(action, &QAction::triggered, menu, [action]() {
auto *item = reinterpret_cast<struct tray_menu *>(action->data().value<quintptr>());
if (item != nullptr && item->cb != nullptr) {
item->cb(item);
});
}
}
});
}
}
return menu;
}
bool menu_layout_matches(const QMenu *menu, const struct tray_menu *items) {
if (menu == nullptr) {
return false;
}
const QList<QAction *> actions = menu->actions();
int action_index = 0;
for (const struct tray_menu *item = items; item != nullptr && item->text != nullptr; item++) {
if (action_index >= actions.size()) {
return false;
}
QAction *action = actions.at(action_index++);
if (std::strcmp(item->text, "-") == 0) {
if (!action->isSeparator()) {
return false;
}
continue;
}
if (item->submenu != nullptr) {
QMenu *submenu = action->menu();
if (submenu == nullptr || !menu_layout_matches(submenu, item->submenu)) {
return false;
}
} else if (action->isSeparator() || action->menu() != nullptr) {
return false;
}
}
return action_index == actions.size();
}
void update_menu_state(QMenu *menu, struct tray_menu *items) {
if (menu == nullptr || items == nullptr) {
return;
}
const QList<QAction *> actions = menu->actions();
int action_index = 0;
for (struct tray_menu *item = items; item != nullptr && item->text != nullptr; item++) {
QAction *action = actions.at(action_index++);
if (std::strcmp(item->text, "-") == 0) {
continue;
}
action->setText(QString::fromUtf8(item->text));
action->setEnabled(item->disabled == 0);
if (item->submenu != nullptr) {
update_menu_state(action->menu(), item->submenu);
continue;
}
action->setCheckable(item->checkbox != 0);
if (item->checkbox != 0) {
action->setChecked(item->checked != 0);
}
action->setData(QVariant::fromValue<quintptr>(reinterpret_cast<quintptr>(item)));
}
}
void destroy_tray() {
close_notification();
if (g_notification_handler != nullptr) {
@@ -353,7 +420,10 @@ namespace {
g_tray_icon->setContextMenu(nullptr);
delete g_tray_icon; // NOSONAR(cpp:S5025) - raw pointer; deleted explicitly before QApplication is destroyed
g_tray_icon = nullptr;
delete menu; // NOSONAR(cpp:S5025) - QSystemTrayIcon does not own the context menu
if (menu != nullptr) {
menu->hide();
delete menu; // NOSONAR(cpp:S5025) - QSystemTrayIcon does not own the context menu
}
}
}
@@ -547,11 +617,21 @@ extern "C" {
}
if (tray->menu != nullptr) {
// setContextMenu does not take ownership; delete the old menu before replacing it.
QMenu *old_menu = g_tray_icon->contextMenu();
QMenu *new_menu = build_menu(tray->menu, nullptr); // NOSONAR(cpp:S5025) - deleted via old_menu path or on next update
g_tray_icon->setContextMenu(new_menu);
delete old_menu; // NOSONAR(cpp:S5025) - required; Qt does not own this
QMenu *existing_menu = g_tray_icon->contextMenu();
if (existing_menu != nullptr && menu_layout_matches(existing_menu, tray->menu)) {
update_menu_state(existing_menu, tray->menu);
} else {
// setContextMenu does not take ownership; delete the old menu before replacing it.
QMenu *new_menu = build_menu(tray->menu, nullptr); // NOSONAR(cpp:S5025) - deleted via old_menu path or on next update
g_tray_icon->setContextMenu(new_menu);
if (existing_menu != nullptr) {
// hide() before delete releases any X11 pointer grab held by the popup.
// Skipping this leaves the grab active, causing future popup menus to appear
// but receive no pointer events, so QAction::triggered is never emitted.
existing_menu->hide();
delete existing_menu; // NOSONAR(cpp:S5025) - required; Qt does not own this
}
}
}
const QString text = tray->notification_text != nullptr ? QString::fromUtf8(tray->notification_text) : QString();
@@ -590,14 +670,18 @@ extern "C" {
if (tray->notification_cb != nullptr) {
actions << QStringLiteral("default") << QString();
}
// Store the callback before calling Notify so tray_simulate_notification_click works
// Store the callback before the async Notify so tray_simulate_notification_click works
// even when the notification daemon is unavailable and the D-Bus reply is invalid.
if (g_notification_handler != nullptr) {
g_notification_handler->cb = tray->notification_cb;
}
QDBusReply<uint> reply = iface.call(
// Use asyncCall to avoid entering a nested Qt event loop while waiting for the D-Bus
// reply. A synchronous call here would allow re-entrant tray_update dispatches from
// other threads (via BlockingQueuedConnection), which can delete and replace the context
// QMenu mid-build, breaking all QAction signal connections.
QDBusPendingCall pending = iface.asyncCall(
QStringLiteral("Notify"),
QStringLiteral("tray"),
QGuiApplication::applicationDisplayName(),
static_cast<uint>(0),
icon,
title,
@@ -606,12 +690,17 @@ extern "C" {
hints,
5000
);
if (reply.isValid()) {
g_notification_id = reply.value();
if (g_notification_handler != nullptr) {
g_notification_handler->notification_id = g_notification_id;
auto *watcher = new QDBusPendingCallWatcher(pending); // NOSONAR(cpp:S5025) - deleted via deleteLater in finished handler
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [watcher]() {
const QDBusPendingReply<uint> reply = *watcher;
if (reply.isValid() && g_tray_icon != nullptr) {
g_notification_id = reply.value();
if (g_notification_handler != nullptr) {
g_notification_handler->notification_id = g_notification_id;
}
}
}
watcher->deleteLater();
});
} else {
// D-Bus unavailable: fall back to Qt's built-in balloon and messageClicked signal.
if (tray->notification_cb != nullptr && g_notification_handler != nullptr) {
@@ -657,6 +746,27 @@ extern "C" {
});
}
void tray_simulate_menu_item_click(int index) {
run_on_qt_thread([index]() {
if (g_tray_icon == nullptr || index < 0) {
return;
}
QMenu *menu = g_tray_icon->contextMenu();
if (menu == nullptr) {
return;
}
const QList<QAction *> actions = menu->actions();
if (index >= actions.size()) {
return;
}
QAction *action = actions.at(index);
if (action == nullptr || action->isSeparator() || action->menu() != nullptr || !action->isEnabled()) {
return;
}
action->trigger();
});
}
void tray_exit(void) {
g_loop_result = -1;
g_exit_pending = true;
+5
View File
@@ -342,6 +342,11 @@ void tray_simulate_notification_click(void) {
// Simulating this from outside the message pump is not supported here.
}
void tray_simulate_menu_item_click(int index) {
// Programmatic menu-item simulation is not supported here.
(void) index;
}
void tray_set_log_callback(void (*cb)(int level, const char *msg)) {
// Qt is not used on Windows; log routing is not applicable.
(void) cb;
+38
View File
@@ -692,4 +692,42 @@ TEST_F(TrayTest, TestNotificationCallbackFiredOnClick) {
tray_update(&testTray);
}
TEST_F(TrayTest, TestMenuCallbackAfterNotificationUpdate) {
static int callbackCount = 0;
callbackCount = 0;
auto first_item_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
callbackCount++;
(void) item;
};
void (*original_cb)(struct tray_menu *) = testTray.menu[0].cb;
testTray.menu[0].cb = first_item_callback;
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
tray_simulate_menu_item_click(0);
tray_loop(0);
EXPECT_EQ(callbackCount, 1);
testTray.notification_title = "Menu Callback Regression";
testTray.notification_text = "Notification update should not break menu callbacks";
testTray.notification_icon = TRAY_ICON1;
tray_update(&testTray);
WaitForTrayReady();
tray_simulate_menu_item_click(0);
tray_loop(0);
EXPECT_EQ(callbackCount, 2);
testTray.notification_title = nullptr;
testTray.notification_text = nullptr;
testTray.notification_icon = nullptr;
tray_update(&testTray);
testTray.menu[0].cb = original_cb;
}
#endif // TRAY_QT