From a011a7c3160c2d3ec3e46c01e58bcd94bb6545a0 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 14 May 2024 14:47:34 +0400 Subject: [PATCH] Fix sending scheduled effects. --- .../SourceFiles/boxes/create_poll_box.cpp | 4 +- Telegram/SourceFiles/boxes/send_files_box.cpp | 2 +- Telegram/SourceFiles/boxes/share_box.cpp | 29 +++++------- Telegram/SourceFiles/boxes/share_box.h | 1 - .../SourceFiles/history/history_widget.cpp | 13 +++-- Telegram/SourceFiles/history/history_widget.h | 2 +- .../view/history_view_context_menu.cpp | 1 + .../view/history_view_schedule_box.cpp | 35 +++++++++----- .../history/view/history_view_schedule_box.h | 8 ++-- Telegram/SourceFiles/menu/menu_send.cpp | 47 ++++++++++--------- Telegram/SourceFiles/menu/menu_send.h | 13 +++-- 11 files changed, 86 insertions(+), 69 deletions(-) diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 8b032b81a..151ab54b8 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -1304,7 +1304,9 @@ object_ptr CreatePollBox::setupContent() { const auto isNormal = (_sendType == Api::SendType::Normal); const auto schedule = [=] { - sendAction(SendMenu::ActionType::Schedule, _sendMenuDetails()); + sendAction( + { .type = SendMenu::ActionType::Schedule }, + _sendMenuDetails()); }; const auto submit = addButton( (isNormal diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 6ba4771f8..c28a1dc70 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -1427,7 +1427,7 @@ void SendFilesBox::send( || _sendType == Api::SendType::ScheduledToUser) && !options.scheduled) { return SendMenu::DefaultCallback(_show, sendCallback())( - SendMenu::ActionType::Schedule, + { .type = SendMenu::ActionType::Schedule }, _sendMenuDetails()); } if (_preparing) { diff --git a/Telegram/SourceFiles/boxes/share_box.cpp b/Telegram/SourceFiles/boxes/share_box.cpp index 3d42c5afe..043a8ad18 100644 --- a/Telegram/SourceFiles/boxes/share_box.cpp +++ b/Telegram/SourceFiles/boxes/share_box.cpp @@ -523,12 +523,19 @@ void ShareBox::showMenu(not_null parent) { using namespace SendMenu; const auto sendAction = crl::guard(this, [=](Action action, Details) { - const auto options = std::get_if(&action); - if (options || v::get(action) == ActionType::Send) { - submit(options ? *options : Api::SendOptions()); - } else { - submitScheduled(); + if (action.type == ActionType::Send) { + submit(action.options); + return; } + uiShow()->showBox( + HistoryView::PrepareScheduleBox( + this, + nullptr, // ChatHelpers::Show for effect attachment. + sendMenuDetails(), + [=](Api::SendOptions options) { submit(options); }, + action.options, + HistoryView::DefaultScheduleTime(), + _descriptor.scheduleBoxStyle)); }); _menu->setForcedVerticalOrigin(Ui::PopupMenu::VerticalOrigin::Bottom); const auto result = FillSendMenu( @@ -620,18 +627,6 @@ void ShareBox::submit(Api::SendOptions options) { } } -void ShareBox::submitScheduled() { - const auto callback = [=](Api::SendOptions options) { submit(options); }; - uiShow()->showBox( - HistoryView::PrepareScheduleBox( - this, - nullptr, // ChatHelpers::Show for effect attachment. - sendMenuDetails(), - callback, - HistoryView::DefaultScheduleTime(), - _descriptor.scheduleBoxStyle)); -} - void ShareBox::copyLink() const { if (const auto onstack = _descriptor.copyCallback) { onstack(); diff --git a/Telegram/SourceFiles/boxes/share_box.h b/Telegram/SourceFiles/boxes/share_box.h index 5e48430cd..32e824b15 100644 --- a/Telegram/SourceFiles/boxes/share_box.h +++ b/Telegram/SourceFiles/boxes/share_box.h @@ -130,7 +130,6 @@ private: void scrollAnimationCallback(); void submit(Api::SendOptions options); - void submitScheduled(); void copyLink() const; bool searchByUsername(bool useCache = false); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index ca3803cff..e68bade1a 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -324,11 +324,10 @@ HistoryWidget::HistoryWidget( { using namespace SendMenu; const auto sendAction = [=](Action action, Details) { - const auto options = std::get_if(&action); - if (options || v::get(action) == ActionType::Send) { - send(options ? *options : Api::SendOptions()); + if (action.type == ActionType::Send) { + send(action.options); } else { - sendScheduled(); + sendScheduled(action.options); } }; SetupMenuAndShortcuts( @@ -4192,7 +4191,7 @@ void HistoryWidget::sendWithModifiers(Qt::KeyboardModifiers modifiers) { send({ .handleSupportSwitch = Support::HandleSwitch(modifiers) }); } -void HistoryWidget::sendScheduled() { +void HistoryWidget::sendScheduled(Api::SendOptions initialOptions) { if (!_list) { return; } @@ -4202,13 +4201,13 @@ void HistoryWidget::sendScheduled() { ignoreSlowmodeCountdown)) { return; } - const auto callback = [=](Api::SendOptions options) { send(options); }; controller()->show( HistoryView::PrepareScheduleBox( _list, controller()->uiShow(), sendMenuDetails(), - callback)); + [=](Api::SendOptions options) { send(options); }, + initialOptions)); } SendMenu::Details HistoryWidget::sendMenuDetails() const { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index ca2a9ed47..bc3641e67 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -395,7 +395,7 @@ private: Api::SendOptions options) const; void send(Api::SendOptions options); void sendWithModifiers(Qt::KeyboardModifiers modifiers); - void sendScheduled(); + void sendScheduled(Api::SendOptions initialOptions); [[nodiscard]] SendMenu::Details sendButtonMenuDetails() const; void handlePendingHistoryUpdate(); void fullInfoUpdated(); diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 781a4875d..a02f4c786 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -592,6 +592,7 @@ bool AddRescheduleAction( request.navigation->uiShow(), { .type = sendMenuType, .effectAllowed = false }, callback, + {}, // initial options date)); owner->itemRemoved( diff --git a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp index 09288fed7..9ab0af794 100644 --- a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp +++ b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp @@ -70,28 +70,34 @@ bool CanScheduleUntilOnline(not_null peer) { void ScheduleBox( not_null box, std::shared_ptr show, + const Api::SendOptions &initialOptions, const SendMenu::Details &details, Fn done, TimeId time, ScheduleBoxStyleArgs style) { - const auto save = [=](bool silent, TimeId scheduleDate) { - if (!scheduleDate) { + const auto submit = [=](Api::SendOptions options) { + if (!options.scheduled) { return; } - auto result = Api::SendOptions(); // Pro tip: Hold Ctrl key to send a silent scheduled message! - result.silent = silent || base::IsCtrlPressed(); - result.scheduled = scheduleDate; + if (base::IsCtrlPressed()) { + options.silent = true; + } const auto copy = done; box->closeBox(); - copy(result); + copy(options); + }; + const auto with = [=](TimeId scheduled) { + auto result = initialOptions; + result.scheduled = scheduled; + return result; }; auto descriptor = Ui::ChooseDateTimeBox(box, { .title = (details.type == SendMenu::Type::Reminder ? tr::lng_remind_title() : tr::lng_schedule_title()), .submit = tr::lng_schedule_button(), - .done = [=](TimeId result) { save(false, result); }, + .done = [=](TimeId result) { submit(with(result)); }, .time = time, .style = style.chooseDateTimeArgs, }); @@ -105,9 +111,16 @@ void ScheduleBox( .effectAllowed = details.effectAllowed, }; const auto sendAction = crl::guard(box, [=](Action action, Details) { - save( - v::get(action).silent, - descriptor.collect()); + Expects(action.type == ActionType::Send); + + auto options = with(descriptor.collect()); + if (action.options.silent) { + options.silent = action.options.silent; + } + if (action.options.effectId) { + options.effectId = action.options.effectId; + } + submit(options); }); SetupMenuAndShortcuts( descriptor.submit.data(), @@ -120,7 +133,7 @@ void ScheduleBox( const auto timestamp = Api::kScheduledUntilOnlineTimestamp; FillSendUntilOnlineMenu( sendUntilOnline.data(), - [=] { save(false, timestamp); }, + [=] { submit(with(timestamp)); }, style); } } diff --git a/Telegram/SourceFiles/history/view/history_view_schedule_box.h b/Telegram/SourceFiles/history/view/history_view_schedule_box.h index a69177102..5f5385c73 100644 --- a/Telegram/SourceFiles/history/view/history_view_schedule_box.h +++ b/Telegram/SourceFiles/history/view/history_view_schedule_box.h @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "api/api_common.h" #include "ui/boxes/choose_date_time.h" namespace style { @@ -14,10 +15,6 @@ struct IconButton; struct PopupMenu; } // namespace style -namespace Api { -struct SendOptions; -} // namespace Api - namespace ChatHelpers { class Show; } // namespace ChatHelpers @@ -41,6 +38,7 @@ struct ScheduleBoxStyleArgs { void ScheduleBox( not_null box, std::shared_ptr show, + const Api::SendOptions &initialOptions, const SendMenu::Details &details, Fn done, TimeId time, @@ -52,11 +50,13 @@ template std::shared_ptr show, const SendMenu::Details &details, Submit &&submit, + const Api::SendOptions &initialOptions = {}, TimeId scheduleTime = DefaultScheduleTime(), ScheduleBoxStyleArgs style = ScheduleBoxStyleArgs()) { return Box( ScheduleBox, std::move(show), + initialOptions, details, crl::guard(std::forward(guard), std::forward(submit)), scheduleTime, diff --git a/Telegram/SourceFiles/menu/menu_send.cpp b/Telegram/SourceFiles/menu/menu_send.cpp index 98645e79f..89be56356 100644 --- a/Telegram/SourceFiles/menu/menu_send.cpp +++ b/Telegram/SourceFiles/menu/menu_send.cpp @@ -214,9 +214,8 @@ void BottomRounded::paintEvent(QPaintEvent *e) { EffectId id, Fn done) { return [=](Action action, Details details) { - if (const auto options = std::get_if(&action)) { - options->effectId = id; - } + action.options.effectId = id; + const auto onstack = done; sendAction(action, details); if (onstack) { @@ -511,7 +510,7 @@ bool EffectPreview::canSend() const { void EffectPreview::setupSend(Details details) { if (_send) { _send->setClickedCallback([=] { - _actionWithEffect(Api::SendOptions(), details); + _actionWithEffect({}, details); }); const auto type = details.type; SetupMenuAndShortcuts(_send.get(), _show, [=] { @@ -588,18 +587,20 @@ Fn DefaultCallback( Fn send) { const auto guard = Ui::MakeWeak(show->toastParent()); return [=](Action action, Details details) { - if (const auto options = std::get_if(&action)) { - send(*options); - } else if (v::get(action) == ActionType::Send) { - send({}); - } else { - using namespace HistoryView; - auto box = PrepareScheduleBox(guard, show, details, send); - const auto weak = Ui::MakeWeak(box.data()); - show->showBox(std::move(box)); - if (const auto strong = weak.data()) { - strong->setCloseByOutsideClick(false); - } + if (action.type == ActionType::Send) { + send(action.options); + return; + } + auto box = HistoryView::PrepareScheduleBox( + guard, + show, + details, + send, + action.options); + const auto weak = Ui::MakeWeak(box.data()); + show->showBox(std::move(box)); + if (const auto strong = weak.data()) { + strong->setCloseByOutsideClick(false); } }; } @@ -622,7 +623,9 @@ FillMenuResult FillSendMenu( if (type != Type::Reminder) { menu->addAction( tr::lng_send_silent_message(tr::now), - [=] { action(Api::SendOptions{ .silent = true }, details); }, + [=] { action( + { Api::SendOptions{ .silent = true } }, + details); }, &icons.menuMute); } if (type != Type::SilentOnly) { @@ -630,13 +633,15 @@ FillMenuResult FillSendMenu( (type == Type::Reminder ? tr::lng_reminder_message(tr::now) : tr::lng_schedule_message(tr::now)), - [=] { action(ActionType::Schedule, details); }, + [=] { action({ .type = ActionType::Schedule }, details); }, &icons.menuSchedule); } if (type == Type::ScheduledToUser) { menu->addAction( tr::lng_scheduled_send_until_online(tr::now), - [=] { action(Api::DefaultSendWhenOnlineOptions(), details); }, + [=] { action( + { Api::DefaultSendWhenOnlineOptions() }, + details); }, &icons.menuWhenOnline); } @@ -730,14 +735,14 @@ void SetupMenuAndShortcuts( ((now != Type::Reminder) && request->check(Command::SendSilentMessage) && request->handle([=] { - action(Api::SendOptions{ .silent = true }, details()); + action({ Api::SendOptions{ .silent = true } }, details()); return true; })) || ((now != Type::SilentOnly) && request->check(Command::ScheduleMessage) && request->handle([=] { - action(ActionType::Schedule, details()); + action({ .type = ActionType::Schedule }, details()); return true; })) || diff --git a/Telegram/SourceFiles/menu/menu_send.h b/Telegram/SourceFiles/menu/menu_send.h index 66004c179..f503a7e56 100644 --- a/Telegram/SourceFiles/menu/menu_send.h +++ b/Telegram/SourceFiles/menu/menu_send.h @@ -7,14 +7,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "api/api_common.h" + namespace style { struct ComposeIcons; } // namespace style -namespace Api { -struct SendOptions; -} // namespace Api - namespace ChatHelpers { class Show; } // namespace ChatHelpers @@ -54,7 +52,12 @@ enum class ActionType { Send, Schedule, }; -using Action = std::variant; +struct Action { + using Type = ActionType; + + Api::SendOptions options; + Type type = Type::Send; +}; [[nodiscard]] Fn DefaultCallback( std::shared_ptr show, Fn send);