From 02daa2a04b41fcc94c6fdabbddca5674fa24274b Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 13 Sep 2022 07:53:20 +0400 Subject: [PATCH] Add new reaction menu to channel comments. --- .../history/view/history_view_list_widget.cpp | 94 ++++++++++++------- .../history/view/history_view_list_widget.h | 1 + 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index 443906fdf..8adb38f74 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/media/history_view_sticker.h" #include "history/view/reactions/history_view_reactions_animation.h" #include "history/view/reactions/history_view_reactions_button.h" +#include "history/view/reactions/history_view_reactions_selector.h" #include "history/view/history_view_context_menu.h" #include "history/view/history_view_element.h" #include "history/view/history_view_emoji_interactions.h" @@ -359,36 +360,7 @@ ListWidget::ListWidget( _reactionsManager->chosen( ) | rpl::start_with_next([=](ChosenReaction reaction) { _reactionsManager->updateButton({}); - - const auto item = session().data().message(reaction.context); - if (!item) { - return; - } else if (Window::ShowReactPremiumError( - _controller, - item, - reaction.id)) { - if (_menu) { - _menu->hideMenu(); - } - return; - } - item->toggleReaction( - reaction.id, - HistoryItem::ReactionSource::Selector); - if (!ranges::contains(item->chosenReactions(), reaction.id)) { - return; - } else if (const auto view = viewForItem(item)) { - const auto geometry = reaction.localGeometry.isEmpty() - ? mapFromGlobal(reaction.globalGeometry) - : reaction.localGeometry; - if (const auto top = itemTop(view); top >= 0) { - view->animateReaction({ - .id = reaction.id, - .flyIcon = reaction.icon, - .flyFrom = geometry.translated(0, -top), - }); - } - } + reactionChosen(reaction); }, lifetime()); _reactionsManager->premiumPromoChosen( @@ -2263,11 +2235,65 @@ void ListWidget::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _overState)); _menu = FillContextMenu(this, request); - if (_menu && !_menu->empty()) { - _menu->popup(e->globalPos()); - e->accept(); - } else if (_menu) { + + using namespace HistoryView::Reactions; + const auto desiredPosition = e->globalPos(); + const auto reactItem = (_overElement + && _overState.pointState != PointState::Outside) + ? _overElement->data().get() + : nullptr; + const auto attached = reactItem + ? AttachSelectorToMenu( + _menu.get(), + _controller, + desiredPosition, + reactItem, + [=](ChosenReaction reaction) { reactionChosen(reaction); }, + [=](FullMsgId context) { ShowPremiumPreviewBox( + _controller, + PremiumPreview::InfiniteReactions); }, + _controller->cachedReactionIconFactory().createMethod()) + : AttachSelectorResult::Skipped; + if (attached == AttachSelectorResult::Failed) { _menu = nullptr; + return; + } else if (attached == AttachSelectorResult::Attached) { + _menu->popupPrepared(); + } else { + _menu->popup(desiredPosition); + } + e->accept(); +} + +void ListWidget::reactionChosen(ChosenReaction reaction) { + const auto item = session().data().message(reaction.context); + if (!item) { + return; + } else if (Window::ShowReactPremiumError( + _controller, + item, + reaction.id)) { + if (_menu) { + _menu->hideMenu(); + } + return; + } + item->toggleReaction( + reaction.id, + HistoryItem::ReactionSource::Selector); + if (!ranges::contains(item->chosenReactions(), reaction.id)) { + return; + } else if (const auto view = viewForItem(item)) { + const auto geometry = reaction.localGeometry.isEmpty() + ? mapFromGlobal(reaction.globalGeometry) + : reaction.localGeometry; + if (const auto top = itemTop(view); top >= 0) { + view->animateReaction({ + .id = reaction.id, + .flyIcon = reaction.icon, + .flyFrom = geometry.translated(0, -top), + }); + } } } diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.h b/Telegram/SourceFiles/history/view/history_view_list_widget.h index 74d0be6ae..8457534d2 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.h @@ -417,6 +417,7 @@ private: QPoint mapPointToItem(QPoint point, const Element *view) const; void showContextMenu(QContextMenuEvent *e, bool showFromTouch = false); + void reactionChosen(ChosenReaction reaction); [[nodiscard]] int findItemIndexByY(int y) const; [[nodiscard]] not_null findItemByY(int y) const;