Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed volume change with hi-res mice and touchpad scrolling #1857

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions plugin-volume/volumepopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <XdgIcon>

#include <QTimer>
#include <QSlider>
#include <QStyleOptionButton>
#include <QPushButton>
Expand All @@ -47,7 +48,8 @@ VolumePopup::VolumePopup(QWidget* parent):
QDialog(parent, Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint | Qt::Popup | Qt::X11BypassWindowManagerHint),
m_pos(0,0),
m_anchor(Qt::TopLeftCorner),
m_device(nullptr)
m_device(nullptr),
mWheelTimer(new QTimer(this))
{
// Under some Wayland compositors, setting window flags in the c-tor of the base class
// may not be enough for a correct positioning of the popup.
Expand Down Expand Up @@ -84,6 +86,15 @@ VolumePopup::VolumePopup(QWidget* parent):
connect(m_mixerButton, &QPushButton::released, this, &VolumePopup::launchMixer);
connect(m_volumeSlider, &QSlider::valueChanged, this, &VolumePopup::handleSliderValueChanged);
connect(m_muteToggleButton, &QPushButton::clicked, this, &VolumePopup::handleMuteToggleClicked);

mWheelTimer->setSingleShot(true);
mWheelTimer->setInterval(350); // "QStyle::SH_ToolTip_WakeUpDelay" is 700 by default
connect(mWheelTimer, &QTimer::timeout, this, [this] {
QTimer::singleShot(0, this, [this] {
if (!QToolTip::isVisible())
QToolTip::showText(QCursor::pos(), m_volumeSlider->toolTip());
});
});
}

bool VolumePopup::event(QEvent *event)
Expand Down Expand Up @@ -127,7 +138,10 @@ void VolumePopup::handleSliderValueChanged(int value)
return;
// qDebug("VolumePopup::handleSliderValueChanged: %d\n", value);
m_device->setVolume(value);
QTimer::singleShot(0, this, [this] { QToolTip::showText(QCursor::pos(), m_volumeSlider->toolTip()); });
QTimer::singleShot(0, this, [this] {
if (!mWheelTimer->isActive()) // a wheel event immediately hides the tooltip
QToolTip::showText(QCursor::pos(), m_volumeSlider->toolTip());
});
}

void VolumePopup::handleMuteToggleClicked()
Expand Down Expand Up @@ -197,8 +211,21 @@ void VolumePopup::openAt(QPoint pos, Qt::Corner anchor)

void VolumePopup::handleWheelEvent(QWheelEvent *event)
{
m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition()
+ (event->angleDelta().y() / QWheelEvent::DefaultDeltasPerStep * m_volumeSlider->singleStep()));
static int _delta = 0; // for high-resolution mice and touchpad scrolling

int delta = event->angleDelta().y();
if ((_delta ^ delta) < 0)
_delta = delta; // the wheel direction is reversed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this case ? Even if the user changes the wheel direction the previous "fraction" of the stroke has to be considered...
As counter example, if you in a normal mouse each tick is emitted every 15°, and you rotate for +14° up and -14° down you don't have any effect. However with your code an hires mouse you have a tick because +14 - -14 = 28 > 15°.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1856 (comment) → 3

I'm afraid, you misread the code.

else
_delta += delta;
if (qAbs(_delta) >= QWheelEvent::DefaultDeltasPerStep) {
m_volumeSlider->setSliderPosition(m_volumeSlider->sliderPosition()
+ (_delta / QWheelEvent::DefaultDeltasPerStep * m_volumeSlider->singleStep()));
_delta = 0;
Copy link
Contributor

@kreijack kreijack Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the same reason as above, I suggest to write this as _delta = _delta % QWheelEvent::DefaultDeltasPerStep

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be very wrong. → #1856 (comment) → 2 and especially 4

}

// show the tooltip only after the wheel rotation is stopped
mWheelTimer->start();
}

void VolumePopup::setDevice(AudioDevice *device)
Expand Down
2 changes: 2 additions & 0 deletions plugin-volume/volumepopup.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <QDialog>

class QTimer;
class QSlider;
class QPushButton;
class AudioDevice;
Expand Down Expand Up @@ -81,6 +82,7 @@ private slots:
QPoint m_pos;
Qt::Corner m_anchor;
AudioDevice *m_device;
QTimer *mWheelTimer; // for showing tooltip with high-resolution mice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are adding a member to the class, we could add also the "_delta", and remove the static variable inside the method handleWheelEvent()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1856 (comment) → first paragraph

#1856 (comment)

};

#endif // VOLUMEPOPUP_H