From 4f22171dd66650dd28c50893570c5b1d792838e7 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 10 Nov 2020 21:51:20 +0300 Subject: [PATCH] Add start bot command and handle via@ links. --- .../admin_log/history_admin_log_inner.cpp | 3 + .../admin_log/history_admin_log_inner.h | 1 + .../history/history_inner_widget.cpp | 11 ++- .../history/history_inner_widget.h | 1 + .../history/history_item_components.cpp | 11 ++- .../history_view_compose_controls.cpp | 72 +++++++++++++++++-- .../controls/history_view_compose_controls.h | 4 ++ .../history/view/history_view_element.cpp | 3 + .../history/view/history_view_element.h | 2 + .../history/view/history_view_list_widget.cpp | 6 +- .../history/view/history_view_list_widget.h | 2 + .../view/history_view_pinned_section.cpp | 3 + .../view/history_view_pinned_section.h | 1 + .../view/history_view_replies_section.cpp | 4 ++ .../view/history_view_replies_section.h | 1 + .../view/history_view_scheduled_section.cpp | 4 ++ .../view/history_view_scheduled_section.h | 1 + Telegram/lib_base | 2 +- Telegram/lib_ui | 2 +- 19 files changed, 122 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp index 9653ecf01b..bf383ddec5 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.cpp @@ -605,6 +605,9 @@ void InnerWidget::elementSendBotCommand( const FullMsgId &context) { } +void InnerWidget::elementHandleViaClick(not_null bot) { +} + void InnerWidget::saveState(not_null memento) { memento->setFilter(std::move(_filter)); memento->setAdmins(std::move(_admins)); diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h index d402460ae6..32a6fcf42c 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_inner.h @@ -119,6 +119,7 @@ public: void elementSendBotCommand( const QString &command, const FullMsgId &context) override; + void elementHandleViaClick(not_null bot) override; ~InnerWidget(); diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 52720fadab..9e96faf10a 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -2566,6 +2566,10 @@ void HistoryInner::elementSendBotCommand( } } +void HistoryInner::elementHandleViaClick(not_null bot) { + App::insertBotCommand('@' + bot->username); +} + auto HistoryInner::getSelectionState() const -> HistoryView::TopBarWidget::SelectedState { auto result = HistoryView::TopBarWidget::SelectedState {}; @@ -3462,11 +3466,16 @@ not_null HistoryInner::ElementDelegate() { } void elementSendBotCommand( const QString &command, - const FullMsgId &context) { + const FullMsgId &context) override { if (Instance) { Instance->elementSendBotCommand(command, context); } } + void elementHandleViaClick(not_null bot) override { + if (Instance) { + Instance->elementHandleViaClick(bot); + } + } }; static Result result; diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index 2532568f87..8e5f5d6f37 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -95,6 +95,7 @@ public: void elementSendBotCommand( const QString &command, const FullMsgId &context); + void elementHandleViaClick(not_null bot); void updateBotInfo(bool recount = true); diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index 4215341c9a..a1292c6bb0 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_message.h" #include "history/view/history_view_service_message.h" #include "history/view/media/history_view_document.h" +#include "core/click_handler_types.h" #include "mainwindow.h" #include "media/audio/media_audio.h" #include "media/player/media_player_instance.h" @@ -45,7 +46,8 @@ void HistoryMessageVia::create( bot = owner->user(userId); maxWidth = st::msgServiceNameFont->width( tr::lng_inline_bot_via(tr::now, lt_inline_bot, '@' + bot->username)); - link = std::make_shared([bot = this->bot] { + link = std::make_shared([bot = this->bot]( + ClickContext context) { if (QGuiApplication::keyboardModifiers() == Qt::ControlModifier) { if (const auto window = App::wnd()) { if (const auto controller = window->sessionController()) { @@ -54,7 +56,12 @@ void HistoryMessageVia::create( } } } - App::insertBotCommand('@' + bot->username); + const auto my = context.other.value(); + if (const auto delegate = my.elementDelegate ? my.elementDelegate() : nullptr) { + delegate->elementHandleViaClick(bot); + } else { + App::insertBotCommand('@' + bot->username); + } }); } diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp index b9c2d42376..97189d361b 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_messages.h" #include "data/data_session.h" #include "data/data_user.h" +#include "data/data_chat.h" #include "data/data_channel.h" #include "data/stickers/data_stickers.h" #include "data/data_web_page.h" @@ -521,6 +522,9 @@ ComposeControls::ComposeControls( st::historyComposeField, Ui::InputField::Mode::MultiLine, tr::lng_message_ph())) +, _botCommandStart(Ui::CreateChild( + _wrap.get(), + st::historyBotCommandStart)) , _autocomplete(std::make_unique( parent, window)) @@ -562,7 +566,21 @@ void ComposeControls::setHistory(SetHistoryArgs &&args) { _window->tabbedSelector()->setCurrentPeer( history ? history->peer.get() : nullptr); initWebpageProcess(); + updateBotCommandShown(); + updateControlsGeometry(_wrap->size()); + updateControlsVisibility(); updateFieldPlaceholder(); + if (!_history) { + return; + } + const auto peer = _history->peer; + if (peer->isChat() && peer->asChat()->noParticipantInfo()) { + session().api().requestFullPeer(peer); + } else if (const auto channel = peer->asMegagroup()) { + if (!channel->mgInfo->botStatus) { + session().api().requestBots(channel); + } + } } void ComposeControls::move(int x, int y) { @@ -791,6 +809,8 @@ void ComposeControls::init() { initWriteRestriction(); initVoiceRecordBar(); + _botCommandStart->setClickedCallback([=] { setText({ "/" }); }); + _wrap->sizeValue( ) | rpl::start_with_next([=](QSize size) { updateControlsGeometry(size); @@ -1051,6 +1071,10 @@ void ComposeControls::fieldChanged() { if (showRecordButton()) { //_previewCancelled = false; } + if (updateBotCommandShown()) { + updateControlsVisibility(); + updateControlsGeometry(_wrap->size()); + } InvokeQueued(_autocomplete.get(), [=] { updateInlineBotQuery(); updateStickersByEmoji(); @@ -1297,13 +1321,14 @@ void ComposeControls::finishAnimating() { void ComposeControls::updateControlsGeometry(QSize size) { // _attachToggle -- _inlineResults ------ _tabbedPanel -- _fieldBarCancel - // (_attachDocument|_attachPhoto) _field _tabbedSelectorToggle _send + // (_attachDocument|_attachPhoto) _field _botCommandStart _tabbedSelectorToggle _send const auto fieldWidth = size.width() - _attachToggle->width() - st::historySendRight - _send->width() - - _tabbedSelectorToggle->width(); + - _tabbedSelectorToggle->width() + - (_botCommandShown ? _botCommandStart->width() : 0); _field->resizeToWidth(fieldWidth); const auto buttonsTop = size.height() - _attachToggle->height(); @@ -1324,6 +1349,8 @@ void ComposeControls::updateControlsGeometry(QSize size) { _send->moveToRight(right, buttonsTop); right += _send->width(); _tabbedSelectorToggle->moveToRight(right, buttonsTop); + right += _tabbedSelectorToggle->width(); + _botCommandStart->moveToRight(right, buttonsTop); _voiceRecordBar->resizeToWidth(size.width()); _voiceRecordBar->moveToLeft( @@ -1331,6 +1358,28 @@ void ComposeControls::updateControlsGeometry(QSize size) { size.height() - _voiceRecordBar->height()); } +void ComposeControls::updateControlsVisibility() { + _botCommandStart->setVisible(_botCommandShown); +} + +bool ComposeControls::updateBotCommandShown() { + auto shown = false; + const auto peer = _history ? _history->peer.get() : nullptr; + if (peer + && ((peer->isChat() && peer->asChat()->botStatus > 0) + || (peer->isMegagroup() && peer->asChannel()->mgInfo->botStatus > 0) + || (peer->isUser() && peer->asUser()->isBot()))) { + if (!HasSendText(_field)) { + shown = true; + } + } + if (_botCommandShown != shown) { + _botCommandShown = shown; + return true; + } + return false; +} + void ComposeControls::updateOuterGeometry(QRect rect) { if (_inlineResults) { _inlineResults->moveBottom(rect.y()); @@ -1604,7 +1653,7 @@ void ComposeControls::initWebpageProcess() { getWebPagePreview(); }); - _window->session().changes().peerUpdates( + session().changes().peerUpdates( Data::PeerUpdate::Flag::Rights ) | rpl::filter([=](const Data::PeerUpdate &update) { return (update.peer.get() == peer); @@ -1614,7 +1663,18 @@ void ComposeControls::initWebpageProcess() { updateFieldPlaceholder(); }, lifetime); - _window->session().downloaderTaskFinished( + base::ObservableViewer( + session().api().fullPeerUpdated() + ) | rpl::filter([=](PeerData *peer) { + return _history && (_history->peer == peer); + }) | rpl::start_with_next([=] { + if (updateBotCommandShown()) { + updateControlsVisibility(); + updateControlsGeometry(_wrap->size()); + } + }, lifetime); + + session().downloaderTaskFinished( ) | rpl::filter([=] { return (*previewData) && ((*previewData)->document || (*previewData)->photo); @@ -1622,7 +1682,7 @@ void ComposeControls::initWebpageProcess() { requestRepaint ), lifetime); - _window->session().data().webPageUpdates( + session().data().webPageUpdates( ) | rpl::filter([=](not_null page) { return (*previewData == page.get()); }) | rpl::start_with_next([=] { @@ -1652,7 +1712,7 @@ WebPageId ComposeControls::webPageId() const { rpl::producer ComposeControls::scrollRequests() const { return _header->scrollToItemRequests( ) | rpl::map([=](FullMsgId id) -> Data::MessagePosition { - if (const auto item = _window->session().data().message(id)) { + if (const auto item = session().data().message(id)) { return item->position(); } return {}; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h index cfb6f7fa21..b04a68b31b 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.h @@ -187,6 +187,7 @@ private: bool showRecordButton() const; void drawRestrictedWrite(Painter &p, const QString &error); void updateOverStates(QPoint pos); + bool updateBotCommandShown(); void cancelInlineBot(); void clearInlineBot(); @@ -217,6 +218,8 @@ private: const not_null _attachToggle; const not_null _tabbedSelectorToggle; const not_null _field; + const not_null _botCommandStart; + std::unique_ptr _inlineResults; std::unique_ptr _tabbedPanel; std::unique_ptr _autocomplete; @@ -243,6 +246,7 @@ private: bool _inlineLookingUpBot = false; mtpRequestId _inlineBotResolveRequestId = 0; bool _isInlineBot = false; + bool _botCommandShown = false; rpl::lifetime _uploaderSubscriptions; diff --git a/Telegram/SourceFiles/history/view/history_view_element.cpp b/Telegram/SourceFiles/history/view/history_view_element.cpp index 471932bd8d..97d128ba04 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.cpp +++ b/Telegram/SourceFiles/history/view/history_view_element.cpp @@ -126,6 +126,9 @@ void SimpleElementDelegate::elementSendBotCommand( const FullMsgId &context) { } +void SimpleElementDelegate::elementHandleViaClick(not_null bot) { +} + TextSelection UnshiftItemSelection( TextSelection selection, uint16 byLength) { diff --git a/Telegram/SourceFiles/history/view/history_view_element.h b/Telegram/SourceFiles/history/view/history_view_element.h index 1f14d0a240..fe64bdf3ec 100644 --- a/Telegram/SourceFiles/history/view/history_view_element.h +++ b/Telegram/SourceFiles/history/view/history_view_element.h @@ -70,6 +70,7 @@ public: virtual void elementSendBotCommand( const QString &command, const FullMsgId &context) = 0; + virtual void elementHandleViaClick(not_null bot) = 0; }; @@ -105,6 +106,7 @@ public: void elementSendBotCommand( const QString &command, const FullMsgId &context) override; + void elementHandleViaClick(not_null bot) override; private: const not_null _controller; diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index 39ebdf2481..15f02c0afe 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -1297,7 +1297,11 @@ bool ListWidget::elementShownUnread(not_null view) { void ListWidget::elementSendBotCommand( const QString &command, const FullMsgId &context) { - return _delegate->listSendBotCommand(command, context); + _delegate->listSendBotCommand(command, context); +} + +void ListWidget::elementHandleViaClick(not_null bot) { + _delegate->listHandleViaClick(bot); } void ListWidget::saveState(not_null memento) { diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.h b/Telegram/SourceFiles/history/view/history_view_list_widget.h index 103416658c..dff78abba4 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.h @@ -90,6 +90,7 @@ public: virtual void listSendBotCommand( const QString &command, const FullMsgId &context) = 0; + virtual void listHandleViaClick(not_null bot) = 0; }; @@ -239,6 +240,7 @@ public: void elementSendBotCommand( const QString &command, const FullMsgId &context) override; + void elementHandleViaClick(not_null bot) override; ~ListWidget(); diff --git a/Telegram/SourceFiles/history/view/history_view_pinned_section.cpp b/Telegram/SourceFiles/history/view/history_view_pinned_section.cpp index a2902fd39f..9699c13f03 100644 --- a/Telegram/SourceFiles/history/view/history_view_pinned_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_pinned_section.cpp @@ -644,6 +644,9 @@ void PinnedWidget::listSendBotCommand( const FullMsgId &context) { } +void PinnedWidget::listHandleViaClick(not_null bot) { +} + void PinnedWidget::confirmDeleteSelected() { ConfirmDeleteSelectedItems(_inner); } diff --git a/Telegram/SourceFiles/history/view/history_view_pinned_section.h b/Telegram/SourceFiles/history/view/history_view_pinned_section.h index bfc8517deb..ec4250346b 100644 --- a/Telegram/SourceFiles/history/view/history_view_pinned_section.h +++ b/Telegram/SourceFiles/history/view/history_view_pinned_section.h @@ -96,6 +96,7 @@ public: void listSendBotCommand( const QString &command, const FullMsgId &context) override; + void listHandleViaClick(not_null bot) override; protected: void resizeEvent(QResizeEvent *e) override; diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index 33a088c853..76755b0b3b 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -1779,6 +1779,10 @@ void RepliesWidget::listSendBotCommand( finishSending(); } +void RepliesWidget::listHandleViaClick(not_null bot) { + _composeControls->setText({ '@' + bot->username + ' ' }); +} + void RepliesWidget::confirmDeleteSelected() { ConfirmDeleteSelectedItems(_inner); } diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.h b/Telegram/SourceFiles/history/view/history_view_replies_section.h index 18acc60305..308368438f 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.h +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.h @@ -131,6 +131,7 @@ public: void listSendBotCommand( const QString &command, const FullMsgId &context) override; + void listHandleViaClick(not_null bot) override; protected: void resizeEvent(QResizeEvent *e) override; diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index 57a1680a8f..f1c14871ac 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -1192,6 +1192,10 @@ void ScheduledWidget::listSendBotCommand( Ui::LayerOption::KeepOther); } +void ScheduledWidget::listHandleViaClick(not_null bot) { + _composeControls->setText({ '@' + bot->username + ' ' }); +} + void ScheduledWidget::confirmSendNowSelected() { ConfirmSendNowSelectedItems(_inner); } diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.h b/Telegram/SourceFiles/history/view/history_view_scheduled_section.h index 5d564f9cb7..31716aaee0 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.h +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.h @@ -113,6 +113,7 @@ public: void listSendBotCommand( const QString &command, const FullMsgId &context) override; + void listHandleViaClick(not_null bot) override; protected: void resizeEvent(QResizeEvent *e) override; diff --git a/Telegram/lib_base b/Telegram/lib_base index ee68d3a63b..4f22126e7e 160000 --- a/Telegram/lib_base +++ b/Telegram/lib_base @@ -1 +1 @@ -Subproject commit ee68d3a63b4f1e550e7f93c1f8d6e8e372bc0f33 +Subproject commit 4f22126e7e855b517857408abf2467cba163a6f2 diff --git a/Telegram/lib_ui b/Telegram/lib_ui index 77b6e43b17..f06346fbf0 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit 77b6e43b17df43d64354ba7198e6a7695a636627 +Subproject commit f06346fbf03900c278e1d59717e1387bffc03f39