Align Search Suggestion with Line Edit

This commit is contained in:
ShaopengLin 2024-10-22 16:17:39 -04:00
parent d80ba1f012
commit 5a6f2b27c8
4 changed files with 54 additions and 3 deletions

View File

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

View File

@ -23,6 +23,11 @@ namespace tab {
}
}
namespace SearchBar{
const int margin = 2;
const int border = 1;
}
namespace TopWidget {
namespace QToolButton {
namespace backButton {

View File

@ -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),

View File

@ -68,6 +68,7 @@ private slots:
void fetchSuggestions(NewSuggestionHandlerFuncPtr callback);
QModelIndex getDefaulSuggestionIndex() const;
QRect getCompleterRect() const;
};