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

@ -427,20 +427,6 @@ void MainWidget::rpcClear() {
RPCSender::rpcClear();
}
QPixmap MainWidget::grabInner() {
if (_overview && !_overview->isHidden()) {
return myGrab(_overview);
} else if (_wideSection && !_wideSection->isHidden()) {
return myGrab(_wideSection, QRect(0, st::topBarHeight, _history->width(), _history->height() - st::topBarHeight));
} else if (Adaptive::OneColumn() && _history->isHidden()) {
return myGrab(_dialogs, QRect(0, st::topBarHeight, _dialogs->width(), _dialogs->height() - st::topBarHeight));
} else if (_history->peer()) {
return myGrab(_history);
} else {
return myGrab(_history, QRect(0, st::topBarHeight, _history->width(), _history->height() - st::topBarHeight));
}
}
bool MainWidget::isItemVisible(HistoryItem *item) {
if (isHidden() || _a_show.animating()) {
return false;
@ -448,18 +434,6 @@ bool MainWidget::isItemVisible(HistoryItem *item) {
return _history->isItemVisible(item);
}
QPixmap MainWidget::grabTopBar() {
if (!_topBar->isHidden()) {
return myGrab(_topBar);
} else if (_wideSection) {
return myGrab(_wideSection, QRect(0, 0, _wideSection->width(), st::topBarHeight));
} else if (Adaptive::OneColumn() && _history->isHidden()) {
return myGrab(_dialogs, QRect(0, 0, _dialogs->width(), st::topBarHeight));
} else {
return myGrab(_history, QRect(0, 0, _history->width(), st::topBarHeight));
}
}
void MainWidget::notify_botCommandsChanged(UserData *bot) {
_history->notify_botCommandsChanged(bot);
}
@ -2335,6 +2309,10 @@ Window::SectionSlideParams MainWidget::prepareShowAnimation(bool willHaveTopBarS
if (_player) {
_player->hideShadow();
}
auto playerVolumeVisible = _playerVolume && !_playerVolume->isHidden();
if (playerVolumeVisible) {
_playerVolume->hide();
}
if (selectingPeer() && Adaptive::OneColumn()) {
result.oldContentCache = myGrab(this, QRect(0, _playerHeight, _dialogsWidth, height() - _playerHeight));
} else if (_wideSection) {
@ -2357,6 +2335,9 @@ Window::SectionSlideParams MainWidget::prepareShowAnimation(bool willHaveTopBarS
if (_overview) _overview->grabFinish();
_history->grabFinish();
}
if (playerVolumeVisible) {
_playerVolume->show();
}
if (_player) {
_player->showShadow();
}
@ -2482,6 +2463,10 @@ QPixmap MainWidget::grabForShowAnimation(const Window::SectionSlideParams &param
if (_player) {
_player->hideShadow();
}
auto playerVolumeVisible = _playerVolume && !_playerVolume->isHidden();
if (playerVolumeVisible) {
_playerVolume->hide();
}
if (Adaptive::OneColumn()) {
result = myGrab(this, QRect(0, _playerHeight, _dialogsWidth, height() - _playerHeight));
} else {
@ -2489,6 +2474,9 @@ QPixmap MainWidget::grabForShowAnimation(const Window::SectionSlideParams &param
result = myGrab(this, QRect(_dialogsWidth, _playerHeight, width() - _dialogsWidth, height() - _playerHeight));
_sideShadow->show();
}
if (playerVolumeVisible) {
_playerVolume->show();
}
if (_player) {
_player->showShadow();
}

View File

@ -373,9 +373,6 @@ public:
bool contentOverlapped(const QRect &globalRect);
QPixmap grabTopBar();
QPixmap grabInner();
void rpcClear() override;
bool isItemVisible(HistoryItem *item);

View File

@ -33,6 +33,7 @@ namespace Player {
VolumeController::VolumeController(QWidget *parent) : TWidget(parent)
, _slider(this, st::mediaPlayerPanelPlayback) {
_slider->setMoveByWheel(true);
_slider->setChangeProgressCallback([this](float64 volume) {
applyVolumeChange(volume);
});

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;