From 1c83d7dfc655745bed621af91d0afaa6fc2ff36f Mon Sep 17 00:00:00 2001 From: Hartmnt Date: Wed, 11 Oct 2023 16:13:04 +0000 Subject: [PATCH] FIX(client): Fix using the keyboad to change local volume adjustment Previously, when using keyboard arrows, the new slider widget action applied the new local volume but did not save it. That was because it used the sliderReleased event to save the value, which is not triggered by keyboard updates. We could simply save the local volume on every sliderChange instead of on sliderRelease, but that would cause many writes to the disk in a short period of time and possibly lag the client. This commit introduces an event filter in addition to the sliderChanged event, which is observing key releases. If the slider has focus and left/right is released, it is then calling the save slot of the slider. Fixes #6211 --- src/mumble/CMakeLists.txt | 2 ++ src/mumble/ListenerVolumeSlider.cpp | 2 +- src/mumble/ListenerVolumeSlider.h | 5 +++- src/mumble/UserLocalVolumeSlider.cpp | 2 +- src/mumble/UserLocalVolumeSlider.h | 2 +- src/mumble/VolumeSliderWidgetAction.cpp | 9 +++++- src/mumble/VolumeSliderWidgetAction.h | 2 +- src/mumble/widgets/EventFilters.cpp | 40 +++++++++++++++++++++++++ src/mumble/widgets/EventFilters.h | 33 ++++++++++++++++++++ 9 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/mumble/widgets/EventFilters.cpp create mode 100644 src/mumble/widgets/EventFilters.h diff --git a/src/mumble/CMakeLists.txt b/src/mumble/CMakeLists.txt index dbbbada4f46..f28361fb007 100644 --- a/src/mumble/CMakeLists.txt +++ b/src/mumble/CMakeLists.txt @@ -291,6 +291,8 @@ set(MUMBLE_SOURCES "widgets/CompletablePage.cpp" "widgets/CompletablePage.h" + "widgets/EventFilters.cpp" + "widgets/EventFilters.h" "widgets/MUComboBox.cpp" "widgets/MUComboBox.h" "widgets/MultiStyleWidgetWrapper.cpp" diff --git a/src/mumble/ListenerVolumeSlider.cpp b/src/mumble/ListenerVolumeSlider.cpp index 3f6239bcbd8..c72dd9a25e8 100644 --- a/src/mumble/ListenerVolumeSlider.cpp +++ b/src/mumble/ListenerVolumeSlider.cpp @@ -28,7 +28,7 @@ void ListenerVolumeSlider::on_VolumeSlider_valueChanged(int value) { displayTooltip(value); } -void ListenerVolumeSlider::on_VolumeSlider_sliderReleased() { +void ListenerVolumeSlider::on_VolumeSlider_changeCompleted() { ServerHandlerPtr handler = Global::get().sh; if (!handler || !m_channel || !m_volumeSlider) { diff --git a/src/mumble/ListenerVolumeSlider.h b/src/mumble/ListenerVolumeSlider.h index 62e1532288c..186d7c0c28b 100644 --- a/src/mumble/ListenerVolumeSlider.h +++ b/src/mumble/ListenerVolumeSlider.h @@ -23,9 +23,12 @@ class ListenerVolumeSlider : public VolumeSliderWidgetAction { /// The channel of the listener proxy this dialog is operating on const Channel *m_channel; + void saveChangesLocal(); + void saveChangesServer(); + private slots: void on_VolumeSlider_valueChanged(int value) override; - void on_VolumeSlider_sliderReleased() override; + void on_VolumeSlider_changeCompleted() override; }; #endif diff --git a/src/mumble/UserLocalVolumeSlider.cpp b/src/mumble/UserLocalVolumeSlider.cpp index 59ccfb3f5e7..ea3f23fa4a3 100644 --- a/src/mumble/UserLocalVolumeSlider.cpp +++ b/src/mumble/UserLocalVolumeSlider.cpp @@ -35,7 +35,7 @@ void UserLocalVolumeSlider::on_VolumeSlider_valueChanged(int value) { } } -void UserLocalVolumeSlider::on_VolumeSlider_sliderReleased() { +void UserLocalVolumeSlider::on_VolumeSlider_changeCompleted() { ClientUser *user = ClientUser::get(m_clientSession); if (user) { if (!user->qsHash.isEmpty()) { diff --git a/src/mumble/UserLocalVolumeSlider.h b/src/mumble/UserLocalVolumeSlider.h index 59b632419e0..6f0ff39e666 100644 --- a/src/mumble/UserLocalVolumeSlider.h +++ b/src/mumble/UserLocalVolumeSlider.h @@ -24,7 +24,7 @@ class UserLocalVolumeSlider : public VolumeSliderWidgetAction { private slots: void on_VolumeSlider_valueChanged(int value); - void on_VolumeSlider_sliderReleased(); + void on_VolumeSlider_changeCompleted(); }; #endif diff --git a/src/mumble/VolumeSliderWidgetAction.cpp b/src/mumble/VolumeSliderWidgetAction.cpp index 0d49e258bb4..b6579f2b420 100644 --- a/src/mumble/VolumeSliderWidgetAction.cpp +++ b/src/mumble/VolumeSliderWidgetAction.cpp @@ -5,6 +5,7 @@ #include "VolumeSliderWidgetAction.h" #include "VolumeAdjustment.h" +#include "widgets/EventFilters.h" #include #include @@ -16,10 +17,16 @@ VolumeSliderWidgetAction::VolumeSliderWidgetAction(QObject *parent) m_volumeSlider->setAccessibleName(tr("Slider for volume adjustment")); m_volumeSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); + KeyEventObserver *eventFilter = new KeyEventObserver(this, QEvent::KeyRelease, false, + { Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, Qt::Key_Down }); + m_volumeSlider->installEventFilter(eventFilter); + connect(m_volumeSlider.get(), &QSlider::valueChanged, this, &VolumeSliderWidgetAction::on_VolumeSlider_valueChanged); connect(m_volumeSlider.get(), &QSlider::sliderReleased, this, - &VolumeSliderWidgetAction::on_VolumeSlider_sliderReleased); + &VolumeSliderWidgetAction::on_VolumeSlider_changeCompleted); + connect(eventFilter, &KeyEventObserver::keyEventObserved, this, + &VolumeSliderWidgetAction::on_VolumeSlider_changeCompleted); setDefaultWidget(m_volumeSlider.get()); diff --git a/src/mumble/VolumeSliderWidgetAction.h b/src/mumble/VolumeSliderWidgetAction.h index 9d7dfae13cb..4cba54179f4 100644 --- a/src/mumble/VolumeSliderWidgetAction.h +++ b/src/mumble/VolumeSliderWidgetAction.h @@ -27,7 +27,7 @@ class VolumeSliderWidgetAction : public QWidgetAction { protected slots: virtual void on_VolumeSlider_valueChanged(int){}; - virtual void on_VolumeSlider_sliderReleased(){}; + virtual void on_VolumeSlider_changeCompleted(){}; }; #endif diff --git a/src/mumble/widgets/EventFilters.cpp b/src/mumble/widgets/EventFilters.cpp new file mode 100644 index 00000000000..ba6b2177356 --- /dev/null +++ b/src/mumble/widgets/EventFilters.cpp @@ -0,0 +1,40 @@ +// Copyright 2023 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 "EventFilters.h" + +#include + +#include +#include + +KeyEventObserver::KeyEventObserver(QObject *parent, QEvent::Type eventType, bool consume, + std::initializer_list< Qt::Key > keys) + : QObject(parent), m_eventType(eventType), m_consume(consume), m_keys(keys) { +} + +bool KeyEventObserver::eventFilter(QObject *obj, QEvent *event) { + QWidget *widget = static_cast< QWidget * >(obj); + + if (!widget || !widget->hasFocus()) { + return false; + } + + QKeyEvent *keyEvent = static_cast< QKeyEvent * >(event); + + if (!keyEvent || keyEvent->type() != m_eventType) { + return false; + } + + Qt::Key key = static_cast< Qt::Key >(keyEvent->key()); + + if (std::find(m_keys.begin(), m_keys.end(), key) == m_keys.end()) { + return false; + } + + emit keyEventObserved(); + + return m_consume; +} diff --git a/src/mumble/widgets/EventFilters.h b/src/mumble/widgets/EventFilters.h new file mode 100644 index 00000000000..0fdef57dfcf --- /dev/null +++ b/src/mumble/widgets/EventFilters.h @@ -0,0 +1,33 @@ +// Copyright 2023 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_EVENTFILTERS_H_ +#define MUMBLE_MUMBLE_WIDGETS_EVENTFILTERS_H_ + +#include +#include + +#include +#include + +class KeyEventObserver : public QObject { + Q_OBJECT + +private: + QEvent::Type m_eventType; + bool m_consume; + std::vector< Qt::Key > m_keys; + +public: + KeyEventObserver(QObject *parent, QEvent::Type eventType, bool consume, std::initializer_list< Qt::Key > keys); + +protected: + bool eventFilter(QObject *obj, QEvent *event) override; + +signals: + void keyEventObserved(); +}; + +#endif