From a5da85b1d5d4311a06dd547f1d0396d9cc989004 Mon Sep 17 00:00:00 2001 From: Hartmnt Date: Fri, 7 Jun 2024 14:25:46 +0000 Subject: [PATCH] FEAT(client): Fully rewrite tray icon implementation The old tray icon implementation was very old and contained a lot of workaround for things that probably are no longer an issue. Furthermore, the event loop was modified in a way such that it could end up in an infinite loop draining CPU time and rendering Mumble unusable. Based on the new Qt5 implementation, this commit introduces a complete rewrite of the tray icon. The following things should be noted: * We assume the information in the Qt documentation [1] is valid. This means that all versions of Windows, all Linux window managers/compositors that implement the d-bus StatusNotifierItem specification, and all versions of macos support the functionality of QSystemTrayIcon and its notification system. That means we can drop the platform-specific code branches and handle messages directly with QSystemTrayIcon::sendMessage. This should for example also be true for recent versions of Gnome, which do not have an actual system tray, but implement the d-bus StatusNotifierItem specification. Therefore, we can actually merge and simplify the notification code for Windows and Unix*. * With regards to the bullet point above, we only limit the "hide to tray" functionality behind QSystemTrayIcon::isSystemTrayAvailable (because otherwise you would not get the Mumble window back without binding a shortcut first). Other code branches that were previously limited when isSystemTrayAvailable returned false were removed. According to Qt, the QSystemTrayIcon code does not actually care if a system tray is available and will even retroactively add itself if a tray becomes available after the application was started. * On (X)Wayland, the minimize button in the window frame does not trigger a minimize change event. This means that users with such a system may only be able to "hide to tray" by 1) pressing the close button in the window frame and enabling "minimize instead of close" 2) clicking the tray icon or the tray icon hide action or 3) binding a shortcut to hide the window. This is either a bug or a deliberate decision by Qt or Wayland and we have no way to do anything about that. (QTBUG-74310) * The "messageClicked" event is buggy in Qt on some platforms. That means that clicking the system notification spawned by Mumble via QSystemTrayIcon::sendMessage will (on some systems) never trigger anything especially not showing and activating the window. This is a long-standing bug in Qt (QTBUG-87329), but we have absolutely no way to work around this. The event is correctly hooked up in Mumble and if this is ever fixed in Qt, this will start working again automatically. * The tray icon has been redesigned according to state-of-the-art tray icon design guidelines [2]. Which basically just means: 1) d9a2d470ab75 has been reverted to provide the user with a consistent menu 2) The main action of the tray icon (toggle show/hide) is the first entry in the context menu and the default action when the icon is clicked and 3) the TalkingUI toggle action was added. Actions for double and middle mouse clicks were removed as they might have contributed to infinite loops. * There is no way in Windows to show and activate a window that is not part of the current active process. If you have Mumble running in the background and receive a message, we can not raise the Window without you clicking the Mumble taskbar item or tray icon yourself. This is deliberate by Microsoft and can and should not be circumvented. (#5701) * This also fixes the case where the Mumble MainWindow would disappear when pressing "OK" in the settings dialog. This happened because users would have "minimize to tray" and "minimize on close" enabled. [1] https://doc.qt.io/qt-5/qsystemtrayicon.html#details https://doc.qt.io/qt-6/qsystemtrayicon.html#details [2] https://learn.microsoft.com/en-us/windows/win32/uxguide/winenv-notification Fixes #1486 Fixes #3722 Fixes #3977 Fixes #3999 Fixes #5012 --- src/mumble/CMakeLists.txt | 2 + src/mumble/Global.cpp | 1 + src/mumble/Global.h | 2 + src/mumble/Log.cpp | 21 ++++- src/mumble/LookConfig.cpp | 10 ++ src/mumble/MainWindow.cpp | 53 ++++++++--- src/mumble/main.cpp | 4 + src/mumble/widgets/TrayIcon.cpp | 161 ++++++++++++++++++++++++++++++++ src/mumble/widgets/TrayIcon.h | 38 ++++++++ 9 files changed, 276 insertions(+), 16 deletions(-) create mode 100644 src/mumble/widgets/TrayIcon.cpp create mode 100644 src/mumble/widgets/TrayIcon.h diff --git a/src/mumble/CMakeLists.txt b/src/mumble/CMakeLists.txt index ed82e3964b5..0a08b140ef1 100644 --- a/src/mumble/CMakeLists.txt +++ b/src/mumble/CMakeLists.txt @@ -309,6 +309,8 @@ set(MUMBLE_SOURCES "widgets/SearchDialogTree.h" "widgets/SemanticSlider.cpp" "widgets/SemanticSlider.h" + "widgets/TrayIcon.cpp" + "widgets/TrayIcon.h" "${SHARED_SOURCE_DIR}/ACL.cpp" diff --git a/src/mumble/Global.cpp b/src/mumble/Global.cpp index 6e0a54c039f..4cc5ead6457 100644 --- a/src/mumble/Global.cpp +++ b/src/mumble/Global.cpp @@ -93,6 +93,7 @@ void Global::migrateDataDir(const QDir &toDir) { Global::Global(const QString &qsConfigPath) { mw = nullptr; + trayIcon = nullptr; db = nullptr; pluginManager = nullptr; nam = nullptr; diff --git a/src/mumble/Global.h b/src/mumble/Global.h index c129d26e214..3f624e67fa4 100644 --- a/src/mumble/Global.h +++ b/src/mumble/Global.h @@ -34,6 +34,7 @@ class OverlayClient; class LogEmitter; class DeveloperConsole; class TalkingUI; +class TrayIcon; class QNetworkAccessManager; @@ -50,6 +51,7 @@ struct Global Q_DECL_FINAL { static Global &get(); MainWindow *mw; + TrayIcon *trayIcon; Settings s; boost::shared_ptr< ServerHandler > sh; boost::shared_ptr< AudioInput > ai; diff --git a/src/mumble/Log.cpp b/src/mumble/Log.cpp index 621747a6164..ee82bd8411d 100644 --- a/src/mumble/Log.cpp +++ b/src/mumble/Log.cpp @@ -809,7 +809,26 @@ void Log::log(MsgType mt, const QString &console, const QString &terse, bool own // Message notification with balloon tooltips if (flags & Settings::LogBalloon) { // Replace any instances of a "Object Replacement Character" from QTextDocumentFragment::toPlainText - // FIXME + plain = plain.replace("\xEF\xBF\xBC", tr("[embedded content]")); + + QSystemTrayIcon::MessageIcon msgIcon; + switch (mt) { + case DebugInfo: + case CriticalError: + msgIcon = QSystemTrayIcon::Critical; + break; + case Warning: + msgIcon = QSystemTrayIcon::Warning; + break; + case TextMessage: + case PrivateTextMessage: + msgIcon = QSystemTrayIcon::NoIcon; + break; + default: + msgIcon = QSystemTrayIcon::Information; + break; + } + Global::get().trayIcon->showMessage(msgName(mt), plain, msgIcon); } } diff --git a/src/mumble/LookConfig.cpp b/src/mumble/LookConfig.cpp index f54cc96eb7f..13a025ebbde 100644 --- a/src/mumble/LookConfig.cpp +++ b/src/mumble/LookConfig.cpp @@ -12,6 +12,7 @@ #include "SearchDialog.h" #include "Global.h" +#include #include #include #include @@ -27,6 +28,15 @@ static ConfigRegistrar registrar(1100, LookConfigNew); LookConfig::LookConfig(Settings &st) : ConfigWidget(st) { setupUi(this); + if (!QSystemTrayIcon::isSystemTrayAvailable()) { + qgbTray->hide(); + } + +#ifdef Q_OS_MAC + // Qt can not hide the window via the native macOS hide function. This should be re-evaluated with new Qt versions. + qcbHideTray->hide(); +#endif + qcbLanguage->addItem(tr("System default")); QDir d(QLatin1String(":"), QLatin1String("mumble_*.qm"), QDir::Name, QDir::Files); foreach (const QString &key, d.entryList()) { diff --git a/src/mumble/MainWindow.cpp b/src/mumble/MainWindow.cpp index bb54772bba0..c75f1fa5780 100644 --- a/src/mumble/MainWindow.cpp +++ b/src/mumble/MainWindow.cpp @@ -82,6 +82,7 @@ #include #include "widgets/SemanticSlider.h" +#include "widgets/TrayIcon.h" #ifdef Q_OS_WIN # include @@ -193,6 +194,7 @@ MainWindow::MainWindow(QWidget *p) QObject::connect(this, &MainWindow::serverSynchronized, Global::get().pluginManager, &PluginManager::on_serverSynchronized); + QObject::connect(this, &MainWindow::serverSynchronized, this, &MainWindow::userStateChanged); QAccessible::installFactory(AccessibleSlider::semanticSliderFactory); } @@ -609,7 +611,6 @@ void MainWindow::closeEvent(QCloseEvent *e) { const bool minimizeDueToConnected = sh && sh->isRunning() && quitBehavior == QuitBehavior::MINIMIZE_WHEN_CONNECTED; if (!forceQuit && (alwaysAsk || askDueToConnected)) { -#ifndef Q_OS_MAC QMessageBox mb(QMessageBox::Warning, QLatin1String("Mumble"), tr("Are you sure you want to close Mumble? Perhaps you prefer to minimize it instead?"), QMessageBox::NoButton, this); @@ -622,7 +623,7 @@ void MainWindow::closeEvent(QCloseEvent *e) { mb.setCheckBox(qcbRemember); mb.exec(); if (mb.clickedButton() == qpbMinimize) { - showMinimized(); + setWindowState(windowState() | Qt::WindowMinimized); e->ignore(); // If checkbox is checked and not connected, always minimize @@ -643,9 +644,8 @@ void MainWindow::closeEvent(QCloseEvent *e) { if (qcbRemember->isChecked()) { Global::get().s.quitBehavior = QuitBehavior::ALWAYS_QUIT; } -#endif } else if (!forceQuit && (alwaysMinimize || minimizeDueToConnected)) { - showMinimized(); + setWindowState(windowState() | Qt::WindowMinimized); e->ignore(); return; } @@ -701,6 +701,20 @@ void MainWindow::showEvent(QShowEvent *e) { } void MainWindow::changeEvent(QEvent *e) { + // Hide in tray when minimized + if (Global::get().s.bHideInTray && e->type() == QEvent::WindowStateChange) { + // This code block is not triggered on (X)Wayland due to a Qt bug we can do nothing about (QTBUG-74310) + QWindowStateChangeEvent *windowStateEvent = static_cast< QWindowStateChangeEvent * >(e); + if (windowStateEvent) { + bool wasMinimizedState = (windowStateEvent->oldState() & Qt::WindowMinimized) != 0; + bool isMinimizedState = (windowState() & Qt::WindowMinimized) != 0; + if (!wasMinimizedState && isMinimizedState) { + emit Global::get().trayIcon->on_hideAction_triggered(); + } + return; + } + } + QWidget::changeEvent(e); } @@ -1432,9 +1446,6 @@ void MainWindow::setupView(bool toggle_minimize) { qaTransmitModeSeparator->setVisible(false); } - show(); - activateWindow(); - // If activated show the PTT window if (Global::get().s.bShowPTTButtonWindow && Global::get().s.atTransmit == Settings::PushToTalk) { if (qwPTTButtonWidget) { @@ -2532,6 +2543,8 @@ void MainWindow::updateMenuPermissions() { } void MainWindow::userStateChanged() { + Global::get().trayIcon->updateIcon(); + ClientUser *user = ClientUser::get(Global::get().uiSession); if (!user) { Global::get().bAttenuateOthers = false; @@ -2602,6 +2615,7 @@ void MainWindow::on_qaAudioMute_triggered() { } updateAudioToolTips(); + Global::get().trayIcon->updateIcon(); } void MainWindow::setAudioMute(bool mute) { @@ -2646,6 +2660,7 @@ void MainWindow::on_qaAudioDeaf_triggered() { } updateAudioToolTips(); + Global::get().trayIcon->updateIcon(); } void MainWindow::setAudioDeaf(bool deaf) { @@ -2758,6 +2773,7 @@ void MainWindow::pttReleased() { void MainWindow::on_PushToMute_triggered(bool down, QVariant) { Global::get().bPushToMute = down; updateUserModel(); + Global::get().trayIcon->updateIcon(); } void MainWindow::on_VolumeUp_triggered(bool down, QVariant) { @@ -3038,7 +3054,9 @@ void MainWindow::on_gsCycleTransmitMode_triggered(bool down, QVariant) { } void MainWindow::on_gsToggleMainWindowVisibility_triggered(bool down, QVariant) { - // FIXME + if (down) { + Global::get().trayIcon->toggleShowHide(); + } } void MainWindow::on_gsListenChannel_triggered(bool down, QVariant scdata) { @@ -3365,6 +3383,7 @@ void MainWindow::serverDisconnected(QAbstractSocket::SocketError err, QString re qaServerBanList->setEnabled(false); qtvUsers->setCurrentIndex(QModelIndex()); qteChat->setEnabled(false); + Global::get().trayIcon->updateIcon(); #ifdef Q_OS_MAC // Remove App Nap suppression now that we're disconnected. @@ -3572,6 +3591,8 @@ void MainWindow::serverDisconnected(QAbstractSocket::SocketError err, QString re if (Global::get().s.bMinimalView) { qdwMinimalViewNote->show(); } + + Global::get().trayIcon->updateIcon(); } void MainWindow::resolverError(QAbstractSocket::SocketError, QString reason) { @@ -3590,13 +3611,13 @@ void MainWindow::resolverError(QAbstractSocket::SocketError, QString reason) { } void MainWindow::showRaiseWindow() { - if (isMinimized()) { - setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive); - } - - show(); - raise(); - activateWindow(); + setWindowState(windowState() & ~Qt::WindowMinimized); + QTimer::singleShot(0, [this]() { + show(); + raise(); + activateWindow(); + setWindowState(windowState() | Qt::WindowActive); + }); } void MainWindow::on_qaTalkingUIToggle_triggered() { @@ -3972,8 +3993,10 @@ void MainWindow::openConfigDialog() { if (dlg->exec() == QDialog::Accepted) { setupView(false); + showRaiseWindow(); updateTransmitModeComboBox(Global::get().s.atTransmit); updateUserModel(); + Global::get().trayIcon->updateIcon(); if (Global::get().s.requireRestartToApply) { if (Global::get().s.requireRestartToApply diff --git a/src/mumble/main.cpp b/src/mumble/main.cpp index be28b37cf8e..73d90ccfee8 100644 --- a/src/mumble/main.cpp +++ b/src/mumble/main.cpp @@ -51,6 +51,8 @@ #include "VersionCheck.h" #include "Global.h" +#include "widgets/TrayIcon.h" + #include #include #include @@ -684,6 +686,8 @@ int main(int argc, char **argv) { Global::get().mw = new MainWindow(nullptr); Global::get().mw->show(); + Global::get().trayIcon = new TrayIcon(); + Global::get().talkingUI = new TalkingUI(); // Set TalkingUI's position diff --git a/src/mumble/widgets/TrayIcon.cpp b/src/mumble/widgets/TrayIcon.cpp new file mode 100644 index 00000000000..701d1e67745 --- /dev/null +++ b/src/mumble/widgets/TrayIcon.cpp @@ -0,0 +1,161 @@ +// Copyright 2024 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#include "TrayIcon.h" + +#include "../ClientUser.h" +#include "../MainWindow.h" +#include "../Global.h" + +#include + +TrayIcon::TrayIcon() : QSystemTrayIcon(Global::get().mw), m_statusIcon(nullptr) { + setIcon(Global::get().mw->qiIcon); + setToolTip("Mumble"); + + QObject::connect(this, &QSystemTrayIcon::activated, this, &TrayIcon::on_icon_clicked); + + // messageClicked is buggy in Qt on some platforms and we can not do anything about this (QTBUG-87329) + QObject::connect(this, &QSystemTrayIcon::messageClicked, this, &TrayIcon::on_showAction_triggered); + + m_showAction = new QAction(tr("Show"), Global::get().mw); + QObject::connect(m_showAction, &QAction::triggered, this, &TrayIcon::on_showAction_triggered); + + m_hideAction = new QAction(tr("Hide"), Global::get().mw); + QObject::connect(m_hideAction, &QAction::triggered, this, &TrayIcon::on_hideAction_triggered); + + QObject::connect(Global::get().mw->qaTalkingUIToggle, &QAction::triggered, this, &TrayIcon::updateContextMenu); + + m_contextMenu = new QMenu(Global::get().mw); + QObject::connect(m_contextMenu, &QMenu::aboutToShow, this, &TrayIcon::updateContextMenu); + + // Some window managers hate it when a tray icon sets an empty context menu... + updateContextMenu(); + + setContextMenu(m_contextMenu); + + show(); +} + +void TrayIcon::updateIcon() { + QIcon *newIcon = nullptr; + + ClientUser *p = ClientUser::get(Global::get().uiSession); + + if (Global::get().s.bDeaf) { + newIcon = &Global::get().mw->qiIconDeafSelf; + } else if (p && p->bDeaf) { + newIcon = &Global::get().mw->qiIconDeafServer; + } else if (Global::get().s.bMute) { + newIcon = &Global::get().mw->qiIconMuteSelf; + } else if (p && p->bMute) { + newIcon = &Global::get().mw->qiIconMuteServer; + } else if (p && p->bSuppress) { + newIcon = &Global::get().mw->qiIconMuteSuppressed; + } else if (Global::get().s.bStateInTray && Global::get().bPushToMute) { + newIcon = &Global::get().mw->qiIconMutePushToMute; + } else if (p && Global::get().s.bStateInTray) { + switch (p->tsState) { + case Settings::Talking: + case Settings::MutedTalking: + newIcon = &Global::get().mw->qiTalkingOn; + break; + case Settings::Whispering: + newIcon = &Global::get().mw->qiTalkingWhisper; + break; + case Settings::Shouting: + newIcon = &Global::get().mw->qiTalkingShout; + break; + case Settings::Passive: + default: + newIcon = &Global::get().mw->qiTalkingOff; + break; + } + } else { + newIcon = &Global::get().mw->qiIcon; + } + + if (newIcon != m_statusIcon) { + m_statusIcon = newIcon; + setIcon(*m_statusIcon); + } +} + +void TrayIcon::on_icon_clicked(QSystemTrayIcon::ActivationReason reason) { + switch (reason) { + case QSystemTrayIcon::Trigger: +#ifndef Q_OS_MAC + // macOS is special as it both shows the context menu AND triggers the action. + // We only want at most one of those and since we can not prevent showing + // the menu, we skip the action. + toggleShowHide(); +#endif + break; + case QSystemTrayIcon::Unknown: + case QSystemTrayIcon::Context: + case QSystemTrayIcon::DoubleClick: + case QSystemTrayIcon::MiddleClick: + default: + break; + } +} + +void TrayIcon::updateContextMenu() { + m_contextMenu->clear(); + + if (Global::get().mw->isVisible() && !Global::get().mw->isMinimized()) { + m_hideAction->setEnabled(QSystemTrayIcon::isSystemTrayAvailable()); + m_contextMenu->addAction(m_hideAction); + } else { + m_contextMenu->addAction(m_showAction); + } + + m_contextMenu->addSeparator(); + + m_contextMenu->addAction(Global::get().mw->qaAudioMute); + m_contextMenu->addAction(Global::get().mw->qaAudioDeaf); + m_contextMenu->addAction(Global::get().mw->qaTalkingUIToggle); + m_contextMenu->addSeparator(); + m_contextMenu->addAction(Global::get().mw->qaQuit); +} + +void TrayIcon::toggleShowHide() { + if (Global::get().mw->isVisible() && !Global::get().mw->isMinimized()) { + on_hideAction_triggered(); + } else { + on_showAction_triggered(); + } +} + +void TrayIcon::on_showAction_triggered() { + Global::get().mw->showRaiseWindow(); + updateContextMenu(); +} + +void TrayIcon::on_hideAction_triggered() { + if (!QSystemTrayIcon::isSystemTrayAvailable()) { + // The system reports that no system tray is available. + // If we would hide Mumble now, there would be no way to + // get it back... + return; + } + + if (qApp->activeModalWidget() || qApp->activePopupWidget()) { + // There is one or multiple modal or popup window(s) active, which + // would not be hidden by this call. So we also do not hide + // the MainWindow... + return; + } + +#ifndef Q_OS_MAC + Global::get().mw->hide(); +#else + // Qt can not hide the window via the native macOS hide function. This should be re-evaluated with new Qt versions. + // Instead we just minimize. + Global::get().mw->setWindowState(Global::get().mw->windowState() | Qt::WindowMinimized); +#endif + + updateContextMenu(); +} diff --git a/src/mumble/widgets/TrayIcon.h b/src/mumble/widgets/TrayIcon.h new file mode 100644 index 00000000000..584a59700f9 --- /dev/null +++ b/src/mumble/widgets/TrayIcon.h @@ -0,0 +1,38 @@ +// Copyright 2024 The Mumble Developers. All rights reserved. +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file at the root of the +// Mumble source tree or at . + +#ifndef MUMBLE_MUMBLE_WIDGETS_TRAYICCON_H_ +#define MUMBLE_MUMBLE_WIDGETS_TRAYICCON_H_ + +#include +#include +#include + +class TrayIcon : public QSystemTrayIcon { + Q_OBJECT + +public: + TrayIcon(); + + void updateIcon(); + void toggleShowHide(); + +public slots: + void on_showAction_triggered(); + void on_hideAction_triggered(); + +private: + QIcon *m_statusIcon; + QMenu *m_contextMenu; + QAction *m_showAction; + QAction *m_hideAction; + + void updateContextMenu(); + +private slots: + void on_icon_clicked(QSystemTrayIcon::ActivationReason reason); +}; + +#endif // MUMBLE_MUMBLE_WIDGETS_TRAYICCON_H_