Don't create top/recent peers unnecessary.

This commit is contained in:
John Preston 2024-04-15 17:54:00 +04:00
parent 74a7e7d1b4
commit 7387dfdd9c
2 changed files with 25 additions and 18 deletions

View File

@ -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<QWidget*> 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();
}

View File

@ -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;