From 08f706937033c7be50114c805d1947397e5e7b24 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 26 Oct 2020 13:22:14 +0300 Subject: [PATCH] Implement new choosing of shown pinned message. --- .../SourceFiles/history/history_widget.cpp | 50 +++++++++++++++---- Telegram/SourceFiles/history/history_widget.h | 4 ++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 4abcb6bef0..8dbe754507 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1608,6 +1608,8 @@ void HistoryWidget::showHistory( const PeerId &peerId, MsgId showAtMsgId, bool reload) { + _pinnedClickedId = FullMsgId(); + MsgId wasMsgId = _showAtMsgId; History *wasHistory = _history; @@ -2747,6 +2749,14 @@ void HistoryWidget::onScroll() { if (!_synteticScrollEvent) { _lastUserScrolled = crl::now(); } + const auto scrollTop = _scroll->scrollTop(); + if (scrollTop != _lastScrollTop) { + if (!_synteticScrollEvent) { + checkLastPinnedClickedIdReset(_lastScrollTop, scrollTop); + } + _lastScrolled = crl::now(); + _lastScrollTop = scrollTop; + } } bool HistoryWidget::isItemCompletelyHidden(HistoryItem *item) const { @@ -2789,12 +2799,6 @@ void HistoryWidget::preloadHistoryIfNeeded() { preloadHistoryByScroll(); checkReplyReturns(); } - - auto scrollTop = _scroll->scrollTop(); - if (scrollTop != _lastScrollTop) { - _lastScrolled = crl::now(); - _lastScrollTop = scrollTop; - } } void HistoryWidget::preloadHistoryByScroll() { @@ -5176,15 +5180,39 @@ void HistoryWidget::updatePinnedViewer() { || !_historyInited) { return; } - const auto visibleTop = _scroll->scrollTop(); - const auto add = (st::historyReplyHeight - _pinnedBarHeight); - auto [view, offset] = _list->findViewForPinnedTracking(visibleTop + add); + const auto visibleBottom = _scroll->scrollTop() + _scroll->height(); + auto [view, offset] = _list->findViewForPinnedTracking(visibleBottom); const auto lessThanId = !view ? (ServerMaxMsgId - 1) : (view->data()->history() != _history) ? (view->data()->id + (offset > 0 ? 1 : 0) - ServerMaxMsgId) : (view->data()->id + (offset > 0 ? 1 : 0)); - _pinnedTracker->trackAround(lessThanId); + const auto lastClickedId = !_pinnedClickedId + ? (ServerMaxMsgId - 1) + : (!_migrated || _pinnedClickedId.channel) + ? _pinnedClickedId.msg + : (_pinnedClickedId.msg - ServerMaxMsgId); + if (_pinnedClickedId && lessThanId <= lastClickedId) { + _pinnedClickedId = FullMsgId(); + } + _pinnedTracker->trackAround(std::min(lessThanId, lastClickedId)); +} + +void HistoryWidget::checkLastPinnedClickedIdReset( + int wasScrollTop, + int nowScrollTop) { + if (_firstLoadRequest + || _delayedShowAtRequest + || _scroll->isHidden() + || !_history + || !_historyInited) { + return; + } + if (wasScrollTop < nowScrollTop && _pinnedClickedId) { + // User scrolled down. + _pinnedClickedId = FullMsgId(); + updatePinnedViewer(); + } } void HistoryWidget::setupPinnedTracker() { @@ -5268,6 +5296,8 @@ void HistoryWidget::checkPinnedBarState() { const auto id = _pinnedTracker->currentMessageId(); if (const auto item = session().data().message(id.message)) { Ui::showPeerHistory(item->history()->peer, item->id); + _pinnedClickedId = id.message; + updatePinnedViewer(); } }, _pinnedBar->lifetime()); diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 197e63176b..8520cda6b3 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -480,6 +480,9 @@ private: void setupPinnedTracker(); void checkPinnedBarState(); void refreshPinnedBarButton(bool many); + void checkLastPinnedClickedIdReset( + int wasScrollTop, + int nowScrollTop); void sendInlineResult( not_null result, @@ -596,6 +599,7 @@ private: std::unique_ptr _pinnedTracker; std::unique_ptr _pinnedBar; int _pinnedBarHeight = 0; + FullMsgId _pinnedClickedId; mtpRequestId _saveEditMsgRequestId = 0;