From 038361a1e9d075284780915295d0910eb64f0a0e Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 8 May 2017 17:01:14 +0300
Subject: [PATCH] Fix overview items layout after message deletion.

---
 Telegram/SourceFiles/overviewwidget.cpp | 60 +++++++++++++++----------
 Telegram/SourceFiles/overviewwidget.h   |  4 +-
 2 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/Telegram/SourceFiles/overviewwidget.cpp b/Telegram/SourceFiles/overviewwidget.cpp
index 613d8a127..d64dd2232 100644
--- a/Telegram/SourceFiles/overviewwidget.cpp
+++ b/Telegram/SourceFiles/overviewwidget.cpp
@@ -1319,22 +1319,7 @@ int32 OverviewInner::resizeToWidth(int32 nwidth, int32 scrollTop, int32 minHeigh
 	_search->setGeometry(_rowsLeft, st::linksSearchTop, _rowWidth, _search->height());
 	_cancelSearch->moveToLeft(_rowsLeft + _rowWidth - _cancelSearch->width(), _search->y());
 
-	if (_type == OverviewPhotos || _type == OverviewVideos) {
-		for (int32 i = 0, l = _items.size(); i < l; ++i) {
-			_items.at(i)->resizeGetHeight(_rowWidth);
-		}
-		_height = countHeight();
-	} else {
-		bool resize = (_type == OverviewLinks);
-		if (resize) _height = 0;
-		for (int32 i = 0, l = _items.size(); i < l; ++i) {
-			int32 h = _items.at(i)->resizeGetHeight(_rowWidth);
-			if (resize) {
-				_items.at(i)->Get<Overview::Layout::Info>()->top = _height + (_reversed ? h : 0);
-				_height += h;
-			}
-		}
-	}
+	resizeItems();
 	recountMargins();
 
 	resize(_width, _marginTop + _height + _marginBottom);
@@ -1346,6 +1331,32 @@ int32 OverviewInner::resizeToWidth(int32 nwidth, int32 scrollTop, int32 minHeigh
     return scrollTop;
 }
 
+void OverviewInner::resizeItems() {
+	if (_type == OverviewPhotos || _type == OverviewVideos || _type == OverviewLinks) {
+		resizeAndRepositionItems();
+	} else {
+		for (auto i = 0, l = _items.size(); i != l; ++i) {
+			_items.at(i)->resizeGetHeight(_rowWidth);
+		}
+	}
+}
+
+void OverviewInner::resizeAndRepositionItems() {
+	if (_type == OverviewPhotos || _type == OverviewVideos) {
+		for (auto i = 0, l = _items.size(); i < l; ++i) {
+			_items.at(i)->resizeGetHeight(_rowWidth);
+		}
+		_height = countHeight();
+	} else {
+		_height = 0;
+		for (auto i = 0, l = _items.size(); i < l; ++i) {
+			auto h = _items.at(i)->resizeGetHeight(_rowWidth);
+			_items.at(i)->Get<Overview::Layout::Info>()->top = _height + (_reversed ? h : 0);
+			_height += h;
+		}
+	}
+}
+
 void OverviewInner::dropResizeIndex() {
 	_resizeIndex = -1;
 }
@@ -1751,6 +1762,8 @@ void OverviewInner::itemRemoved(HistoryItem *item) {
 		}
 		delete j.value();
 		_layoutItems.erase(j);
+
+		resizeAndRepositionItems();
 	}
 
 	if (_dragSelFrom == msgId || _dragSelTo == msgId) {
@@ -1798,16 +1811,15 @@ void OverviewInner::repaintItem(const HistoryItem *msg) {
 	}
 }
 
-int32 OverviewInner::countHeight() {
-	int32 result = _height;
+int OverviewInner::countHeight() {
 	if (_type == OverviewPhotos || _type == OverviewVideos) {
-		int32 count = _items.size();
-		int32 migratedFullCount = _migrated ? _migrated->overviewCount(_type) : 0;
-		int32 fullCount = migratedFullCount + _history->overviewCount(_type);
-		int32 rows = (count / _photosInRow) + ((count % _photosInRow) ? 1 : 0);
-		result = (_rowWidth + st::overviewPhotoSkip) * rows + st::overviewPhotoSkip;
+		auto count = _items.size();
+		auto migratedFullCount = _migrated ? _migrated->overviewCount(_type) : 0;
+		auto fullCount = migratedFullCount + _history->overviewCount(_type);
+		auto rows = (count / _photosInRow) + ((count % _photosInRow) ? 1 : 0);
+		return (_rowWidth + st::overviewPhotoSkip) * rows + st::overviewPhotoSkip;
 	}
-	return result;
+	return _height;
 }
 
 void OverviewInner::recountMargins() {
diff --git a/Telegram/SourceFiles/overviewwidget.h b/Telegram/SourceFiles/overviewwidget.h
index 20876fcc3..2bb213203 100644
--- a/Telegram/SourceFiles/overviewwidget.h
+++ b/Telegram/SourceFiles/overviewwidget.h
@@ -139,6 +139,8 @@ public slots:
 private:
 	void saveDocumentToFile(DocumentData *document);
 	void invalidateCache();
+	void resizeItems();
+	void resizeAndRepositionItems();
 
 	void itemRemoved(HistoryItem *item);
 	MsgId complexMsgId(const HistoryItem *item) const;
@@ -165,7 +167,7 @@ private:
 	void addSelectionRange(int32 selFrom, int32 selTo, History *history);
 
 	void recountMargins();
-	int32 countHeight();
+	int countHeight();
 
 	OverviewWidget *_overview;
 	Ui::ScrollArea *_scroll;