From 7387dfdd9c97265209b533961c795d92f310b245 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 15 Apr 2024 17:54:00 +0400 Subject: [PATCH] Don't create top/recent peers unnecessary. --- .../SourceFiles/dialogs/dialogs_widget.cpp | 41 +++++++++++-------- Telegram/SourceFiles/dialogs/dialogs_widget.h | 2 + 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index c272fd1b9..e5676f3bc 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -361,13 +361,6 @@ Widget::Widget( applySearchUpdate(); }, _search->lifetime()); - _search->focusedChanges( - ) | rpl::start_with_next([=](bool focused) { - if (focused) { - updateHasFocus(_search.data()); - } - }, _search->lifetime()); - _search->submits( ) | rpl::start_with_next([=] { submit(); }, _search->lifetime()); @@ -1097,25 +1090,29 @@ void Widget::updateLockUnlockPosition() { } void Widget::updateHasFocus(not_null focused) { - const auto has = (focused == _search.data()); + const auto has = (focused == _search.data()) + || (focused == _search->rawTextEdit()); if (_searchHasFocus != has) { _searchHasFocus = has; - const auto update = [=] { - updateStoriesVisibility(); - updateForceDisplayWide(); - updateSuggestions(anim::type::normal); - }; - if (has) { - update(); + if (_postponeProcessSearchFocusChange) { + return; + } else if (has) { + processSearchFocusChange(); } else { // Search field may loose focus from the destructor of some // widget, in that case we don't want to destroy _suggestions - // syncrhonously, because it may lead to a crash. - crl::on_main(this, update); + // synchronously, because it may lead to a crash. + crl::on_main(this, [=] { processSearchFocusChange(); }); } } } +void Widget::processSearchFocusChange() { + updateStoriesVisibility(); + updateForceDisplayWide(); + updateSuggestions(anim::type::normal); +} + void Widget::updateSuggestions(anim::type animated) { const auto suggest = _searchHasFocus && !_searchInChat @@ -2556,7 +2553,9 @@ void Widget::applySearchUpdate(bool force) { clearSearchCache(); } _cancelSearch->toggle(!filterText.isEmpty(), anim::type::normal); - updateSuggestions(anim::type::instant); + if (!_postponeProcessSearchFocusChange) { + updateSuggestions(anim::type::instant); + } updateLoadMoreChatsVisibility(); updateJumpToDateVisibility(); updateLockUnlockPosition(); @@ -3219,8 +3218,14 @@ void Widget::keyPressEvent(QKeyEvent *e) { && _search->isVisible() && !_search->hasFocus() && !e->text().isEmpty()) { + // This delay in search focus processing allows us not to create + // _suggestions in case the event inserts some non-whitespace search + // query while still show _suggestions animated, if it is a space. + _postponeProcessSearchFocusChange = true; _search->setFocusFast(); QCoreApplication::sendEvent(_search->rawTextEdit(), e); + _postponeProcessSearchFocusChange = false; + processSearchFocusChange(); } else { e->ignore(); } diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.h b/Telegram/SourceFiles/dialogs/dialogs_widget.h index 2cb979d53..84faf7576 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.h @@ -246,6 +246,7 @@ private: void updateScrollUpPosition(); void updateLockUnlockPosition(); void updateSuggestions(anim::type animated); + void processSearchFocusChange(); MTP::Sender _api; @@ -315,6 +316,7 @@ private: int _storiesExplicitExpandScrollTop = 0; int _aboveScrollAdded = 0; bool _storiesExplicitExpand = false; + bool _postponeProcessSearchFocusChange = false; base::Timer _searchTimer;