From 1fee7d1a4187ac8eebdc427dbced71d480b011e8 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 7 Aug 2020 19:43:00 +0300 Subject: [PATCH] Added ability to schedule and send silently media from inline bots. --- .../SourceFiles/history/history_widget.cpp | 5 +- .../inline_bots/inline_results_widget.cpp | 51 ++++++++++++++++++- .../inline_bots/inline_results_widget.h | 16 ++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 34540cb9f5..2f3ac4ee79 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1199,8 +1199,9 @@ void HistoryWidget::applyInlineBotQuery(UserData *bot, const QString &query) { _inlineResults.create(this, controller()); _inlineResults->setResultSelectedCallback([=]( InlineBots::Result *result, - UserData *bot) { - sendInlineResult(result, bot, Api::SendOptions()); + UserData *bot, + Api::SendOptions options) { + sendInlineResult(result, bot, options); }); _inlineResults->requesting( ) | rpl::start_with_next([=](bool requesting) { diff --git a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp index 23aee7c0ec..828f00e7d5 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "inline_bots/inline_results_widget.h" +#include "api/api_common.h" +#include "chat_helpers/message_field.h" // FillSendMenu #include "data/data_photo.h" #include "data/data_document.h" #include "data/data_channel.h" @@ -15,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_changes.h" #include "ui/widgets/buttons.h" +#include "ui/widgets/popup_menu.h" #include "ui/widgets/shadow.h" #include "ui/effects/ripple_animation.h" #include "ui/image/image_prepare.h" @@ -31,6 +34,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/scroll_area.h" #include "ui/widgets/labels.h" #include "history/view/history_view_cursor_state.h" +#include "history/view/history_view_schedule_box.h" #include "facades.h" #include "app.h" #include "styles/style_chat_helpers.h" @@ -254,14 +258,24 @@ void Inner::mouseReleaseEvent(QMouseEvent *e) { } void Inner::selectInlineResult(int row, int column) { + selectInlineResult(row, column, Api::SendOptions()); +} + +void Inner::selectInlineResult( + int row, + int column, + Api::SendOptions options) { if (row >= _rows.size() || column >= _rows.at(row).items.size()) { return; } auto item = _rows[row].items[column]; - if (auto inlineResult = item->getResult()) { + if (const auto inlineResult = item->getResult()) { if (inlineResult->onChoose(item)) { - _resultSelectedCallback(inlineResult, _inlineBot); + _resultSelectedCallback( + inlineResult, + _inlineBot, + std::move(options)); } } } @@ -285,6 +299,39 @@ void Inner::enterFromChildEvent(QEvent *e, QWidget *child) { updateSelected(); } +void Inner::contextMenuEvent(QContextMenuEvent *e) { + if (_selected < 0 || _pressed >= 0) { + return; + } + const auto row = _selected / MatrixRowShift; + const auto column = _selected % MatrixRowShift; + const auto type = SendMenuType::Scheduled; + + _menu = base::make_unique_q(this); + + const auto send = [=](Api::SendOptions options) { + selectInlineResult(row, column, options); + }; + const auto silent = [=] { send({ .silent = true }); }; + const auto schedule = [=] { + Ui::show( + HistoryView::PrepareScheduleBox( + this, + type, + [=](Api::SendOptions options) { send(options); }), + Ui::LayerOption::KeepOther); + }; + FillSendMenu( + _menu, + [&] { return type; }, + silent, + schedule); + + if (!_menu->actions().empty()) { + _menu->popup(QCursor::pos()); + } +} + void Inner::clearSelection() { if (_selected >= 0) { int srow = _selected / MatrixRowShift, scol = _selected % MatrixRowShift; diff --git a/Telegram/SourceFiles/inline_bots/inline_results_widget.h b/Telegram/SourceFiles/inline_bots/inline_results_widget.h index a86cda86a4..bec1ccb31a 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_widget.h +++ b/Telegram/SourceFiles/inline_bots/inline_results_widget.h @@ -18,6 +18,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include +namespace Api { +struct SendOptions; +} // namespace Api + namespace Ui { class ScrollArea; class IconButton; @@ -25,6 +29,7 @@ class LinkButton; class RoundButton; class FlatLabel; class RippleAnimation; +class PopupMenu; } // namespace Ui namespace Window { @@ -44,6 +49,7 @@ namespace internal { constexpr int kInlineItemsMaxPerRow = 5; using Results = std::vector>; +using ResultSelected = Fn; struct CacheEntry { QString nextOffset; @@ -79,7 +85,7 @@ public: int countHeight(); - void setResultSelectedCallback(Fn callback) { + void setResultSelectedCallback(ResultSelected callback) { _resultSelectedCallback = std::move(callback); } @@ -102,6 +108,7 @@ protected: void leaveEventHook(QEvent *e) override; void leaveToChildEvent(QEvent *e, QWidget *child) override; void enterFromChildEvent(QEvent *e, QWidget *child) override; + void contextMenuEvent(QContextMenuEvent *e) override; private slots: void onSwitchPm(); @@ -140,6 +147,7 @@ private: int validateExistingInlineRows(const Results &results); void selectInlineResult(int row, int column); + void selectInlineResult(int row, int column, Api::SendOptions options); not_null _controller; @@ -157,6 +165,8 @@ private: object_ptr _restrictedLabel = { nullptr }; + base::unique_qptr _menu; + QVector _rows; std::map> _inlineLayouts; @@ -168,7 +178,7 @@ private: base::Timer _previewTimer; bool _previewShown = false; - Fn _resultSelectedCallback; + ResultSelected _resultSelectedCallback; }; @@ -195,7 +205,7 @@ public: void showAnimated(); void hideAnimated(); - void setResultSelectedCallback(Fn callback) { + void setResultSelectedCallback(internal::ResultSelected callback) { _inner->setResultSelectedCallback(std::move(callback)); }