From 03bbb2269d949135e0c316a7045e2b8f15296d66 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Mon, 11 Apr 2016 11:43:40 +0400
Subject: [PATCH] Info button replaced by userpic in single column layout.
 Icons for file inline results never display a download icon. Using
 !lastMsg->detached() to detect that all new messages are loaded.

---
 Telegram/Resources/lang.strings                 |  5 ++++-
 Telegram/Resources/style.txt                    |  7 ++++++-
 Telegram/Resources/style_classes.txt            |  5 +++++
 Telegram/SourceFiles/dialogs/dialogs_layout.cpp | 11 ++++++-----
 Telegram/SourceFiles/history.cpp                | 17 +++++++++++++++--
 Telegram/SourceFiles/history.h                  |  3 +++
 .../inline_bots/inline_bot_layout_internal.cpp  |  2 +-
 .../inline_bots/inline_bot_layout_item.cpp      |  1 +
 .../inline_bots/inline_bot_result.cpp           |  1 +
 Telegram/SourceFiles/mainwidget.cpp             |  3 ++-
 Telegram/SourceFiles/mainwidget.h               |  3 ++-
 Telegram/SourceFiles/structs.cpp                |  2 +-
 Telegram/Telegram.pro                           |  2 ++
 Telegram/Telegram.vcxproj                       |  2 ++
 Telegram/Telegram.vcxproj.filters               |  9 +++++++++
 15 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/Telegram/Resources/lang.strings b/Telegram/Resources/lang.strings
index 9e4522619..771400e51 100644
--- a/Telegram/Resources/lang.strings
+++ b/Telegram/Resources/lang.strings
@@ -399,7 +399,6 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
 "lng_preview_loading" = "Getting Link Info...";
 
 "lng_profile_chat_unaccessible" = "Group is unaccessible";
-"lng_topbar_info" = "Info";
 "lng_profile_about_section" = "About";
 "lng_profile_description_section" = "Description";
 "lng_profile_settings_section" = "Settings";
@@ -899,6 +898,10 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
 
 "lng_full_name" = "{first_name} {last_name}";
 
+// Not used
+
+"lng_topbar_info" = "Info";
+
 // Wnd specific
 
 "lng_wnd_choose_program_menu" = "Choose Default Program...";
diff --git a/Telegram/Resources/style.txt b/Telegram/Resources/style.txt
index 53c3d366e..6e514e15c 100644
--- a/Telegram/Resources/style.txt
+++ b/Telegram/Resources/style.txt
@@ -2514,4 +2514,9 @@ toastBg: medviewSaveMsg;
 toastFg: #FFF;
 toastPadding: margins(19px, 13px, 19px, 12px);
 toastFadeInDuration: 200;
-toastFadeOutDuration: 1000;
\ No newline at end of file
+toastFadeOutDuration: 1000;
+
+infoButton: PeerAvatarButton {
+	size: topBarHeight;
+	photoSize: 42px;
+}
diff --git a/Telegram/Resources/style_classes.txt b/Telegram/Resources/style_classes.txt
index 229b3398b..a3405a048 100644
--- a/Telegram/Resources/style_classes.txt
+++ b/Telegram/Resources/style_classes.txt
@@ -405,3 +405,8 @@ InputField {
 	iconSprite: sprite;
 	iconPosition: point;
 }
+
+PeerAvatarButton {
+	size: number;
+	photoSize: number;
+}
diff --git a/Telegram/SourceFiles/dialogs/dialogs_layout.cpp b/Telegram/SourceFiles/dialogs/dialogs_layout.cpp
index ce7b8a9dd..855bede73 100644
--- a/Telegram/SourceFiles/dialogs/dialogs_layout.cpp
+++ b/Telegram/SourceFiles/dialogs/dialogs_layout.cpp
@@ -182,6 +182,7 @@ void RowPainter::paint(Painter &p, const Row *row, int w, bool active, bool sele
 				unread += h->unreadCount;
 			}
 		}
