diff --git a/Telegram/SourceFiles/api/api_bot.cpp b/Telegram/SourceFiles/api/api_bot.cpp index 80bec3cf8..072758298 100644 --- a/Telegram/SourceFiles/api/api_bot.cpp +++ b/Telegram/SourceFiles/api/api_bot.cpp @@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_item.h" #include "history/history_item_components.h" #include "main/main_session.h" +#include "window/window_session_controller.h" #include "ui/toast/toast.h" #include "ui/layers/generic_box.h" #include "ui/text/text_utilities.h" @@ -31,6 +32,7 @@ namespace Api { namespace { void SendBotCallbackData( + not_null controller, not_null item, int row, int column, @@ -73,6 +75,8 @@ void SendBotCallbackData( if (withPassword) { flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_password; } + const auto weak = base::make_weak(controller.get()); + const auto show = std::make_shared(controller); button->requestId = api->request(MTPmessages_GetBotCallbackAnswer( MTP_flags(flags), history->peer->input, @@ -100,12 +104,12 @@ void SendBotCallbackData( if (!message.isEmpty()) { if (showAlert) { - Ui::show(Ui::MakeInformBox(message)); + show->showBox(Ui::MakeInformBox(message)); } else { if (withPassword) { - Ui::hideLayer(); + show->hideLayer(); } - Ui::Toast::Show(message); + Ui::Toast::Show(show->toastParent(), message); } } else if (!link.isEmpty()) { if (!isGame) { @@ -116,12 +120,18 @@ void SendBotCallbackData( session, link, item->fullId()); - BotGameUrlClickHandler(bot, scoreLink).onClick({}); + BotGameUrlClickHandler(bot, scoreLink).onClick({ + Qt::LeftButton, + QVariant::fromValue(ClickHandlerContext{ + .itemId = item->fullId(), + .sessionWindow = weak, + }), + }); session->sendProgressManager().update( history, Api::SendProgressType::PlayGame); } else if (withPassword) { - Ui::hideLayer(); + show->hideLayer(); } }).fail([=](const MTP::Error &error) { const auto item = owner->message(fullId); @@ -147,13 +157,15 @@ void SendBotCallbackData( } // namespace void SendBotCallbackData( + not_null controller, not_null item, int row, int column) { - SendBotCallbackData(item, row, column, std::nullopt); + SendBotCallbackData(controller, item, row, column, std::nullopt); } void SendBotCallbackDataWithPassword( + not_null controller, not_null item, int row, int column) { @@ -177,7 +189,9 @@ void SendBotCallbackDataWithPassword( return; } api->cloudPassword().reload(); - SendBotCallbackData(item, row, column, std::nullopt, [=](const QString &error) { + const auto weak = base::make_weak(controller.get()); + const auto show = std::make_shared(controller); + SendBotCallbackData(controller, item, row, column, std::nullopt, [=](const QString &error) { auto box = PrePasswordErrorBox( error, session, @@ -185,7 +199,7 @@ void SendBotCallbackDataWithPassword( tr::now, Ui::Text::WithEntities)); if (box) { - Ui::show(std::move(box)); + show->showBox(std::move(box), Ui::LayerOption::CloseOther); } else { auto lifetime = std::make_shared(); button->requestId = -1; @@ -219,14 +233,20 @@ void SendBotCallbackDataWithPassword( return; } if (const auto item = owner->message(fullId)) { - SendBotCallbackData(item, row, column, result, [=](const QString &error) { + const auto strongController = weak.get(); + if (!strongController) { + return; + } + SendBotCallbackData(strongController, item, row, column, result, [=](const QString &error) { if (*box) { (*box)->handleCustomCheckError(error); } }); } }; - *box = Ui::show(Box(session, fields)); + auto object = Box(session, fields); + *box = Ui::MakeWeak(object.data()); + show->showBox(std::move(object), Ui::LayerOption::CloseOther); }, *lifetime); } }); diff --git a/Telegram/SourceFiles/api/api_bot.h b/Telegram/SourceFiles/api/api_bot.h index 06a420a41..45a40ca08 100644 --- a/Telegram/SourceFiles/api/api_bot.h +++ b/Telegram/SourceFiles/api/api_bot.h @@ -9,14 +9,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL class HistoryItem; +namespace Window { +class SessionController; +} // namespace Window + namespace Api { void SendBotCallbackData( + not_null controller, not_null item, int row, int column); void SendBotCallbackDataWithPassword( + not_null controller, not_null item, int row, int column); diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 236d622f5..ad40fd27a 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -72,7 +72,7 @@ bool insertBotCommand(const QString &cmd) { } void activateBotCommand( - Window::SessionController *sessionController, + not_null sessionController, not_null msg, int row, int column) { @@ -90,20 +90,19 @@ void activateBotCommand( case ButtonType::Default: { // Copy string before passing it to the sending method // because the original button can be destroyed inside. - if (sessionController) { - MsgId replyTo = msg->isRegular() ? msg->id : 0; - sessionController->content()->sendBotCommand({ - .peer = msg->history()->peer, - .command = QString(button->text), - .context = msg->fullId(), - .replyTo = replyTo, - }); - } + const auto replyTo = msg->isRegular() ? msg->id : 0; + sessionController->content()->sendBotCommand({ + .peer = msg->history()->peer, + .command = QString(button->text), + .context = msg->fullId(), + .replyTo = replyTo, + }); } break; case ButtonType::Callback: case ButtonType::Game: { Api::SendBotCallbackData( + sessionController, const_cast(msg.get()), row, column); @@ -111,6 +110,7 @@ void activateBotCommand( case ButtonType::CallbackWithPassword: { Api::SendBotCallbackDataWithPassword( + sessionController, const_cast(msg.get()), row, column); @@ -120,7 +120,9 @@ void activateBotCommand( Payments::CheckoutProcess::Start( msg, Payments::Mode::Payment, - crl::guard(App::wnd(), [](auto) { App::wnd()->activate(); })); + crl::guard(sessionController, [=](auto) { + sessionController->widget()->activate(); + })); } break; case ButtonType::Url: { @@ -140,14 +142,15 @@ void activateBotCommand( case ButtonType::RequestLocation: { hideSingleUseKeyboard(msg); - Ui::show(Ui::MakeInformBox(tr::lng_bot_share_location_unavailable())); + sessionController->show( + Ui::MakeInformBox(tr::lng_bot_share_location_unavailable())); } break; case ButtonType::RequestPhone: { hideSingleUseKeyboard(msg); const auto msgId = msg->id; const auto history = msg->history(); - Ui::show(Ui::MakeConfirmBox({ + sessionController->show(Ui::MakeConfirmBox({ .text = tr::lng_bot_share_phone(), .confirmed = [=] { Ui::showPeerHistory(history, ShowAtTheEndMsgId); @@ -224,24 +227,20 @@ void activateBotCommand( case ButtonType::WebView: { if (const auto bot = msg->getMessageBot()) { - if (sessionController) { - bot->session().attachWebView().request( - sessionController, - bot, - bot, - { .text = button->text, .url = button->data }); - } + bot->session().attachWebView().request( + sessionController, + bot, + bot, + { .text = button->text, .url = button->data }); } } break; case ButtonType::SimpleWebView: { if (const auto bot = msg->getMessageBot()) { - if (sessionController) { - bot->session().attachWebView().requestSimple( - sessionController, - bot, - { .text = button->text, .url = button->data }); - } + bot->session().attachWebView().requestSimple( + sessionController, + bot, + { .text = button->text, .url = button->data }); } } break; } diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index 3bd1a0566..5edcc152c 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -36,7 +36,7 @@ template bool insertBotCommand(const QString &cmd); void activateBotCommand( - Window::SessionController *sessionController, + not_null sessionController, not_null msg, int row, int column);