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>
192 lines
6.7 KiB
C++
192 lines
6.7 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/. */
|
|
|
|
#include <QDebug>
|
|
#include "systemTrayNotificationHandler.h"
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
# include "platforms/macos/macosstatusicon.h"
|
|
#endif
|
|
|
|
#include <QApplication>
|
|
#include <QDesktopServices>
|
|
#include <QIcon>
|
|
#include <QWindow>
|
|
|
|
#include "version.h"
|
|
|
|
SystemTrayNotificationHandler::SystemTrayNotificationHandler(QObject* parent) :
|
|
NotificationHandler(parent)
|
|
#ifndef Q_OS_MAC
|
|
, m_systemTrayIcon(parent)
|
|
#endif
|
|
{
|
|
m_trayActionShow = m_menu.addAction(QIcon(":/images/tray/application.png"), tr("Show") + " " + APPLICATION_NAME, this, [this](){
|
|
emit raiseRequested();
|
|
});
|
|
m_menu.addSeparator();
|
|
m_trayActionConnect = m_menu.addAction(tr("Connect"), this, [this](){ emit connectRequested(); });
|
|
m_trayActionDisconnect = m_menu.addAction(tr("Disconnect"), this, [this](){ emit disconnectRequested(); });
|
|
|
|
m_menu.addSeparator();
|
|
|
|
m_trayActionVisitWebSite = m_menu.addAction(QIcon(":/images/tray/link.png"), tr("Visit Website"), [&](){
|
|
QDesktopServices::openUrl(QUrl(websiteUrl));
|
|
});
|
|
|
|
// Quit action: disconnect VPN first on macOS NE, else quit directly
|
|
m_trayActionQuit = m_menu.addAction(QIcon(":/images/tray/cancel.png"),
|
|
tr("Quit") + " " + APPLICATION_NAME,
|
|
this,
|
|
[&](){ qApp->quit(); });
|
|
|
|
#ifdef Q_OS_MAC
|
|
// QSystemTrayIcon::setContextMenu crashes on macOS 14+: its menu-tracking
|
|
// observer reads -[NSEvent clickCount] off a non-mouse event. Own the
|
|
// NSStatusItem and attach the native NSMenu instead.
|
|
m_statusIcon = new MacOSStatusIcon(this);
|
|
m_statusIcon->setMenu(&m_menu);
|
|
#else
|
|
m_systemTrayIcon.show();
|
|
connect(&m_systemTrayIcon, &QSystemTrayIcon::activated, this,
|
|
&SystemTrayNotificationHandler::onTrayActivated);
|
|
m_systemTrayIcon.setContextMenu(&m_menu);
|
|
#endif
|
|
setTrayState(Vpn::ConnectionState::Disconnected);
|
|
}
|
|
|
|
SystemTrayNotificationHandler::~SystemTrayNotificationHandler() {
|
|
#ifdef Q_OS_MAC
|
|
delete m_statusIcon; // before m_menu: the status item references its NSMenu
|
|
m_statusIcon = nullptr;
|
|
#endif
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::setConnectionState(Vpn::ConnectionState state)
|
|
{
|
|
setTrayState(state);
|
|
NotificationHandler::setConnectionState(state);
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::onTranslationsUpdated()
|
|
{
|
|
m_trayActionShow->setText(tr("Show") + " " + APPLICATION_NAME);
|
|
m_trayActionConnect->setText(tr("Connect"));
|
|
m_trayActionDisconnect->setText(tr("Disconnect"));
|
|
m_trayActionVisitWebSite->setText(tr("Visit Website"));
|
|
m_trayActionQuit->setText(tr("Quit")+ " " + APPLICATION_NAME);
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::updateWebsiteUrl(const QString &newWebsiteUrl) {
|
|
qDebug() << "Updated website URL:" << newWebsiteUrl;
|
|
websiteUrl = newWebsiteUrl;
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::setTrayIcon(const QString &iconPath)
|
|
{
|
|
#ifdef Q_OS_MAC
|
|
m_statusIcon->setIcon(iconPath);
|
|
#else
|
|
QIcon trayIconMask(QPixmap(iconPath).scaled(128,128));
|
|
trayIconMask.setIsMask(true);
|
|
m_systemTrayIcon.setIcon(trayIconMask);
|
|
#endif
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
|
|
{
|
|
if(reason == QSystemTrayIcon::DoubleClick || reason == QSystemTrayIcon::Trigger) {
|
|
emit raiseRequested();
|
|
}
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::setTrayState(Vpn::ConnectionState state)
|
|
{
|
|
QString resourcesPath = ":/images/tray/%1";
|
|
|
|
switch (state) {
|
|
case Vpn::ConnectionState::Disconnected:
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(true);
|
|
m_trayActionDisconnect->setEnabled(false);
|
|
break;
|
|
case Vpn::ConnectionState::Preparing:
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
break;
|
|
case Vpn::ConnectionState::Connecting:
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
break;
|
|
case Vpn::ConnectionState::Connected:
|
|
setTrayIcon(QString(resourcesPath).arg(ConnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
break;
|
|
case Vpn::ConnectionState::Disconnecting:
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
break;
|
|
case Vpn::ConnectionState::Reconnecting:
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
break;
|
|
case Vpn::ConnectionState::Error:
|
|
setTrayIcon(QString(resourcesPath).arg(ErrorTrayIconName));
|
|
m_trayActionConnect->setEnabled(true);
|
|
m_trayActionDisconnect->setEnabled(false);
|
|
break;
|
|
case Vpn::ConnectionState::Unknown:
|
|
default:
|
|
m_trayActionConnect->setEnabled(false);
|
|
m_trayActionDisconnect->setEnabled(true);
|
|
setTrayIcon(QString(resourcesPath).arg(DisconnectedTrayIconName));
|
|
}
|
|
|
|
//#ifdef Q_OS_MAC
|
|
// // Get theme from current user (note, this app can be launched as root application and in this case this theme can be different from theme of real current user )
|
|
// bool darkTaskBar = MacOSFunctions::instance().isMenuBarUseDarkTheme();
|
|
// darkTaskBar = forceUseBrightIcons ? true : darkTaskBar;
|
|
// resourcesPath = ":/images_mac/tray_icon/%1";
|
|
// useIconName = useIconName.replace(".png", darkTaskBar ? "@2x.png" : " dark@2x.png");
|
|
//#endif
|
|
}
|
|
|
|
|
|
void SystemTrayNotificationHandler::notify(NotificationHandler::Message type,
|
|
const QString& title,
|
|
const QString& message,
|
|
int timerMsec) {
|
|
Q_UNUSED(type);
|
|
|
|
#ifdef Q_OS_MAC
|
|
Q_UNUSED(timerMsec);
|
|
m_statusIcon->showMessage(title, message);
|
|
#else
|
|
QIcon icon(ConnectedTrayIconName);
|
|
m_systemTrayIcon.showMessage(title, message, icon, timerMsec);
|
|
#endif
|
|
}
|
|
|
|
void SystemTrayNotificationHandler::showHideWindow() {
|
|
// QmlEngineHolder* engine = QmlEngineHolder::instance();
|
|
// if (engine->window()->isVisible()) {
|
|
// engine->hideWindow();
|
|
//#ifdef MVPN_MACOS
|
|
// MacOSUtils::hideDockIcon();
|
|
//#endif
|
|
// } else {
|
|
// engine->showWindow();
|
|
//#ifdef MVPN_MACOS
|
|
// MacOSUtils::showDockIcon();
|
|
//#endif
|
|
// }
|
|
}
|
|
|