Use accumulator for volume scrolling events

This results in much smoother experience on my touchpad.
This commit is contained in:
Eri the Switch 2024-07-21 13:02:08 +03:00 committed by Jonas Kvinge
parent 37743606a2
commit 3ecf224d91
3 changed files with 26 additions and 10 deletions

View File

@ -1652,7 +1652,7 @@ void MainWindow::PlaylistDoubleClick(const QModelIndex &idx) {
}
void MainWindow::VolumeWheelEvent(const int delta) {
ui_->volume->setValue(ui_->volume->value() + delta / 30);
ui_->volume->HandleWheel(delta);
}
void MainWindow::ToggleShowHide() {

View File

@ -54,6 +54,7 @@
VolumeSlider::VolumeSlider(QWidget *parent, const uint max)
: SliderSlider(Qt::Horizontal, parent, static_cast<int>(max)),
wheel_accumulator_(0),
anim_enter_(false),
anim_count_(0),
timer_anim_(new QTimer(this)),
@ -80,6 +81,23 @@ void VolumeSlider::SetEnabled(const bool enabled) {
QSlider::setVisible(enabled);
}
void VolumeSlider::HandleWheel(const int delta) {
const int scroll_state = wheel_accumulator_ + delta;
const int steps = scroll_state / WHEEL_ROTATION_PER_STEP;
wheel_accumulator_ = scroll_state % WHEEL_ROTATION_PER_STEP;
if (steps != 0) {
wheeling_ = true;
QSlider::setValue(SliderSlider::value() + steps);
emit SliderReleased(value());
wheeling_ = false;
}
}
void VolumeSlider::paintEvent(QPaintEvent*) {
QPainter p(this);
@ -272,13 +290,5 @@ void VolumeSlider::mousePressEvent(QMouseEvent *e) {
}
void VolumeSlider::wheelEvent(QWheelEvent *e) {
wheeling_ = true;
const int step = e->angleDelta().y() / (e->angleDelta().x() == 0 ? 30 : -30);
QSlider::setValue(SliderSlider::value() + step);
emit SliderReleased(value());
wheeling_ = false;
HandleWheel(e->angleDelta().y());
}

View File

@ -48,6 +48,7 @@ class VolumeSlider : public SliderSlider {
public:
explicit VolumeSlider(QWidget *parent, uint max = 0);
void SetEnabled(const bool enabled);
void HandleWheel(const int delta);
protected:
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@ -70,6 +71,9 @@ class VolumeSlider : public SliderSlider {
static const int ANIM_INTERVAL = 18;
static const int ANIM_MAX = 18;
// Units are eighths of a degree
static const int WHEEL_ROTATION_PER_STEP = 30;
VolumeSlider(const VolumeSlider&);
VolumeSlider &operator=(const VolumeSlider&);
@ -77,6 +81,8 @@ class VolumeSlider : public SliderSlider {
QPixmap drawVolumePixmap() const;
void drawVolumeSliderHandle();
int wheel_accumulator_;
bool anim_enter_;
int anim_count_;
QTimer *timer_anim_;