From ec6d6a78cf6180bbeeed08fb2bd219b991d6b88b Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 16 May 2024 13:29:03 +0400 Subject: [PATCH] Tab toggles search focus, Backspace doesn't focus. --- .../SourceFiles/dialogs/dialogs_widget.cpp | 33 ++++++++++++------- Telegram/SourceFiles/dialogs/dialogs_widget.h | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index e3ee48b9e..a777cddca 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -98,6 +98,15 @@ base::options::toggle OptionForumHideChatsList({ .description = "Don't keep a narrow column of chats list.", }); +[[nodiscard]] bool RedirectTextToSearch(const QString &text) { + for (const auto &ch : text) { + if (ch.unicode() >= 32) { + return true; + } + } + return false; +} + } // namespace const char kOptionForumHideChatsList[] = "forum-hide-chats-list"; @@ -454,7 +463,6 @@ Widget::Widget( loadMoreBlockedByDate(); }, lifetime()); - _search->setFocusPolicy(Qt::StrongFocus); _search->customUpDown(true); updateJumpToDateVisibility(true); @@ -1514,16 +1522,17 @@ void Widget::checkUpdateStatus() { updateControlsGeometry(); } -void Widget::setInnerFocus() { +void Widget::setInnerFocus(bool unfocusSearch) { if (_childList) { _childList->setInnerFocus(); } else if ((_openedFolder || _openedForum) && _subsectionTopBar->searchSetFocus()) { return; - } else if (!_search->getLastText().isEmpty() - || _searchInChat - || _searchHasFocus - || _searchSuggestionsLocked) { + } else if (!unfocusSearch + && (!_search->getLastText().isEmpty() + || _searchInChat + || _searchHasFocus + || _searchSuggestionsLocked)) { _search->setFocus(); } else { setFocus(); @@ -2874,7 +2883,7 @@ bool Widget::setSearchInChat( if (_searchInChat || !_search->getLastText().isEmpty()) { _search->setFocus(); } else { - setFocus(); + setInnerFocus(true); } updateForceDisplayWide(); return true; @@ -3255,7 +3264,7 @@ void Widget::keyPressEvent(QKeyEvent *e) { //} else { // e->ignore(); //} - } else if (e->key() == Qt::Key_Backspace + } else if ((e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Tab) && _searchHasFocus && !_searchInChat) { escape(); @@ -3339,7 +3348,7 @@ bool Widget::redirectKeyToSearch(QKeyEvent *e) const { } const auto character = !(e->modifiers() & ~Qt::ShiftModifier) && (e->key() != Qt::Key_Shift) - && !e->text().isEmpty(); + && RedirectTextToSearch(e->text()); if (character) { return true; } else if (e != QKeySequence::Paste) { @@ -3467,7 +3476,7 @@ bool Widget::cancelSearch() { if (!clearingQuery && _subsectionTopBar && _subsectionTopBar->toggleSearch(false, anim::type::normal)) { - setFocus(); + setInnerFocus(true); clearingInChat = true; } const auto clearSearchFocus = !_searchInChat @@ -3478,7 +3487,7 @@ bool Widget::cancelSearch() { } if (!_suggestions && clearSearchFocus) { // Don't create suggestions in unfocus case. - setFocus(); + setInnerFocus(true); } _lastSearchPeer = nullptr; _lastSearchId = _lastSearchMigratedId = 0; @@ -3486,7 +3495,7 @@ bool Widget::cancelSearch() { clearSearchField(); applySearchUpdate(); if (_suggestions && clearSearchFocus) { - setFocus(); + setInnerFocus(true); } return clearingQuery || clearingInChat || clearSearchFocus; } diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.h b/Telegram/SourceFiles/dialogs/dialogs_widget.h index aa6dbdf93..b183cb36f 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.h +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.h @@ -99,7 +99,7 @@ public: not_null forum, const Window::SectionShow ¶ms); void searchInChat(Key chat); - void setInnerFocus(); + void setInnerFocus(bool unfocusSearch = false); [[nodiscard]] bool searchHasFocus() const; void jumpToTop(bool belowPinned = false);