tdesktop/Telegram/SourceFiles/ui/effects/animations.cpp

155 lines
3.0 KiB
C++
Raw Normal View History

2019-02-04 13:34:50 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/effects/animations.h"
#include "core/application.h"
2019-03-07 13:23:19 +00:00
#include "core/sandbox.h"
2019-02-04 13:34:50 +00:00
namespace Ui {
namespace Animations {
namespace {
constexpr auto kAnimationTimeout = crl::time(1000) / 60;
constexpr auto kIgnoreUpdatesTimeout = crl::time(4);
} // namespace
void Basic::start() {
2019-03-11 07:11:02 +00:00
if (animating()) {
restart();
} else {
2019-02-04 13:34:50 +00:00
Core::App().animationManager().start(this);
}
}
void Basic::stop() {
if (animating()) {
Core::App().animationManager().stop(this);
}
}
2019-03-11 07:11:02 +00:00
void Basic::restart() {
Expects(_started >= 0);
_started = crl::now();
}
void Basic::markStarted() {
Expects(_started < 0);
_started = crl::now();
}
void Basic::markStopped() {
Expects(_started >= 0);
_started = -1;
}
2019-03-07 13:23:19 +00:00
Manager::Manager() {
Core::Sandbox::Instance().widgetUpdateRequests(
) | rpl::start_with_next([=] {
update();
}, _lifetime);
}
2019-02-04 13:34:50 +00:00
void Manager::start(not_null<Basic*> animation) {
if (_updating) {
2019-03-11 07:11:02 +00:00
_starting.emplace_back(animation.get());
2019-02-04 13:34:50 +00:00
} else {
if (empty(_active)) {
updateQueued();
}
2019-03-11 07:11:02 +00:00
_active.emplace_back(animation.get());
2019-02-04 13:34:50 +00:00
}
}
void Manager::stop(not_null<Basic*> animation) {
2019-03-11 07:11:02 +00:00
if (empty(_active) && empty(_starting)) {
2019-02-04 13:34:50 +00:00
return;
}
2019-03-11 07:11:02 +00:00
const auto value = animation.get();
const auto proj = &ActiveBasicPointer::get;
auto &list = _updating ? _starting : _active;
list.erase(ranges::remove(list, value, proj), end(list));
2019-02-04 13:34:50 +00:00
if (_updating) {
2019-03-11 07:11:02 +00:00
const auto i = ranges::find(_active, value, proj);
2019-02-04 13:34:50 +00:00
if (i != end(_active)) {
*i = nullptr;
}
2019-03-11 07:11:02 +00:00
} else if (empty(_active)) {
2019-02-04 13:34:50 +00:00
stopTimer();
}
}
void Manager::update() {
if (_active.empty() || _updating || _scheduled) {
return;
}
const auto now = crl::now();
if (_lastUpdateTime + kIgnoreUpdatesTimeout >= now) {
return;
}
schedule();
_updating = true;
const auto guard = gsl::finally([&] { _updating = false; });
_lastUpdateTime = now;
2019-03-11 07:11:02 +00:00
const auto isFinished = [&](const ActiveBasicPointer &element) {
return !element.call(now);
};
_active.erase(ranges::remove_if(_active, isFinished), end(_active));
2019-02-04 13:34:50 +00:00
if (!empty(_starting)) {
2019-03-11 07:11:02 +00:00
_active.insert(
end(_active),
std::make_move_iterator(begin(_starting)),
std::make_move_iterator(end(_starting)));
_starting.clear();
2019-02-04 13:34:50 +00:00
}
}
void Manager::updateQueued() {
InvokeQueued(this, [=] { update(); });
}
void Manager::schedule() {
if (_scheduled) {
return;
}
stopTimer();
_scheduled = true;
Ui::PostponeCall([=] {
_scheduled = false;
const auto next = _lastUpdateTime + kAnimationTimeout;
const auto now = crl::now();
if (now < next) {
_timerId = startTimer(next - now, Qt::PreciseTimer);
} else {
updateQueued();
}
});
}
void Manager::stopTimer() {
if (_timerId) {
killTimer(base::take(_timerId));
}
}
void Manager::timerEvent(QTimerEvent *e) {
update();
}
} // namespace Animations
} // namespace Ui