diff --git a/Telegram/SourceFiles/history/media/history_media_gif.cpp b/Telegram/SourceFiles/history/media/history_media_gif.cpp index c56a406b3..7f28b9055 100644 --- a/Telegram/SourceFiles/history/media/history_media_gif.cpp +++ b/Telegram/SourceFiles/history/media/history_media_gif.cpp @@ -319,7 +319,7 @@ void HistoryGif::draw(Painter &p, const QRect &r, TextSelection selection, crl:: p.drawPixmap(rthumb.topLeft(), reader->current(_thumbw, _thumbh, usew, painth, roundRadius, roundCorners, paused ? 0 : ms)); if (const auto playback = videoPlayback()) { - const auto value = playback->value(ms); + const auto value = playback->value(); if (value > 0.) { auto pen = st::historyVideoMessageProgressFg->p; auto was = p.pen(); diff --git a/Telegram/SourceFiles/media/player/media_player_float.cpp b/Telegram/SourceFiles/media/player/media_player_float.cpp index 8b0d57bec..23d62dd7f 100644 --- a/Telegram/SourceFiles/media/player/media_player_float.cpp +++ b/Telegram/SourceFiles/media/player/media_player_float.cpp @@ -174,7 +174,7 @@ void Float::paintEvent(QPaintEvent *e) { p.drawImage(inner.topLeft(), _frame); const auto playback = getPlayback(); - const auto progress = playback ? playback->value(crl::now()) : 1.; + const auto progress = playback ? playback->value() : 1.; if (progress > 0.) { auto pen = st::historyVideoMessageProgressFg->p; auto was = p.pen(); diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 4ac92abf6..bf55844a2 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -185,7 +185,7 @@ OverlayWidget::OverlayWidget() , _docCancel(this, lang(lng_cancel), st::mediaviewFileLink) , _radial(animation(this, &OverlayWidget::step_radial)) , _lastAction(-st::mediaviewDeltaFromLastAction, -st::mediaviewDeltaFromLastAction) -, _a_state(animation(this, &OverlayWidget::step_state)) +, _a_state([=](float64 now) { step_state(now); }) , _dropdown(this, st::mediaviewDropdownMenu) , _dropdownShowTimer(this) { subscribe(Lang::Current().updated(), [this] { refreshLang(); }); @@ -605,9 +605,9 @@ auto OverlayWidget::computeOverviewType() const return std::nullopt; } -void OverlayWidget::step_state(crl::time ms, bool timer) { +void OverlayWidget::step_state(crl::time now) { if (anim::Disabled()) { - ms += st::mediaviewShowDuration + st::mediaviewHideDuration; + now += st::mediaviewShowDuration + st::mediaviewHideDuration; } bool result = false; for (auto i = _animations.begin(); i != _animations.end();) { @@ -624,7 +624,7 @@ void OverlayWidget::step_state(crl::time ms, bool timer) { case OverMore: update(_moreNav); break; default: break; } - float64 dt = float64(ms - start) / st::mediaviewFadeDuration; + const auto dt = float64(now - start) / st::mediaviewFadeDuration; if (dt >= 1) { _animOpacities.remove(i.key()); i = _animations.erase(i); @@ -634,7 +634,7 @@ void OverlayWidget::step_state(crl::time ms, bool timer) { } } if (_controlsState == ControlsShowing || _controlsState == ControlsHiding) { - float64 dt = float64(ms - _controlsAnimStarted) / (_controlsState == ControlsShowing ? st::mediaviewShowDuration : st::mediaviewHideDuration); + float64 dt = float64(now - _controlsAnimStarted) / (_controlsState == ControlsShowing ? st::mediaviewShowDuration : st::mediaviewHideDuration); if (dt >= 1) { a_cOpacity.finish(); _controlsState = (_controlsState == ControlsShowing ? ControlsShown : ControlsHidden); diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.h b/Telegram/SourceFiles/media/view/media_view_overlay_widget.h index 32773305e..1cd62baab 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.h +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rp_widget.h" #include "ui/widgets/dropdown_menu.h" +#include "ui/effects/animations.h" #include "ui/effects/radial_animation.h" #include "data/data_shared_media.h" #include "data/data_user_photos.h" @@ -263,7 +264,7 @@ private: void updateHeader(); void snapXY(); - void step_state(crl::time ms, bool timer); + void step_state(crl::time ms); void step_radial(crl::time ms, bool timer); void step_waiting(crl::time ms, bool timer); @@ -385,7 +386,7 @@ private: QPoint _lastAction, _lastMouseMovePos; bool _ignoringDropdown = false; - BasicAnimation _a_state; + Ui::Animations::Basic _a_state; enum ControlsState { ControlsShowing, diff --git a/Telegram/SourceFiles/media/view/media_view_playback_progress.cpp b/Telegram/SourceFiles/media/view/media_view_playback_progress.cpp index 66c8836bc..ad8aeceff 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_progress.cpp +++ b/Telegram/SourceFiles/media/view/media_view_playback_progress.cpp @@ -19,8 +19,8 @@ constexpr auto kPlaybackAnimationDurationMs = crl::time(200); } // namespace PlaybackProgress::PlaybackProgress() -: _a_value(animation(this, &PlaybackProgress::step_value)) -, _a_receivedTill(animation(this, &PlaybackProgress::step_receivedTill)) { +: _a_value([=](float64 ms) { step_value(ms); }) +, _a_receivedTill([=](float64 ms) { step_receivedTill(ms); }) { } void PlaybackProgress::updateState(const Player::TrackState &state) { @@ -83,11 +83,6 @@ float64 PlaybackProgress::value() const { return qMin(a_value.current(), 1.); } -float64 PlaybackProgress::value(crl::time ms) { - _a_value.step(ms); - return value(); -} - void PlaybackProgress::setValue(float64 value, bool animated) { if (animated) { a_value.start(value); @@ -114,30 +109,32 @@ void PlaybackProgress::setReceivedTill(float64 value) { emitUpdatedValue(); } -void PlaybackProgress::step_value(float64 ms, bool timer) { - auto dt = anim::Disabled() ? 1. : (ms / kPlaybackAnimationDurationMs); +void PlaybackProgress::step_value(float64 now) { + const auto time = (now - _a_value.started()); + const auto dt = anim::Disabled() + ? 1. + : (time / kPlaybackAnimationDurationMs); if (dt >= 1.) { _a_value.stop(); a_value.finish(); } else { a_value.update(dt, anim::linear); } - if (timer) { - emitUpdatedValue(); - } + emitUpdatedValue(); } -void PlaybackProgress::step_receivedTill(float64 ms, bool timer) { - auto dt = anim::Disabled() ? 1. : (ms / kPlaybackAnimationDurationMs); +void PlaybackProgress::step_receivedTill(float64 now) { + const auto time = now - _a_receivedTill.started(); + const auto dt = anim::Disabled() + ? 1. + : (time / kPlaybackAnimationDurationMs); if (dt >= 1.) { _a_receivedTill.stop(); a_receivedTill.finish(); } else { a_receivedTill.update(dt, anim::linear); } - if (timer) { - emitUpdatedValue(); - } + emitUpdatedValue(); } void PlaybackProgress::emitUpdatedValue() { diff --git a/Telegram/SourceFiles/media/view/media_view_playback_progress.h b/Telegram/SourceFiles/media/view/media_view_playback_progress.h index 6692797ac..b5c1963a9 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_progress.h +++ b/Telegram/SourceFiles/media/view/media_view_playback_progress.h @@ -28,22 +28,21 @@ public: } void setValue(float64 value, bool animated); float64 value() const; - float64 value(crl::time ms); void updateState(const Player::TrackState &state); void updateLoadingState(float64 progress); private: - void step_value(float64 ms, bool timer); - void step_receivedTill(float64 ms, bool timer); + void step_value(float64 now); + void step_receivedTill(float64 now); void setReceivedTill(float64 value); void emitUpdatedValue(); // This can animate for a very long time (like in music playing), - // so it should be a BasicAnimation, not an Animation, because - // Animation pauses mtproto responses/updates handling while playing. + // so it should be a Basic, not a Simple animation, because + // Simple-s pauses mtproto responses/updates handling while playing. anim::value a_value, a_receivedTill; - BasicAnimation _a_value, _a_receivedTill; + Ui::Animations::Basic _a_value, _a_receivedTill; Fn _valueChanged; bool _inLoadingState = false; diff --git a/Telegram/SourceFiles/ui/effects/animations.h b/Telegram/SourceFiles/ui/effects/animations.h index 26fba2e57..440069fde 100644 --- a/Telegram/SourceFiles/ui/effects/animations.h +++ b/Telegram/SourceFiles/ui/effects/animations.h @@ -25,6 +25,7 @@ public: void start(); void stop(); + [[nodiscard]] crl::time started() const; [[nodiscard]] bool animating() const; ~Basic(); @@ -159,13 +160,17 @@ inline void Basic::init(Callback &&callback) { _callback = Prepare(std::forward(callback)); } +TG_FORCE_INLINE crl::time Basic::started() const { + return _started; +} + TG_FORCE_INLINE bool Basic::animating() const { return (_started >= 0); } TG_FORCE_INLINE bool Basic::call(crl::time now) const { const auto onstack = _callback; - return onstack(now - _started); + return onstack(now); } inline Basic::~Basic() { @@ -217,7 +222,8 @@ inline void Simple::start( _data->animation.init([ that = _data.get(), callback = Prepare(std::forward(callback)) - ](crl::time time) { + ](crl::time now) { + const auto time = (now - that->animation.started()); const auto finished = (time >= that->duration); const auto progress = finished ? that->delta @@ -243,7 +249,7 @@ inline void Simple::start( } inline void Simple::prepare(float64 from, crl::time duration) { - const auto isLong = (duration >= kLongAnimationDuration); + const auto isLong = (duration > kLongAnimationDuration); if (!_data) { _data = std::make_unique(from); } else if (!isLong) { diff --git a/Telegram/SourceFiles/ui/effects/fade_animation.cpp b/Telegram/SourceFiles/ui/effects/fade_animation.cpp index f56773de3..4bc81fb96 100644 --- a/Telegram/SourceFiles/ui/effects/fade_animation.cpp +++ b/Telegram/SourceFiles/ui/effects/fade_animation.cpp @@ -23,7 +23,7 @@ bool FadeAnimation::paint(Painter &p) { if (_cache.isNull()) return false; const auto cache = _cache; - auto opacity = _animation.current(crl::now(), _visible ? 1. : 0.); + auto opacity = _animation.value(_visible ? 1. : 0.); p.setOpacity(opacity); if (_scale < 1.) { PainterHighQualityEnabler hq(p); @@ -105,7 +105,7 @@ void FadeAnimation::hide() { } void FadeAnimation::stopAnimation() { - _animation.finish(); + _animation.stop(); if (!_cache.isNull()) { _cache = QPixmap(); if (_finishedCallback) { @@ -149,7 +149,7 @@ void FadeAnimation::updateCallback() { if (_animation.animating()) { _widget->update(); if (_updatedCallback) { - _updatedCallback(_animation.current(_visible ? 1. : 0.)); + _updatedCallback(_animation.value(_visible ? 1. : 0.)); } } else { stopAnimation(); diff --git a/Telegram/SourceFiles/ui/effects/fade_animation.h b/Telegram/SourceFiles/ui/effects/fade_animation.h index 3e815d13f..96afd5c47 100644 --- a/Telegram/SourceFiles/ui/effects/fade_animation.h +++ b/Telegram/SourceFiles/ui/effects/fade_animation.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_widgets.h" #include "ui/rp_widget.h" +#include "ui/effects/animations.h" namespace Ui { @@ -52,7 +53,7 @@ private: TWidget *_widget = nullptr; float64 _scale = 1.; - Animation _animation; + Ui::Animations::Simple _animation; QSize _size; QPixmap _cache; bool _visible = false; diff --git a/Telegram/SourceFiles/ui/widgets/buttons.cpp b/Telegram/SourceFiles/ui/widgets/buttons.cpp index 9e16acfa4..883e19e61 100644 --- a/Telegram/SourceFiles/ui/widgets/buttons.cpp +++ b/Telegram/SourceFiles/ui/widgets/buttons.cpp @@ -594,7 +594,7 @@ void CrossButton::paintEvent(QPaintEvent *e) { auto loading = 0.; if (_a_loading.animating()) { - const auto duration = (ms - _loadingStartMs); + const auto duration = (ms - _a_loading.started()); if (stopLoadingAnimation(duration)) { _a_loading.stop(); } else if (anim::Disabled()) { @@ -627,7 +627,7 @@ bool CrossButton::stopLoadingAnimation(crl::time duration) { if (!_loadingStopMs) { return false; } - const auto stopPeriod = (_loadingStopMs - _loadingStartMs) + const auto stopPeriod = (_loadingStopMs - _a_loading.started()) / _st.loadingPeriod; const auto currentPeriod = duration / _st.loadingPeriod; if (currentPeriod != stopPeriod) { @@ -641,12 +641,11 @@ void CrossButton::setLoadingAnimation(bool enabled) { if (enabled) { _loadingStopMs = 0; if (!_a_loading.animating()) { - _loadingStartMs = crl::now(); _a_loading.start(); } } else if (_a_loading.animating()) { _loadingStopMs = crl::now(); - if (!((_loadingStopMs - _loadingStartMs) % _st.loadingPeriod)) { + if (!((_loadingStopMs - _a_loading.started()) % _st.loadingPeriod)) { _a_loading.stop(); } } diff --git a/Telegram/SourceFiles/ui/widgets/buttons.h b/Telegram/SourceFiles/ui/widgets/buttons.h index fd0bec44f..ab04ba799 100644 --- a/Telegram/SourceFiles/ui/widgets/buttons.h +++ b/Telegram/SourceFiles/ui/widgets/buttons.h @@ -244,7 +244,6 @@ private: bool _shown = false; Ui::Animations::Simple _a_show; - crl::time _loadingStartMs = 0; crl::time _loadingStopMs = 0; Ui::Animations::Basic _a_loading; diff --git a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp index 50304f4a8..fb3ae4506 100644 --- a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp +++ b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp @@ -173,9 +173,8 @@ void FilledSlider::paintEvent(QPaintEvent *e) { p.setPen(Qt::NoPen); const auto masterOpacity = fadeOpacity(); - const auto ms = crl::now(); const auto disabled = isDisabled(); - const auto over = getCurrentOverFactor(ms); + const auto over = getCurrentOverFactor(); const auto lineWidth = _st.lineWidth + ((_st.fullWidth - _st.lineWidth) * over); const auto lineWidthRounded = qFloor(lineWidth); const auto lineWidthPartial = lineWidth - lineWidthRounded; @@ -229,10 +228,9 @@ void MediaSlider::paintEvent(QPaintEvent *e) { p.setOpacity(fadeOpacity()); const auto horizontal = isHorizontal(); - const auto ms = crl::now(); const auto radius = _st.width / 2; const auto disabled = isDisabled(); - const auto over = getCurrentOverFactor(ms); + const auto over = getCurrentOverFactor(); const auto seekRect = getSeekRect(); // invert colors and value for vertical diff --git a/Telegram/SourceFiles/ui/widgets/continuous_sliders.h b/Telegram/SourceFiles/ui/widgets/continuous_sliders.h index 55fce2e1f..1a7073796 100644 --- a/Telegram/SourceFiles/ui/widgets/continuous_sliders.h +++ b/Telegram/SourceFiles/ui/widgets/continuous_sliders.h @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "styles/style_widgets.h" +#include "ui/effects/animations.h" #include "ui/rp_widget.h" namespace base { @@ -70,8 +71,8 @@ protected: float64 getCurrentReceivedTill() const { return _receivedTill; } - float64 getCurrentOverFactor(crl::time ms) { - return _disabled ? 0. : _a_over.current(ms, _over ? 1. : 0.); + float64 getCurrentOverFactor() { + return _disabled ? 0. : _a_over.value(_over ? 1. : 0.); } Direction getDirection() const { return _direction; @@ -103,7 +104,7 @@ private: Fn _changeFinishedCallback; bool _over = false; - Animation _a_over; + Ui::Animations::Simple _a_over; float64 _value = 0.; float64 _receivedTill = 0.;