From df38dde29651e9e1189b84f1b656ef7090835235 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 22 Mar 2022 16:18:36 +0300 Subject: [PATCH] Added search messages to HistoryWidget. --- .../SourceFiles/history/history_widget.cpp | 85 ++++++++++++++++--- Telegram/SourceFiles/history/history_widget.h | 5 ++ .../controls/history_view_compose_search.cpp | 41 ++++++++- .../controls/history_view_compose_search.h | 6 ++ .../view/history_view_top_bar_widget.cpp | 11 +-- .../view/history_view_top_bar_widget.h | 12 +-- 6 files changed, 136 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 5041c72dc..18122f3a7 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -76,6 +76,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_inner_widget.h" #include "history/history_item_components.h" #include "history/history_unread_things.h" +#include "history/view/controls/history_view_compose_search.h" #include "history/view/controls/history_view_voice_record_bar.h" #include "history/view/controls/history_view_ttl_button.h" #include "history/view/history_view_cursor_state.h" @@ -246,10 +247,10 @@ HistoryWidget::HistoryWidget( , _botKeyboardHide(this, st::historyBotKeyboardHide) , _botCommandStart(this, st::historyBotCommandStart) , _voiceRecordBar(std::make_unique( - this, - controller, - _send, - st::historySendSize.height())) + this, + controller, + _send, + st::historySendSize.height())) , _field( this, st::historyComposeField, @@ -801,6 +802,10 @@ HistoryWidget::HistoryWidget( ) | rpl::start_with_next([=] { setChooseReportMessagesDetails({}, nullptr); }, _topBar->lifetime()); + _topBar->searchRequest( + ) | rpl::start_with_next([=] { + searchInChat(); + }, _topBar->lifetime()); session().api().sendActions( ) | rpl::filter([=](const Api::SendAction &action) { @@ -1795,7 +1800,7 @@ void HistoryWidget::setupShortcuts() { using Command = Shortcuts::Command; if (_history) { request->check(Command::Search, 1) && request->handle([=] { - controller()->content()->searchInChat(_history); + searchInChat(); return true; }); if (session().supportMode()) { @@ -2072,6 +2077,7 @@ void HistoryWidget::showHistory( } else { session().data().sponsoredMessages().clearItems(_history); session().data().hideShownSpoilers(); + _composeSearch = nullptr; } session().sendProgressManager().update( _history, @@ -2525,8 +2531,15 @@ bool HistoryWidget::contentOverlapped(const QRect &globalRect) { } bool HistoryWidget::canWriteMessage() const { - if (!_history || !_canSendMessages) return false; - if (isBlocked() || isJoinChannel() || isMuteUnmute() || isBotStart()) return false; + if (!_history || !_canSendMessages) { + return false; + } + if (isBlocked() || isJoinChannel() || isMuteUnmute() || isBotStart()) { + return false; + } + if (isSearching()) { + return false; + } return true; } @@ -2567,7 +2580,8 @@ void HistoryWidget::updateControlsVisibility() { } if (isChoosingTheme() || (!editingMessage() - && (isBlocked() + && (isSearching() + || isBlocked() || isJoinChannel() || isMuteUnmute() || isBotStart() @@ -2736,6 +2750,9 @@ void HistoryWidget::updateControlsVisibility() { if (_voiceRecordBar) { _voiceRecordBar->hideFast(); } + if (_composeSearch) { + _composeSearch->hideAnimated(); + } if (_inlineResults) { _inlineResults->hide(); } @@ -3606,6 +3623,9 @@ void HistoryWidget::hideChildWidgets() { if (_voiceRecordBar) { _voiceRecordBar->hideFast(); } + if (_composeSearch) { + _composeSearch->hideAnimated(); + } if (_chooseTheme) { _chooseTheme->hide(); } @@ -4297,6 +4317,10 @@ bool HistoryWidget::isMuteUnmute() const { || _peer->isRepliesChat()); } +bool HistoryWidget::isSearching() const { + return _composeSearch != nullptr; +} + bool HistoryWidget::showRecordButton() const { return Media::Capture::instance()->available() && !_voiceRecordBar->isListenState() @@ -4353,6 +4377,37 @@ bool HistoryWidget::updateCmdStartShown() { return false; } +void HistoryWidget::searchInChat() { + if (!_history) { + return; + } + if (controller()->isPrimary()) { + controller()->content()->searchInChat(_history); + } else { + const auto update = [=] { + updateControlsVisibility(); + updateBotKeyboard(); + updateFieldPlaceholder(); + + updateControlsGeometry(); + }; + _composeSearch = std::make_unique( + this, + controller(), + _history); + + update(); + _composeSearch->destroyRequests( + ) | rpl::take( + 1 + ) | rpl::start_with_next([=] { + _composeSearch = nullptr; + + update(); + }, _composeSearch->lifetime()); + } +} + bool HistoryWidget::kbWasHidden() const { return _history && (_keyboard->forMsgId() @@ -5260,7 +5315,8 @@ void HistoryWidget::updateHistoryGeometry( if (isChoosingTheme()) { newScrollHeight -= _chooseTheme->height(); } else if (!editingMessage() - && (isBlocked() + && (isSearching() + || isBlocked() || isBotStart() || isJoinChannel() || isMuteUnmute() @@ -5532,7 +5588,7 @@ void HistoryWidget::updateBotKeyboard(History *h, bool force) { && _history->lastKeyboardUsed) { _history->lastKeyboardHiddenId = _history->lastKeyboardId; } - if (!isBotStart() && !isBlocked() && _canSendMessages && (wasVisible || (_replyToId && _replyEditMsg) || (!HasSendText(_field) && !kbWasHidden()))) { + if (!isSearching() && !isBotStart() && !isBlocked() && _canSendMessages && (wasVisible || (_replyToId && _replyEditMsg) || (!HasSendText(_field) && !kbWasHidden()))) { if (!_a_show.animating()) { if (hasMarkup) { _kbScroll->show(); @@ -6574,6 +6630,9 @@ void HistoryWidget::replyToMessage(not_null item) { } session().data().cancelForwarding(_history); + if (_composeSearch) { + _composeSearch->hideAnimated(); + } if (_editMsgId) { if (auto localDraft = _history->localDraft()) { @@ -6621,6 +6680,8 @@ void HistoryWidget::editMessage(not_null item) { controller()->show( Ui::MakeInformBox(tr::lng_edit_caption_voice())); return; + } else if (_composeSearch) { + _composeSearch->hideAnimated(); } if (isRecording()) { @@ -7140,6 +7201,10 @@ void HistoryWidget::updateTopBarSelection() { || selectedState.textSelected; _topBar->showSelected(selectedState); + if ((selectedState.count > 0) && _composeSearch) { + _composeSearch->hideAnimated(); + } + const auto transparent = Qt::WA_TransparentForMouseEvents; if (selectedState.count == 0) { _reportMessages->clearState(); diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 9c87e5660..05ad24b63 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -105,6 +105,7 @@ class TopBarWidget; class ContactStatus; class Element; class PinnedTracker; +class ComposeSearch; namespace Controls { class RecordLock; class VoiceRecordBar; @@ -609,6 +610,7 @@ private: void inlineBotResolveFail(const MTP::Error &error, const QString &username); [[nodiscard]] bool isRecording() const; + [[nodiscard]] bool isSearching() const; [[nodiscard]] bool isBotStart() const; [[nodiscard]] bool isBlocked() const; @@ -630,6 +632,8 @@ private: bool kbWasHidden() const; + void searchInChat(); + MTP::Sender _api; MsgId _replyToId = 0; Ui::Text::String _replyToName; @@ -743,6 +747,7 @@ private: object_ptr _scheduled = { nullptr }; std::unique_ptr _ttlInfo; const std::unique_ptr _voiceRecordBar; + std::unique_ptr _composeSearch; bool _cmdStartShown = false; object_ptr _field; bool _inReplyEditForward = false; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp index d3cbc1bf2..675344e3b 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.cpp @@ -252,6 +252,7 @@ public: [[nodiscard]] rpl::producer searchRequests() const; [[nodiscard]] rpl::producer fromValue() const; [[nodiscard]] rpl::producer<> queryChanges() const; + [[nodiscard]] rpl::producer<> closeRequests() const; void setFrom(PeerData *peer); @@ -346,6 +347,10 @@ rpl::producer<> TopBar::queryChanges() const { return _queryChanges.events(); } +rpl::producer<> TopBar::closeRequests() const { + return _cancel->clicks() | rpl::to_empty; +} + rpl::producer TopBar::fromValue() const { return _from.value(); } @@ -688,9 +693,13 @@ public: not_null history); ~Inner(); + void hideAnimated(); + + [[nodiscard]] rpl::producer<> destroyRequests() const; + [[nodiscard]] rpl::lifetime &lifetime(); + private: void showAnimated(); - void hideAnimated(); void hideList(); const not_null _window; @@ -709,6 +718,8 @@ private: rpl::event_stream jumps; } _pendingJump; + rpl::event_stream<> _destroyRequests; + }; ComposeSearch::Inner::Inner( @@ -746,6 +757,11 @@ ComposeSearch::Inner::Inner( hideList(); }, _topBar->lifetime()); + _topBar->closeRequests( + ) | rpl::start_with_next([=] { + hideAnimated(); + }, _topBar->lifetime()); + _apiSearch.newFounds( ) | rpl::start_with_next([=] { const auto &apiData = _apiSearch.messages(); @@ -854,7 +870,10 @@ void ComposeSearch::Inner::showAnimated() { } void ComposeSearch::Inner::hideAnimated() { + hideList(); Ui::Animations::HideWidgets({ _topBar.get(), _bottomBar.get() }); + + _destroyRequests.fire({}); } void ComposeSearch::Inner::hideList() { @@ -863,6 +882,14 @@ void ComposeSearch::Inner::hideList() { } } +rpl::producer<> ComposeSearch::Inner::destroyRequests() const { + return _destroyRequests.events(); +} + +rpl::lifetime &ComposeSearch::Inner::lifetime() { + return _topBar->lifetime(); +} + ComposeSearch::Inner::~Inner() { } @@ -876,4 +903,16 @@ ComposeSearch::ComposeSearch( ComposeSearch::~ComposeSearch() { } +void ComposeSearch::hideAnimated() { + _inner->hideAnimated(); +} + +rpl::producer<> ComposeSearch::destroyRequests() const { + return _inner->destroyRequests(); +} + +rpl::lifetime &ComposeSearch::lifetime() { + return _inner->lifetime(); +} + } // namespace HistoryView diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h index d630df9d2..840feccc8 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_search.h @@ -27,6 +27,12 @@ public: not_null history); ~ComposeSearch(); + void hideAnimated(); + + [[nodiscard]] rpl::producer<> destroyRequests() const; + + [[nodiscard]] rpl::lifetime &lifetime(); + private: class Inner; const std::unique_ptr _inner; diff --git a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp index c993a7fb0..ab450ba7c 100644 --- a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp @@ -112,7 +112,6 @@ TopBarWidget::TopBarWidget( _clear->setClickedCallback([=] { _clearSelection.fire({}); }); _call->setClickedCallback([=] { call(); }); _groupCall->setClickedCallback([=] { groupCall(); }); - _search->setClickedCallback([=] { search(); }); _menuToggle->setClickedCallback([=] { showPeerMenu(); }); _infoToggle->setClickedCallback([=] { toggleInfoSection(); }); _back->addClickHandler([=] { backClicked(); }); @@ -230,12 +229,6 @@ void TopBarWidget::refreshLang() { InvokeQueued(this, [this] { updateControlsGeometry(); }); } -void TopBarWidget::search() { - if (_activeChat.key) { - _controller->content()->searchInChat(_activeChat.key); - } -} - void TopBarWidget::call() { if (const auto peer = _activeChat.key.peer()) { if (const auto user = peer->asUser()) { @@ -262,6 +255,10 @@ void TopBarWidget::clearChooseMessagesForReport() { setChooseForReportReason(std::nullopt); } +rpl::producer<> TopBarWidget::searchRequest() const { + return _search->clicks() | rpl::to_empty; +} + void TopBarWidget::setChooseForReportReason( std::optional reason) { if (_chooseForReportReason == reason) { diff --git a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h index edd8b00ce..1384f03f0 100644 --- a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h @@ -71,21 +71,22 @@ public: void showChooseMessagesForReport(Ui::ReportReason reason); void clearChooseMessagesForReport(); - rpl::producer<> forwardSelectionRequest() const { + [[nodiscard]] rpl::producer<> forwardSelectionRequest() const { return _forwardSelection.events(); } - rpl::producer<> sendNowSelectionRequest() const { + [[nodiscard]] rpl::producer<> sendNowSelectionRequest() const { return _sendNowSelection.events(); } - rpl::producer<> deleteSelectionRequest() const { + [[nodiscard]] rpl::producer<> deleteSelectionRequest() const { return _deleteSelection.events(); } - rpl::producer<> clearSelectionRequest() const { + [[nodiscard]] rpl::producer<> clearSelectionRequest() const { return _clearSelection.events(); } - rpl::producer<> cancelChooseForReportRequest() const { + [[nodiscard]] rpl::producer<> cancelChooseForReportRequest() const { return _cancelChooseForReport.events(); } + [[nodiscard]] rpl::producer<> searchRequest() const; protected: void paintEvent(QPaintEvent *e) override; @@ -108,7 +109,6 @@ private: void call(); void groupCall(); void startGroupCall(not_null megagroup, bool confirmed); - void search(); void showPeerMenu(); void showGroupCallMenu(not_null peer); void toggleInfoSection();