From 1cfcbb94ec6eb449d0deacc98b5bfea0b1765685 Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Sun, 11 Sep 2022 02:44:04 +0300 Subject: [PATCH] [Improvement] Allow to send inline result preview --- Telegram/Resources/langs/rewrites/en.json | 4 ++ .../SourceFiles/history/history_widget.cpp | 52 +++++++++++++---- Telegram/SourceFiles/history/history_widget.h | 2 + .../view/history_view_replies_section.cpp | 57 +++++++++++++------ .../view/history_view_replies_section.h | 2 + .../view/history_view_scheduled_section.cpp | 42 ++++++++++---- .../view/history_view_scheduled_section.h | 2 + .../inline_bots/inline_bot_result.h | 1 + .../inline_bots/inline_results_inner.cpp | 21 ++++++- .../inline_bots/inline_results_inner.h | 3 +- Telegram/SourceFiles/menu/menu_send.cpp | 35 ++++++++++++ Telegram/SourceFiles/menu/menu_send.h | 8 +++ 12 files changed, 187 insertions(+), 42 deletions(-) diff --git a/Telegram/Resources/langs/rewrites/en.json b/Telegram/Resources/langs/rewrites/en.json index c9ae3e3e2..28ff4dcd0 100644 --- a/Telegram/Resources/langs/rewrites/en.json +++ b/Telegram/Resources/langs/rewrites/en.json @@ -166,6 +166,10 @@ "ktg_filters_hide_all_chats_toast": "\"All Chats\" folder is hidden.\nYou can enable it back in Kotatogram Settings.", "ktg_filters_hide_edit_toast": "Edit button is hidden.\nYou can enable it back in Kotatogram Settings.", "ktg_settings_telegram_sites_autologin": "Auto-login on Telegram sites", + "ktg_send_preview": "Send preview", + "ktg_send_silent_preview": "Send preview silent", + "ktg_reminder_preview": "Remind with preview", + "ktg_schedule_preview": "Send preview scheduled", "ktg_forward_sender_names_and_captions_removed": "Sender names and captions removed", "ktg_forward_remember_mode": "Remember forward mode", "ktg_forward_mode": "Forward mode", diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index fc9a01a48..d9e2978a3 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1202,7 +1202,20 @@ void HistoryWidget::initTabbedSelector() { } return (data.recipientOverride == _peer); }) | rpl::start_with_next([=](ChatHelpers::InlineChosen data) { - sendInlineResult(data); + if (data.sendPreview) { + const auto request = data.result->openRequest(); + if (const auto photo = request.photo()) { + sendExistingPhoto(photo, data.options); + } else if (const auto document = request.document()) { + sendExistingDocument(document, data.options); + } + + addRecentBot(data.bot); + clearFieldText(); + saveCloudDraft(); + } else { + sendInlineResult(data); + } }, lifetime()); selector->contextMenuRequested( @@ -1574,6 +1587,17 @@ void HistoryWidget::applyInlineBotQuery(UserData *bot, const QString &query) { } else if (const auto document = request.document()) { controller()->openDocument(document, false, {}); } + } else if (result.sendPreview) { + const auto request = result.result->openRequest(); + if (const auto photo = request.photo()) { + sendExistingPhoto(photo, result.options); + } else if (const auto document = request.document()) { + sendExistingDocument(document, result.options); + } + + addRecentBot(result.bot); + clearFieldText(); + saveCloudDraft(); } else { sendInlineResult(result); } @@ -6920,17 +6944,7 @@ void HistoryWidget::sendInlineResult(InlineBots::ResultSelected result) { _saveDraftStart = crl::now(); saveDraft(); - auto &bots = cRefRecentInlineBots(); - const auto index = bots.indexOf(result.bot); - if (index) { - if (index > 0) { - bots.removeAt(index); - } else if (bots.size() >= RecentInlineBotsLimit) { - bots.resize(RecentInlineBotsLimit - 1); - } - bots.push_front(result.bot); - session().local().writeRecentHashtagsAndBots(); - } + addRecentBot(result.bot); hideSelectorControlsAnimated(); @@ -8150,6 +8164,20 @@ void HistoryWidget::messageDataReceived( } } +void HistoryWidget::addRecentBot(not_null bot) { + auto &bots = cRefRecentInlineBots(); + const auto index = bots.indexOf(bot); + if (index) { + if (index > 0) { + bots.removeAt(index); + } else if (bots.size() >= RecentInlineBotsLimit) { + bots.resize(RecentInlineBotsLimit - 1); + } + bots.push_front(bot); + session().local().writeRecentHashtagsAndBots(); + } +} + void HistoryWidget::updateReplyEditText(not_null item) { const auto context = Core::MarkedTextContext{ .session = &session(), diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index e8e59f4c5..164705481 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -389,6 +389,8 @@ private: void requestMessageData(MsgId msgId); void messageDataReceived(not_null peer, MsgId msgId); + void addRecentBot(not_null bot); + [[nodiscard]] Api::SendAction prepareSendAction( Api::SendOptions options) const; void send(Api::SendOptions options); diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index c4e6cf163..d960ba81a 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -69,6 +69,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "main/main_session_settings.h" #include "data/data_session.h" +#include "data/data_document.h" #include "data/data_user.h" #include "data/data_chat.h" #include "data/data_channel.h" @@ -804,7 +805,20 @@ void RepliesWidget::setupComposeControls() { controller()->sendingAnimation().appendSending( chosen.messageSendingFrom); const auto localId = chosen.messageSendingFrom.localId; - sendInlineResult(chosen.result, chosen.bot, chosen.options, localId); + if (chosen.sendPreview) { + const auto request = chosen.result->openRequest(); + if (const auto photo = request.photo()) { + sendExistingPhoto(photo, chosen.options); + } else if (const auto document = request.document()) { + sendExistingDocument(document, chosen.options, localId); + } + + addRecentBot(chosen.bot); + _composeControls->clear(); + finishSending(); + } else { + sendInlineResult(chosen.result, chosen.bot, chosen.options, localId); + } }, lifetime()); _composeControls->jumpToItemRequests( @@ -1155,6 +1169,20 @@ bool RepliesWidget::showSendingFilesError( return true; } +void RepliesWidget::addRecentBot(not_null bot) { + auto &bots = cRefRecentInlineBots(); + const auto index = bots.indexOf(bot); + if (index) { + if (index > 0) { + bots.removeAt(index); + } else if (bots.size() >= RecentInlineBotsLimit) { + bots.resize(RecentInlineBotsLimit - 1); + } + bots.push_front(bot); + bot->session().local().writeRecentHashtagsAndBots(); + } +} + Api::SendAction RepliesWidget::prepareSendAction( Api::SendOptions options) const { auto result = Api::SendAction(_history, options); @@ -1380,10 +1408,17 @@ bool RepliesWidget::sendExistingDocument( return false; } - Api::SendExistingDocument( - Api::MessageToSend(prepareSendAction(options)), - document, - localId); + if (document->hasRemoteLocation()) { + Api::SendExistingDocument( + Api::MessageToSend(prepareSendAction(options)), + document, + localId); + } else { + Api::SendWebDocument( + Api::MessageToSend(prepareSendAction(options)), + document, + localId); + } _composeControls->cancelReplyMessage(); finishSending(); @@ -1454,17 +1489,7 @@ void RepliesWidget::sendInlineResult( //_saveDraftStart = crl::now(); //onDraftSave(); - auto &bots = cRefRecentInlineBots(); - const auto index = bots.indexOf(bot); - if (index) { - if (index > 0) { - bots.removeAt(index); - } else if (bots.size() >= RecentInlineBotsLimit) { - bots.resize(RecentInlineBotsLimit - 1); - } - bots.push_front(bot); - bot->session().local().writeRecentHashtagsAndBots(); - } + addRecentBot(bot); finishSending(); } diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.h b/Telegram/SourceFiles/history/view/history_view_replies_section.h index 3da1c44f0..3bb10d34e 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.h +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.h @@ -238,6 +238,8 @@ private: void clearSelected(); void setPinnedVisibility(bool shown); + void addRecentBot(not_null bot); + [[nodiscard]] Api::SendAction prepareSendAction( Api::SendOptions options) const; void send(); diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index ca81ba290..596f567a0 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -276,7 +276,21 @@ void ScheduledWidget::setupComposeControls() { _composeControls->inlineResultChosen( ) | rpl::start_with_next([=](ChatHelpers::InlineChosen chosen) { - sendInlineResult(chosen.result, chosen.bot); + if (chosen.sendPreview) { + const auto request = chosen.result->openRequest(); + if (const auto photo = request.photo()) { + sendExistingPhoto(photo); + } else if (const auto document = request.document()) { + sendExistingDocument(document); + } + + addRecentBot(chosen.bot); + _composeControls->clear(); + _composeControls->hidePanelsAnimated(); + _composeControls->focus(); + } else { + sendInlineResult(chosen.result, chosen.bot); + } }, lifetime()); _composeControls->jumpToItemRequests( @@ -570,6 +584,20 @@ bool ScheduledWidget::showSendingFilesError( return true; } +void ScheduledWidget::addRecentBot(not_null bot) { + auto &bots = cRefRecentInlineBots(); + const auto index = bots.indexOf(bot); + if (index) { + if (index > 0) { + bots.removeAt(index); + } else if (bots.size() >= RecentInlineBotsLimit) { + bots.resize(RecentInlineBotsLimit - 1); + } + bots.push_front(bot); + bot->session().local().writeRecentHashtagsAndBots(); + } +} + Api::SendAction ScheduledWidget::prepareSendAction( Api::SendOptions options) const { auto result = Api::SendAction(_history, options); @@ -805,17 +833,7 @@ void ScheduledWidget::sendInlineResult( //_saveDraftStart = crl::now(); //onDraftSave(); - auto &bots = cRefRecentInlineBots(); - const auto index = bots.indexOf(bot); - if (index) { - if (index > 0) { - bots.removeAt(index); - } else if (bots.size() >= RecentInlineBotsLimit) { - bots.resize(RecentInlineBotsLimit - 1); - } - bots.push_front(bot); - bot->session().local().writeRecentHashtagsAndBots(); - } + addRecentBot(bot); _composeControls->hidePanelsAnimated(); _composeControls->focus(); diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.h b/Telegram/SourceFiles/history/view/history_view_scheduled_section.h index 857410b4f..5fc994a43 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.h +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.h @@ -196,6 +196,8 @@ private: void confirmDeleteSelected(); void clearSelected(); + void addRecentBot(not_null bot); + [[nodiscard]] Api::SendAction prepareSendAction( Api::SendOptions options) const; void send(); diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.h b/Telegram/SourceFiles/inline_bots/inline_bot_result.h index 296deb613..efcbfd197 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_result.h +++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.h @@ -137,6 +137,7 @@ struct ResultSelected { Ui::MessageSendingAnimationFrom messageSendingFrom; // Open in OverlayWidget; bool open = false; + bool sendPreview = false; }; } // namespace InlineBots diff --git a/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp b/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp index 0103789ec..f3523a3dd 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp @@ -262,7 +262,8 @@ void Inner::mouseReleaseEvent(QMouseEvent *e) { void Inner::selectInlineResult( int index, Api::SendOptions options, - bool open) { + bool open, + bool sendPreview) { const auto item = _mosaic.maybeItemAt(index); if (!item) { return; @@ -301,6 +302,7 @@ void Inner::selectInlineResult( .options = std::move(options), .messageSendingFrom = messageSendingFrom(), .open = open, + .sendPreview = sendPreview, }); } } @@ -347,8 +349,23 @@ void Inner::contextMenuEvent(QContextMenuEvent *e) { SendMenu::DefaultScheduleCallback(this, type, send), SendMenu::DefaultWhenOnlineCallback(send)); + const auto hideViaActions = [&] { + const auto sendPreview = [=, selected = _selected](Api::SendOptions options) { + selectInlineResult(selected, options, false, true); + }; + + SendMenu::FillSendPreviewMenu( + _menu, + type, + [=] { sendPreview({}); }, + SendMenu::DefaultSilentCallback(sendPreview), + SendMenu::DefaultScheduleCallback(this, type, sendPreview)); + }; + const auto item = _mosaic.itemAt(_selected); if (const auto previewDocument = item->getPreviewDocument()) { + hideViaActions(); + auto callback = [&]( const QString &text, Fn &&done, @@ -359,6 +376,8 @@ void Inner::contextMenuEvent(QContextMenuEvent *e) { std::move(callback), _controller->uiShow(), previewDocument); + } else if (const auto previewPhoto = item->getPreviewPhoto()) { + hideViaActions(); } if (!_menu->empty()) { diff --git a/Telegram/SourceFiles/inline_bots/inline_results_inner.h b/Telegram/SourceFiles/inline_bots/inline_results_inner.h index 5aa22578a..56725360a 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_inner.h +++ b/Telegram/SourceFiles/inline_bots/inline_results_inner.h @@ -142,7 +142,8 @@ private: void selectInlineResult( int index, Api::SendOptions options, - bool open); + bool open, + bool sendPreview = false); not_null _controller; const std::unique_ptr _pathGradient; diff --git a/Telegram/SourceFiles/menu/menu_send.cpp b/Telegram/SourceFiles/menu/menu_send.cpp index 02ab8ed4b..f0954af2c 100644 --- a/Telegram/SourceFiles/menu/menu_send.cpp +++ b/Telegram/SourceFiles/menu/menu_send.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "menu/menu_send.h" +#include "kotato/kotato_lang.h" #include "api/api_common.h" #include "base/event_filter.h" #include "boxes/abstract_box.h" @@ -67,6 +68,7 @@ FillMenuResult FillSendMenu( : st::defaultComposeIcons; const auto now = type; if (now == Type::Disabled + || now == Type::PreviewOnly || (!silent && now == Type::SilentOnly)) { return FillMenuResult::None; } @@ -94,6 +96,38 @@ FillMenuResult FillSendMenu( return FillMenuResult::Success; } +FillMenuResult FillSendPreviewMenu( + not_null menu, + Type type, + Fn defaultSend, + Fn silent, + Fn schedule) { + if (!defaultSend && !silent && !schedule) { + return FillMenuResult::None; + } + const auto now = type; + if (now == Type::Disabled) { + return FillMenuResult::None; + } + + if (defaultSend) { + menu->addAction(ktr("ktg_send_preview"), defaultSend); + } + if (type != Type::PreviewOnly) { + if (silent && now != Type::Reminder) { + menu->addAction(ktr("ktg_send_silent_preview"), silent); + } + if (schedule && now != Type::SilentOnly) { + menu->addAction( + (now == Type::Reminder + ? ktr("ktg_reminder_preview") + : ktr("ktg_schedule_preview")), + schedule); + } + } + return FillMenuResult::Success; +} + void SetupMenuAndShortcuts( not_null button, Fn type, @@ -130,6 +164,7 @@ void SetupMenuAndShortcuts( const auto now = type(); if (now == Type::Disabled + || now == Type::PreviewOnly || (!silent && now == Type::SilentOnly)) { return; } diff --git a/Telegram/SourceFiles/menu/menu_send.h b/Telegram/SourceFiles/menu/menu_send.h index 79edc3840..ff1f9afcd 100644 --- a/Telegram/SourceFiles/menu/menu_send.h +++ b/Telegram/SourceFiles/menu/menu_send.h @@ -32,6 +32,7 @@ enum class Type { Scheduled, ScheduledToUser, // For "Send when online". Reminder, + PreviewOnly, }; enum class FillMenuResult { @@ -54,6 +55,13 @@ FillMenuResult FillSendMenu( Fn whenOnline, const style::ComposeIcons *iconsOverride = nullptr); +FillMenuResult FillSendPreviewMenu( + not_null menu, + Type type, + Fn defaultSend, + Fn silent, + Fn schedule); + void SetupMenuAndShortcuts( not_null button, Fn type,