From 4efeaacf5c27be22fb3b29fe81351251b580ee05 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 2 Nov 2021 20:11:20 +0400 Subject: [PATCH] Add send context menu to StickerSetBox. --- .../SourceFiles/boxes/sticker_set_box.cpp | 84 +++++++++++++++---- Telegram/SourceFiles/history/history_widget.h | 2 +- Telegram/SourceFiles/mainwidget.cpp | 12 ++- Telegram/SourceFiles/mainwidget.h | 11 ++- 4 files changed, 92 insertions(+), 17 deletions(-) diff --git a/Telegram/SourceFiles/boxes/sticker_set_box.cpp b/Telegram/SourceFiles/boxes/sticker_set_box.cpp index 66129523c9..003257f910 100644 --- a/Telegram/SourceFiles/boxes/sticker_set_box.cpp +++ b/Telegram/SourceFiles/boxes/sticker_set_box.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_document_media.h" #include "data/stickers/data_stickers.h" +#include "chat_helpers/send_context_menu.h" #include "lang/lang_keys.h" #include "ui/boxes/confirm_box.h" #include "core/application.h" @@ -35,6 +36,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/unixtime.h" #include "main/main_session.h" #include "apiwrap.h" +#include "api/api_toggling_media.h" +#include "api/api_common.h" #include "mainwidget.h" #include "mainwindow.h" #include "styles/style_layers.h" @@ -87,6 +90,7 @@ protected: void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; + void contextMenuEvent(QContextMenuEvent *e) override; void paintEvent(QPaintEvent *e) override; void leaveEventHook(QEvent *e) override; @@ -113,6 +117,8 @@ private: void gotSet(const MTPmessages_StickerSet &set); void installDone(const MTPmessages_StickerSetInstallResult &result); + void send(not_null sticker, Api::SendOptions options); + not_null getLottiePlayer(); void showPreview(); @@ -144,6 +150,8 @@ private: base::Timer _previewTimer; int _previewShown = -1; + base::unique_qptr _menu; + rpl::event_stream _setInstalled; rpl::event_stream _setArchived; rpl::event_stream<> _updateControls; @@ -577,10 +585,14 @@ void StickerSetBox::Inner::installDone( } void StickerSetBox::Inner::mousePressEvent(QMouseEvent *e) { - int index = stickerFromGlobalPos(e->globalPos()); - if (index >= 0 && index < _pack.size()) { - _previewTimer.callOnce(QApplication::startDragTime()); + if (e->button() != Qt::LeftButton) { + return; } + const auto index = stickerFromGlobalPos(e->globalPos()); + if (index < 0 || index >= _pack.size()) { + return; + } + _previewTimer.callOnce(QApplication::startDragTime()); } void StickerSetBox::Inner::mouseMoveEvent(QMouseEvent *e) { @@ -605,18 +617,62 @@ void StickerSetBox::Inner::mouseReleaseEvent(QMouseEvent *e) { _previewShown = -1; return; } - if (_previewTimer.isActive()) { - _previewTimer.cancel(); - const auto index = stickerFromGlobalPos(e->globalPos()); - if (index >= 0 && index < _pack.size() && !isMasksSet()) { - const auto sticker = _pack[index]; - Ui::PostponeCall(crl::guard(_controller, [=] { - if (_controller->content()->sendExistingDocument(sticker)) { - Ui::hideSettingsAndLayer(); - } - })); - } + if (!_previewTimer.isActive()) { + return; } + _previewTimer.cancel(); + const auto index = stickerFromGlobalPos(e->globalPos()); + if (index < 0 || index >= _pack.size() || isMasksSet()) { + return; + } + send(_pack[index], Api::SendOptions()); +} + +void StickerSetBox::Inner::send( + not_null sticker, + Api::SendOptions options) { + const auto controller = _controller; + Ui::PostponeCall(controller, [=] { + if (controller->content()->sendExistingDocument(sticker, options)) { + Ui::hideSettingsAndLayer(); + } + }); +} + +void StickerSetBox::Inner::contextMenuEvent(QContextMenuEvent *e) { + const auto index = stickerFromGlobalPos(e->globalPos()); + if (index < 0 || index >= _pack.size()) { + return; + } + const auto type = _controller->content()->sendMenuType(); + if (type == SendMenu::Type::Disabled) { + return; + } + _previewTimer.cancel(); + _menu = base::make_unique_q(this); + + const auto document = _pack[index]; + const auto sendSelected = [=](Api::SendOptions options) { + send(document, options); + }; + SendMenu::FillSendMenu( + _menu.get(), + type, + SendMenu::DefaultSilentCallback(sendSelected), + SendMenu::DefaultScheduleCallback(this, type, sendSelected)); + + const auto toggleFavedSticker = [=] { + Api::ToggleFavedSticker( + document, + Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0)); + }; + _menu->addAction( + (document->owner().stickers().isFaved(document) + ? tr::lng_faved_stickers_remove + : tr::lng_faved_stickers_add)(tr::now), + toggleFavedSticker); + + _menu->popup(QCursor::pos()); } void StickerSetBox::Inner::updateSelected() { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 7592d8ca82..74a8a05f57 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -265,6 +265,7 @@ public: void confirmDeleteSelected(); void clearSelected(); + [[nodiscard]] SendMenu::Type sendMenuType() const; bool sendExistingDocument( not_null document, Api::SendOptions options); @@ -374,7 +375,6 @@ private: void sendWithModifiers(Qt::KeyboardModifiers modifiers); void sendSilent(); void sendScheduled(); - [[nodiscard]] SendMenu::Type sendMenuType() const; [[nodiscard]] SendMenu::Type sendButtonMenuType() const; void handlePendingHistoryUpdate(); void fullInfoUpdated(); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index f098abd65f..4f1a361612 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1067,8 +1067,18 @@ void MainWidget::inlineResultLoadFailed(FileLoader *loader, bool started) { //Ui::repaintInlineItem(); } +SendMenu::Type MainWidget::sendMenuType() const { + return _history->sendMenuType(); +} + bool MainWidget::sendExistingDocument(not_null document) { - return _history->sendExistingDocument(document, Api::SendOptions()); + return sendExistingDocument(document, Api::SendOptions()); +} + +bool MainWidget::sendExistingDocument( + not_null document, + Api::SendOptions options) { + return _history->sendExistingDocument(document, options); } void MainWidget::dialogsCancelled() { diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 8a68dac08c..e8ecf4a112 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -29,8 +29,13 @@ class Error; namespace Api { struct SendAction; +struct SendOptions; } // namespace Api +namespace SendMenu { +enum class Type; +} // namespace SendMenu + namespace Main { class Session; } // namespace Main @@ -150,7 +155,11 @@ public: QPixmap grabForShowAnimation(const Window::SectionSlideParams ¶ms); void checkMainSectionToLayer(); - bool sendExistingDocument(not_null sticker); + [[nodiscard]] SendMenu::Type sendMenuType() const; + bool sendExistingDocument(not_null document); + bool sendExistingDocument( + not_null document, + Api::SendOptions options); bool isActive() const; [[nodiscard]] bool doWeMarkAsRead() const;