diff --git a/Telegram/SourceFiles/core/shortcuts.cpp b/Telegram/SourceFiles/core/shortcuts.cpp index 21fc3d4a6..84b35fbc5 100644 --- a/Telegram/SourceFiles/core/shortcuts.cpp +++ b/Telegram/SourceFiles/core/shortcuts.cpp @@ -417,10 +417,17 @@ bool Request::handle(FnMut handler) { return true; } -bool Launch(Command command) { +FnMut RequestHandler(Command command) { auto request = Request(command); RequestsStream.fire(&request); - return request._handler ? request._handler() : false; + return std::move(request._handler); +} + +bool Launch(Command command) { + if (auto handler = RequestHandler(command)) { + return handler(); + } + return false; } rpl::producer> Requests() { diff --git a/Telegram/SourceFiles/core/shortcuts.h b/Telegram/SourceFiles/core/shortcuts.h index b15cd2870..b818eeeea 100644 --- a/Telegram/SourceFiles/core/shortcuts.h +++ b/Telegram/SourceFiles/core/shortcuts.h @@ -34,7 +34,7 @@ enum class Command { SupportScrollToCurrent, }; -bool Launch(Command command); +[[nodiscard]] FnMut RequestHandler(Command command); class Request { public: @@ -48,7 +48,7 @@ private: int _handlerPriority = -1; FnMut _handler; - friend bool Launch(Command command); + friend FnMut RequestHandler(Command command); }; @@ -57,6 +57,7 @@ rpl::producer> Requests(); void Start(); void Finish(); +bool Launch(Command command); bool HandleEvent(not_null event); const QStringList &Errors(); diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index bb9087d34..07a0fb141 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -2792,18 +2792,20 @@ void DialogsInner::setupShortcuts() { } const auto row = _controller->activeChatEntryCurrent(); if (row.key) { + const auto prev = computeJump(chatListEntryBefore(row), -1); + const auto next = computeJump(chatListEntryAfter(row), 1); request->check(Command::ChatPrevious) && request->handle([=] { - return showPreviousChat(row); + return jumpToDialogRow(prev); }); request->check(Command::ChatNext) && request->handle([=] { - return showNextChat(row); + return jumpToDialogRow(next); }); } request->check(Command::ChatFirst) && request->handle([=] { - return showFirstChat(); + return jumpToDialogRow(computeJump(chatListEntryFirst(), 1)); }); request->check(Command::ChatLast) && request->handle([=] { - return showLastChat(); + return jumpToDialogRow(computeJump(chatListEntryLast(), -1)); }); if (Auth().supportMode() && row.key.history()) { request->check( @@ -2816,43 +2818,28 @@ void DialogsInner::setupShortcuts() { }, lifetime()); } -bool DialogsInner::showNextChat(const Dialogs::RowDescriptor ¤t) { - return jumpToDialogRow(chatListEntryAfter(current), 1); -} - -bool DialogsInner::showPreviousChat(const Dialogs::RowDescriptor ¤t) { - return jumpToDialogRow(chatListEntryBefore(current), -1); -} - -bool DialogsInner::showFirstChat() { - return jumpToDialogRow(chatListEntryFirst(), 1); -} - -bool DialogsInner::showLastChat() { - return jumpToDialogRow(chatListEntryLast(), -1); -} - -bool DialogsInner::jumpToDialogRow( +Dialogs::RowDescriptor DialogsInner::computeJump( const Dialogs::RowDescriptor &to, int skipDirection) { - const auto entry = [&] { - auto result = to; - if (Auth().supportMode()) { - while (result.key - && !result.key.entry()->chatListUnreadCount() - && !result.key.entry()->chatListUnreadMark()) { - result = (skipDirection > 0) - ? chatListEntryAfter(result) - : chatListEntryBefore(result); - } + auto result = to; + if (Auth().supportMode()) { + while (result.key + && !result.key.entry()->chatListUnreadCount() + && !result.key.entry()->chatListUnreadMark()) { + result = (skipDirection > 0) + ? chatListEntryAfter(result) + : chatListEntryBefore(result); } - return result; - }(); - if (const auto history = entry.key.history()) { - Ui::showPeerHistory(history, entry.fullId.msg); + } + return result; +} + +bool DialogsInner::jumpToDialogRow(const Dialogs::RowDescriptor &to) { + if (const auto history = to.key.history()) { + Ui::showPeerHistory(history, to.fullId.msg); return true; - } else if (const auto feed = entry.key.feed()) { - if (const auto item = App::histItemById(entry.fullId)) { + } else if (const auto feed = to.key.feed()) { + if (const auto item = App::histItemById(to.fullId)) { _controller->showSection( HistoryFeed::Memento(feed, item->position())); } else { diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h index c50abdde3..3ab30ce66 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h @@ -188,13 +188,10 @@ private: bool hasHistoryInSearchResults(not_null history) const; void setupShortcuts(); - bool showNextChat(const Dialogs::RowDescriptor ¤t); - bool showPreviousChat(const Dialogs::RowDescriptor ¤t); - bool showFirstChat(); - bool showLastChat(); - bool jumpToDialogRow( + Dialogs::RowDescriptor computeJump( const Dialogs::RowDescriptor &to, int skipDirection); + bool jumpToDialogRow(const Dialogs::RowDescriptor &to); Dialogs::RowDescriptor chatListEntryBefore( const Dialogs::RowDescriptor &which) const; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 9072af10c..cba0b6e90 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -3738,9 +3738,10 @@ void HistoryWidget::handleSupportSwitch(not_null updated) { if (_history != updated || !Auth().supportMode()) { return; } - crl::on_main(this, [to = Auth().settings().supportSwitch()] { - Support::PerformSwitch(to); - }); + + crl::on_main( + this, + Support::GetSwitchMethod(Auth().settings().supportSwitch())); } void HistoryWidget::inlineBotResolveDone( diff --git a/Telegram/SourceFiles/support/support_common.cpp b/Telegram/SourceFiles/support/support_common.cpp index 2eac396d8..21deb1499 100644 --- a/Telegram/SourceFiles/support/support_common.cpp +++ b/Telegram/SourceFiles/support/support_common.cpp @@ -33,17 +33,14 @@ Qt::KeyboardModifiers SkipSwitchModifiers() { return Qt::ControlModifier | Qt::ShiftModifier; } -void PerformSwitch(SwitchSettings value) { +FnMut GetSwitchMethod(SwitchSettings value) { switch (value) { case SwitchSettings::Next: - Shortcuts::Launch(Shortcuts::Command::ChatNext); - break; + return Shortcuts::RequestHandler(Shortcuts::Command::ChatNext); case SwitchSettings::Previous: - Shortcuts::Launch(Shortcuts::Command::ChatPrevious); - break; - default: - break; + return Shortcuts::RequestHandler(Shortcuts::Command::ChatPrevious); } + return nullptr; } } // namespace Support diff --git a/Telegram/SourceFiles/support/support_common.h b/Telegram/SourceFiles/support/support_common.h index 1cc1aadb0..7b2c17392 100644 --- a/Telegram/SourceFiles/support/support_common.h +++ b/Telegram/SourceFiles/support/support_common.h @@ -19,6 +19,6 @@ enum class SwitchSettings { Qt::KeyboardModifiers SkipSwitchModifiers(); bool HandleSwitch(Qt::KeyboardModifiers modifiers); -void PerformSwitch(SwitchSettings value); +FnMut GetSwitchMethod(SwitchSettings value); } // namespace Support