Stop timer for infinite radial animation when animations are disabled

This commit is contained in:
Ilya Fedin 2024-01-13 04:15:42 +04:00 committed by John Preston
parent 564e354c1d
commit cb1a041ca3
2 changed files with 21 additions and 5 deletions

View file

@ -112,13 +112,25 @@ RadialState RadialAnimation::computeState() const {
return { _opacity, from, length }; return { _opacity, from, length };
} }
void InfiniteRadialAnimation::init() {
anim::Disables() | rpl::filter([=] {
return animating();
}) | rpl::start_with_next([=](bool disabled) {
if (!disabled && !_animation.animating()) {
_animation.start();
} else if (disabled && _animation.animating()) {
_animation.stop();
}
}, _lifetime);
}
void InfiniteRadialAnimation::start(crl::time skip) { void InfiniteRadialAnimation::start(crl::time skip) {
const auto now = crl::now(); if (!animating()) {
if (_workFinished <= now && (_workFinished || !_workStarted)) { const auto now = crl::now();
_workStarted = std::max(now + _st.sineDuration - skip, crl::time(1)); _workStarted = std::max(now + _st.sineDuration - skip, crl::time(1));
_workFinished = 0; _workFinished = 0;
} }
if (!_animation.animating()) { if (!anim::Disabled() && !_animation.animating()) {
_animation.start(); _animation.start();
} }
} }
@ -213,7 +225,7 @@ RadialState InfiniteRadialAnimation::computeState() {
const auto now = crl::now(); const auto now = crl::now();
const auto linear = kFullArcLength const auto linear = kFullArcLength
- int(((now * kFullArcLength) / _st.linearPeriod) % kFullArcLength); - int(((now * kFullArcLength) / _st.linearPeriod) % kFullArcLength);
if (!_workStarted || (_workFinished && _workFinished <= now)) { if (!animating()) {
const auto shown = 0.; const auto shown = 0.;
_animation.stop(); _animation.stop();
return { return {

View file

@ -73,7 +73,7 @@ public:
const style::InfiniteRadialAnimation &st); const style::InfiniteRadialAnimation &st);
[[nodiscard]] bool animating() const { [[nodiscard]] bool animating() const {
return _animation.animating(); return _workStarted && (!_workFinished || _workFinished > crl::now());
} }
void start(crl::time skip = 0); void start(crl::time skip = 0);
@ -101,10 +101,13 @@ public:
[[nodiscard]] RadialState computeState(); [[nodiscard]] RadialState computeState();
private: private:
void init();
const style::InfiniteRadialAnimation &_st; const style::InfiniteRadialAnimation &_st;
crl::time _workStarted = 0; crl::time _workStarted = 0;
crl::time _workFinished = 0; crl::time _workFinished = 0;
Ui::Animations::Basic _animation; Ui::Animations::Basic _animation;
rpl::lifetime _lifetime;
}; };
@ -114,6 +117,7 @@ inline InfiniteRadialAnimation::InfiniteRadialAnimation(
const style::InfiniteRadialAnimation &st) const style::InfiniteRadialAnimation &st)
: _st(st) : _st(st)
, _animation(std::forward<Callback>(callback)) { , _animation(std::forward<Callback>(callback)) {
init();
} }
} // namespace Ui } // namespace Ui