From 4ec27d69bee5023225a77399283817b58442e9d3 Mon Sep 17 00:00:00 2001 From: askmeaboutloom Date: Fri, 11 Aug 2023 09:36:22 +0200 Subject: [PATCH] Allow muting notifications for a window In the chat options. Turns off sound effects, net status popups and attention demands of the window. --- ChangeLog | 1 + src/desktop/chat/chatbox.cpp | 1 + src/desktop/chat/chatbox.h | 2 ++ src/desktop/chat/chatwidget.cpp | 20 ++++++++++++++++---- src/desktop/chat/chatwidget.h | 4 ++++ src/desktop/mainwindow.cpp | 20 +++++++++++++++----- src/desktop/mainwindow.h | 2 ++ src/desktop/widgets/netstatus.cpp | 7 ++++--- src/desktop/widgets/netstatus.h | 4 ++++ 9 files changed, 49 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0d4f651442..c74ed9cf4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,7 @@ Unreleased Version 2.2.0-pre * Feature: Mark window when a chat message is received. On Windows, this makes the icon in the task bar orange. On macOS, it supposedly bounces an icon somewhere. On other platforms it probably does something similar, indicating which window is the one that got a message ready. Thanks Radio for suggesting. * Fix: Don't add current color to the palette when creating it. Thanks xxxx for reporting. * Fix: Make exported palettes not start using the exported location to save changes to. Thanks xxxx for reporting. + * Feature: Allow muting notifications for a window. Thanks Blozzom for suggesting. 2023-07-31 Version 2.2.0-beta.6 * Fix: Don't forget account password when entering a wrong session password. diff --git a/src/desktop/chat/chatbox.cpp b/src/desktop/chat/chatbox.cpp index ff83c9ed0a..3451b9ee10 100644 --- a/src/desktop/chat/chatbox.cpp +++ b/src/desktop/chat/chatbox.cpp @@ -87,6 +87,7 @@ ChatBox::ChatBox(Document *doc, QWidget *parent) emit expandPlease(); } }); + connect(m_chatWidget, &ChatWidget::muteChanged, this, &ChatBox::muteChanged); connect(doc, &Document::canvasChanged, this, &ChatBox::onCanvasChanged); connect(doc, &Document::serverLoggedIn, this, &ChatBox::onServerLogin); diff --git a/src/desktop/chat/chatbox.h b/src/desktop/chat/chatbox.h index f3f348fc89..941de0ef51 100644 --- a/src/desktop/chat/chatbox.h +++ b/src/desktop/chat/chatbox.h @@ -63,6 +63,8 @@ private slots: //! Detached chat box should be re-attached and reparented (or it will be destroyed) void reattachNowPlease(); + void muteChanged(bool muted); + protected: void resizeEvent(QResizeEvent *event) override; diff --git a/src/desktop/chat/chatwidget.cpp b/src/desktop/chat/chatwidget.cpp index e93d9ecc02..07e32e93fa 100644 --- a/src/desktop/chat/chatwidget.cpp +++ b/src/desktop/chat/chatwidget.cpp @@ -4,7 +4,6 @@ #include "desktop/chat/chatwidget.h" #include "libclient/utils/html.h" #include "libclient/utils/funstuff.h" -#include "desktop/notifications.h" #include "desktop/main.h" #include "libclient/canvas/userlist.h" @@ -86,6 +85,7 @@ struct ChatWidget::Private { QAction *compactAction = nullptr; QAction *attachAction = nullptr; QAction *detachAction = nullptr; + QAction *muteAction = nullptr; QList announcedUsers; canvas::UserListModel *userlist = nullptr; @@ -200,6 +200,11 @@ ChatWidget::ChatWidget(QWidget *parent) tr("Detach"), this, &ChatWidget::detachRequested); } + d->muteAction = d->externalMenu->addAction( + tr("Mute notifications"), this, &ChatWidget::muteChanged); + d->muteAction->setStatusTip(tr("Toggle notifications for this window")); + d->muteAction->setCheckable(true); + connect(d->externalMenu, &QMenu::aboutToShow, this, &ChatWidget::contextMenuAboutToShow); setPreserveMode(false); @@ -551,7 +556,7 @@ void ChatWidget::userJoined(int id, const QString &name) d->scrollChatToEnd(id); } - notification::playSound(notification::Event::LOGIN); + playSound(notification::Event::LOGIN); } void ChatWidget::userParted(int id) @@ -571,7 +576,7 @@ void ChatWidget::userParted(int id) d->announcedUsers.removeAll(id); - notification::playSound(notification::Event::LOGOUT); + playSound(notification::Event::LOGOUT); } void ChatWidget::kicked(const QString &kickedBy) @@ -630,7 +635,7 @@ void ChatWidget::receiveMessage(int sender, int recipient, uint8_t tflags, uint8 } if(!d->myline->hasFocus() || chatId != d->currentChat || isValidAlert) - notification::playSound(notification::Event::CHAT); + playSound(notification::Event::CHAT); if(wasAtEnd || isValidAlert) { d->scrollChatToEnd(chatId); @@ -875,4 +880,11 @@ void ChatWidget::resizeEvent(QResizeEvent *) } } +void ChatWidget::playSound(notification::Event event) +{ + if(!d->muteAction->isChecked()) { + notification::playSound(event); + } +} + } diff --git a/src/desktop/chat/chatwidget.h b/src/desktop/chat/chatwidget.h index a30d8e08e3..a48956cfdb 100644 --- a/src/desktop/chat/chatwidget.h +++ b/src/desktop/chat/chatwidget.h @@ -3,6 +3,7 @@ #ifndef CHATWIDGET_H #define CHATWIDGET_H +#include "desktop/notifications.h" #include class QMenu; @@ -82,6 +83,7 @@ private slots: void message(const drawdance::Message &msg); void detachRequested(); void expandRequested(); + void muteChanged(bool muted); private: #ifdef Q_OS_ANDROID @@ -95,6 +97,8 @@ private slots: static constexpr bool ALLOW_DETACH = true; #endif + void playSound(notification::Event event); + struct Private; Private *d; }; diff --git a/src/desktop/mainwindow.cpp b/src/desktop/mainwindow.cpp index 692b623755..58bccb063d 100644 --- a/src/desktop/mainwindow.cpp +++ b/src/desktop/mainwindow.cpp @@ -171,6 +171,7 @@ MainWindow::MainWindow(bool restoreWindowPosition) m_tempToolSwitchShortcut(nullptr), m_titleBarsHidden(false), m_wasSessionLocked(false), + m_notificationsMuted(false), m_doc(nullptr), m_exitAfterSave(false) { @@ -516,7 +517,7 @@ void MainWindow::onCanvasChanged(canvas::CanvasModel *canvas) connect(canvas->paintEngine(), &canvas::PaintEngine::undoDepthLimitSet, this, &MainWindow::onUndoDepthLimitSet); connect(canvas, &canvas::CanvasModel::chatMessageReceived, this, [this]() { - if(dpApp().settings().notificationChat()) { + if(dpApp().settings().notificationChat() && !m_notificationsMuted) { // Demand attention if the window isn't focused. QApplication::alert(this); } @@ -2037,10 +2038,12 @@ void MainWindow::updateLockWidget() lock.setFlag(Lock::Canvas); } - if(sessionLocked && !m_wasSessionLocked) { - notification::playSound(notification::Event::LOCKED); - } else if(!sessionLocked && m_wasSessionLocked) { - notification::playSound(notification::Event::UNLOCKED); + if(!m_notificationsMuted) { + if(sessionLocked && !m_wasSessionLocked) { + notification::playSound(notification::Event::LOCKED); + } else if(!sessionLocked && m_wasSessionLocked) { + notification::playSound(notification::Event::UNLOCKED); + } } m_wasSessionLocked = sessionLocked; @@ -2287,6 +2290,12 @@ void MainWindow::setDockTitleBarsHidden(bool hidden) } } +void MainWindow::setNotificationsMuted(bool muted) +{ + m_notificationsMuted = muted; + m_netstatus->setNotificationsMuted(muted); +} + /** * User selected a tool * @param tool action representing the tool @@ -3242,6 +3251,7 @@ void MainWindow::setupActions() m_splitter->setSizes(sizes); m_saveSplitterDebounce.start(); }); + connect(m_chatbox, &widgets::ChatBox::muteChanged, this, &MainWindow::setNotificationsMuted); connect(moveleft, &QAction::triggered, m_view, [this] { m_view->moveStep(widgets::CanvasView::Direction::Left); diff --git a/src/desktop/mainwindow.h b/src/desktop/mainwindow.h index 945d99ac6d..ddf142e0fd 100644 --- a/src/desktop/mainwindow.h +++ b/src/desktop/mainwindow.h @@ -205,6 +205,7 @@ private slots: void setFreezeDocks(bool freeze); void setDocksHidden(bool hidden); void setDockTitleBarsHidden(bool hidden); + void setNotificationsMuted(bool muted); void updateTitle(); @@ -316,6 +317,7 @@ private slots: ShortcutDetector *m_tempToolSwitchShortcut; bool m_titleBarsHidden; bool m_wasSessionLocked; + bool m_notificationsMuted; Document *m_doc; MainActions *m_ma; diff --git a/src/desktop/widgets/netstatus.cpp b/src/desktop/widgets/netstatus.cpp index 301356a529..c5c9dcdf80 100644 --- a/src/desktop/widgets/netstatus.cpp +++ b/src/desktop/widgets/netstatus.cpp @@ -357,9 +357,10 @@ void NetStatus::kicked(const QString& user) void NetStatus::message(const QString &msg) { - m_popup->showMessage( - mapToGlobal(m_label->pos() + QPoint(m_label->width()/2, 2)), - msg); + if(!m_notificationsMuted) { + m_popup->showMessage( + mapToGlobal(m_label->pos() + QPoint(m_label->width() / 2, 2)), msg); + } } void NetStatus::updateLabel() diff --git a/src/desktop/widgets/netstatus.h b/src/desktop/widgets/netstatus.h index c626510fdd..9afe740856 100644 --- a/src/desktop/widgets/netstatus.h +++ b/src/desktop/widgets/netstatus.h @@ -40,6 +40,8 @@ Q_OBJECT const QUrl &sessionUrl() const { return m_sessionUrl; } QString joinPassword() const { return m_haveJoinPassword ? m_joinPassword : QString{}; } + void setNotificationsMuted(bool muted) { m_notificationsMuted = muted; } + signals: void remoteAddressDiscovered(); @@ -113,6 +115,8 @@ private slots: quint64 _sentbytes, _recvbytes, _lag; QScopedPointer m_certificate; + + bool m_notificationsMuted; }; }