From ec3957fcf304f817670678e6374249eab22f07e9 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 19 Jan 2023 11:40:39 +0400 Subject: [PATCH] Active round video moving to active window. --- Telegram/SourceFiles/core/application.cpp | 76 ++++++++++--------- Telegram/SourceFiles/core/application.h | 12 +-- .../SourceFiles/info/info_layer_widget.cpp | 17 ++++- Telegram/SourceFiles/info/info_layer_widget.h | 1 + Telegram/SourceFiles/intro/intro_widget.cpp | 5 +- Telegram/SourceFiles/intro/intro_widget.h | 1 + Telegram/SourceFiles/mainwidget.cpp | 15 +++- Telegram/SourceFiles/mainwidget.h | 1 + .../media/player/media_player_float.h | 1 + .../media/player/media_player_instance.cpp | 26 ++----- .../media/player/media_player_instance.h | 2 - .../SourceFiles/window/window_controller.cpp | 34 +++++++++ .../SourceFiles/window/window_controller.h | 23 +++++- .../window/window_session_controller.cpp | 4 - 14 files changed, 140 insertions(+), 78 deletions(-) diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 9d16ded2b..25124779f 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -184,6 +184,8 @@ Application::~Application() { Local::writeSettings(); } + setLastActiveWindow(nullptr); + _lastActivePrimaryWindow = nullptr; _closingAsyncWindows.clear(); _secondaryWindows.clear(); _primaryWindows.clear(); @@ -287,9 +289,8 @@ void Application::run() { QMimeDatabase().mimeTypeForName(u"text/plain"_q); _primaryWindows.emplace(nullptr, std::make_unique()); - _lastActiveWindow - = _lastActivePrimaryWindow - = _primaryWindows.front().second.get(); + setLastActiveWindow(_primaryWindows.front().second.get()); + _lastActivePrimaryWindow = _lastActiveWindow; _domain->activeChanges( ) | rpl::start_with_next([=](not_null account) { @@ -737,35 +738,12 @@ bool Application::screenIsLocked() const { return _screenIsLocked; } -void Application::setDefaultFloatPlayerDelegate( - not_null delegate) { - Expects(!_defaultFloatPlayerDelegate == !_floatPlayers); - - _defaultFloatPlayerDelegate = delegate; - _replacementFloatPlayerDelegate = nullptr; - if (_floatPlayers) { - _floatPlayers->replaceDelegate(delegate); - } else { - _floatPlayers = std::make_unique( - delegate); - } -} - -void Application::replaceFloatPlayerDelegate( - not_null replacement) { - Expects(_floatPlayers != nullptr); - - _replacementFloatPlayerDelegate = replacement; - _floatPlayers->replaceDelegate(replacement); -} - -void Application::restoreFloatPlayerDelegate( - not_null replacement) { - Expects(_floatPlayers != nullptr); - - if (_replacementFloatPlayerDelegate == replacement) { - _replacementFloatPlayerDelegate = nullptr; - _floatPlayers->replaceDelegate(_defaultFloatPlayerDelegate); +void Application::floatPlayerToggleGifsPaused(bool paused) { + _floatPlayerGifsPaused = paused; + if (_lastActiveWindow) { + if (const auto delegate = _lastActiveWindow->floatPlayerDelegate()) { + delegate->floatPlayerToggleGifsPaused(paused); + } } } @@ -1304,6 +1282,35 @@ bool Application::closeNonLastAsync(not_null window) { return true; } +void Application::setLastActiveWindow(Window::Controller *window) { + _floatPlayerDelegateLifetime.destroy(); + + if (_floatPlayerGifsPaused && _lastActiveWindow) { + if (const auto delegate = _lastActiveWindow->floatPlayerDelegate()) { + delegate->floatPlayerToggleGifsPaused(false); + } + } + _lastActiveWindow = window; + if (!window) { + _floatPlayers = nullptr; + return; + } + window->floatPlayerDelegateValue( + ) | rpl::start_with_next([=](Media::Player::FloatDelegate *value) { + if (!value) { + _floatPlayers = nullptr; + } else if (_floatPlayers) { + _floatPlayers->replaceDelegate(value); + } else if (value) { + _floatPlayers = std::make_unique( + value); + } + if (value && _floatPlayerGifsPaused) { + value->floatPlayerToggleGifsPaused(true); + } + }, _floatPlayerDelegateLifetime); +} + void Application::closeWindow(not_null window) { const auto next = (_primaryWindows.front().second.get() != window) ? _primaryWindows.front().second.get() @@ -1314,7 +1321,7 @@ void Application::closeWindow(not_null window) { _lastActivePrimaryWindow = next; } if (_lastActiveWindow == window) { - _lastActiveWindow = next; + setLastActiveWindow(next); if (_lastActiveWindow) { _lastActiveWindow->activate(); _lastActiveWindow->widget()->updateGlobalMenu(); @@ -1371,7 +1378,8 @@ void Application::closeChatFromWindows(not_null peer) { void Application::windowActivated(not_null window) { const auto was = _lastActiveWindow; const auto now = window; - _lastActiveWindow = window; + + setLastActiveWindow(window); if (window->isPrimary()) { _lastActivePrimaryWindow = window; diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index 48a2512d4..79c96e8b7 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -258,12 +258,7 @@ public: [[nodiscard]] QString changelogLink() const; // Float player. - void setDefaultFloatPlayerDelegate( - not_null delegate); - void replaceFloatPlayerDelegate( - not_null replacement); - void restoreFloatPlayerDelegate( - not_null replacement); + void floatPlayerToggleGifsPaused(bool paused); [[nodiscard]] rpl::producer floatPlayerClosed() const; // Calls. @@ -339,6 +334,7 @@ private: void startSystemDarkModeViewer(); void startTray(); + void setLastActiveWindow(Window::Controller *window); void showAccount(not_null account); void enumerateWindows( Fn)> callback) const; @@ -412,8 +408,8 @@ private: const std::unique_ptr _tray; std::unique_ptr _floatPlayers; - Media::Player::FloatDelegate *_defaultFloatPlayerDelegate = nullptr; - Media::Player::FloatDelegate *_replacementFloatPlayerDelegate = nullptr; + rpl::lifetime _floatPlayerDelegateLifetime; + bool _floatPlayerGifsPaused = false; rpl::variable _passcodeLock; bool _screenIsLocked = false; diff --git a/Telegram/SourceFiles/info/info_layer_widget.cpp b/Telegram/SourceFiles/info/info_layer_widget.cpp index 18a37fd4d..fc39cf621 100644 --- a/Telegram/SourceFiles/info/info_layer_widget.cpp +++ b/Telegram/SourceFiles/info/info_layer_widget.cpp @@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/buttons.h" #include "ui/cached_round_corners.h" #include "window/section_widget.h" +#include "window/window_controller.h" #include "window/window_session_controller.h" #include "window/main_window.h" #include "main/main_session.h" @@ -31,7 +32,7 @@ LayerWidget::LayerWidget( : _controller(controller) , _content(this, controller, Wrap::Layer, memento) { setupHeightConsumers(); - Core::App().replaceFloatPlayerDelegate(floatPlayerDelegate()); + controller->window().replaceFloatPlayerDelegate(floatPlayerDelegate()); } LayerWidget::LayerWidget( @@ -40,7 +41,7 @@ LayerWidget::LayerWidget( : _controller(controller) , _content(memento->takeContent(this, Wrap::Layer)) { setupHeightConsumers(); - Core::App().replaceFloatPlayerDelegate(floatPlayerDelegate()); + controller->window().replaceFloatPlayerDelegate(floatPlayerDelegate()); } auto LayerWidget::floatPlayerDelegate() @@ -52,6 +53,15 @@ not_null LayerWidget::floatPlayerWidget() { return this; } +void LayerWidget::floatPlayerToggleGifsPaused(bool paused) { + constexpr auto kReason = Window::GifPauseReason::RoundPlaying; + if (paused) { + _controller->enableGifPauseReason(kReason); + } else { + _controller->disableGifPauseReason(kReason); + } +} + auto LayerWidget::floatPlayerGetSection(Window::Column column) -> not_null<::Media::Player::FloatSectionDelegate*> { Expects(_content != nullptr); @@ -357,7 +367,8 @@ void LayerWidget::paintEvent(QPaintEvent *e) { void LayerWidget::restoreFloatPlayerDelegate() { if (!_floatPlayerDelegateRestored) { _floatPlayerDelegateRestored = true; - Core::App().restoreFloatPlayerDelegate(floatPlayerDelegate()); + _controller->window().restoreFloatPlayerDelegate( + floatPlayerDelegate()); } } diff --git a/Telegram/SourceFiles/info/info_layer_widget.h b/Telegram/SourceFiles/info/info_layer_widget.h index dc7665d3a..c223b9d82 100644 --- a/Telegram/SourceFiles/info/info_layer_widget.h +++ b/Telegram/SourceFiles/info/info_layer_widget.h @@ -58,6 +58,7 @@ private: void restoreFloatPlayerDelegate(); not_null<::Media::Player::FloatDelegate*> floatPlayerDelegate(); not_null floatPlayerWidget() override; + void floatPlayerToggleGifsPaused(bool paused) override; not_null<::Media::Player::FloatSectionDelegate*> floatPlayerGetSection( Window::Column column) override; void floatPlayerEnumerateSections(FnsetDefaultFloatPlayerDelegate(floatPlayerDelegate()); getData()->country = ComputeNewAccountCountry(); @@ -175,6 +175,9 @@ not_null Widget::floatPlayerWidget() { return this; } +void Widget::floatPlayerToggleGifsPaused(bool paused) { +} + auto Widget::floatPlayerGetSection(Window::Column column) -> not_null { return this; diff --git a/Telegram/SourceFiles/intro/intro_widget.h b/Telegram/SourceFiles/intro/intro_widget.h index ef224b831..8b804c623 100644 --- a/Telegram/SourceFiles/intro/intro_widget.h +++ b/Telegram/SourceFiles/intro/intro_widget.h @@ -158,6 +158,7 @@ private: [[nodiscard]] auto floatPlayerSectionDelegate() -> not_null; not_null floatPlayerWidget() override; + void floatPlayerToggleGifsPaused(bool paused) override; not_null floatPlayerGetSection( Window::Column column) override; void floatPlayerEnumerateSections(FnfinishAnimating(); } - if (isPrimary()) { - Core::App().setDefaultFloatPlayerDelegate(floatPlayerDelegate()); - } + controller->window().setDefaultFloatPlayerDelegate( + floatPlayerDelegate()); + Core::App().floatPlayerClosed( ) | rpl::start_with_next([=](FullMsgId itemId) { floatPlayerClosed(itemId); @@ -431,6 +431,15 @@ not_null MainWidget::floatPlayerWidget() { return this; } +void MainWidget::floatPlayerToggleGifsPaused(bool paused) { + constexpr auto kReason = Window::GifPauseReason::RoundPlaying; + if (paused) { + _controller->enableGifPauseReason(kReason); + } else { + _controller->disableGifPauseReason(kReason); + } +} + auto MainWidget::floatPlayerGetSection(Window::Column column) -> not_null { if (isThreeColumn()) { diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 69d85d8d8..c2a2bc204 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -306,6 +306,7 @@ private: [[nodiscard]] auto floatPlayerDelegate() -> not_null; not_null floatPlayerWidget() override; + void floatPlayerToggleGifsPaused(bool paused) override; not_null floatPlayerGetSection( Window::Column column) override; void floatPlayerEnumerateSections(Fn floatPlayerWidget() = 0; + virtual void floatPlayerToggleGifsPaused(bool paused) = 0; virtual not_null floatPlayerGetSection( Window::Column column) = 0; virtual void floatPlayerEnumerateSections(Fnaccount().sessionChanges(). #include "main/main_session_settings.h" @@ -308,13 +308,7 @@ void Instance::clearStreamed(not_null data, bool savePosition) { data->streamed = nullptr; _roundPlaying = false; - // #TODO windows - if (const auto window = Core::App().activePrimaryWindow()) { - if (const auto controller = window->sessionController()) { - controller->disableGifPauseReason( - Window::GifPauseReason::RoundPlaying); - } - } + Core::App().floatPlayerToggleGifsPaused(false); } void Instance::refreshPlaylist(not_null data) { @@ -1277,10 +1271,6 @@ void Instance::setupShortcuts() { }, _lifetime); } -bool Instance::pauseGifByRoundVideo() const { - return _roundPlaying; -} - void Instance::stopAndClose() { _closePlayerRequests.fire({}); @@ -1303,13 +1293,7 @@ void Instance::handleStreamingUpdate( requestRoundVideoRepaint(); }); _roundPlaying = true; - // #TODO windows - if (const auto window = Core::App().activePrimaryWindow()) { - if (const auto controller = window->sessionController()) { - controller->enableGifPauseReason( - Window::GifPauseReason::RoundPlaying); - } - } + Core::App().floatPlayerToggleGifsPaused(true); requestRoundVideoResize(); } emitUpdate(data->type); diff --git a/Telegram/SourceFiles/media/player/media_player_instance.h b/Telegram/SourceFiles/media/player/media_player_instance.h index 6e74b162c..f6879e55f 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.h +++ b/Telegram/SourceFiles/media/player/media_player_instance.h @@ -166,8 +166,6 @@ public: [[nodiscard]] rpl::producer seekingChanges( AudioMsgId::Type type) const; - [[nodiscard]] bool pauseGifByRoundVideo() const; - [[nodiscard]] rpl::producer<> closePlayerRequests() const { return _closePlayerRequests.events(); } diff --git a/Telegram/SourceFiles/window/window_controller.cpp b/Telegram/SourceFiles/window/window_controller.cpp index cde941da2..b9ea7fc30 100644 --- a/Telegram/SourceFiles/window/window_controller.cpp +++ b/Telegram/SourceFiles/window/window_controller.cpp @@ -480,6 +480,40 @@ auto Controller::openInMediaViewRequests() const return _openInMediaViewRequests.events(); } +void Controller::setDefaultFloatPlayerDelegate( + not_null delegate) { + _defaultFloatPlayerDelegate = delegate; + _replacementFloatPlayerDelegate = nullptr; + _floatPlayerDelegate = delegate; +} + +void Controller::replaceFloatPlayerDelegate( + not_null replacement) { + Expects(_defaultFloatPlayerDelegate != nullptr); + + _replacementFloatPlayerDelegate = replacement; + _floatPlayerDelegate = replacement; +} + +void Controller::restoreFloatPlayerDelegate( + not_null replacement) { + Expects(_defaultFloatPlayerDelegate != nullptr); + + if (_replacementFloatPlayerDelegate == replacement) { + _replacementFloatPlayerDelegate = nullptr; + _floatPlayerDelegate = _defaultFloatPlayerDelegate; + } +} + +auto Controller::floatPlayerDelegate() const -> FloatDelegate* { + return _floatPlayerDelegate.current(); +} + +auto Controller::floatPlayerDelegateValue() const +-> rpl::producer { + return _floatPlayerDelegate.value(); +} + rpl::lifetime &Controller::lifetime() { return _lifetime; } diff --git a/Telegram/SourceFiles/window/window_controller.h b/Telegram/SourceFiles/window/window_controller.h index 2f352560d..69693da24 100644 --- a/Telegram/SourceFiles/window/window_controller.h +++ b/Telegram/SourceFiles/window/window_controller.h @@ -20,6 +20,10 @@ namespace Media::View { struct OpenRequest; } // namespace Media::View +namespace Media::Player { +class FloatDelegate; +} // namespace Media::Player + namespace Window { class Controller final : public base::has_weak_ptr { @@ -114,9 +118,20 @@ public: [[nodiscard]] auto openInMediaViewRequests() const -> rpl::producer; - QPoint getPointForCallPanelCenter() const; + [[nodiscard]] QPoint getPointForCallPanelCenter() const; - rpl::lifetime &lifetime(); + using FloatDelegate = Media::Player::FloatDelegate; + void setDefaultFloatPlayerDelegate( + not_null delegate); + void replaceFloatPlayerDelegate( + not_null replacement); + void restoreFloatPlayerDelegate( + not_null replacement); + [[nodiscard]] FloatDelegate *floatPlayerDelegate() const; + [[nodiscard]] auto floatPlayerDelegateValue() const + -> rpl::producer; + + [[nodiscard]] rpl::lifetime &lifetime(); private: struct CreateArgs { @@ -154,6 +169,10 @@ private: rpl::event_stream _openInMediaViewRequests; + FloatDelegate *_defaultFloatPlayerDelegate = nullptr; + FloatDelegate *_replacementFloatPlayerDelegate = nullptr; + rpl::variable _floatPlayerDelegate = nullptr; + rpl::lifetime _accountLifetime; rpl::lifetime _lifetime; diff --git a/Telegram/SourceFiles/window/window_session_controller.cpp b/Telegram/SourceFiles/window/window_session_controller.cpp index b5361b861..72a2c5f0b 100644 --- a/Telegram/SourceFiles/window/window_session_controller.cpp +++ b/Telegram/SourceFiles/window/window_session_controller.cpp @@ -745,10 +745,6 @@ SessionController::SessionController( } }, _lifetime); - if (Media::Player::instance()->pauseGifByRoundVideo()) { - enableGifPauseReason(GifPauseReason::RoundPlaying); - } - session->changes().peerUpdates( Data::PeerUpdate::Flag::FullInfo ) | rpl::filter([=](const Data::PeerUpdate &update) {