From 5a6f2b27c81b1554ea96be47b124f055145de5d1 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Tue, 22 Oct 2024 16:17:39 -0400 Subject: [PATCH] Align Search Suggestion with Line Edit --- resources/css/style.css | 4 ++-- src/css_constants.h | 5 +++++ src/searchbar.cpp | 47 ++++++++++++++++++++++++++++++++++++++++- src/searchbar.h | 1 + 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/resources/css/style.css b/resources/css/style.css index 90c79d0..ee91655 100644 --- a/resources/css/style.css +++ b/resources/css/style.css @@ -32,8 +32,8 @@ QToolButton { SearchBar { background-color: white; - margin: 2px; - border: 1px solid #ccc; + margin: 2px; /* XXX: duplicated in css_constants.h */ + border: 1px solid #ccc; /* XXX: duplicated in css_constants.h */ border-radius: 3px; max-height: 40px; diff --git a/src/css_constants.h b/src/css_constants.h index 987c77c..28fa741 100644 --- a/src/css_constants.h +++ b/src/css_constants.h @@ -23,6 +23,11 @@ namespace tab { } } +namespace SearchBar{ + const int margin = 2; + const int border = 1; +} + namespace TopWidget { namespace QToolButton { namespace backButton { diff --git a/src/searchbar.cpp b/src/searchbar.cpp index 3a2e63c..ea503f7 100644 --- a/src/searchbar.cpp +++ b/src/searchbar.cpp @@ -6,6 +6,7 @@ #include "kiwixapp.h" #include "suggestionlistworker.h" +#include "css_constants.h" BookmarkButton::BookmarkButton(QWidget *parent) : QToolButton(parent) @@ -285,7 +286,7 @@ void SearchBarLineEdit::onInitialSuggestions(int) if (m_returnPressed) { openCompletion(getDefaulSuggestionIndex()); } else { - m_completer.complete(); + m_completer.complete(getCompleterRect()); /* Make row 0 appear but do not highlight it */ const auto completerFirstIdx = m_suggestionView->model()->index(0, 0); @@ -339,6 +340,50 @@ QModelIndex SearchBarLineEdit::getDefaulSuggestionIndex() const return QModelIndex(); } +/* Line edit does not span the entire searchBar. Completer is displayed + based on line edit, and thus shifting and resizing is needed. +*/ +QRect SearchBarLineEdit::getCompleterRect() const +{ + auto& searchBar = KiwixApp::instance()->getSearchBar(); + const auto& searchGeo = searchBar.geometry(); + const auto& searchLineEditGeo = searchBar.getLineEdit().geometry(); + + const int margin = CSS::SearchBar::margin; + const int border = CSS::SearchBar::border; + const int spaceAround = margin + border; + + /* Border and margin are not accounted in height and width. */ + const int top = searchGeo.height() - 2 * spaceAround; + const int width = searchGeo.width() - 2 * spaceAround; + + /* Direction in Qt6 and Qt5 are determined differently. */ +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const bool lineEditFlipped = KiwixApp::isRightToLeft(); +#else + const bool lineEditFlipped = m_searchbarInput.isRightToLeft(); +#endif + + /* Shift completer to one of the two laterals of search bar, where which + one it shifted to dependes on whether the line edit is flipped. + */ + int left = -searchLineEditGeo.left(); + + /* When not flipped, left() is relative to within the search bar border, + thus, we shift by spaceAround to match the side of search bar. + + When flipped, the completer starts at the right end of the search bar + We shift it by width to make the completer start at left lateral of + search bar. Since in a flipped state, left() also considered the opposite + side's border, which means we need to shift by a border width in + addition to spaceAround. + */ + left += lineEditFlipped ? -width + spaceAround + border : spaceAround; + + /* Can't set height to 0. Will cause rectangle to be ignored. */ + return QRect(QPoint(left, top), QSize(width, 1)); +} + SearchBar::SearchBar(QWidget *parent) : QToolBar(parent), m_searchBarLineEdit(this), diff --git a/src/searchbar.h b/src/searchbar.h index b759830..516f5eb 100644 --- a/src/searchbar.h +++ b/src/searchbar.h @@ -68,6 +68,7 @@ private slots: void fetchSuggestions(NewSuggestionHandlerFuncPtr callback); QModelIndex getDefaulSuggestionIndex() const; + QRect getCompleterRect() const; };