2016-09-15 16:32:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-09-15 16:32:49 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-09-15 16:32:49 +00:00
|
|
|
*/
|
|
|
|
#include "media/player/media_player_volume_controller.h"
|
|
|
|
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2016-12-13 17:07:56 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2016-11-22 09:48:13 +00:00
|
|
|
#include "ui/widgets/continuous_sliders.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "ui/ui_utility.h"
|
|
|
|
#include "base/object_ptr.h"
|
2016-10-12 19:34:25 +00:00
|
|
|
#include "mainwindow.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2019-09-13 12:22:54 +00:00
|
|
|
#include "styles/style_media_player.h"
|
|
|
|
#include "styles/style_widgets.h"
|
2016-09-17 19:28:33 +00:00
|
|
|
|
2016-09-15 16:32:49 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Player {
|
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
VolumeController::VolumeController(QWidget *parent)
|
|
|
|
: TWidget(parent)
|
2016-10-12 19:34:25 +00:00
|
|
|
, _slider(this, st::mediaPlayerPanelPlayback) {
|
2016-10-13 08:33:12 +00:00
|
|
|
_slider->setMoveByWheel(true);
|
2019-08-06 16:40:08 +00:00
|
|
|
_slider->setChangeProgressCallback([=](float64 volume) {
|
2016-09-23 16:04:26 +00:00
|
|
|
applyVolumeChange(volume);
|
|
|
|
});
|
2019-08-06 16:40:08 +00:00
|
|
|
_slider->setChangeFinishedCallback([=](float64 volume) {
|
2016-09-23 16:04:26 +00:00
|
|
|
if (volume > 0) {
|
2016-10-12 19:34:25 +00:00
|
|
|
Global::SetRememberedSongVolume(volume);
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
|
|
|
applyVolumeChange(volume);
|
2019-03-12 05:09:53 +00:00
|
|
|
Auth().saveSettingsDelayed();
|
2016-09-23 16:04:26 +00:00
|
|
|
});
|
2016-10-12 19:34:25 +00:00
|
|
|
subscribe(Global::RefSongVolumeChanged(), [this] {
|
|
|
|
if (!_slider->isChanging()) {
|
2017-05-18 16:10:39 +00:00
|
|
|
_slider->setValue(Global::SongVolume());
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
});
|
2017-05-18 16:10:39 +00:00
|
|
|
setVolume(Global::SongVolume());
|
2016-09-17 19:28:33 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
resize(st::mediaPlayerPanelVolumeWidth, 2 * st::mediaPlayerPanelPlaybackPadding + st::mediaPlayerPanelPlayback.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeController::setIsVertical(bool vertical) {
|
|
|
|
using Direction = Ui::MediaSlider::Direction;
|
|
|
|
_slider->setDirection(vertical ? Direction::Vertical : Direction::Horizontal);
|
|
|
|
_slider->setAlwaysDisplayMarker(vertical);
|
2016-09-17 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void VolumeController::resizeEvent(QResizeEvent *e) {
|
2016-10-12 19:34:25 +00:00
|
|
|
_slider->setGeometry(rect());
|
2016-09-17 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
void VolumeController::setVolume(float64 volume) {
|
|
|
|
_slider->setValue(volume);
|
2016-09-23 16:04:26 +00:00
|
|
|
if (volume > 0) {
|
2016-10-12 19:34:25 +00:00
|
|
|
Global::SetRememberedSongVolume(volume);
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
|
|
|
applyVolumeChange(volume);
|
2016-09-17 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void VolumeController::applyVolumeChange(float64 volume) {
|
|
|
|
if (volume != Global::SongVolume()) {
|
|
|
|
Global::SetSongVolume(volume);
|
2017-05-18 20:18:59 +00:00
|
|
|
mixer()->setSongVolume(Global::SongVolume());
|
2016-10-12 19:34:25 +00:00
|
|
|
Global::RefSongVolumeChanged().notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
VolumeWidget::VolumeWidget(QWidget *parent)
|
|
|
|
: RpWidget(parent)
|
2016-10-12 19:34:25 +00:00
|
|
|
, _controller(this) {
|
|
|
|
hide();
|
|
|
|
_controller->setIsVertical(true);
|
|
|
|
|
|
|
|
_hideTimer.setSingleShot(true);
|
|
|
|
connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(onHideStart()));
|
|
|
|
|
|
|
|
_showTimer.setSingleShot(true);
|
|
|
|
connect(&_showTimer, SIGNAL(timeout()), this, SLOT(onShowStart()));
|
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
macWindowDeactivateEvents(
|
|
|
|
) | rpl::filter([=] {
|
|
|
|
return !isHidden();
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
leaveEvent(nullptr);
|
|
|
|
}, lifetime());
|
2016-10-12 19:34:25 +00:00
|
|
|
|
|
|
|
hide();
|
|
|
|
auto margin = getMargin();
|
|
|
|
resize(margin.left() + st::mediaPlayerVolumeSize.width() + margin.right(), margin.top() + st::mediaPlayerVolumeSize.height() + margin.bottom());
|
|
|
|
}
|
|
|
|
|
|
|
|
QMargins VolumeWidget::getMargin() const {
|
2019-01-17 08:17:38 +00:00
|
|
|
const auto top = st::mediaPlayerHeight
|
|
|
|
+ st::lineWidth
|
|
|
|
- st::mediaPlayerPlayTop
|
|
|
|
- st::mediaPlayerVolumeToggle.height;
|
|
|
|
return QMargins(st::mediaPlayerVolumeMargin, top, st::mediaPlayerVolumeMargin, st::mediaPlayerVolumeMargin);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VolumeWidget::overlaps(const QRect &globalRect) {
|
|
|
|
if (isHidden() || _a_appearance.animating()) return false;
|
|
|
|
|
|
|
|
return rect().marginsRemoved(getMargin()).contains(QRect(mapFromGlobal(globalRect.topLeft()), globalRect.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
auto inner = rect().marginsRemoved(getMargin());
|
|
|
|
_controller->setGeometry(inner.x(), inner.y() - st::lineWidth, inner.width(), inner.height() + st::lineWidth - ((st::mediaPlayerVolumeSize.width() - st::mediaPlayerPanelPlayback.width) / 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (!_cache.isNull()) {
|
2019-04-02 09:13:30 +00:00
|
|
|
bool animating = _a_appearance.animating();
|
2016-10-12 19:34:25 +00:00
|
|
|
if (animating) {
|
2019-04-02 09:13:30 +00:00
|
|
|
p.setOpacity(_a_appearance.value(_hiding ? 0. : 1.));
|
2016-10-13 15:04:40 +00:00
|
|
|
} else if (_hiding || isHidden()) {
|
2016-10-12 19:34:25 +00:00
|
|
|
hidingFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p.drawPixmap(0, 0, _cache);
|
|
|
|
if (!animating) {
|
|
|
|
showChildren();
|
|
|
|
_cache = QPixmap();
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2016-10-12 19:34:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw shadow
|
|
|
|
auto shadowedRect = rect().marginsRemoved(getMargin());
|
2017-05-24 12:20:50 +00:00
|
|
|
auto shadowedSides = RectPart::Left | RectPart::Right | RectPart::Bottom;
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::Shadow::paint(p, shadowedRect, width(), st::defaultRoundShadow, shadowedSides);
|
2017-05-24 12:20:50 +00:00
|
|
|
auto parts = RectPart::NoTopBottom | RectPart::FullBottom;
|
2016-12-13 17:07:56 +00:00
|
|
|
App::roundRect(p, QRect(shadowedRect.x(), -st::buttonRadius, shadowedRect.width(), shadowedRect.y() + shadowedRect.height() + st::buttonRadius), st::menuBg, MenuCorners, nullptr, parts);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void VolumeWidget::enterEventHook(QEvent *e) {
|
2016-10-12 19:34:25 +00:00
|
|
|
_hideTimer.stop();
|
2019-04-02 09:13:30 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2016-10-12 19:34:25 +00:00
|
|
|
onShowStart();
|
|
|
|
} else {
|
|
|
|
_showTimer.start(0);
|
|
|
|
}
|
2019-04-05 10:13:54 +00:00
|
|
|
return RpWidget::enterEventHook(e);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void VolumeWidget::leaveEventHook(QEvent *e) {
|
2016-10-12 19:34:25 +00:00
|
|
|
_showTimer.stop();
|
2019-04-02 09:13:30 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2016-10-12 19:34:25 +00:00
|
|
|
onHideStart();
|
|
|
|
} else {
|
|
|
|
_hideTimer.start(300);
|
|
|
|
}
|
2019-04-05 10:13:54 +00:00
|
|
|
return RpWidget::leaveEventHook(e);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::otherEnter() {
|
|
|
|
_hideTimer.stop();
|
2019-04-02 09:13:30 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2016-10-12 19:34:25 +00:00
|
|
|
onShowStart();
|
|
|
|
} else {
|
|
|
|
_showTimer.start(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::otherLeave() {
|
|
|
|
_showTimer.stop();
|
2019-04-02 09:13:30 +00:00
|
|
|
if (_a_appearance.animating()) {
|
2016-10-12 19:34:25 +00:00
|
|
|
onHideStart();
|
|
|
|
} else {
|
|
|
|
_hideTimer.start(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::onShowStart() {
|
|
|
|
if (isHidden()) {
|
|
|
|
show();
|
|
|
|
} else if (!_hiding) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_hiding = false;
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::onHideStart() {
|
|
|
|
if (_hiding) return;
|
|
|
|
|
|
|
|
_hiding = true;
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::startAnimation() {
|
|
|
|
if (_cache.isNull()) {
|
|
|
|
showChildren();
|
2017-12-26 12:41:48 +00:00
|
|
|
_cache = Ui::GrabWidget(this);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
hideChildren();
|
2019-04-02 09:13:30 +00:00
|
|
|
_a_appearance.start(
|
|
|
|
[=] { appearanceCallback(); },
|
|
|
|
_hiding ? 1. : 0.,
|
|
|
|
_hiding ? 0. : 1.,
|
|
|
|
st::defaultInnerDropdown.duration);
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::appearanceCallback() {
|
|
|
|
if (!_a_appearance.animating() && _hiding) {
|
|
|
|
_hiding = false;
|
|
|
|
hidingFinished();
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeWidget::hidingFinished() {
|
|
|
|
hide();
|
|
|
|
_cache = QPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VolumeWidget::eventFilter(QObject *obj, QEvent *e) {
|
|
|
|
if (e->type() == QEvent::Enter) {
|
|
|
|
otherEnter();
|
|
|
|
} else if (e->type() == QEvent::Leave) {
|
|
|
|
otherLeave();
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2016-10-12 19:34:25 +00:00
|
|
|
return false;
|
2016-09-17 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:32:49 +00:00
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|