From 4a4fb00ec02a36301af26bf5afab7fcd9015b2ee Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 25 Dec 2015 18:21:18 +0300
Subject: [PATCH] fixed animation shutdown and some radial progresses

---
 Telegram/SourceFiles/gui/animation.cpp | 44 +++++++++++++++-----------
 Telegram/SourceFiles/gui/animation.h   |  3 ++
 Telegram/SourceFiles/history.cpp       |  4 +--
 Telegram/SourceFiles/layout.cpp        |  2 +-
 4 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/Telegram/SourceFiles/gui/animation.cpp b/Telegram/SourceFiles/gui/animation.cpp
index f1a5fe498..1295534ad 100644
--- a/Telegram/SourceFiles/gui/animation.cpp
+++ b/Telegram/SourceFiles/gui/animation.cpp
@@ -500,14 +500,14 @@ public:
 				if (_frameMs + _currentFrameDelay < frameMs) {
 					_currentFrameDelay = int32(frameMs - _frameMs);
 				}
-				_frameMs = frameMs;
 				if (duration == AV_NOPTS_VALUE) {
 					_nextFrameDelay = 0;
 				} else {
 					_nextFrameDelay = (duration * 1000LL * _fmtContext->streams[_streamId]->time_base.num) / _fmtContext->streams[_streamId]->time_base.den;
 				}
+                _frameMs = frameMs;
 
-				av_frame_unref(_frame);
+                av_frame_unref(_frame);
 				return true;
 			}
 
@@ -821,6 +821,7 @@ private:
 ClipReadManager::ClipReadManager(QThread *thread) : _processingInThread(0), _needReProcess(false) {
 	moveToThread(thread);
 	connect(thread, SIGNAL(started()), this, SLOT(process()));
+    connect(thread, SIGNAL(finished()), this, SLOT(finish()));
 	connect(this, SIGNAL(processDelayed()), this, SLOT(process()), Qt::QueuedConnection);
 
 	_timer.setSingleShot(true);
@@ -922,7 +923,7 @@ void ClipReadManager::process() {
 		return;
 	}
 
-	_timer.stop();
+    _timer.stop();
 	_processingInThread = thread();
 
 	uint64 ms = getms(), minms = ms + 86400 * 1000ULL;
@@ -977,19 +978,26 @@ void ClipReadManager::process() {
 	_processingInThread = 0;
 }
 
-ClipReadManager::~ClipReadManager() {
-	{
-		QMutexLocker lock(&_readerPointersMutex);
-		for (ReaderPointers::iterator i = _readerPointers.begin(), e = _readerPointers.end(); i != e; ++i) {
-			if (i.value()) {
-				i.key()->_private = 0;
-			}
-		}
-		_readerPointers.clear();
-
-		for (Readers::iterator i = _readers.begin(), e = _readers.end(); i != e; ++i) {
-			delete i.key();
-		}
-		_readers.clear();
-	}
+void ClipReadManager::finish() 	{
+    _timer.stop();
+    clear();
+}
+
+void ClipReadManager::clear() {
+    QMutexLocker lock(&_readerPointersMutex);
+    for (ReaderPointers::iterator i = _readerPointers.begin(), e = _readerPointers.end(); i != e; ++i) {
+        if (i.value()) {
+            i.key()->_private = 0;
+        }
+    }
+    _readerPointers.clear();
+
+    for (Readers::iterator i = _readers.begin(), e = _readers.end(); i != e; ++i) {
+        delete i.key();
+    }
+    _readers.clear();
+}
+
+ClipReadManager::~ClipReadManager() {
+    clear();
 }
diff --git a/Telegram/SourceFiles/gui/animation.h b/Telegram/SourceFiles/gui/animation.h
index ca57cf765..59f7f246d 100644
--- a/Telegram/SourceFiles/gui/animation.h
+++ b/Telegram/SourceFiles/gui/animation.h
@@ -556,9 +556,12 @@ signals:
 public slots:
 
 	void process();
+    void finish();
 
 private:
 
+    void clear();
+
 	QAtomicInt _loadLevel;
 	typedef QMap<ClipReader*, ClipReaderPrivate*> ReaderPointers;
 	ReaderPointers _readerPointers;
diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp
index b6b9cf8a7..31f29d65d 100644
--- a/Telegram/SourceFiles/history.cpp
+++ b/Telegram/SourceFiles/history.cpp
@@ -4074,7 +4074,7 @@ void HistoryDocument::draw(Painter &p, const HistoryItem *parent, const QRect &r
 		}
 
 		if (radial || (!loaded && !_data->loading())) {
-			float64 radialOpacity = (radial && loaded) ? _animation->radial.opacity() : 1;
+            float64 radialOpacity = (radial && loaded && !_data->uploading()) ? _animation->radial.opacity() : 1;
 			QRect inner(rthumb.x() + (rthumb.width() - st::msgFileSize) / 2, rthumb.y() + (rthumb.height() - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize);
 			p.setPen(Qt::NoPen);
 			if (selected) {
@@ -4476,7 +4476,7 @@ void HistoryGif::draw(Painter &p, const HistoryItem *parent, const QRect &r, boo
 	}
 
 	if (radial || (!_gif && !loaded && !_data->loading()) || (_gif == BadClipReader)) {
-		float64 radialOpacity = radial ? _animation->radial.opacity() : 1;
+        float64 radialOpacity = (radial && loaded && !_data->uploading()) ? _animation->radial.opacity() : 1;
 		QRect inner(rthumb.x() + (rthumb.width() - st::msgFileSize) / 2, rthumb.y() + (rthumb.height() - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize);
 		p.setPen(Qt::NoPen);
 		if (selected) {
diff --git a/Telegram/SourceFiles/layout.cpp b/Telegram/SourceFiles/layout.cpp
index 8f59309dc..235a83d6e 100644
--- a/Telegram/SourceFiles/layout.cpp
+++ b/Telegram/SourceFiles/layout.cpp
@@ -855,7 +855,7 @@ void LayoutOverviewDocument::paint(Painter &p, const QRect &clip, uint32 selecti
 			if (radial || (!loaded && !_data->loading())) {
 				QRect inner(rthumb.x() + (rthumb.width() - st::msgFileSize) / 2, rthumb.y() + (rthumb.height() - st::msgFileSize) / 2, st::msgFileSize, st::msgFileSize);
 				if (clip.intersects(inner)) {
-					float64 radialOpacity = (radial && loaded) ? _radial->opacity() : 1;
+                    float64 radialOpacity = (radial && loaded && !_data->uploading()) ? _radial->opacity() : 1;
 					p.setPen(Qt::NoPen);
 					if (selected) {
 						p.setBrush(st::msgDateImgBgSelected);