+		int texttop = st::dlgPaddingVer + st::dlgFont->height + st::dlgSep;
 		if (unread) {
 			QString unreadStr = QString::number(unread);
 			int unreadWidth = st::dlgUnreadFont->width(unreadStr);
@@ -190,7 +191,7 @@ void RowPainter::paint(Painter &p, const Row *row, int w, bool active, bool sele
 			accumulate_max(unreadRectWidth, unreadRectHeight);
 
 			int unreadRectLeft = w - st::dlgPaddingHor - unreadRectWidth;
-			int unreadRectTop = st::dlgHeight - st::dlgPaddingVer - unreadRectHeight;
+			int unreadRectTop = texttop + st::dlgHistFont->ascent - st::dlgUnreadFont->ascent - st::dlgUnreadTop;
 			lastWidth -= unreadRectWidth + st::dlgUnreadPaddingHor;
 
 			paintUnreadBadge(p, QRect(unreadRectLeft, unreadRectTop, unreadRectWidth, unreadRectHeight), active, history->mute);
@@ -200,10 +201,10 @@ void RowPainter::paint(Painter &p, const Row *row, int w, bool active, bool sele
 			p.drawText(unreadRectLeft + (unreadRectWidth - unreadWidth) / 2, unreadRectTop + st::dlgUnreadTop + st::dlgUnreadFont->ascent, unreadStr);
 		}
 		if (history->typing.isEmpty() && history->sendActions.isEmpty()) {
-			item->drawInDialog(p, QRect(nameleft, st::dlgPaddingVer + st::dlgFont->height + st::dlgSep, lastWidth, st::dlgFont->height), active, history->textCachedFor, history->lastItemTextCache);
+			item->drawInDialog(p, QRect(nameleft, texttop, lastWidth, st::dlgFont->height), active, history->textCachedFor, history->lastItemTextCache);
 		} else {
 			p.setPen(active ? st::dlgActiveColor : st::dlgSystemColor);
-			history->typingText.drawElided(p, nameleft, st::dlgPaddingVer + st::dlgFont->height + st::dlgSep, lastWidth);
+			history->typingText.drawElided(p, nameleft, texttop, lastWidth);
 		}
 	});
 }
@@ -212,8 +213,8 @@ void RowPainter::paint(Painter &p, const FakeRow *row, int w, bool active, bool
 	auto item = row->item();
 	auto history = item->history();
 	paintRow(p, history, item, w, active, selected, onlyBackground, [&p, row, active, item](int nameleft, int namewidth) {
-		int32 lastWidth = namewidth;
-		item->drawInDialog(p, QRect(nameleft, st::dlgPaddingVer + st::dlgFont->height + st::dlgSep, lastWidth, st::dlgFont->height), active, row->_cacheFor, row->_cache);
+		int lastWidth = namewidth, texttop = st::dlgPaddingVer + st::dlgFont->height + st::dlgSep;
+		item->drawInDialog(p, QRect(nameleft, texttop, lastWidth, st::dlgFont->height), active, row->_cacheFor, row->_cache);
 	});
 }
 
diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp
index 500e47da3..cc987fb09 100644
--- a/Telegram/SourceFiles/history.cpp
+++ b/Telegram/SourceFiles/history.cpp
@@ -1566,7 +1566,7 @@ void History::addOlderSlice(const QVector<MTPMessage> &slice, const QVector<MTPM
 		asChannelHistory()->checkJoinedMessage();
 		asChannelHistory()->checkMaxReadMessageDate();
 	}
