2016-07-11 18:05:46 +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-07-11 18:05:46 +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-07-11 18:05:46 +00:00
|
|
|
*/
|
2019-02-27 11:36:19 +00:00
|
|
|
#include "media/view/media_view_playback_controls.h"
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2019-02-27 11:36:19 +00:00
|
|
|
#include "media/view/media_view_playback_progress.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
2016-11-22 09:48:13 +00:00
|
|
|
#include "ui/widgets/continuous_sliders.h"
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "ui/effects/fade_animation.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2021-01-28 05:46:40 +00:00
|
|
|
#include "ui/widgets/menu/menu_item_base.h"
|
2020-01-31 12:14:21 +00:00
|
|
|
#include "ui/widgets/popup_menu.h"
|
2020-09-30 11:32:02 +00:00
|
|
|
#include "ui/text/format_values.h"
|
2020-10-13 15:11:53 +00:00
|
|
|
#include "ui/cached_round_corners.h"
|
2019-05-31 11:12:07 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2020-02-03 09:26:37 +00:00
|
|
|
#include "styles/style_media_view.h"
|
2016-07-11 18:05:46 +00:00
|
|
|
|
|
|
|
namespace Media {
|
2019-02-27 11:36:19 +00:00
|
|
|
namespace View {
|
2021-01-28 05:46:40 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMinSpeed = 50;
|
|
|
|
constexpr auto kMaxSpeed = 200;
|
|
|
|
|
|
|
|
constexpr float64 SpeedShiftToValue(float64 value) {
|
|
|
|
const auto valueAsSpeedF = value * 100.;
|
|
|
|
const auto valueAsSpeed = int(valueAsSpeedF + 0.5); // constexpr round.
|
|
|
|
return float64(valueAsSpeed) / (kMaxSpeed - kMinSpeed);
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr float64 SpeedToValue(float64 value) {
|
|
|
|
const auto valueAsSpeedF = value * 100.;
|
|
|
|
const auto valueAsSpeed = int(valueAsSpeedF + 0.5); // constexpr round.
|
|
|
|
return float64(valueAsSpeed - kMinSpeed) / (kMaxSpeed - kMinSpeed);
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr auto kSpeedStickedValues =
|
|
|
|
std::array<std::pair<float64, float64>, 5>{{
|
|
|
|
{ SpeedToValue(0.75), SpeedShiftToValue(0.03) },
|
|
|
|
{ SpeedToValue(1.00), SpeedShiftToValue(0.05) },
|
|
|
|
{ SpeedToValue(1.25), SpeedShiftToValue(0.03) },
|
|
|
|
{ SpeedToValue(1.50), SpeedShiftToValue(0.03) },
|
|
|
|
{ SpeedToValue(1.75), SpeedShiftToValue(0.03) },
|
|
|
|
}};
|
|
|
|
|
|
|
|
class MenuSpeedItem final : public Ui::Menu::ItemBase {
|
|
|
|
public:
|
|
|
|
MenuSpeedItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
|
|
|
float64 startSpeed);
|
|
|
|
|
|
|
|
not_null<QAction*> action() const override;
|
|
|
|
bool isEnabled() const override;
|
|
|
|
|
|
|
|
[[nodiscard]] rpl::producer<float64> changeSpeedRequests() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int contentHeight() const override;
|
|
|
|
|
|
|
|
private:
|
2021-03-09 06:36:25 +00:00
|
|
|
float64 computeSpeed(float64 value) const;
|
|
|
|
QString speedString(float64 value) const;
|
|
|
|
|
2021-01-28 05:46:40 +00:00
|
|
|
QRect _itemRect;
|
|
|
|
QRect _textRect;
|
|
|
|
|
|
|
|
const style::MediaSlider &_sliderSt;
|
|
|
|
const base::unique_qptr<Ui::MediaSlider> _slider;
|
|
|
|
const not_null<QAction*> _dummyAction;
|
2021-03-09 06:36:25 +00:00
|
|
|
const int _lineCount;
|
2021-01-28 05:46:40 +00:00
|
|
|
const int _height;
|
|
|
|
|
|
|
|
rpl::event_stream<float64> _changeSpeedRequests;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
MenuSpeedItem::MenuSpeedItem(
|
|
|
|
not_null<RpWidget*> parent,
|
|
|
|
const style::Menu &st,
|
|
|
|
float64 startSpeed)
|
|
|
|
: Ui::Menu::ItemBase(parent, st)
|
|
|
|
, _sliderSt(st::mediaviewPlayback)
|
|
|
|
, _slider(base::make_unique_q<Ui::MediaSlider>(
|
|
|
|
this,
|
|
|
|
_sliderSt))
|
|
|
|
, _dummyAction(new QAction(parent))
|
2021-03-09 06:36:25 +00:00
|
|
|
, _lineCount(std::ceil(st.itemStyle.font->width(speedString(0.9))
|
|
|
|
/ float(st.widthMax)))
|
2021-01-28 05:46:40 +00:00
|
|
|
, _height(st.itemPadding.top() * 2
|
2021-03-09 06:36:25 +00:00
|
|
|
+ st.itemStyle.font->height * _lineCount
|
2021-01-28 05:46:40 +00:00
|
|
|
+ _sliderSt.seekSize.height()
|
|
|
|
+ st.itemPadding.bottom() * 2) {
|
|
|
|
|
|
|
|
initResizeHook(parent->sizeValue());
|
|
|
|
enableMouseSelecting();
|
|
|
|
enableMouseSelecting(_slider.get());
|
|
|
|
|
|
|
|
_slider->setAlwaysDisplayMarker(true);
|
2021-09-27 08:13:57 +00:00
|
|
|
_slider->setValue((base::SafeRound(startSpeed * 100.) - kMinSpeed)
|
2021-01-28 05:46:40 +00:00
|
|
|
/ (kMaxSpeed - kMinSpeed));
|
|
|
|
|
2021-02-11 14:46:46 +00:00
|
|
|
for (const auto &sticked : kSpeedStickedValues) {
|
|
|
|
_slider->addDivider(sticked.first, st::speedSliderDividerSize);
|
|
|
|
}
|
|
|
|
//_slider->addDivider(
|
|
|
|
// kSpeedStickedValues[1].first,
|
|
|
|
// st::speedSliderDividerSize);
|
2021-01-28 05:46:40 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const auto goodWidth = st.itemPadding.left()
|
|
|
|
+ st.itemPadding.right()
|
|
|
|
+ st.itemStyle.font->width(speedString(0.9));
|
|
|
|
setMinWidth(std::clamp(goodWidth, st.widthMin, st.widthMax));
|
|
|
|
}
|
|
|
|
|
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](const QSize &size) {
|
|
|
|
const auto geometry = QRect(QPoint(), size);
|
|
|
|
_itemRect = geometry - st.itemPadding;
|
|
|
|
|
|
|
|
const auto height = _itemRect.height();
|
2021-03-09 06:36:25 +00:00
|
|
|
const auto penWidth = QPen(st.itemBgOver).width();
|
2021-01-28 05:46:40 +00:00
|
|
|
_textRect = _itemRect
|
2021-03-09 06:36:25 +00:00
|
|
|
- style::margins(
|
|
|
|
-penWidth,
|
|
|
|
0,
|
|
|
|
-penWidth,
|
|
|
|
height - st.itemStyle.font->height * _lineCount);
|
2021-01-28 05:46:40 +00:00
|
|
|
|
|
|
|
const auto sliderGeometry = _itemRect
|
|
|
|
- style::margins(0, height - _sliderSt.seekSize.height(), 0, 0);
|
|
|
|
_slider->setGeometry(sliderGeometry);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
paintRequest(
|
|
|
|
) | rpl::start_with_next([=](const QRect &clip) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
const auto selected = isSelected();
|
|
|
|
p.fillRect(clip, selected ? st.itemBgOver : st.itemBg);
|
|
|
|
|
|
|
|
const auto value = _slider->value();
|
|
|
|
|
2021-02-04 16:50:46 +00:00
|
|
|
p.setPen(selected ? st.itemFgOver : st.itemFg);
|
2021-01-28 05:46:40 +00:00
|
|
|
p.setFont(st.itemStyle.font);
|
|
|
|
p.drawText(_textRect, speedString(value), style::al_left);
|
|
|
|
}, lifetime());
|
|
|
|
|
|
|
|
_slider->setChangeProgressCallback([=](float64 value) {
|
|
|
|
update(_textRect);
|
|
|
|
});
|
|
|
|
|
|
|
|
_slider->setChangeFinishedCallback([=](float64 value) {
|
|
|
|
_changeSpeedRequests.fire_copy(computeSpeed(value));
|
|
|
|
});
|
|
|
|
|
|
|
|
_slider->setAdjustCallback([=](float64 value) {
|
|
|
|
for (const auto &snap : kSpeedStickedValues) {
|
|
|
|
if (value > (snap.first - snap.second)
|
|
|
|
&& value < (snap.first + snap.second)) {
|
|
|
|
return snap.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:36:25 +00:00
|
|
|
float64 MenuSpeedItem::computeSpeed(float64 value) const {
|
2021-06-14 07:22:57 +00:00
|
|
|
return anim::interpolate(kMinSpeed, kMaxSpeed, std::clamp(value, 0., 1.))
|
|
|
|
/ 100.;
|
2021-03-09 06:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString MenuSpeedItem::speedString(float64 value) const {
|
|
|
|
return tr::lng_mediaview_playback_speed(
|
|
|
|
tr::now,
|
|
|
|
lt_speed,
|
|
|
|
QString::number(computeSpeed(value), 'f', 2) + 'x');
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:46:40 +00:00
|
|
|
not_null<QAction*> MenuSpeedItem::action() const {
|
|
|
|
return _dummyAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuSpeedItem::isEnabled() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MenuSpeedItem::contentHeight() const {
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<float64> MenuSpeedItem::changeSpeedRequests() const {
|
|
|
|
return _changeSpeedRequests.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2019-03-12 05:09:53 +00:00
|
|
|
PlaybackControls::PlaybackControls(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Delegate*> delegate)
|
2019-02-27 11:36:19 +00:00
|
|
|
: RpWidget(parent)
|
|
|
|
, _delegate(delegate)
|
2016-07-11 18:05:46 +00:00
|
|
|
, _playPauseResume(this, st::mediaviewPlayButton)
|
2017-05-18 16:10:39 +00:00
|
|
|
, _playbackSlider(this, st::mediaviewPlayback)
|
2019-02-27 11:36:19 +00:00
|
|
|
, _playbackProgress(std::make_unique<PlaybackProgress>())
|
2020-01-28 13:48:25 +00:00
|
|
|
, _volumeToggle(this, st::mediaviewVolumeToggle)
|
2018-10-15 08:06:27 +00:00
|
|
|
, _volumeController(this, st::mediaviewPlayback)
|
2020-01-31 09:54:57 +00:00
|
|
|
, _menuToggle(this, st::mediaviewMenuToggle)
|
2016-07-11 18:05:46 +00:00
|
|
|
, _fullScreenToggle(this, st::mediaviewFullScreenButton)
|
2020-01-31 09:54:57 +00:00
|
|
|
, _pictureInPicture(this, st::mediaviewPipButton)
|
2016-07-11 18:05:46 +00:00
|
|
|
, _playedAlready(this, st::mediaviewPlayProgressLabel)
|
|
|
|
, _toPlayLeft(this, st::mediaviewPlayProgressLabel)
|
2021-01-28 05:46:40 +00:00
|
|
|
, _menuStyle(st::mediaviewControlsPopupMenu)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _fadeAnimation(std::make_unique<Ui::FadeAnimation>(this)) {
|
2016-07-11 18:05:46 +00:00
|
|
|
_fadeAnimation->show();
|
2019-02-27 11:36:19 +00:00
|
|
|
_fadeAnimation->setFinishedCallback([=] {
|
|
|
|
fadeFinished();
|
|
|
|
});
|
|
|
|
_fadeAnimation->setUpdatedCallback([=](float64 opacity) {
|
|
|
|
fadeUpdated(opacity);
|
|
|
|
});
|
2016-07-12 14:11:59 +00:00
|
|
|
|
2020-01-28 11:48:29 +00:00
|
|
|
_pictureInPicture->addClickHandler([=] {
|
|
|
|
_delegate->playbackControlsToPictureInPicture();
|
|
|
|
});
|
2020-01-31 12:14:21 +00:00
|
|
|
_menuToggle->addClickHandler([=] {
|
|
|
|
showMenu();
|
|
|
|
});
|
2020-01-28 11:48:29 +00:00
|
|
|
|
2019-03-12 05:09:53 +00:00
|
|
|
_volumeController->setValue(_delegate->playbackControlsCurrentVolume());
|
2018-10-15 08:06:27 +00:00
|
|
|
_volumeController->setChangeProgressCallback([=](float64 value) {
|
2019-02-27 11:36:19 +00:00
|
|
|
_delegate->playbackControlsVolumeChanged(value);
|
2020-01-28 13:48:25 +00:00
|
|
|
updateVolumeToggleIcon();
|
2018-10-15 08:06:27 +00:00
|
|
|
});
|
2020-01-28 13:48:25 +00:00
|
|
|
_volumeController->setChangeFinishedCallback([=](float64) {
|
|
|
|
_delegate->playbackControlsVolumeChangeFinished();
|
|
|
|
});
|
|
|
|
updateVolumeToggleIcon();
|
|
|
|
_volumeToggle->setClickedCallback([=] {
|
|
|
|
_delegate->playbackControlsVolumeToggled();
|
|
|
|
_volumeController->setValue(_delegate->playbackControlsCurrentVolume());
|
|
|
|
updateVolumeToggleIcon();
|
|
|
|
});
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
_playPauseResume->addClickHandler([=] {
|
|
|
|
if (_showPause) {
|
|
|
|
_delegate->playbackControlsPause();
|
|
|
|
} else {
|
|
|
|
_delegate->playbackControlsPlay();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_fullScreenToggle->addClickHandler([=] {
|
|
|
|
if (_inFullScreen) {
|
|
|
|
_delegate->playbackControlsFromFullScreen();
|
|
|
|
} else {
|
|
|
|
_delegate->playbackControlsToFullScreen();
|
|
|
|
}
|
|
|
|
});
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
_playbackProgress->setValueChangedCallback([=](
|
|
|
|
float64 value,
|
|
|
|
float64 receivedTill) {
|
|
|
|
_playbackSlider->setValue(value, receivedTill);
|
2017-05-18 16:10:39 +00:00
|
|
|
});
|
2019-02-27 11:36:19 +00:00
|
|
|
_playbackSlider->setChangeProgressCallback([=](float64 value) {
|
|
|
|
_playbackProgress->setValue(value, false);
|
|
|
|
|
|
|
|
// This may destroy PlaybackControls.
|
|
|
|
handleSeekProgress(value);
|
2016-09-23 16:04:26 +00:00
|
|
|
});
|
2019-02-27 11:36:19 +00:00
|
|
|
_playbackSlider->setChangeFinishedCallback([=](float64 value) {
|
|
|
|
_playbackProgress->setValue(value, false);
|
2017-05-31 08:58:43 +00:00
|
|
|
handleSeekFinished(value);
|
2016-09-23 16:04:26 +00:00
|
|
|
});
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::handleSeekProgress(float64 progress) {
|
2016-07-13 17:34:57 +00:00
|
|
|
if (!_lastDurationMs) return;
|
|
|
|
|
2021-01-23 03:29:50 +00:00
|
|
|
const auto positionMs = std::clamp(
|
2019-03-01 14:41:10 +00:00
|
|
|
static_cast<crl::time>(progress * _lastDurationMs),
|
|
|
|
crl::time(0),
|
|
|
|
_lastDurationMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
if (_seekPositionMs != positionMs) {
|
|
|
|
_seekPositionMs = positionMs;
|
|
|
|
refreshTimeTexts();
|
2019-02-27 11:36:19 +00:00
|
|
|
|
|
|
|
// This may destroy PlaybackControls.
|
|
|
|
_delegate->playbackControlsSeekProgress(positionMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::handleSeekFinished(float64 progress) {
|
2016-07-13 17:34:57 +00:00
|
|
|
if (!_lastDurationMs) return;
|
|
|
|
|
2021-01-23 03:29:50 +00:00
|
|
|
const auto positionMs = std::clamp(
|
2019-03-01 14:41:10 +00:00
|
|
|
static_cast<crl::time>(progress * _lastDurationMs),
|
|
|
|
crl::time(0),
|
|
|
|
_lastDurationMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
_seekPositionMs = -1;
|
2019-02-27 11:36:19 +00:00
|
|
|
_delegate->playbackControlsSeekFinished(positionMs);
|
2016-07-13 17:34:57 +00:00
|
|
|
refreshTimeTexts();
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 16:23:09 +00:00
|
|
|
template <typename Callback>
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::startFading(Callback start) {
|
2018-04-06 16:23:09 +00:00
|
|
|
if (!_fadeAnimation->animating()) {
|
|
|
|
showChildren();
|
2018-06-06 08:56:39 +00:00
|
|
|
_playbackSlider->disablePaint(true);
|
2018-10-15 08:06:27 +00:00
|
|
|
_volumeController->disablePaint(true);
|
2018-04-06 16:23:09 +00:00
|
|
|
_childrenHidden = false;
|
|
|
|
}
|
|
|
|
start();
|
|
|
|
if (_fadeAnimation->animating()) {
|
2018-06-06 08:56:39 +00:00
|
|
|
for (const auto child : children()) {
|
2018-10-15 08:06:27 +00:00
|
|
|
if (child->isWidgetType()
|
|
|
|
&& child != _playbackSlider
|
2020-01-31 09:54:57 +00:00
|
|
|
&& child != _volumeController) {
|
2018-06-06 08:56:39 +00:00
|
|
|
static_cast<QWidget*>(child)->hide();
|
|
|
|
}
|
|
|
|
}
|
2018-04-06 16:23:09 +00:00
|
|
|
_childrenHidden = true;
|
|
|
|
} else {
|
|
|
|
fadeFinished();
|
|
|
|
}
|
2018-06-06 08:56:39 +00:00
|
|
|
_playbackSlider->disablePaint(false);
|
2018-10-15 08:06:27 +00:00
|
|
|
_volumeController->disablePaint(false);
|
2018-04-06 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::showAnimated() {
|
2016-07-12 11:38:16 +00:00
|
|
|
startFading([this]() {
|
2016-12-23 13:21:01 +00:00
|
|
|
_fadeAnimation->fadeIn(st::mediaviewShowDuration);
|
2016-07-12 11:38:16 +00:00
|
|
|
});
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::hideAnimated() {
|
2016-07-12 11:38:16 +00:00
|
|
|
startFading([this]() {
|
2016-12-23 13:21:01 +00:00
|
|
|
_fadeAnimation->fadeOut(st::mediaviewHideDuration);
|
2016-07-12 11:38:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::fadeFinished() {
|
2018-04-06 16:23:09 +00:00
|
|
|
fadeUpdated(_fadeAnimation->visible() ? 1. : 0.);
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::fadeUpdated(float64 opacity) {
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->setFadeOpacity(opacity);
|
2018-10-15 08:06:27 +00:00
|
|
|
_volumeController->setFadeOpacity(opacity);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 12:14:21 +00:00
|
|
|
void PlaybackControls::showMenu() {
|
|
|
|
if (_menu) {
|
|
|
|
_menu = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:46:40 +00:00
|
|
|
_menu.emplace(this, _menuStyle);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto speedItem = base::make_unique_q<MenuSpeedItem>(
|
|
|
|
_menu,
|
|
|
|
_menuStyle.menu,
|
|
|
|
_delegate->playbackControlsCurrentSpeed());
|
|
|
|
speedItem->changeSpeedRequests(
|
|
|
|
) | rpl::start_with_next([=](float64 speed) {
|
|
|
|
updatePlaybackSpeed(speed);
|
|
|
|
}, speedItem->lifetime());
|
|
|
|
_menu->addAction(std::move(speedItem));
|
|
|
|
}
|
|
|
|
|
|
|
|
_menu->addSeparator();
|
2020-01-31 12:14:21 +00:00
|
|
|
|
2020-02-13 12:34:19 +00:00
|
|
|
_menu->addAction(tr::lng_mediaview_rotate_video(tr::now), [=] {
|
2020-02-05 15:27:53 +00:00
|
|
|
_delegate->playbackControlsRotate();
|
|
|
|
});
|
2021-01-28 05:46:40 +00:00
|
|
|
|
2020-01-31 12:14:21 +00:00
|
|
|
_menu->setForcedOrigin(Ui::PanelAnimation::Origin::BottomLeft);
|
|
|
|
_menu->popup(mapToGlobal(_menuToggle->geometry().topLeft()));
|
|
|
|
}
|
|
|
|
|
2020-01-28 13:48:25 +00:00
|
|
|
void PlaybackControls::updatePlaybackSpeed(float64 speed) {
|
2020-02-13 12:34:19 +00:00
|
|
|
DEBUG_LOG(("Media playback speed: update to %1.").arg(speed));
|
2020-01-28 13:48:25 +00:00
|
|
|
_delegate->playbackControlsSpeedChanged(speed);
|
|
|
|
resizeEvent(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::updatePlayback(const Player::TrackState &state) {
|
2017-01-24 21:24:39 +00:00
|
|
|
updatePlayPauseResumeState(state);
|
2019-05-31 11:45:35 +00:00
|
|
|
_playbackProgress->updateState(state, countDownloadedTillPercent(state));
|
2017-01-24 21:24:39 +00:00
|
|
|
updateTimeTexts(state);
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 13:48:25 +00:00
|
|
|
void PlaybackControls::updateVolumeToggleIcon() {
|
|
|
|
const auto volume = _delegate->playbackControlsCurrentVolume();
|
|
|
|
_volumeToggle->setIconOverride([&] {
|
|
|
|
return (volume <= 0.)
|
|
|
|
? nullptr
|
2020-01-31 09:54:57 +00:00
|
|
|
: (volume < 1 / 2.)
|
2020-01-28 13:48:25 +00:00
|
|
|
? &st::mediaviewVolumeIcon1
|
2020-01-31 09:54:57 +00:00
|
|
|
: &st::mediaviewVolumeIcon2;
|
2020-01-28 13:48:25 +00:00
|
|
|
}(), [&] {
|
|
|
|
return (volume <= 0.)
|
|
|
|
? nullptr
|
2020-01-31 09:54:57 +00:00
|
|
|
: (volume < 1 / 2.)
|
2020-01-28 13:48:25 +00:00
|
|
|
? &st::mediaviewVolumeIcon1Over
|
2020-01-31 09:54:57 +00:00
|
|
|
: &st::mediaviewVolumeIcon2Over;
|
2020-01-28 13:48:25 +00:00
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
float64 PlaybackControls::countDownloadedTillPercent(
|
|
|
|
const Player::TrackState &state) const {
|
|
|
|
if (_loadingReady > 0 && _loadingReady == _loadingTotal) {
|
|
|
|
return 1.;
|
|
|
|
}
|
|
|
|
const auto header = state.fileHeaderSize;
|
|
|
|
if (!header || _loadingReady <= header || _loadingTotal <= header) {
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
return (_loadingReady - header) / float64(_loadingTotal - header);
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void PlaybackControls::setLoadingProgress(int64 ready, int64 total) {
|
2019-05-31 11:12:07 +00:00
|
|
|
if (_loadingReady == ready && _loadingTotal == total) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_loadingReady = ready;
|
|
|
|
_loadingTotal = total;
|
|
|
|
if (_loadingReady != 0 && _loadingReady != _loadingTotal) {
|
|
|
|
if (!_downloadProgress) {
|
|
|
|
_downloadProgress.create(this, st::mediaviewPlayProgressLabel);
|
|
|
|
_downloadProgress->setVisible(!_fadeAnimation->animating());
|
|
|
|
_loadingPercent = -1;
|
|
|
|
}
|
|
|
|
const auto progress = total ? (ready / float64(total)) : 0.;
|
2021-09-27 08:13:57 +00:00
|
|
|
const auto percent = int(base::SafeRound(progress * 100));
|
2019-05-31 11:12:07 +00:00
|
|
|
if (_loadingPercent != percent) {
|
|
|
|
_loadingPercent = percent;
|
2020-01-31 09:54:57 +00:00
|
|
|
_downloadProgress->setText(QString::number(percent) + '%');
|
|
|
|
updateDownloadProgressPosition();
|
2019-05-31 11:12:07 +00:00
|
|
|
refreshFadeCache();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_downloadProgress.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackControls::refreshFadeCache() {
|
|
|
|
if (!_fadeAnimation->animating()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
startFading([&] {
|
|
|
|
_fadeAnimation->refreshCache();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::updatePlayPauseResumeState(const Player::TrackState &state) {
|
2019-02-22 14:28:10 +00:00
|
|
|
auto showPause = ShowPauseIcon(state.state) || (_seekPositionMs >= 0);
|
2016-07-11 18:05:46 +00:00
|
|
|
if (showPause != _showPause) {
|
|
|
|
_showPause = showPause;
|
2016-12-05 11:01:08 +00:00
|
|
|
_playPauseResume->setIconOverride(_showPause ? &st::mediaviewPauseIcon : nullptr, _showPause ? &st::mediaviewPauseIconOver : nullptr);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::updateTimeTexts(const Player::TrackState &state) {
|
2021-07-13 17:51:07 +00:00
|
|
|
qint64 position = 0;
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2017-05-21 13:16:39 +00:00
|
|
|
if (Player::IsStoppedAtEnd(state.state)) {
|
2017-05-03 13:01:15 +00:00
|
|
|
position = state.length;
|
2017-05-21 13:16:39 +00:00
|
|
|
} else if (!Player::IsStoppedOrStopping(state.state)) {
|
|
|
|
position = state.position;
|
2016-07-12 11:38:16 +00:00
|
|
|
} else {
|
|
|
|
position = 0;
|
|
|
|
}
|
2017-01-24 21:24:39 +00:00
|
|
|
auto playFrequency = state.frequency;
|
2016-07-12 11:38:16 +00:00
|
|
|
auto playAlready = position / playFrequency;
|
2017-05-03 13:01:15 +00:00
|
|
|
auto playLeft = (state.length / playFrequency) - playAlready;
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2019-03-01 14:41:10 +00:00
|
|
|
_lastDurationMs = (state.length * crl::time(1000)) / playFrequency;
|
2016-07-13 17:34:57 +00:00
|
|
|
|
2020-09-30 11:32:02 +00:00
|
|
|
_timeAlready = Ui::FormatDurationText(playAlready);
|
2016-07-12 11:38:16 +00:00
|
|
|
auto minus = QChar(8722);
|
2020-09-30 11:32:02 +00:00
|
|
|
_timeLeft = minus + Ui::FormatDurationText(playLeft);
|
2016-07-12 11:38:16 +00:00
|
|
|
|
2016-07-13 17:34:57 +00:00
|
|
|
if (_seekPositionMs < 0) {
|
|
|
|
refreshTimeTexts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::refreshTimeTexts() {
|
2016-07-12 11:38:16 +00:00
|
|
|
auto alreadyChanged = false, leftChanged = false;
|
2016-07-13 17:34:57 +00:00
|
|
|
auto timeAlready = _timeAlready;
|
|
|
|
auto timeLeft = _timeLeft;
|
|
|
|
if (_seekPositionMs >= 0) {
|
2019-03-01 14:41:10 +00:00
|
|
|
auto playAlready = _seekPositionMs / crl::time(1000);
|
|
|
|
auto playLeft = (_lastDurationMs / crl::time(1000)) - playAlready;
|
2016-07-13 17:34:57 +00:00
|
|
|
|
2020-09-30 11:32:02 +00:00
|
|
|
timeAlready = Ui::FormatDurationText(playAlready);
|
2016-07-13 17:34:57 +00:00
|
|
|
auto minus = QChar(8722);
|
2020-09-30 11:32:02 +00:00
|
|
|
timeLeft = minus + Ui::FormatDurationText(playLeft);
|
2016-07-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
_playedAlready->setText(timeAlready, &alreadyChanged);
|
|
|
|
_toPlayLeft->setText(timeLeft, &leftChanged);
|
|
|
|
if (alreadyChanged || leftChanged) {
|
2016-07-16 06:54:02 +00:00
|
|
|
resizeEvent(nullptr);
|
2019-05-31 11:12:07 +00:00
|
|
|
refreshFadeCache();
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::setInFullScreen(bool inFullScreen) {
|
|
|
|
if (_inFullScreen != inFullScreen) {
|
|
|
|
_inFullScreen = inFullScreen;
|
|
|
|
_fullScreenToggle->setIconOverride(
|
|
|
|
_inFullScreen ? &st::mediaviewFullScreenOutIcon : nullptr,
|
|
|
|
_inFullScreen ? &st::mediaviewFullScreenOutIconOver : nullptr);
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::resizeEvent(QResizeEvent *e) {
|
2020-01-31 09:54:57 +00:00
|
|
|
const auto textSkip = st::mediaviewPlayProgressSkip;
|
|
|
|
const auto textLeft = st::mediaviewPlayProgressLeft;
|
|
|
|
const auto textTop = st::mediaviewPlaybackTop + (_playbackSlider->height() - _playedAlready->height()) / 2;
|
|
|
|
_playedAlready->moveToLeft(textLeft + textSkip, textTop);
|
|
|
|
_toPlayLeft->moveToRight(textLeft + textSkip, textTop);
|
|
|
|
const auto remove = 2 * textLeft + 4 * textSkip + _playedAlready->width() + _toPlayLeft->width();
|
|
|
|
auto playbackWidth = width() - remove;
|
2017-05-18 16:10:39 +00:00
|
|
|
_playbackSlider->resize(playbackWidth, st::mediaviewPlayback.seekSize.height());
|
2020-01-31 09:54:57 +00:00
|
|
|
_playbackSlider->moveToLeft(textLeft + 2 * textSkip + _playedAlready->width(), st::mediaviewPlaybackTop);
|
|
|
|
|
|
|
|
_playPauseResume->moveToLeft(
|
|
|
|
(width() - _playPauseResume->width()) / 2,
|
|
|
|
st::mediaviewPlayButtonTop);
|
|
|
|
|
|
|
|
auto right = st::mediaviewMenuToggleSkip;
|
|
|
|
_menuToggle->moveToRight(right, st::mediaviewButtonsTop);
|
|
|
|
right += _menuToggle->width() + st::mediaviewPipButtonSkip;
|
|
|
|
_pictureInPicture->moveToRight(right, st::mediaviewButtonsTop);
|
|
|
|
right += _pictureInPicture->width() + st::mediaviewFullScreenButtonSkip;
|
|
|
|
_fullScreenToggle->moveToRight(right, st::mediaviewButtonsTop);
|
|
|
|
|
|
|
|
updateDownloadProgressPosition();
|
|
|
|
|
|
|
|
auto left = st::mediaviewVolumeToggleSkip;
|
|
|
|
_volumeToggle->moveToLeft(left, st::mediaviewVolumeTop);
|
|
|
|
left += _volumeToggle->width() + st::mediaviewVolumeSkip;
|
|
|
|
_volumeController->resize(
|
|
|
|
st::mediaviewVolumeWidth,
|
|
|
|
st::mediaviewPlayback.seekSize.height());
|
|
|
|
_volumeController->moveToLeft(left, st::mediaviewVolumeTop + (_volumeToggle->height() - _volumeController->height()) / 2);
|
|
|
|
}
|
2019-05-31 11:12:07 +00:00
|
|
|
|
2020-01-31 09:54:57 +00:00
|
|
|
void PlaybackControls::updateDownloadProgressPosition() {
|
|
|
|
if (!_downloadProgress) {
|
|
|
|
return;
|
2019-05-31 11:12:07 +00:00
|
|
|
}
|
2020-01-31 09:54:57 +00:00
|
|
|
const auto left = _playPauseResume->x() + _playPauseResume->width();
|
|
|
|
const auto right = _fullScreenToggle->x();
|
|
|
|
const auto available = right - left;
|
|
|
|
const auto x = left + (available - _downloadProgress->width()) / 2;
|
|
|
|
const auto y = _playPauseResume->y() + (_playPauseResume->height() - _downloadProgress->height()) / 2;
|
|
|
|
_downloadProgress->move(x, y);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::paintEvent(QPaintEvent *e) {
|
2016-07-11 18:05:46 +00:00
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (_fadeAnimation->paint(p)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-06 16:23:09 +00:00
|
|
|
if (_childrenHidden) {
|
|
|
|
showChildren();
|
|
|
|
_playbackSlider->setFadeOpacity(1.);
|
2018-10-15 08:06:27 +00:00
|
|
|
_volumeController->setFadeOpacity(1.);
|
2018-04-06 16:23:09 +00:00
|
|
|
_childrenHidden = false;
|
|
|
|
}
|
2020-10-13 15:11:53 +00:00
|
|
|
Ui::FillRoundRect(p, rect(), st::mediaviewSaveMsgBg, Ui::MediaviewSaveCorners);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackControls::mousePressEvent(QMouseEvent *e) {
|
2019-02-12 11:00:47 +00:00
|
|
|
e->accept(); // Don't pass event to the Media::View::OverlayWidget.
|
2016-07-12 14:11:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 13:25:40 +00:00
|
|
|
bool PlaybackControls::hasMenu() const {
|
|
|
|
return _menu != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
PlaybackControls::~PlaybackControls() = default;
|
2016-07-19 12:31:48 +00:00
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
} // namespace View
|
2016-07-11 18:05:46 +00:00
|
|
|
} // namespace Media
|