From f72cb979c0638683bf98e541a95feb28b22a02af Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 20 Feb 2020 20:22:30 +0400 Subject: [PATCH] Create unread bar when jumping to a message. --- .../SourceFiles/history/history_widget.cpp | 69 ++++++++++++++----- Telegram/SourceFiles/history/history_widget.h | 1 + 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index e0ce67b25b..4316bf8821 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1634,6 +1634,9 @@ void HistoryWidget::showHistory( if (_peer->id == peerId && !reload) { updateForwarding(); + if (showAtMsgId == ShowAtUnreadMsgId) { + showAtMsgId = ShowAtTheEndMsgId; + } const auto canShowNow = _history->isReadyFor(showAtMsgId); if (!canShowNow) { delayedShowAt(showAtMsgId); @@ -1658,7 +1661,12 @@ void HistoryWidget::showHistory( if (_historyInited) { const auto item = getItemFromHistoryOrMigrated( _showAtMsgId); - animatedScrollToY(countInitialScrollTop(), item); + animatedScrollToY( + std::clamp( + countInitialScrollTop(), + 0, + _scroll->scrollTopMax()), + item); } else { historyLoaded(); } @@ -4963,9 +4971,8 @@ MsgId HistoryWidget::replyToId() const { } int HistoryWidget::countInitialScrollTop() { - auto result = ScrollMax; if (_history->scrollTopItem || (_migrated && _migrated->scrollTopItem)) { - result = _list->historyScrollTop(); + return _list->historyScrollTop(); } else if (_showAtMsgId && (IsServerMsgId(_showAtMsgId) || IsServerMsgId(-_showAtMsgId))) { @@ -4978,31 +4985,44 @@ int HistoryWidget::countInitialScrollTop() { const auto view = item->mainView(); Assert(view != nullptr); - result = itemTopForHighlight(view); enqueueMessageHighlight(view); + const auto result = itemTopForHighlight(view); + createUnreadBarIfBelowVisibleArea(result); + return result; } } else if (const auto top = unreadBarTop()) { - result = *top; + return *top; } else { + _history->calculateFirstUnreadMessage(); return countAutomaticScrollTop(); } - return qMin(result, _scroll->scrollTopMax()); +} + +void HistoryWidget::createUnreadBarIfBelowVisibleArea(int withScrollTop) { + if (_history->unreadBar()) { + return; + } + _history->calculateFirstUnreadMessage(); + if (const auto unread = _history->firstUnreadMessage()) { + if (_list->itemTop(unread) > withScrollTop) { + _history->addUnreadBar(); + if (hasPendingResizedItems()) { + updateListSize(); + } + } + } } int HistoryWidget::countAutomaticScrollTop() { Expects(_history != nullptr); Expects(_list != nullptr); - auto result = ScrollMax; - if (!_historyInited) { - _history->calculateFirstUnreadMessage(); - } if (const auto unread = _history->firstUnreadMessage()) { - result = _list->itemTop(unread); + const auto firstUnreadTop = _list->itemTop(unread); const auto possibleUnreadBarTop = _scroll->scrollTopMax() + HistoryView::UnreadBar::height() - HistoryView::UnreadBar::marginTop(); - if (result < possibleUnreadBarTop) { + if (firstUnreadTop < possibleUnreadBarTop) { const auto history = unread->data()->history(); history->addUnreadBar(); if (hasPendingResizedItems()) { @@ -5010,15 +5030,11 @@ int HistoryWidget::countAutomaticScrollTop() { } if (history->unreadBar() != nullptr) { setMsgId(ShowAtUnreadMsgId); - result = countInitialScrollTop(); - if (session().supportMode()) { - history->unsetFirstUnreadMessage(); - } - return result; + return countInitialScrollTop(); } } } - return qMin(result, _scroll->scrollTopMax()); + return ScrollMax; } void HistoryWidget::updateHistoryGeometry( @@ -5165,8 +5181,23 @@ void HistoryWidget::addMessagesToFront(PeerData *peer, const QVector } } -void HistoryWidget::addMessagesToBack(PeerData *peer, const QVector &messages) { +void HistoryWidget::addMessagesToBack( + PeerData *peer, + const QVector &messages) { + const auto checkForUnreadStart = [&] { + if (_history->unreadBar() || !_history->inChatList()) { + return false; + } + _history->calculateFirstUnreadMessage(); + return !_history->firstUnreadMessage(); + }(); _list->messagesReceivedDown(peer, messages); + if (checkForUnreadStart) { + _history->calculateFirstUnreadMessage(); + if (const auto unread = _history->firstUnreadMessage()) { + _history->addUnreadBar(); + } + } if (!_firstLoadRequest) { updateHistoryGeometry(false, true, { ScrollChangeNoJumpToBottom, 0 }); } diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 432991e880..41cb147614 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -558,6 +558,7 @@ private: // destroys _history and _migrated unread bars void destroyUnreadBar(); void destroyUnreadBarOnClose(); + void createUnreadBarIfBelowVisibleArea(int withScrollTop); void saveEditMsg(); void saveEditMsgDone(History *history, const MTPUpdates &updates, mtpRequestId req);