From 1eea07d88a9174e489fb907375f9052b0950560d Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 9 Aug 2020 17:52:51 +0300 Subject: [PATCH] Added ability to schedule and send silently stickers from autocomplete. --- .../chat_helpers/field_autocomplete.cpp | 57 +++++++++++++++---- .../chat_helpers/field_autocomplete.h | 9 +++ .../SourceFiles/history/history_widget.cpp | 2 +- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 847fb71356..fe47fe47d0 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_session.h" #include "data/stickers/data_stickers.h" +#include "chat_helpers/send_context_menu.h" // FillSendMenu #include "chat_helpers/stickers_lottie.h" #include "mainwindow.h" #include "apiwrap.h" @@ -24,6 +25,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "core/core_settings.h" #include "lottie/lottie_single_player.h" +#include "ui/widgets/popup_menu.h" #include "ui/widgets/scroll_area.h" #include "ui/image/image.h" #include "ui/ui_utility.h" @@ -901,26 +903,38 @@ bool FieldAutocompleteInner::moveSel(int key) { return true; } -bool FieldAutocompleteInner::chooseSelected(FieldAutocomplete::ChooseMethod method) const { +bool FieldAutocompleteInner::chooseSelected( + FieldAutocomplete::ChooseMethod method) const { + return chooseAtIndex(method, _sel); +} + +bool FieldAutocompleteInner::chooseAtIndex( + FieldAutocomplete::ChooseMethod method, + int index, + Api::SendOptions options) const { + if (index < 0) { + return false; + } if (!_srows->empty()) { - if (_sel >= 0 && _sel < _srows->size()) { - _stickerChosen.fire({ (*_srows)[_sel].document, method }); + if (index < _srows->size()) { + const auto document = (*_srows)[index].document; + _stickerChosen.fire({ document, options, method }); return true; } } else if (!_mrows->empty()) { - if (_sel >= 0 && _sel < _mrows->size()) { - _mentionChosen.fire({ _mrows->at(_sel).user, method }); + if (index < _mrows->size()) { + _mentionChosen.fire({ _mrows->at(index).user, method }); return true; } } else if (!_hrows->empty()) { - if (_sel >= 0 && _sel < _hrows->size()) { - _hashtagChosen.fire({ '#' + _hrows->at(_sel), method }); + if (index < _hrows->size()) { + _hashtagChosen.fire({ '#' + _hrows->at(index), method }); return true; } } else if (!_brows->empty()) { - if (_sel >= 0 && _sel < _brows->size()) { - const auto user = _brows->at(_sel).user; - const auto command = _brows->at(_sel).command; + if (index < _brows->size()) { + const auto user = _brows->at(index).user; + const auto command = _brows->at(index).command; const auto botStatus = _parent->chat() ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) @@ -1003,6 +1017,29 @@ void FieldAutocompleteInner::mouseReleaseEvent(QMouseEvent *e) { chooseSelected(FieldAutocomplete::ChooseMethod::ByClick); } +void FieldAutocompleteInner::contextMenuEvent(QContextMenuEvent *e) { + if (_sel < 0 || _srows->empty() || _down >= 0) { + return; + } + const auto index = _sel; + const auto type = SendMenuType::Scheduled; + const auto method = FieldAutocomplete::ChooseMethod::ByClick; + _menu = base::make_unique_q(this); + + const auto send = [=](Api::SendOptions options) { + chooseAtIndex(method, index, options); + }; + FillSendMenu( + _menu, + [&] { return type; }, + DefaultSilentCallback(send), + DefaultScheduleCallback(this, type, send)); + + if (!_menu->actions().empty()) { + _menu->popup(QCursor::pos()); + } +} + void FieldAutocompleteInner::enterEventHook(QEvent *e) { setMouseTracking(true); } diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h index 5a0ea82b35..e7d3f32056 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h @@ -7,12 +7,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "api/api_common.h" #include "ui/effects/animations.h" #include "ui/rp_widget.h" #include "base/timer.h" #include "base/object_ptr.h" namespace Ui { +class PopupMenu; class ScrollArea; } // namespace Ui @@ -103,6 +105,7 @@ public: }; struct StickerChosen { not_null sticker; + Api::SendOptions options; ChooseMethod method; }; @@ -210,6 +213,10 @@ public: void clearSel(bool hidden = false); bool moveSel(int key); bool chooseSelected(FieldAutocomplete::ChooseMethod method) const; + bool chooseAtIndex( + FieldAutocomplete::ChooseMethod method, + int index, + Api::SendOptions options = Api::SendOptions()) const; void setRecentInlineBotsInRows(int32 bots); void rowsUpdated(); @@ -233,6 +240,7 @@ private: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; void updateSelectedRow(); void setSel(int sel, bool scroll = false); @@ -252,6 +260,7 @@ private: const not_null _srows; rpl::lifetime _stickersLifetime; std::weak_ptr _lottieRenderer; + base::unique_qptr _menu; int _stickersPerRow = 1; int _recentInlineBotsInRows = 0; int _sel = -1; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 07447e41ff..81bad4bbef 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -400,7 +400,7 @@ HistoryWidget::HistoryWidget( _fieldAutocomplete->stickerChosen( ) | rpl::start_with_next([=](FieldAutocomplete::StickerChosen data) { - sendExistingDocument(data.sticker, Api::SendOptions()); + sendExistingDocument(data.sticker, data.options); }, lifetime()); _fieldAutocomplete->setModerateKeyActivateCallback([=](int key) {