2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-30 22:25:12 +00:00

Changing volume in media player by mouse wheel events.

This commit is contained in:
John Preston
2016-10-13 11:33:12 +03:00
parent 9eb8a93719
commit 956d048d56
5 changed files with 70 additions and 29 deletions

View File

@@ -22,6 +22,11 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "ui/widgets/continuous_slider.h"
namespace Ui {
namespace {
constexpr auto kByWheelFinishedTimeout = 1000;
} // namespace
ContinuousSlider::ContinuousSlider(QWidget *parent) : TWidget(parent)
, _a_value(animation(this, &ContinuousSlider::step_value)) {
@@ -40,6 +45,22 @@ void ContinuousSlider::setDisabled(bool disabled) {
}
}
void ContinuousSlider::setMoveByWheel(bool moveByWheel) {
if (_moveByWheel != moveByWheel) {
_moveByWheel = moveByWheel;
if (_moveByWheel) {
_byWheelFinished = std_::make_unique<SingleTimer>();
_byWheelFinished->setTimeoutHandler([this] {
if (_changeFinishedCallback) {
_changeFinishedCallback(getCurrentValue(getms()));
}
});
} else {
_byWheelFinished.reset();
}
}
}
void ContinuousSlider::setValue(float64 value, bool animated) {
if (animated) {
a_value.start(value);
@@ -102,6 +123,34 @@ void ContinuousSlider::mouseReleaseEvent(QMouseEvent *e) {
}
}
void ContinuousSlider::wheelEvent(QWheelEvent *e) {
if (_mouseDown) {
return;
}
#ifdef OS_MAC_OLD
constexpr auto step = 120;
#else // OS_MAC_OLD
constexpr auto step = static_cast<int>(QWheelEvent::DefaultDeltasPerStep);
#endif // OS_MAC_OLD
constexpr auto coef = 1. / (step * 5.);
auto deltaX = e->angleDelta().x(), deltaY = e->angleDelta().y();
if (cPlatform() == dbipMac || cPlatform() == dbipMacOld) {
deltaY *= -1;
}
if (deltaX * deltaY < 0) {
return;
}
auto delta = (deltaX >= 0 && deltaY >= 0) ? qMax(deltaX, deltaY) : qMin(deltaX, deltaY);
auto finalValue = snap(a_value.to() + delta * coef, 0., 1.);
setValue(finalValue, false);
if (_changeProgressCallback) {
_changeProgressCallback(finalValue);
}
_byWheelFinished->start(kByWheelFinishedTimeout);
}
void ContinuousSlider::updateDownValueFromPos(const QPoint &pos) {
_downValue = computeValue(pos);
update();

View File

@@ -51,10 +51,13 @@ public:
return _mouseDown;
}
void setMoveByWheel(bool moveByWheel);
protected:
void mouseMoveEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void wheelEvent(QWheelEvent *e) override;
void enterEvent(QEvent *e) override;
void leaveEvent(QEvent *e) override;
@@ -90,6 +93,9 @@ private:
Direction _direction = Direction::Horizontal;
bool _disabled = false;
bool _moveByWheel = false;
std_::unique_ptr<SingleTimer> _byWheelFinished;
Callback _changeProgressCallback;
Callback _changeFinishedCallback;