From 282c502b718cbde2238c14a53cabd3d37d980193 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 3 Feb 2020 21:24:45 +0400 Subject: [PATCH] Show playback time in PiP. --- .../SourceFiles/media/view/media_view.style | 4 +- .../SourceFiles/media/view/media_view_pip.cpp | 45 +++++++++++++++++++ .../SourceFiles/media/view/media_view_pip.h | 4 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Telegram/SourceFiles/media/view/media_view.style b/Telegram/SourceFiles/media/view/media_view.style index 28f1fa5ef..9490a2c0f 100644 --- a/Telegram/SourceFiles/media/view/media_view.style +++ b/Telegram/SourceFiles/media/view/media_view.style @@ -282,7 +282,7 @@ themePreviewButtonsSkip: 20px; themePreviewDialogsWidth: 312px; pipDefaultSize: 320px; -pipMinimalSize: 100px; +pipMinimalSize: 120px; pipBorderSkip: 20px; pipBorderSnapArea: 16px; pipResizeArea: 10px; @@ -290,6 +290,8 @@ pipControlSkip: 6px; pipPlaybackWidth: 2px; pipPlaybackWide: 4px; pipPlaybackSkip: 4px; +pipPlaybackTextSkip: 6px; +pipPlaybackFont: font(11px); pipPlayIcon: icon {{ "player_pip_play", mediaviewPipControlsFg }}; pipPlayIconOver: icon {{ "player_pip_play", mediaviewPipControlsFgOver }}; pipPauseIcon: icon {{ "player_pip_pause", mediaviewPipControlsFg }}; diff --git a/Telegram/SourceFiles/media/view/media_view_pip.cpp b/Telegram/SourceFiles/media/view/media_view_pip.cpp index a626b391e..4b3ee9e59 100644 --- a/Telegram/SourceFiles/media/view/media_view_pip.cpp +++ b/Telegram/SourceFiles/media/view/media_view_pip.cpp @@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/wrap/fade_wrap.h" #include "ui/widgets/shadow.h" #include "window/window_controller.h" +#include "layout.h" // formatDurationText #include "styles/style_window.h" #include "styles/style_media_view.h" #include "styles/style_calls.h" // st::callShadow @@ -35,6 +36,7 @@ namespace { constexpr auto kPipLoaderPriority = 2; constexpr auto kSaveGeometryTimeout = crl::time(1000); +constexpr auto kMsInSecond = 1000; [[nodiscard]] bool IsWindowControlsOnLeft() { return Platform::IsMac(); @@ -920,6 +922,7 @@ void Pip::seekProgress(float64 value) { _pausedBySeek = true; playbackPauseResume(); } + updatePlaybackTexts(_seekPositionMs, _lastDurationMs, kMsInSecond); } } @@ -1041,6 +1044,7 @@ void Pip::paintControls(QPainter &p) const { paintFade(p); paintButtons(p); paintPlayback(p); + paintPlaybackTexts(p); } void Pip::paintFade(QPainter &p) const { @@ -1126,6 +1130,21 @@ void Pip::paintPlayback(QPainter &p) const { p.setClipping(false); } +void Pip::paintPlaybackTexts(QPainter &p) const { + const auto left = _playback.area.x() + st::pipPlaybackTextSkip; + const auto right = _playback.area.x() + + _playback.area.width() + - st::pipPlaybackTextSkip; + const auto top = _playback.icon.y() + - st::pipPlaybackFont->height + + st::pipPlaybackFont->ascent; + + p.setFont(st::pipPlaybackFont); + p.setPen(st::mediaviewPipControlsFgOver); + p.drawText(left, top, _timeAlready); + p.drawText(right - _timeLeftWidth, top, _timeLeft); +} + void Pip::handleStreamingUpdate(Streaming::Update &&update) { using namespace Streaming; @@ -1168,6 +1187,32 @@ void Pip::updatePlaybackState() { } const auto playFrequency = state.frequency; _lastDurationMs = (state.length * crl::time(1000)) / playFrequency; + + if (_seekPositionMs < 0) { + updatePlaybackTexts(position, state.length, playFrequency); + } +} + +void Pip::updatePlaybackTexts( + int64 position, + int64 length, + int64 frequency) { + const auto playAlready = position / frequency; + const auto playLeft = (length / frequency) - playAlready; + const auto already = formatDurationText(playAlready); + const auto minus = QChar(8722); + const auto left = minus + formatDurationText(playLeft); + if (_timeAlready == already && _timeLeft == left) { + return; + } + _timeAlready = already; + _timeLeft = left; + _timeLeftWidth = st::pipPlaybackFont->width(_timeLeft); + _panel.update(QRect( + _playback.area.x(), + _playback.icon.y() - st::pipPlaybackFont->height, + _playback.area.width(), + st::pipPlaybackFont->height)); } void Pip::handleStreamingError(Streaming::Error &&error) { diff --git a/Telegram/SourceFiles/media/view/media_view_pip.h b/Telegram/SourceFiles/media/view/media_view_pip.h index eb1aeb56f..a1d3bef53 100644 --- a/Telegram/SourceFiles/media/view/media_view_pip.h +++ b/Telegram/SourceFiles/media/view/media_view_pip.h @@ -169,6 +169,7 @@ private: [[nodiscard]] OverState activeState() const; [[nodiscard]] float64 activeValue(const Button &button) const; void updateActiveState(OverState was); + void updatePlaybackTexts(int64 position, int64 length, int64 frequency); void handleMouseMove(QPoint position); void handleMousePress(QPoint position, Qt::MouseButton button); @@ -181,6 +182,7 @@ private: void paintFade(QPainter &p) const; void paintButtons(QPainter &p) const; void paintPlayback(QPainter &p) const; + void paintPlaybackTexts(QPainter &p) const; void paintRadialLoading(QPainter &p) const; void paintRadialLoadingContent(QPainter &p, const QRect &inner) const; [[nodiscard]] QRect countRadialRect() const; @@ -200,6 +202,8 @@ private: bool _showPause = false; bool _startPaused = false; bool _pausedBySeek = false; + QString _timeAlready, _timeLeft; + int _timeLeftWidth = 0; crl::time _seekPositionMs = -1; crl::time _lastDurationMs = 0; OverState _over = OverState::None;