From e6c0f0f774c328f6c5b991040aed79f00859d3dd Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 9 Mar 2018 15:18:28 +0300 Subject: [PATCH] Up arrow always edits last available message. Fixes #4480. --- Telegram/SourceFiles/history/history.cpp | 19 +++++++++++++++---- Telegram/SourceFiles/history/history.h | 2 +- .../SourceFiles/history/history_widget.cpp | 17 ++++++++++------- Telegram/SourceFiles/mainwidget.cpp | 2 -- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 5489bc31f..2d12ca128 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1223,9 +1223,6 @@ void History::clearSendAction(not_null from) { void History::mainViewRemoved( not_null block, not_null view) { - if (lastSentMsg == view->data()) { - lastSentMsg = nullptr; - } if (_joinedMessage == view->data()) { _joinedMessage = nullptr; } @@ -2271,6 +2268,21 @@ MsgId History::msgIdForRead() const { : result; } +HistoryItem *History::lastSentMessage() const { + if (!loadedAtBottom()) { + return nullptr; + } + for (const auto &block : base::reversed(blocks)) { + for (const auto &message : base::reversed(block->messages)) { + const auto item = message->data(); + if (IsServerMsgId(item->id) && item->out()) { + return item; + } + } + } + return nullptr; +} + void History::resizeToWidth(int newWidth) { const auto resizeAllItems = (_width != newWidth); @@ -2517,7 +2529,6 @@ void History::unloadBlocks() { void History::clearBlocks(bool leaveItems) { _unreadBarView = nullptr; _firstUnreadView = nullptr; - lastSentMsg = nullptr; _joinedMessage = nullptr; if (scrollTopItem) { diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index 2915f3ab0..92f87262a 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -242,6 +242,7 @@ public: MsgId minMsgId() const; MsgId maxMsgId() const; MsgId msgIdForRead() const; + HistoryItem *lastSentMessage() const; void resizeToWidth(int newWidth); int height() const; @@ -351,7 +352,6 @@ public: std::deque> blocks; not_null peer; - HistoryItem *lastSentMsg = nullptr; typedef QList NotifyQueue; NotifyQueue notifies; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 5af89a341..da6bf6fa0 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -5153,13 +5153,16 @@ void HistoryWidget::keyPressEvent(QKeyEvent *e) { } } else if (e->key() == Qt::Key_Up) { if (!(e->modifiers() & (Qt::ShiftModifier | Qt::MetaModifier | Qt::ControlModifier))) { - if (_history - && _history->lastSentMsg - && _history->lastSentMsg->allowsEdit(unixtime())) { - if (_field->isEmpty() && !_editMsgId && !_replyToId && _history->lastSentMsg) { - editMessage(_history->lastSentMsg); - return; - } + const auto item = _history + ? _history->lastSentMessage() + : nullptr; + if (item + && item->allowsEdit(unixtime()) + && _field->isEmpty() + && !_editMsgId + && !_replyToId) { + editMessage(item); + return; } _scroll->keyPressEvent(e); } else if ((e->modifiers() & (Qt::ShiftModifier | Qt::MetaModifier | Qt::ControlModifier)) == Qt::ControlModifier) { diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index f50a0268b..28acafb53 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1345,8 +1345,6 @@ void MainWidget::sendMessage(const MessageToSend &message) { history->sendRequestId); } - history->lastSentMsg = lastMessage; - finishForwarding(history); }