Added ability to Save / Delete GIFs from menu for inline bots.

Fixed #10511.
This commit is contained in:
23rd 2021-03-13 12:52:15 +03:00
parent b373a9ed22
commit a5abe3d813
4 changed files with 50 additions and 30 deletions

View file

@ -46,19 +46,31 @@ constexpr auto kSearchBotUsername = "gif"_cs;
} // namespace } // namespace
void DeleteSavedGif(not_null<DocumentData*> document) { void AddGifAction(
Fn<void(QString, Fn<void()> &&)> callback,
not_null<DocumentData*> document) {
if (!document->isGifv()) {
return;
}
auto &data = document->owner(); 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( Api::ToggleSavedGif(
document, document,
Data::FileOriginSavedGifs(), Data::FileOriginSavedGifs(),
false); !saved);
const auto index = data.stickers().savedGifs().indexOf(document); auto &data = document->owner();
if (index >= 0) { if (saved) {
data.stickers().savedGifsRef().remove(index); data.stickers().savedGifsRef().remove(index);
document->session().local().writeSavedGifs(); document->session().local().writeSavedGifs();
} }
data.stickers().notifySavedGifsUpdated(); data.stickers().notifySavedGifsUpdated();
});
} }
class GifsListWidget::Footer : public TabbedSelector::InnerFooter { class GifsListWidget::Footer : public TabbedSelector::InnerFooter {
@ -381,23 +393,18 @@ void GifsListWidget::fillContextMenu(
SendMenu::DefaultSilentCallback(send), SendMenu::DefaultSilentCallback(send),
SendMenu::DefaultScheduleCallback(this, type, send)); SendMenu::DefaultScheduleCallback(this, type, send));
[&] { if (!(row >= _rows.size() || column >= _rows[row].items.size())) {
const auto row = _selected / MatrixRowShift;
const auto column = _selected % MatrixRowShift;
if (row >= _rows.size() || column >= _rows[row].items.size()) {
return;
}
const auto item = _rows[row].items[column]; const auto item = _rows[row].items[column];
if (const auto document = item->getDocument()) { const auto document = item->getDocument()
auto &data = document->owner(); ? item->getDocument() // Saved GIF.
if (data.stickers().savedGifs().indexOf(document) < 0) { : item->getPreviewDocument(); // Searched GIF.
return; if (document) {
auto callback = [&](const QString &text, Fn<void()> &&done) {
menu->addAction(text, std::move(done));
};
AddGifAction(std::move(callback), document);
} }
menu->addAction(tr::lng_context_delete_gif(tr::now), [=] { };
ChatHelpers::DeleteSavedGif(document);
});
}
}();
} }
void GifsListWidget::mouseReleaseEvent(QMouseEvent *e) { void GifsListWidget::mouseReleaseEvent(QMouseEvent *e) {

View file

@ -40,7 +40,9 @@ enum class Type;
namespace ChatHelpers { namespace ChatHelpers {
void DeleteSavedGif(not_null<DocumentData*> document); void AddGifAction(
Fn<void(QString, Fn<void()> &&)> callback,
not_null<DocumentData*> document);
class GifsListWidget class GifsListWidget
: public TabbedSelector::Inner : public TabbedSelector::Inner

View file

@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_photo_media.h" #include "data/data_photo_media.h"
#include "data/data_document_media.h" #include "data/data_document_media.h"
#include "data/stickers/data_stickers.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 "chat_helpers/stickers_lottie.h"
#include "inline_bots/inline_bot_result.h" #include "inline_bots/inline_bot_result.h"
#include "lottie/lottie_single_player.h" #include "lottie/lottie_single_player.h"
@ -127,7 +127,9 @@ void Gif::setPosition(int32 position) {
} }
void DeleteSavedGifClickHandler::onClickImpl() const { void DeleteSavedGifClickHandler::onClickImpl() const {
ChatHelpers::DeleteSavedGif(_data); ChatHelpers::AddGifAction(
[](QString, Fn<void()> &&done) { done(); },
_data);
} }
int Gif::resizeGetHeight(int width) { int Gif::resizeGetHeight(int width) {

View file

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "inline_bots/inline_results_inner.h" #include "inline_bots/inline_results_inner.h"
#include "api/api_common.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 "chat_helpers/send_context_menu.h" // SendMenu::FillSendMenu
#include "data/data_file_origin.h" #include "data/data_file_origin.h"
#include "data/data_user.h" #include "data/data_user.h"
@ -305,6 +306,14 @@ void Inner::contextMenuEvent(QContextMenuEvent *e) {
SendMenu::DefaultSilentCallback(send), SendMenu::DefaultSilentCallback(send),
SendMenu::DefaultScheduleCallback(this, type, send)); SendMenu::DefaultScheduleCallback(this, type, send));
auto item = _rows[row].items[column];
if (const auto previewDocument = item->getPreviewDocument()) {
auto callback = [&](const QString &text, Fn<void()> &&done) {
_menu->addAction(text, std::move(done));
};
ChatHelpers::AddGifAction(std::move(callback), previewDocument);
}
if (!_menu->empty()) { if (!_menu->empty()) {
_menu->popup(QCursor::pos()); _menu->popup(QCursor::pos());
} }