From db631acf802f91f14e4480077efbd5530e90398f Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 2 Apr 2019 14:29:28 +0400
Subject: [PATCH] Remove old animations code.

---
 Telegram/SourceFiles/core/application.cpp     |   7 +-
 .../media/clip/media_clip_reader.cpp          |  18 +-
 .../media/clip/media_clip_reader.h            |   9 +-
 Telegram/SourceFiles/ui/animation.cpp         | 120 --------
 Telegram/SourceFiles/ui/animation.h           | 279 ------------------
 5 files changed, 19 insertions(+), 414 deletions(-)

diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp
index f6ae4dacc..015e61d75 100644
--- a/Telegram/SourceFiles/core/application.cpp
+++ b/Telegram/SourceFiles/core/application.cpp
@@ -40,6 +40,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "media/audio/media_audio.h"
 #include "media/audio/media_audio_track.h"
 #include "media/player/media_player_instance.h"
+#include "media/clip/media_clip_reader.h" // For Media::Clip::Finish().
 #include "window/notifications_manager.h"
 #include "window/themes/window_theme.h"
 #include "window/window_lock_widgets.h"
@@ -112,8 +113,7 @@ Application::~Application() {
 	Shortcuts::Finish();
 
 	Ui::Emoji::Clear();
-
-	anim::stopManager();
+	Media::Clip::Finish();
 
 	stopWebLoadManager();
 	App::deinitMedia();
@@ -153,7 +153,6 @@ void Application::run() {
 	QCoreApplication::instance()->installTranslator(_translator.get());
 
 	style::startManager();
-	anim::startManager();
 	Ui::InitTextOptions();
 	Ui::Emoji::Init();
 	Media::Player::start(_audio.get());
@@ -841,7 +840,7 @@ QString Application::createInternalLinkFull(const QString &query) const {
 
 void Application::checkStartUrl() {
 	if (!cStartUrl().isEmpty() && !locked()) {
-		auto url = cStartUrl();
+		const auto url = cStartUrl();
 		cSetStartUrl(QString());
 		if (!openLocalUrl(url, {})) {
 			cSetStartUrl(url);
diff --git a/Telegram/SourceFiles/media/clip/media_clip_reader.cpp b/Telegram/SourceFiles/media/clip/media_clip_reader.cpp
index 1468ea7d6..21a352e62 100644
--- a/Telegram/SourceFiles/media/clip/media_clip_reader.cpp
+++ b/Telegram/SourceFiles/media/clip/media_clip_reader.cpp
@@ -187,10 +187,10 @@ void Reader::moveToNextWrite() const {
 	}
 }
 
-void Reader::callback(Reader *reader, int32 threadIndex, Notification notification) {
-	// check if reader is not deleted already
+void Reader::callback(Reader *reader, qint32 threadIndex, qint32 notification) {
+	// Check if reader is not deleted already
 	if (managers.size() > threadIndex && managers.at(threadIndex)->carries(reader) && reader->_callback) {
-		reader->_callback(notification);
+		reader->_callback(Notification(notification));
 	}
 }
 
@@ -598,7 +598,7 @@ private:
 
 };
 
-Manager::Manager(QThread *thread) : _processingInThread(0), _needReProcess(false) {
+Manager::Manager(QThread *thread) {
 	moveToThread(thread);
 	connect(thread, SIGNAL(started()), this, SLOT(process()));
 	connect(thread, SIGNAL(finished()), this, SLOT(finish()));
@@ -608,7 +608,11 @@ Manager::Manager(QThread *thread) : _processingInThread(0), _needReProcess(false
 	_timer.moveToThread(thread);
 	connect(&_timer, SIGNAL(timeout()), this, SLOT(process()));
 
-	anim::registerClipManager(this);
+	connect(
+		this,
+		&Manager::callback,
+		QCoreApplication::instance(),
+		&Reader::callback);
 }
 
 void Manager::append(Reader *reader, const FileLocation &location, const QByteArray &data) {
@@ -800,7 +804,7 @@ void Manager::process() {
 				i = _readers.erase(i);
 				continue;
 			} else if (state == ResultHandleStop) {
-				_processingInThread = 0;
+				_processingInThread = nullptr;
 				return;
 			}
 			ms = crl::now();
@@ -835,7 +839,7 @@ void Manager::process() {
 		_timer.start(minms - ms);
 	}
 
-	_processingInThread = 0;
+	_processingInThread = nullptr;
 }
 
 void Manager::finish() {
diff --git a/Telegram/SourceFiles/media/clip/media_clip_reader.h b/Telegram/SourceFiles/media/clip/media_clip_reader.h
index 8fe876350..48a7df1c9 100644
--- a/Telegram/SourceFiles/media/clip/media_clip_reader.h
+++ b/Telegram/SourceFiles/media/clip/media_clip_reader.h
@@ -52,7 +52,8 @@ public:
 	Reader(const QString &filepath, Callback &&callback, Mode mode = Mode::Gif, crl::time seekMs = 0);
 	Reader(not_null<DocumentData*> document, FullMsgId msgId, Callback &&callback, Mode mode = Mode::Gif, crl::time seekMs = 0);
 
-	static void callback(Reader *reader, int threadIndex, Notification notification); // reader can be deleted
+	// Reader can be already deleted.
+	static void callback(Reader *reader, qint32 threadIndex, qint32 notification);
 
 	void setAutoplay() {
 		_autoplay = true;
@@ -228,12 +229,12 @@ private:
 	};
 	ResultHandleState handleResult(ReaderPrivate *reader, ProcessResult result, crl::time ms);
 
-	typedef QMap<ReaderPrivate*, crl::time> Readers;
+	using Readers = QMap<ReaderPrivate*, crl::time>;
 	Readers _readers;
 
 	QTimer _timer;
-	QThread *_processingInThread;
-	bool _needReProcess;
+	QThread *_processingInThread = nullptr;
+	bool _needReProcess = false;
 
 };
 
diff --git a/Telegram/SourceFiles/ui/animation.cpp b/Telegram/SourceFiles/ui/animation.cpp
index 9428ce4d1..febd37450 100644
--- a/Telegram/SourceFiles/ui/animation.cpp
+++ b/Telegram/SourceFiles/ui/animation.cpp
@@ -26,7 +26,6 @@ ReaderPointer::~ReaderPointer() {
 
 namespace {
 
-AnimationManager *_manager = nullptr;
 bool AnimationsDisabled = false;
 
 } // namespace
@@ -80,34 +79,12 @@ transition easeOutQuint = [](const float64 &delta, const float64 &dt) {
 	return delta * (t2 * t2 * t + 1);
 };
 
-void startManager() {
-	stopManager();
-
-	_manager = new AnimationManager();
-}
-
-void stopManager() {
-	delete _manager;
-	_manager = nullptr;
-
-	Media::Clip::Finish();
-}
-
-void registerClipManager(not_null<Media::Clip::Manager*> manager) {
-	Expects(_manager != nullptr);
-
-	_manager->registerClip(manager);
-}
-
 bool Disabled() {
 	return AnimationsDisabled;
 }
 
 void SetDisabled(bool disabled) {
 	AnimationsDisabled = disabled;
-	if (disabled && _manager) {
-		_manager->step();
-	}
 }
 
 void DrawStaticLoading(
@@ -139,100 +116,3 @@ void DrawStaticLoading(
 }
 
 } // anim
-
-void BasicAnimation::start() {
-	if (!_manager) return;
-
-	_callbacks.start();
-	_manager->start(this);
-	_animating = true;
-}
-
-void BasicAnimation::stop() {
-	if (!_manager) return;
-
-	_animating = false;
-	_manager->stop(this);
-}
-
-AnimationManager::AnimationManager() : _timer(this) {
-	_timer.setSingleShot(false);
-	connect(&_timer, &QTimer::timeout, this, &AnimationManager::step);
-}
-
-void AnimationManager::start(BasicAnimation *obj) {
-	if (_iterating) {
-		_starting.insert(obj);
-		if (!_stopping.empty()) {
-			_stopping.erase(obj);
-		}
-	} else {
-		if (_objects.empty()) {
-			_timer.start(AnimationTimerDelta);
-		}
-		_objects.insert(obj);
-	}
-}
-
-void AnimationManager::stop(BasicAnimation *obj) {
-	if (_iterating) {
-		_stopping.insert(obj);
-		if (!_starting.empty()) {
-			_starting.erase(obj);
-		}
-	} else {
-		auto i = _objects.find(obj);
-		if (i != _objects.cend()) {
-			_objects.erase(i);
-			if (_objects.empty()) {
-				_timer.stop();
-			}
-		}
-	}
-}
-
-void AnimationManager::registerClip(not_null<Media::Clip::Manager*> clip) {
-	connect(
-		clip,
-		&Media::Clip::Manager::callback,
-		this,
-		&AnimationManager::clipCallback);
-}
-
-void AnimationManager::step() {
-	_iterating = true;
-	const auto ms = crl::now();
-	for (const auto object : _objects) {
-		if (!_stopping.contains(object)) {
-			object->step(ms, true);
-		}
-	}
-	_iterating = false;
-
-	if (!_starting.empty()) {
-		for (const auto object : _starting) {
-			_objects.emplace(object);
-		}
-		_starting.clear();
-	}
-	if (!_stopping.empty()) {
-		for (const auto object : _stopping) {
-			_objects.erase(object);
-		}
-		_stopping.clear();
-	}
-	if (_objects.empty()) {
-		_timer.stop();
-	}
-}
-
-void AnimationManager::clipCallback(
-		Media::Clip::Reader *reader,
-		qint32 threadIndex,
-		qint32 notification) {
-	Media::Clip::Reader::callback(
-		reader,
-		threadIndex,
-		Media::Clip::Notification(notification));
-}
-
diff --git a/Telegram/SourceFiles/ui/animation.h b/Telegram/SourceFiles/ui/animation.h
index 24fe812cc..bfc4acb4f 100644
--- a/Telegram/SourceFiles/ui/animation.h
+++ b/Telegram/SourceFiles/ui/animation.h
@@ -161,10 +161,6 @@ private:
 
 };
 
-void startManager();
-void stopManager();
-void registerClipManager(not_null<Media::Clip::Manager*> manager);
-
 TG_FORCE_INLINE int interpolate(int a, int b, float64 b_ratio) {
 	return qRound(a + float64(b - a) * b_ratio);
 }
@@ -427,282 +423,11 @@ void DrawStaticLoading(
 
 };
 
-class BasicAnimation;
-
-class AnimationImplementation {
-public:
-	virtual void start() {}
-	virtual void step(BasicAnimation *a, crl::time ms, bool timer) = 0;
-	virtual ~AnimationImplementation() {}
-
-};
-
-class AnimationCallbacks {
-public:
-	AnimationCallbacks(AnimationImplementation *implementation) : _implementation(implementation) {}
-	AnimationCallbacks(const AnimationCallbacks &other) = delete;
-	AnimationCallbacks &operator=(const AnimationCallbacks &other) = delete;
-	AnimationCallbacks(AnimationCallbacks &&other) : _implementation(other._implementation) {
-		other._implementation = nullptr;
-	}
-	AnimationCallbacks &operator=(AnimationCallbacks &&other) {
-		std::swap(_implementation, other._implementation);
-		return *this;
-	}
-
-	void start() { _implementation->start();  }
-	void step(BasicAnimation *a, crl::time ms, bool timer) { _implementation->step(a, ms, timer); }
-	~AnimationCallbacks() { delete base::take(_implementation); }
-
-private:
-	AnimationImplementation *_implementation;
-
-};
-
-class BasicAnimation {
-public:
-	BasicAnimation(AnimationCallbacks &&callbacks)
-		: _callbacks(std::move(callbacks))
-		, _animating(false) {
-	}
-
-	void start();
-	void stop();
-
-	void step(crl::time ms, bool timer = false) {
-		_callbacks.step(this, ms, timer);
-	}
-
-	void step() {
-		step(crl::now(), false);
-	}
-
-	bool animating() const {
-		return _animating;
-	}
-
-	~BasicAnimation() {
-		if (_animating) stop();
-	}
-
-private:
-	AnimationCallbacks _callbacks;
-	bool _animating;
-
-};
-
-template <typename Type>
-class AnimationCallbacksRelative : public AnimationImplementation {
-public:
-	typedef void (Type::*Method)(float64, bool);
-
-	AnimationCallbacksRelative(Type *obj, Method method) : _obj(obj), _method(method) {
-	}
-
-	void start() {
-		_started = crl::now();
-	}
-
-	void step(BasicAnimation *a, crl::time ms, bool timer) {
-		(_obj->*_method)(qMax(ms - _started, crl::time(0)), timer);
-	}
-
-private:
-	crl::time _started = 0;
-	Type *_obj = nullptr;
-	Method _method = nullptr;
-
-};
-
-template <typename Type>
-AnimationCallbacks animation(Type *obj, typename AnimationCallbacksRelative<Type>::Method method) {
-	return AnimationCallbacks(new AnimationCallbacksRelative<Type>(obj, method));
-}
-
-template <typename Type>
-class AnimationCallbacksAbsolute : public AnimationImplementation {
-public:
-	typedef void (Type::*Method)(crl::time, bool);
-
-	AnimationCallbacksAbsolute(Type *obj, Method method) : _obj(obj), _method(method) {
-	}
-
-	void step(BasicAnimation *a, crl::time ms, bool timer) {
-		(_obj->*_method)(ms, timer);
-	}
-
-private:
-	Type *_obj = nullptr;
-	Method _method = nullptr;
-
-};
-
-template <typename Type>
-AnimationCallbacks animation(Type *obj, typename AnimationCallbacksAbsolute<Type>::Method method) {
-	return AnimationCallbacks(new AnimationCallbacksAbsolute<Type>(obj, method));
-}
-
-template <typename Type, typename Param>
-class AnimationCallbacksRelativeWithParam : public AnimationImplementation {
-public:
-	typedef void (Type::*Method)(Param, float64, bool);
-
-	AnimationCallbacksRelativeWithParam(Param param, Type *obj, Method method) : _param(param), _obj(obj), _method(method) {
-	}
-
-	void start() {
-		_started = crl::now();
-	}
-
-	void step(BasicAnimation *a, crl::time ms, bool timer) {
-		(_obj->*_method)(_param, qMax(ms - _started, crl::time(0)), timer);
-	}
-
-private:
-	crl::time _started = 0;
-	Param _param;
-	Type *_obj = nullptr;
-	Method _method = nullptr;
-
-};
-
-template <typename Type, typename Param>
-AnimationCallbacks animation(Param param, Type *obj, typename AnimationCallbacksRelativeWithParam<Type, Param>::Method method) {
-	return AnimationCallbacks(new AnimationCallbacksRelativeWithParam<Type, Param>(param, obj, method));
-}
-
-template <typename Type, typename Param>
-class AnimationCallbacksAbsoluteWithParam : public AnimationImplementation {
-public:
-	typedef void (Type::*Method)(Param, crl::time, bool);
-
-	AnimationCallbacksAbsoluteWithParam(Param param, Type *obj, Method method) : _param(param), _obj(obj), _method(method) {
-	}
-
-	void step(BasicAnimation *a, crl::time ms, bool timer) {
-		(_obj->*_method)(_param, ms, timer);
-	}
-
-private:
-	Param _param;
-	Type *_obj = nullptr;
-	Method _method = nullptr;
-
-};
-
-template <typename Type, typename Param>
-AnimationCallbacks animation(Param param, Type *obj, typename AnimationCallbacksAbsoluteWithParam<Type, Param>::Method method) {
-	return AnimationCallbacks(new AnimationCallbacksAbsoluteWithParam<Type, Param>(param, obj, method));
-}
-
-class Animation {
-public:
-	void step(crl::time ms) {
-		if (_data) {
-			_data->a_animation.step(ms);
-			if (_data && !_data->a_animation.animating()) {
-				_data.reset();
-			}
-		}
-	}
-
-	bool animating() const {
-		if (_data) {
-			if (_data->a_animation.animating()) {
-				return true;
-			}
-			_data.reset();
-		}
-		return false;
-	}
-	bool animating(crl::time ms) {
-		step(ms);
-		return animating();
-	}
-
-	float64 current() const {
-		Assert(_data != nullptr);
-		return _data->value.current();
-	}
-	float64 current(float64 def) const {
-		return animating() ? current() : def;
-	}
-	float64 current(crl::time ms, float64 def) {
-		return animating(ms) ? current() : def;
-	}
-
-	static constexpr auto kLongAnimationDuration = 1000;
-
-	template <typename Lambda>
-	void start(Lambda &&updateCallback, float64 from, float64 to, float64 duration, anim::transition transition = anim::linear) {
-		auto isLong = (duration >= kLongAnimationDuration);
-		if (_data) {
-			if (!isLong) {
-				_data->pause.restart();
-			}
-		} else {
-			_data = std::make_unique<Data>(from, std::forward<Lambda>(updateCallback));
-		}
-		if (isLong) {
-			_data->pause.release();
-		}
-		_data->value.start(to);
-		_data->duration = duration;
-		_data->transition = transition;
-		_data->a_animation.start();
-	}
-
-	void finish() {
-		if (_data) {
-			_data->value.finish();
-			_data->a_animation.stop();
-			_data.reset();
-		}
-	}
-
-private:
-	struct Data {
-		template <typename Lambda>
-		Data(float64 from, Lambda updateCallback)
-		: value(from, from)
-		, a_animation(animation(this, &Data::step))
-		, updateCallback(std::move(updateCallback)) {
-		}
-		void step(float64 ms, bool timer) {
-			const auto callback = updateCallback;
-			const auto dt = (ms >= duration || anim::Disabled())
-				? 1.
-				: (ms / duration);
-			if (dt >= 1) {
-				value.finish();
-				a_animation.stop();
-				pause.release();
-			} else {
-				value.update(dt, transition);
-			}
-			callback();
-		}
-
-		anim::value value;
-		BasicAnimation a_animation;
-		Fn<void()> updateCallback;
-		float64 duration = 0.;
-		anim::transition transition = anim::linear;
-		MTP::PauseHolder pause;
-	};
-	mutable std::unique_ptr<Data> _data;
-
-};
-
 class AnimationManager : public QObject {
 public:
 	AnimationManager();
 
-	void start(BasicAnimation *obj);
-	void stop(BasicAnimation *obj);
-
 	void registerClip(not_null<Media::Clip::Manager*> clip);
-	void step();
 
 private:
 	void clipCallback(
@@ -710,8 +435,4 @@ private:
 		qint32 threadIndex,
 		qint32 notification);
 
-	base::flat_set<BasicAnimation*> _objects, _starting, _stopping;
-	QTimer _timer;
-	bool _iterating = false;
-
 };