-	if (newLoaded && !lastMsg) setLastMessage(lastImportantMessage());
+	checkLastMsg();
 }
 
 void History::addNewerSlice(const QVector<MTPMessage> &slice, const QVector<MTPMessageGroup> *collapsed) {
@@ -1574,7 +1574,9 @@ void History::addNewerSlice(const QVector<MTPMessage> &slice, const QVector<MTPM
 
 	if (slice.isEmpty()) {
 		newLoaded = true;
-		if (!lastMsg) setLastMessage(lastImportantMessage());
+		if (!lastMsg) {
+			setLastMessage(lastImportantMessage());
+		}
 	}
 
 	t_assert(!isBuildingFrontBlock());
@@ -1632,6 +1634,17 @@ void History::addNewerSlice(const QVector<MTPMessage> &slice, const QVector<MTPM
 	}
 
 	if (isChannel()) asChannelHistory()->checkJoinedMessage();
+	checkLastMsg();
+}
+
+void History::checkLastMsg() {
+	if (lastMsg) {
+		if (!newLoaded && !lastMsg->detached() && (!isChannel() || asChannelHistory()->onlyImportant())) {
+			newLoaded = true;
+		}
+	} else if (newLoaded) {
+		setLastMessage(lastImportantMessage());
+	}
 }
 
 int History::countUnread(MsgId upTo) {
diff --git a/Telegram/SourceFiles/history.h b/Telegram/SourceFiles/history.h
index b37a51961..607d8292b 100644
--- a/Telegram/SourceFiles/history.h
+++ b/Telegram/SourceFiles/history.h
@@ -518,6 +518,9 @@ protected:
 
 private:
 
+	// After adding a new history slice check the lastMsg and newLoaded.
+	void checkLastMsg();
+
 	enum class Flag {
 		f_has_pending_resized_items = (1 << 0),
 		f_pending_resize            = (1 << 1),
diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp
index 9b241af0a..5eaf00f75 100644
--- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp
+++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp
@@ -731,7 +731,7 @@ void File::paint(Painter &p, const QRect &clip, uint32 selection, const PaintCon
 		icon = st::msgFileInPause;
 	} else if (radial || document->loading()) {
 		icon = st::msgFileInCancel;
-	} else if (document->loaded()) {
+	} else if (true || document->loaded()) {
 		if (document->isImage()) {
 			icon = st::msgFileInImage;
 		} else if (document->voice() || document->song()) {
diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp
index b1e1c084f..398bf3e53 100644
--- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp
+++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp
@@ -110,6 +110,7 @@ std_::unique_ptr<ItemBase> ItemBase::createLayout(Result *result, bool forceThum
 	case Type::Sticker: return std_::make_unique<internal::Sticker>(result); break;
 	case Type::Gif: return std_::make_unique<internal::Gif>(result); break;
 	case Type::Article:
+	case Type::Geo:
 	case Type::Venue: return std_::make_unique<internal::Article>(result, forceThumb); break;
 	case Type::Contact: return std_::make_unique<internal::Contact>(result); break;
 	}
diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp
index 4cee40a80..eb735f586 100644
--- a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp
+++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp
@@ -74,6 +74,7 @@ std_::unique_ptr<Result> Result::create(uint64 queryId, const MTPBotInlineResult
 		result->insert(qsl("article"), Result::Type::Article);
 		result->insert(qsl("contact"), Result::Type::Contact);
 		result->insert(qsl("venue"), Result::Type::Venue);
+		result->insert(qsl("geo"), Result::Type::Geo);
 		return result.release();
 	})() };
 
diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp
index a6547fad9..7d79406ed 100644
--- a/Telegram/SourceFiles/mainwidget.cpp
+++ b/Telegram/SourceFiles/mainwidget.cpp
@@ -55,7 +55,7 @@ TopBarWidget::TopBarWidget(MainWidget *w) : TWidget(w)
 , _forward(this, lang(lng_selected_forward), st::topBarActionButton)
 , _delete(this, lang(lng_selected_delete), st::topBarActionButton)
 , _selectionButtonsWidth(_clearSelection.width() + _forward.width() + _delete.width()), _forwardDeleteWidth(qMax(_forward.textWidth(), _delete.textWidth()))
-, _info(this, lang(lng_topbar_info), st::topBarButton)
+, _info(this, nullptr, st::infoButton)
 , _edit(this, lang(lng_profile_edit_contact), st::topBarButton)
 , _leaveGroup(this, lang(lng_profile_delete_and_exit), st::topBarButton)
 , _addContact(this, lang(lng_profile_add_contact), st::topBarButton)
@@ -362,6 +362,7 @@ void TopBarWidget::showAll() {
 		}
 		if (h && !o && !p && _clearSelection.isHidden()) {
 			if (Adaptive::OneColumn()) {
+				_info.setPeer(h);
 				_info.show();
 			} else {
 				_info.hide();
diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h
index 8c36fe4bc..dc787f08a 100644
--- a/Telegram/SourceFiles/mainwidget.h
+++ b/Telegram/SourceFiles/mainwidget.h
@@ -26,6 +26,7 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
 #include "profilewidget.h"
 #include "overviewwidget.h"
 #include "playerwidget.h"
+#include "ui/buttons/peer_avatar_button.h"
 
 class Window;
 class ApiWrap;
@@ -111,7 +112,7 @@ private:
 	FlatButton _forward, _delete;
 	int32 _selectionButtonsWidth, _forwardDeleteWidth;
 
-	FlatButton _info;
+	PeerAvatarButton _info;
 	FlatButton _edit, _leaveGroup, _addContact, _deleteContact;
 	FlatButton _mediaType;
 
diff --git a/Telegram/SourceFiles/structs.cpp b/Telegram/SourceFiles/structs.cpp
index db48dbefe..cc31492ef 100644
--- a/Telegram/SourceFiles/structs.cpp
+++ b/Telegram/SourceFiles/structs.cpp
@@ -214,7 +214,7 @@ void UserData::setPhoto(const MTPUserProfilePhoto &p) { // see Local::readPeer a
 	default: {
 		newPhotoId = 0;
 		if (id == ServiceUserId) {
-			if (_userpic->isNull()) {
+			if (_userpic.v() == userDefPhoto(colorIndex).v()) {
 				newPhoto = ImagePtr(QPixmap::fromImage(App::wnd()->iconLarge().scaledToWidth(160, Qt::SmoothTransformation), Qt::ColorOnly), "PNG");
 			}
 		} else {
diff --git a/Telegram/Telegram.pro b/Telegram/Telegram.pro
index 40af4d79f..ff1580fb4 100644
--- a/Telegram/Telegram.pro
+++ b/Telegram/Telegram.pro
@@ -128,6 +128,7 @@ SOURCES += \
     ./SourceFiles/mtproto/rpc_sender.cpp \
     ./SourceFiles/mtproto/scheme_auto.cpp \
     ./SourceFiles/mtproto/session.cpp \
+    ./SourceFiles/ui/buttons/peer_avatar_button.cpp \
     ./SourceFiles/ui/toast/toast.cpp \
     ./SourceFiles/ui/toast/toast_manager.cpp \
     ./SourceFiles/ui/toast/toast_widget.cpp \
@@ -236,6 +237,7 @@ HEADERS += \
     ./SourceFiles/mtproto/scheme_auto.h \
     ./SourceFiles/mtproto/session.h \
     ./SourceFiles/pspecific.h \
+    ./SourceFiles/ui/buttons/peer_avatar_button.h \
     ./SourceFiles/ui/toast/toast.h \
     ./SourceFiles/ui/toast/toast_manager.h \
     ./SourceFiles/ui/toast/toast_widget.h \
diff --git a/Telegram/Telegram.vcxproj b/Telegram/Telegram.vcxproj
index 7e7bfeeee..ea81c7795 100644
--- a/Telegram/Telegram.vcxproj
+++ b/Telegram/Telegram.vcxproj
@@ -1165,8 +1165,10 @@
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Deploy|Win32'">NotUsing</PrecompiledHeader>
     </ClCompile>
+    <ClCompile Include="ui\buttons\peer_avatar_button.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <CustomBuild Include="ui\buttons\peer_avatar_button.h" />
     <CustomBuild Include="SourceFiles\pspecific_winrt.h">
       <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Deploy|Win32'">$(QTDIR)\bin\moc.exe;%(FullPath)</AdditionalInputs>
       <Message Condition="'$(Configuration)|$(Platform)'=='Deploy|Win32'">Moc%27ing pspecific_winrt.h...</Message>
diff --git a/Telegram/Telegram.vcxproj.filters b/Telegram/Telegram.vcxproj.filters
index 24a016e94..998cda50d 100644
--- a/Telegram/Telegram.vcxproj.filters
+++ b/Telegram/Telegram.vcxproj.filters
@@ -64,6 +64,9 @@
     <Filter Include="dialogs">
       <UniqueIdentifier>{405e59c2-0800-4f73-b975-1749c8c36e87}</UniqueIdentifier>
     </Filter>
+    <Filter Include="ui\buttons">
+      <UniqueIdentifier>{24292a88-6707-4070-b2d2-8b53acd5cdd0}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="SourceFiles\main.cpp">
@@ -1029,6 +1032,9 @@
     <ClCompile Include="SourceFiles\dialogs\dialogs_indexed_list.cpp">
       <Filter>dialogs</Filter>
     </ClCompile>
+    <ClCompile Include="ui\buttons\peer_avatar_button.cpp">
+      <Filter>ui\buttons</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="SourceFiles\stdafx.h">
@@ -1408,6 +1414,9 @@
     <CustomBuild Include="SourceFiles\ui\toast\toast_manager.h">
       <Filter>ui\toast</Filter>
     </CustomBuild>
+    <CustomBuild Include="ui\buttons\peer_avatar_button.h">
+      <Filter>ui\buttons</Filter>
+    </CustomBuild>
   </ItemGroup>
   <ItemGroup>
     <None Include="Resources\langs\lang_it.strings">