From 63b5eb66ff7af95c8643eb8ccb04f3683e828423 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 23 Feb 2022 10:37:59 +0300 Subject: [PATCH] Provided Window::SessionController to FastShareMessage. --- Telegram/SourceFiles/boxes/share_box.cpp | 101 ++++++++++-------- Telegram/SourceFiles/boxes/share_box.h | 4 +- .../history/view/history_view_message.cpp | 2 +- 3 files changed, 59 insertions(+), 48 deletions(-) diff --git a/Telegram/SourceFiles/boxes/share_box.cpp b/Telegram/SourceFiles/boxes/share_box.cpp index 09f57de14..d10e35741 100644 --- a/Telegram/SourceFiles/boxes/share_box.cpp +++ b/Telegram/SourceFiles/boxes/share_box.cpp @@ -10,8 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/random.h" #include "dialogs/dialogs_indexed_list.h" #include "lang/lang_keys.h" -#include "mainwindow.h" -#include "mainwidget.h" #include "base/qthelp_url.h" #include "storage/storage_account.h" #include "ui/boxes/confirm_box.h" @@ -1265,7 +1263,9 @@ QString AppendShareGameScoreUrl( return url + shareComponent; } -void FastShareMessage(not_null item) { +void FastShareMessage( + not_null controller, + not_null item) { struct ShareData { ShareData(not_null peer, MessageIdsList &&ids) : peer(peer) @@ -1275,6 +1275,7 @@ void FastShareMessage(not_null item) { MessageIdsList msgIds; base::flat_set requests; }; + const auto show = std::make_shared(controller); const auto history = item->history(); const auto owner = &history->owner(); const auto session = &history->session(); @@ -1298,29 +1299,30 @@ void FastShareMessage(not_null item) { return item->media() && item->media()->forceForwardedInfo(); }); - auto copyCallback = [=]() { - if (const auto item = owner->message(data->msgIds[0])) { - if (item->hasDirectLink()) { - HistoryView::CopyPostLink( - session, - item->fullId(), - HistoryView::Context::History); - } else if (const auto bot = item->getMessageBot()) { - if (const auto media = item->media()) { - if (const auto game = media->game()) { - const auto link = session->createInternalLinkFull( - bot->username - + qsl("?game=") - + game->shortName); + auto copyCallback = [=, toastParent = show->toastParent()] { + const auto item = owner->message(data->msgIds[0]); + if (!item) { + return; + } + if (item->hasDirectLink()) { + using namespace HistoryView; + CopyPostLink(session, item->fullId(), Context::History); + } else if (const auto bot = item->getMessageBot()) { + if (const auto media = item->media()) { + if (const auto game = media->game()) { + const auto link = session->createInternalLinkFull( + bot->username + qsl("?game=") + game->shortName); - QGuiApplication::clipboard()->setText(link); + QGuiApplication::clipboard()->setText(link); - Ui::Toast::Show(tr::lng_share_game_link_copied(tr::now)); - } + Ui::Toast::Show( + toastParent, + tr::lng_share_game_link_copied(tr::now)); } } } }; + auto submitCallback = [=]( std::vector> &&result, TextWithTags &&comment, @@ -1354,7 +1356,7 @@ void FastShareMessage(not_null item) { ).append("\n\n"); } text.append(error.first); - Ui::show( + show->showBox( Ui::MakeInformBox(text), Ui::LayerOption::KeepOther); return; @@ -1401,20 +1403,25 @@ void FastShareMessage(not_null item) { | (ShouldSendSilent(peer, options) ? MTPmessages_ForwardMessages::Flag::f_silent : MTPmessages_ForwardMessages::Flag(0)); - history->sendRequestId = api.request(MTPmessages_ForwardMessages( - MTP_flags(sendFlags), - data->peer->input, - MTP_vector(msgIds), - MTP_vector(generateRandom()), - peer->input, - MTP_int(options.scheduled), - MTP_inputPeerEmpty() // send_as - )).done([=](const MTPUpdates &updates, mtpRequestId requestId) { + history->sendRequestId = api.request( + MTPmessages_ForwardMessages( + MTP_flags(sendFlags), + data->peer->input, + MTP_vector(msgIds), + MTP_vector(generateRandom()), + peer->input, + MTP_int(options.scheduled), + MTP_inputPeerEmpty() // send_as + )).done([=](const MTPUpdates &updates, mtpRequestId reqId) { history->session().api().applyUpdates(updates); - data->requests.remove(requestId); + data->requests.remove(reqId); if (data->requests.empty()) { - Ui::Toast::Show(tr::lng_share_done(tr::now)); - Ui::hideLayer(); + if (show->valid()) { + Ui::Toast::Show( + show->toastParent(), + tr::lng_share_done(tr::now)); + show->hideLayer(); + } } finish(); }).fail([=] { @@ -1437,17 +1444,19 @@ void FastShareMessage(not_null item) { auto copyLinkCallback = canCopyLink ? Fn(std::move(copyCallback)) : Fn(); - Ui::show(Box(ShareBox::Descriptor{ - .session = session, - .copyCallback = std::move(copyLinkCallback), - .submitCallback = std::move(submitCallback), - .filterCallback = std::move(filterCallback), - .forwardOptions = { - .messagesCount = int(data->msgIds.size()), - .show = !hasOnlyForcedForwardedInfo, - .hasCaptions = hasCaptions, - }, - })); + controller->show( + Box(ShareBox::Descriptor{ + .session = session, + .copyCallback = std::move(copyLinkCallback), + .submitCallback = std::move(submitCallback), + .filterCallback = std::move(filterCallback), + .forwardOptions = { + .messagesCount = int(data->msgIds.size()), + .show = !hasOnlyForcedForwardedInfo, + .hasCaptions = hasCaptions, + }, + }), + Ui::LayerOption::CloseOther); } void ShareGameScoreByHash( @@ -1512,7 +1521,7 @@ void ShareGameScoreByHash( const auto msgId = MsgId(int64(hashDataInts[2])); if (const auto item = session.data().message(peerId, msgId)) { - FastShareMessage(item); + FastShareMessage(controller, item); } else { const auto weak = base::make_weak(controller.get()); const auto resolveMessageAndShareScore = crl::guard(weak, [=]( @@ -1522,7 +1531,7 @@ void ShareGameScoreByHash( peerId, msgId); if (item) { - FastShareMessage(item); + FastShareMessage(weak.get(), item); } else { weak->show( Ui::MakeInformBox(tr::lng_edit_deleted()), diff --git a/Telegram/SourceFiles/boxes/share_box.h b/Telegram/SourceFiles/boxes/share_box.h index 98fcdd456..7feb45281 100644 --- a/Telegram/SourceFiles/boxes/share_box.h +++ b/Telegram/SourceFiles/boxes/share_box.h @@ -62,7 +62,9 @@ QString AppendShareGameScoreUrl( void ShareGameScoreByHash( not_null controller, const QString &hash); -void FastShareMessage(not_null item); +void FastShareMessage( + not_null controller, + not_null item); class ShareBox final : public Ui::BoxContent { public: diff --git a/Telegram/SourceFiles/history/view/history_view_message.cpp b/Telegram/SourceFiles/history/view/history_view_message.cpp index 7bf78dee1..380cdc33b 100644 --- a/Telegram/SourceFiles/history/view/history_view_message.cpp +++ b/Telegram/SourceFiles/history/view/history_view_message.cpp @@ -2634,7 +2634,7 @@ ClickHandlerPtr Message::rightActionLink() const { Window::SectionShow::Way::Forward, savedFromMsgId); } else { - FastShareMessage(item); + FastShareMessage(controller, item); } } });