mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-08-31 06:26:18 +00:00
Changing volume in media player by mouse wheel events.
This commit is contained in:
@@ -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();
|
||||
|
@@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user