tdesktop/Telegram/SourceFiles/media/player/media_player_volume_control...

117 lines
3.4 KiB
C++
Raw Normal View History

2016-09-15 16:32:49 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-09-15 16:32:49 +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"
#include "media/audio/media_audio.h"
#include "media/player/media_player_dropdown.h"
#include "ui/widgets/buttons.h"
#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 "ui/cached_round_corners.h"
#include "mainwindow.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
2020-06-08 15:17:33 +00:00
#include "window/window_session_controller.h"
#include "core/application.h"
#include "core/core_settings.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
2022-01-24 16:33:30 +00:00
#include <QtGui/QGuiApplication>
namespace Media::Player {
2016-09-15 16:32:49 +00:00
2020-06-08 15:17:33 +00:00
VolumeController::VolumeController(
QWidget *parent,
not_null<Window::SessionController*> controller)
: RpWidget(parent)
, _slider(this, st::mediaPlayerPanelPlayback) {
_slider->setMoveByWheel(true);
2019-08-06 16:40:08 +00:00
_slider->setChangeProgressCallback([=](float64 volume) {
applyVolumeChange(volume);
});
2019-08-06 16:40:08 +00:00
_slider->setChangeFinishedCallback([=](float64 volume) {
if (volume > 0) {
Core::App().settings().setRememberedSongVolume(volume);
}
applyVolumeChange(volume);
Core::App().saveSettingsDelayed();
});
Core::App().settings().songVolumeChanges(
) | rpl::start_with_next([=](float64 volume) {
if (!_slider->isChanging()) {
_slider->setValue(volume);
}
}, lifetime());
setVolume(Core::App().settings().songVolume());
2016-09-17 19:28:33 +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
}
void VolumeController::outerWheelEvent(not_null<QWheelEvent*> e) {
QGuiApplication::sendEvent(_slider.data(), e);
}
void VolumeController::resizeEvent(QResizeEvent *e) {
_slider->setGeometry(rect());
2016-09-17 19:28:33 +00:00
}
void VolumeController::setVolume(float64 volume) {
_slider->setValue(volume);
if (volume > 0) {
Core::App().settings().setRememberedSongVolume(volume);
}
applyVolumeChange(volume);
2016-09-17 19:28:33 +00:00
}
void VolumeController::applyVolumeChange(float64 volume) {
if (volume != Core::App().settings().songVolume()) {
mixer()->setSongVolume(volume);
Core::App().settings().setSongVolume(volume);
}
}
void PrepareVolumeDropdown(
not_null<Dropdown*> dropdown,
not_null<Window::SessionController*> controller,
rpl::producer<not_null<QWheelEvent*>> outerWheelEvents) {
const auto volume = Ui::CreateChild<VolumeController>(
dropdown.get(),
controller);
volume->show();
volume->setIsVertical(true);
dropdown->sizeValue(
) | rpl::start_with_next([=](QSize size) {
const auto rect = QRect(QPoint(), size);
const auto inner = rect.marginsRemoved(dropdown->getMargin());
volume->setGeometry(
inner.x(),
inner.y() - st::lineWidth,
inner.width(),
(inner.height()
+ st::lineWidth
- ((st::mediaPlayerVolumeSize.width()
- st::mediaPlayerPanelPlayback.width) / 2)));
}, volume->lifetime());
std::move(
outerWheelEvents
) | rpl::start_with_next([=](not_null<QWheelEvent*> e) {
volume->outerWheelEvent(e);
}, volume->lifetime());
}
} // namespace Media::Player