diff --git a/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp b/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp index 08271abd6..dea870e14 100644 --- a/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp +++ b/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp @@ -14,6 +14,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_schedule_box.h" #include "lang/lang_keys.h" #include "ui/widgets/popup_menu.h" +#include "data/data_peer.h" +#include "main/main_session.h" +#include "apiwrap.h" #include @@ -133,4 +136,45 @@ void SetupMenuAndShortcuts( }, button->lifetime()); } +void SetupUnreadMentionsMenu( + not_null button, + Fn currentPeer) { + struct State { + base::unique_qptr menu; + base::flat_set> sentForPeers; + }; + const auto state = std::make_shared(); + const auto showMenu = [=] { + const auto peer = currentPeer(); + if (!peer) { + return; + } + state->menu = base::make_unique_q(button); + const auto text = tr::lng_context_mark_read_mentions_all(tr::now); + state->menu->addAction(text, [=] { + if (!state->sentForPeers.emplace(peer).second) { + return; + } + peer->session().api().request(MTPmessages_ReadMentions( + peer->input + )).done([=](const MTPmessages_AffectedHistory &result) { + state->sentForPeers.remove(peer); + peer->session().api().applyAffectedHistory(peer, result); + }).fail([=](const MTP::Error &error) { + state->sentForPeers.remove(peer); + }).send(); + }); + state->menu->popup(QCursor::pos()); + }; + + base::install_event_filter(button, [=](not_null e) { + if (e->type() == QEvent::ContextMenu) { + showMenu(); + return base::EventFilterResult::Cancel; + } + return base::EventFilterResult::Continue; + }); + +} + } // namespace SendMenu diff --git a/Telegram/SourceFiles/chat_helpers/send_context_menu.h b/Telegram/SourceFiles/chat_helpers/send_context_menu.h index 11f9824b6..1b79feffb 100644 --- a/Telegram/SourceFiles/chat_helpers/send_context_menu.h +++ b/Telegram/SourceFiles/chat_helpers/send_context_menu.h @@ -50,4 +50,8 @@ void SetupMenuAndShortcuts( Fn silent, Fn schedule); +void SetupUnreadMentionsMenu( + not_null button, + Fn currentPeer); + } // namespace SendMenu diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index e775fe6aa..f723e1813 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -309,7 +309,9 @@ HistoryWidget::HistoryWidget( _historyDown->installEventFilter(this); _unreadMentions->installEventFilter(this); - setupUnreadMentionsButtonContextMenu(_unreadMentions.data()); + SendMenu::SetupUnreadMentionsMenu(_unreadMentions.data(), [=] { + return _history ? _history->peer.get() : nullptr; + }); InitMessageField(controller, _field); @@ -6965,42 +6967,6 @@ void HistoryWidget::synteticScrollToY(int y) { _synteticScrollEvent = false; } -void HistoryWidget::setupUnreadMentionsButtonContextMenu( - not_null button) { - struct State { - base::unique_qptr menu; - base::flat_set> sentForPeers; - }; - const auto state = std::make_shared(); - const auto showMenu = [=] { - state->menu = base::make_unique_q(button); - const auto text = tr::lng_context_mark_read_mentions_all(tr::now); - const auto peer = _history->peer; - state->menu->addAction(text, [=] { - if (!state->sentForPeers.emplace(peer).second) { - return; - } - peer->session().api().request(MTPmessages_ReadMentions( - peer->input - )).done([=](const MTPmessages_AffectedHistory &result) { - state->sentForPeers.remove(peer); - peer->session().api().applyAffectedHistory(peer, result); - }).fail([=](const MTP::Error &error) { - state->sentForPeers.remove(peer); - }).send(); - }); - state->menu->popup(QCursor::pos()); - }; - - base::install_event_filter(button, [=](not_null e) { - if (e->type() == QEvent::ContextMenu) { - showMenu(); - return base::EventFilterResult::Cancel; - } - return base::EventFilterResult::Continue; - }); -} - HistoryWidget::~HistoryWidget() { if (_history) { // Saving a draft on account switching. diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index cdf882b2e..ca7847214 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -597,8 +597,6 @@ private: bool kbWasHidden() const; - void setupUnreadMentionsButtonContextMenu(not_null button); - MTP::Sender _api; MsgId _replyToId = 0; Ui::Text::String _replyToName;