mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-12-04 06:01:26 +09:00
* 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>
64 lines
1.8 KiB
C++
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
|