Files
amnezia-client/client/ui/utils/systemTrayNotificationHandler.h
T
vkamnandAlexei 0d116bfc67 fix: use native NSStatusItem for the macOS tray menu (#2759) (#2941)
* fix: use native NSStatusItem for the macOS tray menu (#2759)

On macOS 14+ the status-item menu is tracked out of process, so Qt's
QCocoaSystemTrayIcon menu-tracking observer (installed by setContextMenu)
fires asynchronously and calls emitActivated(), which reads
-[NSEvent clickCount] when NSApp.currentEvent is no longer a mouse event.
clickCount then raises NSInternalInconsistencyException, crashing the app
the first time the tray menu is opened.

Stop using QSystemTrayIcon on macOS and own the NSStatusItem ourselves
(MacOSStatusIcon), attaching the menu's backing native NSMenu via
QMenu::toNSMenu(). AppKit shows the menu natively - with menu-bar highlight
and correct anchoring - without registering the crashing observer, and Qt
keeps the NSMenu in sync with the QActions so the existing menu logic
(enable/disable, translations) is reused. The icon stays colored per state
and notifications go through UNUserNotificationCenter. Windows and Linux
keep QSystemTrayIcon unchanged.

This also wires up the previously-unused MacOSStatusIcon and removes the
orphaned neNotificationHandler.h.

* chore: return error messages

---------

Co-authored-by: Alexei <alexei.selivanov@gmail.com>
2026-08-06 00:13:05 +08:00

64 lines
1.8 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef SYSTEMTRAYNOTIFICATIONHANDLER_H
#define SYSTEMTRAYNOTIFICATIONHANDLER_H
#include "notificationHandler.h"
#include <QMenu>
#include <QSystemTrayIcon>
class MacOSStatusIcon;
class SystemTrayNotificationHandler : public NotificationHandler {
Q_OBJECT
public:
explicit SystemTrayNotificationHandler(QObject* parent);
~SystemTrayNotificationHandler();
void setConnectionState(Vpn::ConnectionState state) override;
void onTranslationsUpdated() override;
public slots:
void updateWebsiteUrl(const QString &newWebsiteUrl);
protected:
virtual void notify(Message type, const QString& title,
const QString& message, int timerMsec) override;
private:
void showHideWindow();
void setTrayState(Vpn::ConnectionState state);
void onTrayActivated(QSystemTrayIcon::ActivationReason reason);
void setTrayIcon(const QString &iconPath);
private:
QMenu m_menu;
#ifdef Q_OS_MAC
MacOSStatusIcon* m_statusIcon = nullptr;
#else
QSystemTrayIcon m_systemTrayIcon;
#endif
QAction* m_trayActionShow = nullptr;
QAction* m_trayActionConnect = nullptr;
QAction* m_trayActionDisconnect = nullptr;
QAction* m_trayActionVisitWebSite = nullptr;
QAction* m_trayActionQuit = nullptr;
QAction* m_statusLabel = nullptr;
QAction* m_separator = nullptr;
const QString ConnectedTrayIconName = "active.png";
const QString DisconnectedTrayIconName = "default.png";
const QString ErrorTrayIconName = "error.png";
QString websiteUrl = "https://amnezia.org";
};
#endif // SYSTEMTRAYNOTIFICATIONHANDLER_H