mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-24 04:32:15 -04:00
Align Search Suggestion with Line Edit
This commit is contained in:
parent
d80ba1f012
commit
5a6f2b27c8
@ -32,8 +32,8 @@ QToolButton {
|
|||||||
|
|
||||||
SearchBar {
|
SearchBar {
|
||||||
background-color: white;
|
background-color: white;
|
||||||
margin: 2px;
|
margin: 2px; /* XXX: duplicated in css_constants.h */
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc; /* XXX: duplicated in css_constants.h */
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
|
||||||
max-height: 40px;
|
max-height: 40px;
|
||||||
|
@ -23,6 +23,11 @@ namespace tab {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace SearchBar{
|
||||||
|
const int margin = 2;
|
||||||
|
const int border = 1;
|
||||||
|
}
|
||||||
|
|
||||||
namespace TopWidget {
|
namespace TopWidget {
|
||||||
namespace QToolButton {
|
namespace QToolButton {
|
||||||
namespace backButton {
|
namespace backButton {
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "kiwixapp.h"
|
#include "kiwixapp.h"
|
||||||
#include "suggestionlistworker.h"
|
#include "suggestionlistworker.h"
|
||||||
|
#include "css_constants.h"
|
||||||
|
|
||||||
BookmarkButton::BookmarkButton(QWidget *parent) :
|
BookmarkButton::BookmarkButton(QWidget *parent) :
|
||||||
QToolButton(parent)
|
QToolButton(parent)
|
||||||
@ -285,7 +286,7 @@ void SearchBarLineEdit::onInitialSuggestions(int)
|
|||||||
if (m_returnPressed) {
|
if (m_returnPressed) {
|
||||||
openCompletion(getDefaulSuggestionIndex());
|
openCompletion(getDefaulSuggestionIndex());
|
||||||
} else {
|
} else {
|
||||||
m_completer.complete();
|
m_completer.complete(getCompleterRect());
|
||||||
|
|
||||||
/* Make row 0 appear but do not highlight it */
|
/* Make row 0 appear but do not highlight it */
|
||||||
const auto completerFirstIdx = m_suggestionView->model()->index(0, 0);
|
const auto completerFirstIdx = m_suggestionView->model()->index(0, 0);
|
||||||
@ -339,6 +340,50 @@ QModelIndex SearchBarLineEdit::getDefaulSuggestionIndex() const
|
|||||||
return QModelIndex();
|
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) :
|
SearchBar::SearchBar(QWidget *parent) :
|
||||||
QToolBar(parent),
|
QToolBar(parent),
|
||||||
m_searchBarLineEdit(this),
|
m_searchBarLineEdit(this),
|
||||||
|
@ -68,6 +68,7 @@ private slots:
|
|||||||
void fetchSuggestions(NewSuggestionHandlerFuncPtr callback);
|
void fetchSuggestions(NewSuggestionHandlerFuncPtr callback);
|
||||||
|
|
||||||
QModelIndex getDefaulSuggestionIndex() const;
|
QModelIndex getDefaulSuggestionIndex() const;
|
||||||
|
QRect getCompleterRect() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user