From e13325ca227c04b75b23c80c1ee64e23a11f294a Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 31 Jan 2020 15:14:21 +0300 Subject: [PATCH] Support menu with playback speed. --- Telegram/Resources/langs/lang.strings | 2 + .../view/media_view_playback_controls.cpp | 60 +++++++++++++++++++ .../media/view/media_view_playback_controls.h | 7 +++ .../SourceFiles/media/view/mediaview.style | 32 ++++++++++ Telegram/lib_ui | 2 +- 5 files changed, 102 insertions(+), 1 deletion(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index b9af988c1..ddeb6c9cb 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1579,6 +1579,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_mediaview_saved_to" = "Image was saved to your {downloads} folder"; "lng_mediaview_downloads" = "Downloads"; "lng_mediaview_video_loading" = "Loading - {percent}"; +"lng_mediaview_playback_speed" = "Playback speed"; +"lng_mediaview_playback_speed_normal" = "Normal"; "lng_theme_preview_title" = "Theme Preview"; "lng_theme_preview_generating" = "Generating color theme preview..."; diff --git a/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp b/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp index 4ccc929e2..1ab0c9d7d 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp +++ b/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp @@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/continuous_sliders.h" #include "ui/effects/fade_animation.h" #include "ui/widgets/buttons.h" +#include "ui/widgets/popup_menu.h" #include "lang/lang_keys.h" #include "layout.h" #include "app.h" @@ -36,6 +37,7 @@ PlaybackControls::PlaybackControls( , _pictureInPicture(this, st::mediaviewPipButton) , _playedAlready(this, st::mediaviewPlayProgressLabel) , _toPlayLeft(this, st::mediaviewPlayProgressLabel) +, _speedMenuStyle(st::mediaviewControlsPopupMenu) , _fadeAnimation(std::make_unique(this)) { _fadeAnimation->show(); _fadeAnimation->setFinishedCallback([=] { @@ -48,6 +50,9 @@ PlaybackControls::PlaybackControls( _pictureInPicture->addClickHandler([=] { _delegate->playbackControlsToPictureInPicture(); }); + _menuToggle->addClickHandler([=] { + showMenu(); + }); _volumeController->setValue(_delegate->playbackControlsCurrentVolume()); _volumeController->setChangeProgressCallback([=](float64 value) { @@ -170,6 +175,61 @@ void PlaybackControls::fadeUpdated(float64 opacity) { _volumeController->setFadeOpacity(opacity); } +void PlaybackControls::validateSpeedMenuStyle() { + auto &st = _speedMenuStyle.menu; + const auto &check = st::mediaviewMenuCheck; + const auto normal = tr::lng_mediaview_playback_speed_normal(tr::now); + const auto itemHeight = st.itemPadding.top() + + st.itemStyle.font->height + + st.itemPadding.bottom(); + const auto itemWidth = st.itemPadding.left() + + st.itemStyle.font->width(normal) + + st.itemPadding.right(); + if (itemWidth + st.itemPadding.right() + check.width() > st.widthMin) { + st.widthMin = itemWidth + st.itemPadding.right() + check.width(); + } + const auto realWidth = std::clamp(itemWidth, st.widthMin, st.widthMax); + st.itemIconPosition = QPoint( + realWidth - st.itemPadding.right() - check.width(), + (itemHeight - check.height()) / 2); +} + +void PlaybackControls::showMenu() { + if (_menu) { + _menu = nullptr; + return; + } + + validateSpeedMenuStyle(); + + auto submenu = std::make_unique( + this, + _speedMenuStyle); + const auto addSpeed = [&](float64 speed, QString text = QString()) { + if (text.isEmpty()) { + text = QString::number(speed); + } + const auto checked = (speed == _delegate->playbackControlsCurrentSpeed()); + const auto action = submenu->addAction( + text, + [=] { updatePlaybackSpeed(speed); }, + checked ? &st::mediaviewMenuCheck : nullptr); + }; + addSpeed(0.5); + addSpeed(0.75); + addSpeed(1., tr::lng_mediaview_playback_speed_normal(tr::now)); + addSpeed(1.25); + addSpeed(1.5); + addSpeed(1.75); + addSpeed(2.); + _menu.emplace(this, st::mediaviewControlsPopupMenu); + _menu->addAction( + tr::lng_mediaview_playback_speed(tr::now), + std::move(submenu)); + _menu->setForcedOrigin(Ui::PanelAnimation::Origin::BottomLeft); + _menu->popup(mapToGlobal(_menuToggle->geometry().topLeft())); +} + void PlaybackControls::updatePlaybackSpeed(float64 speed) { _delegate->playbackControlsSpeedChanged(speed); resizeEvent(nullptr); diff --git a/Telegram/SourceFiles/media/view/media_view_playback_controls.h b/Telegram/SourceFiles/media/view/media_view_playback_controls.h index 31fed6214..69059be13 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_controls.h +++ b/Telegram/SourceFiles/media/view/media_view_playback_controls.h @@ -9,12 +9,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rp_widget.h" #include "base/object_ptr.h" +#include "base/unique_qptr.h" +#include "styles/style_widgets.h" namespace Ui { class LabelSimple; class FadeAnimation; class IconButton; class MediaSlider; +class PopupMenu; } // namespace Ui namespace Media { @@ -80,6 +83,8 @@ private: void updatePlayPauseResumeState(const Player::TrackState &state); void updateTimeTexts(const Player::TrackState &state); void refreshTimeTexts(); + void validateSpeedMenuStyle(); + void showMenu(); not_null _delegate; @@ -106,6 +111,8 @@ private: object_ptr _toPlayLeft; object_ptr _downloadProgress = { nullptr }; + style::PopupMenu _speedMenuStyle; + base::unique_qptr _menu; std::unique_ptr _fadeAnimation; }; diff --git a/Telegram/SourceFiles/media/view/mediaview.style b/Telegram/SourceFiles/media/view/mediaview.style index 211e02ece..62335e980 100644 --- a/Telegram/SourceFiles/media/view/mediaview.style +++ b/Telegram/SourceFiles/media/view/mediaview.style @@ -174,6 +174,38 @@ mediaviewDropdownMenu: DropdownMenu(defaultDropdownMenu) { } } +mediaviewControlsMenu: Menu(defaultMenu) { + itemBg: mediaviewSaveMsgBg; + itemBgOver: mediaviewPlaybackIconRipple; + itemFg: mediaviewPlaybackProgressFg; + itemFgOver: mediaviewPlaybackProgressFg; + itemFgDisabled: mediaviewPlaybackProgressFg; + itemFgShortcut: mediaviewPlaybackProgressFg; + itemFgShortcutOver: mediaviewPlaybackProgressFg; + itemFgShortcutDisabled: mediaviewPlaybackProgressFg; + + separatorFg: mediaviewPlaybackIconRipple; + + arrow: icon {{ "dropdown_submenu_arrow", mediaviewPlaybackProgressFg }}; + + ripple: RippleAnimation(defaultRippleAnimation) { + color: mediaviewPlaybackIconRipple; + } +} +mediaviewControlsMenuShadow: Shadow(defaultEmptyShadow) { + fallback: mediaviewSaveMsgBg; +} +mediaviewControlsPanelAnimation: PanelAnimation(defaultPanelAnimation) { + fadeBg: mediaviewSaveMsgBg; + shadow: mediaviewControlsMenuShadow; +} +mediaviewControlsPopupMenu: PopupMenu(defaultPopupMenu) { + shadow: mediaviewControlsMenuShadow; + menu: mediaviewControlsMenu; + animation: mediaviewControlsPanelAnimation; +} +mediaviewMenuCheck: icon {{ "player_check", mediaviewPlaybackProgressFg }}; + mediaviewSaveMsgCheck: icon {{ "mediaview_save_check", mediaviewSaveMsgFg }}; mediaviewSaveMsgPadding: margins(55px, 19px, 29px, 20px); mediaviewSaveMsgCheckPos: point(23px, 21px); diff --git a/Telegram/lib_ui b/Telegram/lib_ui index 33ea14969..18ba60ec8 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit 33ea14969edfbe2546726d82925a1a66ae49310e +Subproject commit 18ba60ec86f1494d994ce507bb3e43ebe1766c20