Skip to content

Commit

Permalink
FEAT(client): Fully rewrite tray icon implementation
Browse files Browse the repository at this point in the history
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) d9a2d47 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. (mumble-voip#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 mumble-voip#1486
Fixes mumble-voip#3028
Fixes mumble-voip#3722
Fixes mumble-voip#3977
Fixes mumble-voip#3999
Fixes mumble-voip#5012
  • Loading branch information
Hartmnt committed Sep 16, 2024
1 parent 00f35bd commit ff6e849
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/mumble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/mumble/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/mumble/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class OverlayClient;
class LogEmitter;
class DeveloperConsole;
class TalkingUI;
class TrayIcon;

class QNetworkAccessManager;

Expand All @@ -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;
Expand Down
47 changes: 46 additions & 1 deletion src/mumble/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,52 @@ 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;
case Information:
case ServerConnected:
case ServerDisconnected:
case UserJoin:
case UserLeave:
case Recording:
case YouKicked:
case UserKicked:
case SelfMute:
case OtherSelfMute:
case YouMuted:
case YouMutedOther:
case OtherMutedOther:
case ChannelJoin:
case ChannelLeave:
case PermissionDenied:
case SelfUnmute:
case SelfDeaf:
case SelfUndeaf:
case UserRenamed:
case SelfChannelJoin:
case SelfChannelJoinOther:
case ChannelJoinConnect:
case ChannelLeaveDisconnect:
case ChannelListeningAdd:
case ChannelListeningRemove:
case PluginMessage:
msgIcon = QSystemTrayIcon::Information;
break;
}
Global::get().trayIcon->showMessage(msgName(mt), plain, msgIcon);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/mumble/LookConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "SearchDialog.h"
#include "Global.h"

#include <QSystemTrayIcon>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QStack>
#include <QtCore/QTimer>
Expand All @@ -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()) {
Expand Down
55 changes: 40 additions & 15 deletions src/mumble/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include <QtWidgets/QWhatsThis>

#include "widgets/SemanticSlider.h"
#include "widgets/TrayIcon.h"

#ifdef Q_OS_WIN
# include <dbt.h>
Expand Down Expand Up @@ -194,6 +195,10 @@ MainWindow::MainWindow(QWidget *p)
QObject::connect(this, &MainWindow::serverSynchronized, Global::get().pluginManager,
&PluginManager::on_serverSynchronized);

// Set up initial client side talking state without the need for the user to do anything.
// This will, for example, make sure the correct status tray icon is used on connect.
QObject::connect(this, &MainWindow::serverSynchronized, this, &MainWindow::userStateChanged);

QAccessible::installFactory(AccessibleSlider::semanticSliderFactory);
}

Expand Down Expand Up @@ -609,7 +614,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);
Expand All @@ -622,7 +626,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
Expand All @@ -643,9 +647,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;
}
Expand Down Expand Up @@ -701,6 +704,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);
bool isMinimizedState = (windowState() & Qt::WindowMinimized);
if (!wasMinimizedState && isMinimizedState) {
Global::get().trayIcon->on_hideAction_triggered();
}
return;
}
}

QWidget::changeEvent(e);
}

Expand Down Expand Up @@ -1432,9 +1449,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) {
Expand Down Expand Up @@ -2532,6 +2546,8 @@ void MainWindow::updateMenuPermissions() {
}

void MainWindow::userStateChanged() {
emit talkingStatusChanged();

ClientUser *user = ClientUser::get(Global::get().uiSession);
if (!user) {
Global::get().bAttenuateOthers = false;
Expand Down Expand Up @@ -2602,6 +2618,7 @@ void MainWindow::on_qaAudioMute_triggered() {
}

updateAudioToolTips();
emit talkingStatusChanged();
}

void MainWindow::setAudioMute(bool mute) {
Expand Down Expand Up @@ -2646,6 +2663,7 @@ void MainWindow::on_qaAudioDeaf_triggered() {
}

updateAudioToolTips();
emit talkingStatusChanged();
}

void MainWindow::setAudioDeaf(bool deaf) {
Expand Down Expand Up @@ -2758,6 +2776,7 @@ void MainWindow::pttReleased() {
void MainWindow::on_PushToMute_triggered(bool down, QVariant) {
Global::get().bPushToMute = down;
updateUserModel();
emit talkingStatusChanged();
}

void MainWindow::on_VolumeUp_triggered(bool down, QVariant) {
Expand Down Expand Up @@ -3038,7 +3057,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) {
Expand Down Expand Up @@ -3572,6 +3593,8 @@ void MainWindow::serverDisconnected(QAbstractSocket::SocketError err, QString re
if (Global::get().s.bMinimalView) {
qdwMinimalViewNote->show();
}

emit disconnectedFromServer();
}

void MainWindow::resolverError(QAbstractSocket::SocketError, QString reason) {
Expand All @@ -3590,13 +3613,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() {
Expand Down Expand Up @@ -3972,8 +3995,10 @@ void MainWindow::openConfigDialog() {

if (dlg->exec() == QDialog::Accepted) {
setupView(false);
showRaiseWindow();
updateTransmitModeComboBox(Global::get().s.atTransmit);
updateUserModel();
emit talkingStatusChanged();

if (Global::get().s.requireRestartToApply) {
if (Global::get().s.requireRestartToApply
Expand Down
5 changes: 5 additions & 0 deletions src/mumble/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ public slots:
void userRemovedChannelListener(ClientUser *user, Channel *channel);
void transmissionModeChanged(Settings::AudioTransmit newMode);

/// Signal emitted when the local user changes their talking status either actively or passively
void talkingStatusChanged();
/// Signal emitted when the connection was terminated and all cleanup code has been run
void disconnectedFromServer();

public:
MainWindow(QWidget *parent);
~MainWindow() Q_DECL_OVERRIDE;
Expand Down
4 changes: 4 additions & 0 deletions src/mumble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include "VersionCheck.h"
#include "Global.h"

#include "widgets/TrayIcon.h"

#include <QLocale>
#include <QScreen>
#include <QtCore/QProcess>
Expand Down Expand Up @@ -685,6 +687,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
Expand Down
Loading

0 comments on commit ff6e849

Please sign in to comment.