Add tray log callback and menu positioning fixes

Expose tray_set_log_callback in the public API and implement it on Linux to forward Qt diagnostic messages to a user-provided callback (NULL restores default behavior). Add a Qt message handler and global callback state, and route qInstallMessageHandler accordingly; provide no-op stubs on macOS and Windows since Qt logging is not used there. Improve context-menu positioning by calculating a sensible popup point from the tray icon geometry (falling back to cursor position) and use it when showing menus. Defer D-Bus ActionInvoked connection via QTimer::singleShot to avoid QSocketNotifier warnings when the tray runs in a std::thread, and add a few related include adjustments and minor activation handling cleanup.
This commit is contained in:
ReenigneArcher committed 2026-03-28 23:22:53 -04:00
1 parent 39992ed86e
commit 7f3e3bd81a
4 files changed
+108 -17

No files matched your search

+12
View File
@@ -86,6 +86,18 @@ extern "C" {
*/
void tray_exit(void);
/**
* @brief Set a callback for log messages produced by the tray library.
*
* On Linux the callback is installed as a Qt message handler so all Qt
* diagnostic output is routed through it. On other platforms this function
* is a no-op.
*
* @param cb Callback invoked with level (0=debug, 1=info, 2=warning, 3=error)
* and the message string. Pass NULL to restore the default logging behaviour.
*/
void tray_set_log_callback(void (*cb)(int level, const char *msg));
#if defined(TRAY_WINAPI)
/**
* @brief Get the tray window handle.
+5
View File
@@ -135,6 +135,11 @@ void tray_simulate_notification_click(void) {
// Simulation is not supported here.
}
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;
}
void tray_exit(void) {
// Remove the status item from the status bar on the main thread
// NSStatusBar operations must be performed on the main thread
+86 -17
View File
@@ -16,9 +16,12 @@
#include <QDBusInterface>
#include <QDBusReply>
#include <QFileInfo>
#include <QGuiApplication>
#include <QIcon>
#include <QMenu>
#include <QScreen>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QUrl>
#include <QVariantMap>
@@ -63,6 +66,55 @@ namespace {
bool g_app_owned = false; // NOSONAR(cpp:S5421) - mutable state, not const
bool g_exit_pending = false; // NOSONAR(cpp:S5421) - mutable state, not const
uint g_notification_id = 0; // NOSONAR(cpp:S5421) - tracks last D-Bus notification ID for proper cleanup
void (*g_log_cb)(int, const char *) = nullptr; // NOSONAR(cpp:S5421) - mutable state, not const
/**
* @brief Qt message handler that forwards to the registered log callback.
* @param type The Qt message type.
* @param msg The message string.
*/
void qt_message_handler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
if (g_log_cb == nullptr) {
return;
}
int level;
switch (type) {
case QtDebugMsg:
level = 0;
break;
case QtInfoMsg:
level = 1;
break;
case QtWarningMsg:
level = 2;
break;
default:
level = 3;
break;
}
g_log_cb(level, msg.toUtf8().constData());
}
/**
* @brief Calculate the best position to show the context menu.
*
* Uses the tray icon geometry when available (reliable on X11/XEmbed and
* some SNI desktops). Falls back to the current cursor position on systems
* where the icon geometry cannot be determined. Qt's QMenu::popup() will
* adjust the final position to keep the menu fully on-screen.
*
* @return The point at which to show the context menu.
*/
QPoint calculateMenuPosition() {
if (g_tray_icon != nullptr) {
const QRect iconGeometry = g_tray_icon->geometry();
if (iconGeometry.isValid()) {
// Qt's popup() will flip the menu above the icon if it would go off-screen.
return iconGeometry.bottomLeft();
}
}
return QCursor::pos();
}
void close_notification() {
if (g_notification_id == 0) {
@@ -166,33 +218,41 @@ extern "C" {
return -1;
}
// Show the context menu on left-click (Trigger) in addition to the default right-click path.
// QSystemTrayIcon::setContextMenu only handles right-click on X11/XEmbed; SNI-based desktops
// may not show the menu at all without this explicit connection.
// Show the context menu on the default trigger (clicked).
// QSystemTrayIcon::setContextMenu only handles right-click on X11/XEmbed;
// SNI-based desktops may not show the menu at all without this explicit connection.
// The menu is positioned using the tray icon geometry rather than the cursor position,
// which is unreliable on Wayland.
QObject::connect(g_tray_icon, &QSystemTrayIcon::activated, [](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::MiddleClick) {
if (reason == QSystemTrayIcon::Trigger) {
if (g_tray_icon != nullptr) {
QMenu *menu = g_tray_icon->contextMenu();
if (menu != nullptr) {
menu->popup(QCursor::pos());
menu->popup(calculateMenuPosition());
}
}
}
});
// Create the D-Bus ActionInvoked handler once per QApplication lifetime.
// This receives the "default" action event when the user clicks a D-Bus notification,
// which QSystemTrayIcon::messageClicked does NOT emit for D-Bus-dispatched notifications.
// Defer D-Bus ActionInvoked handler setup to the first event-loop iteration.
// Creating QDBusConnection socket notifiers before the event loop starts can
// trigger a "QSocketNotifier: Can only be used with threads started with QThread"
// warning when the tray runs in a std::thread. Deferring via QTimer::singleShot
// ensures the socket notifiers are created while the event dispatcher is active.
if (g_notification_handler == nullptr) {
g_notification_handler = new TrayNotificationHandler(); // NOSONAR(cpp:S5025) - deleted in destroy_app()
QDBusConnection::sessionBus().connect(
QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("ActionInvoked"),
g_notification_handler,
SLOT(onActionInvoked(uint, QString))
);
QTimer::singleShot(0, g_notification_handler, []() {
if (g_notification_handler != nullptr) {
QDBusConnection::sessionBus().connect(
QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("ActionInvoked"),
g_notification_handler,
SLOT(onActionInvoked(uint, QString))
);
}
});
}
tray_update(tray);
@@ -320,7 +380,7 @@ extern "C" {
if (g_tray_icon != nullptr) {
QMenu *menu = g_tray_icon->contextMenu();
if (menu != nullptr) {
menu->popup(QCursor::pos());
menu->popup(calculateMenuPosition());
QApplication::processEvents();
}
}
@@ -349,6 +409,15 @@ extern "C" {
}
}
void tray_set_log_callback(void (*cb)(int level, const char *msg)) {
g_log_cb = cb;
if (cb != nullptr) {
qInstallMessageHandler(qt_message_handler);
} else {
qInstallMessageHandler(nullptr);
}
}
} // extern "C"
// Must be included at the end of a .cpp file when Q_OBJECT classes are defined
+5
View File
@@ -336,6 +336,11 @@ void tray_simulate_notification_click(void) {
// Simulating this from outside the message pump is not supported here.
}
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;
}
void tray_exit(void) {
Shell_NotifyIconW(NIM_DELETE, &nid);
SendMessage(hwnd, WM_CLOSE, 0, 0);