From a5abe3d813e1e0dd59f254c7b5f3d95b4e7fc164 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 13 Mar 2021 12:52:15 +0300 Subject: [PATCH] Added ability to Save / Delete GIFs from menu for inline bots. Fixed #10511. --- .../chat_helpers/gifs_list_widget.cpp | 61 +++++++++++-------- .../chat_helpers/gifs_list_widget.h | 4 +- .../inline_bot_layout_internal.cpp | 6 +- .../inline_bots/inline_results_inner.cpp | 9 +++ 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp index ab46361a9f..5e1c93333d 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp @@ -46,19 +46,31 @@ constexpr auto kSearchBotUsername = "gif"_cs; } // namespace -void DeleteSavedGif(not_null document) { - auto &data = document->owner(); - Api::ToggleSavedGif( - document, - Data::FileOriginSavedGifs(), - false); - - const auto index = data.stickers().savedGifs().indexOf(document); - if (index >= 0) { - data.stickers().savedGifsRef().remove(index); - document->session().local().writeSavedGifs(); +void AddGifAction( + Fn &&)> callback, + not_null document) { + if (!document->isGifv()) { + return; } - data.stickers().notifySavedGifsUpdated(); + auto &data = document->owner(); + const auto index = data.stickers().savedGifs().indexOf(document); + const auto saved = (index >= 0); + const auto text = (saved + ? tr::lng_context_delete_gif + : tr::lng_context_save_gif)(tr::now); + callback(text, [=] { + Api::ToggleSavedGif( + document, + Data::FileOriginSavedGifs(), + !saved); + + auto &data = document->owner(); + if (saved) { + data.stickers().savedGifsRef().remove(index); + document->session().local().writeSavedGifs(); + } + data.stickers().notifySavedGifsUpdated(); + }); } class GifsListWidget::Footer : public TabbedSelector::InnerFooter { @@ -381,23 +393,18 @@ void GifsListWidget::fillContextMenu( SendMenu::DefaultSilentCallback(send), SendMenu::DefaultScheduleCallback(this, type, send)); - [&] { - const auto row = _selected / MatrixRowShift; - const auto column = _selected % MatrixRowShift; - if (row >= _rows.size() || column >= _rows[row].items.size()) { - return; - } + if (!(row >= _rows.size() || column >= _rows[row].items.size())) { const auto item = _rows[row].items[column]; - if (const auto document = item->getDocument()) { - auto &data = document->owner(); - if (data.stickers().savedGifs().indexOf(document) < 0) { - return; - } - menu->addAction(tr::lng_context_delete_gif(tr::now), [=] { - ChatHelpers::DeleteSavedGif(document); - }); + const auto document = item->getDocument() + ? item->getDocument() // Saved GIF. + : item->getPreviewDocument(); // Searched GIF. + if (document) { + auto callback = [&](const QString &text, Fn &&done) { + menu->addAction(text, std::move(done)); + }; + AddGifAction(std::move(callback), document); } - }(); + }; } void GifsListWidget::mouseReleaseEvent(QMouseEvent *e) { diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h index f1235213f1..0f1bce421b 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.h @@ -40,7 +40,9 @@ enum class Type; namespace ChatHelpers { -void DeleteSavedGif(not_null document); +void AddGifAction( + Fn &&)> callback, + not_null document); class GifsListWidget : public TabbedSelector::Inner diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp index 5b6086416b..a88bb5dfea 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_photo_media.h" #include "data/data_document_media.h" #include "data/stickers/data_stickers.h" -#include "chat_helpers/gifs_list_widget.h" // ChatHelpers::DeleteSavedGif +#include "chat_helpers/gifs_list_widget.h" // ChatHelpers::AddGifAction #include "chat_helpers/stickers_lottie.h" #include "inline_bots/inline_bot_result.h" #include "lottie/lottie_single_player.h" @@ -127,7 +127,9 @@ void Gif::setPosition(int32 position) { } void DeleteSavedGifClickHandler::onClickImpl() const { - ChatHelpers::DeleteSavedGif(_data); + ChatHelpers::AddGifAction( + [](QString, Fn &&done) { done(); }, + _data); } int Gif::resizeGetHeight(int width) { diff --git a/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp b/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp index 9c837b05ef..48b77fe2e1 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_results_inner.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_results_inner.h" #include "api/api_common.h" +#include "chat_helpers/gifs_list_widget.h" // ChatHelpers::AddGifAction #include "chat_helpers/send_context_menu.h" // SendMenu::FillSendMenu #include "data/data_file_origin.h" #include "data/data_user.h" @@ -305,6 +306,14 @@ void Inner::contextMenuEvent(QContextMenuEvent *e) { SendMenu::DefaultSilentCallback(send), SendMenu::DefaultScheduleCallback(this, type, send)); + auto item = _rows[row].items[column]; + if (const auto previewDocument = item->getPreviewDocument()) { + auto callback = [&](const QString &text, Fn &&done) { + _menu->addAction(text, std::move(done)); + }; + ChatHelpers::AddGifAction(std::move(callback), previewDocument); + } + if (!_menu->empty()) { _menu->popup(QCursor::pos()); }