From 6068dc418d034d3f4b0650cf44fa6e1db05d78de Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 26 Jun 2020 15:48:28 +0400 Subject: [PATCH] More auto-switching between accounts. --- Telegram/SourceFiles/facades.cpp | 161 ++++++++++-------- .../media/view/media_view_overlay_widget.cpp | 2 - 2 files changed, 86 insertions(+), 77 deletions(-) diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 284635127c..a95a506662 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -21,6 +21,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "apiwrap.h" #include "main/main_session.h" +#include "main/main_domain.h" #include "boxes/confirm_box.h" #include "boxes/url_auth_box.h" #include "ui/layers/layer_widget.h" @@ -32,24 +33,41 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_history.h" #include "data/data_session.h" +namespace { + +[[nodiscard]] MainWidget *CheckMainWidget(not_null session) { + if (const auto m = App::main()) { // multi good + if (&m->session() == session) { + return m; + } + } + if (&Core::App().domain().active() != &session->account()) { + Core::App().domain().activate(&session->account()); + } + if (const auto m = App::main()) { // multi good + if (&m->session() == session) { + return m; + } + } + return nullptr; +} + +} // namespace + namespace App { void sendBotCommand( not_null peer, UserData *bot, const QString &cmd, MsgId replyTo) { - if (const auto m = App::main()) { // multi good - if (&m->session() == &peer->session()) { - m->sendBotCommand(peer, bot, cmd, replyTo); - } + if (const auto m = CheckMainWidget(&peer->session())) { + m->sendBotCommand(peer, bot, cmd, replyTo); } } void hideSingleUseKeyboard(not_null message) { - if (const auto m = App::main()) { // multi good - if (&m->session() == &message->history()->session()) { - m->hideSingleUseKeyboard(message->history()->peer, message->id); - } + if (const auto m = CheckMainWidget(&message->history()->session())) { + m->hideSingleUseKeyboard(message->history()->peer, message->id); } } @@ -88,10 +106,8 @@ void activateBotCommand( case ButtonType::Callback: case ButtonType::Game: { - if (const auto m = App::main()) { // multi good - if (&m->session() == &msg->history()->session()) { - m->app_sendBotCallback(button, msg, row, column); - } + if (const auto m = CheckMainWidget(&msg->history()->session())) { + m->app_sendBotCallback(button, msg, row, column); } } break; @@ -145,34 +161,30 @@ void activateBotCommand( chosen |= PollData::Flag::Quiz; } } - if (const auto m = App::main()) { // multi good - if (&m->session() == &msg->history()->session()) { - Window::PeerMenuCreatePoll(m->controller(), msg->history()->peer, chosen, disabled); - } + if (const auto m = CheckMainWidget(&msg->history()->session())) { + Window::PeerMenuCreatePoll(m->controller(), msg->history()->peer, chosen, disabled); } } break; case ButtonType::SwitchInlineSame: case ButtonType::SwitchInline: { const auto session = &msg->history()->session(); - if (const auto m = App::main()) { // multi good - if (&m->session() == session) { - if (const auto bot = msg->getMessageBot()) { - const auto fastSwitchDone = [&] { - auto samePeer = (button->type == ButtonType::SwitchInlineSame); - if (samePeer) { - Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data), bot, msg->id); + if (const auto m = CheckMainWidget(session)) { + if (const auto bot = msg->getMessageBot()) { + const auto fastSwitchDone = [&] { + auto samePeer = (button->type == ButtonType::SwitchInlineSame); + if (samePeer) { + Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data), bot, msg->id); + return true; + } else if (bot->isBot() && bot->botInfo->inlineReturnPeerId) { + if (Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data))) { return true; - } else if (bot->isBot() && bot->botInfo->inlineReturnPeerId) { - if (Notify::switchInlineBotButtonReceived(session, QString::fromUtf8(button->data))) { - return true; - } } - return false; - }(); - if (!fastSwitchDone) { - m->inlineSwitchLayer('@' + bot->username + ' ' + QString::fromUtf8(button->data)); } + return false; + }(); + if (!fastSwitchDone) { + m->inlineSwitchLayer('@' + bot->username + ' ' + QString::fromUtf8(button->data)); } } } @@ -185,19 +197,20 @@ void activateBotCommand( } void searchByHashtag(const QString &tag, PeerData *inPeer) { - if (const auto m = App::main()) { // multi good - if (!inPeer || &m->session() == &inPeer->session()) { - if (m->controller()->openedFolder().current()) { - m->controller()->closeFolder(); - } - Ui::hideSettingsAndLayer(); - Core::App().hideMediaView(); - m->searchMessages( - tag + ' ', - (inPeer && !inPeer->isUser()) - ? inPeer->owner().history(inPeer).get() - : Dialogs::Key()); + const auto m = inPeer + ? CheckMainWidget(&inPeer->session()) + : App::main(); // multi good + if (m) { + if (m->controller()->openedFolder().current()) { + m->controller()->closeFolder(); } + Ui::hideSettingsAndLayer(); + Core::App().hideMediaView(); + m->searchMessages( + tag + ' ', + (inPeer && !inPeer->isUser()) + ? inPeer->owner().history(inPeer).get() + : Dialogs::Key()); } } @@ -218,6 +231,14 @@ void showPeerProfile(not_null peer) { controller->showPeerInfo(peer); } } + if (&Core::App().domain().active() != &peer->session().account()) { + Core::App().domain().activate(&peer->session().account()); + } + if (const auto controller = window->sessionController()) { + if (&controller->session() == &peer->session()) { + controller->showPeerInfo(peer); + } + } } } @@ -226,13 +247,11 @@ void showPeerProfile(not_null history) { } void showChatsList(not_null session) { - if (const auto m = App::main()) { // multi good - if (&m->session() == session) { - m->ui_showPeerHistory( - 0, - Window::SectionShow::Way::ClearStack, - 0); - } + if (const auto m = CheckMainWidget(session)) { + m->ui_showPeerHistory( + 0, + Window::SectionShow::Way::ClearStack, + 0); } } @@ -245,13 +264,11 @@ void showPeerHistory(not_null history, MsgId msgId) { } void showPeerHistory(not_null peer, MsgId msgId) { - if (const auto m = App::main()) { // multi good - if (&m->session() == &peer->session()) { - m->ui_showPeerHistory( - peer->id, - Window::SectionShow::Way::ClearStack, - msgId); - } + if (const auto m = CheckMainWidget(&peer->session())) { + m->ui_showPeerHistory( + peer->id, + Window::SectionShow::Way::ClearStack, + msgId); } } @@ -273,10 +290,8 @@ bool skipPaintEvent(QWidget *widget, QPaintEvent *event) { namespace Notify { void replyMarkupUpdated(not_null item) { - if (const auto m = App::main()) { // multi good - if (&m->session() == &item->history()->session()) { - m->notify_replyMarkupUpdated(item); - } + if (const auto m = CheckMainWidget(&item->history()->session())) { + m->notify_replyMarkupUpdated(item); } } @@ -284,13 +299,11 @@ void inlineKeyboardMoved( not_null item, int oldKeyboardTop, int newKeyboardTop) { - if (const auto m = App::main()) { // multi good - if (&m->session() == &item->history()->session()) { - m->notify_inlineKeyboardMoved( - item, - oldKeyboardTop, - newKeyboardTop); - } + if (const auto m = CheckMainWidget(&item->history()->session())) { + m->notify_inlineKeyboardMoved( + item, + oldKeyboardTop, + newKeyboardTop); } } @@ -299,13 +312,11 @@ bool switchInlineBotButtonReceived( const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo) { - if (const auto m = App::main()) { // multi good - if (session == &m->session()) { - return m->notify_switchInlineBotButtonReceived( - query, - samePeerBot, - samePeerReplyTo); - } + if (const auto m = CheckMainWidget(session)) { + return m->notify_switchInlineBotButtonReceived( + query, + samePeerBot, + samePeerReplyTo); } return false; } diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 327aaa1bf4..322f606f9d 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -1219,7 +1219,6 @@ void OverlayWidget::onToMessage() { } if (const auto item = _session->data().message(_msgid)) { close(); - Core::App().domain().activate(&_session->account()); Ui::showPeerHistoryAtItem(item); } } @@ -3852,7 +3851,6 @@ void OverlayWidget::mouseReleaseEvent(QMouseEvent *e) { if (_over == OverName && _down == OverName) { if (_from) { close(); - Core::App().domain().activate(&_from->account()); Ui::showPeerProfile(_from); } } else if (_over == OverDate && _down == OverDate) {