Skip to content

Commit c3fdd6a

Browse files
fix: updated refactored code to pass required checks (#1273)
1 parent 40762fa commit c3fdd6a

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

src/searchbar.cpp

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,45 +193,79 @@ bool SearchBarLineEdit::eventFilter(QObject *watched, QEvent *event)
193193
bool SearchBarLineEdit::handleSuggestionKeyPress(QKeyEvent *keyEvent)
194194
{
195195
const int key = keyEvent->key();
196-
auto selectionModel = m_suggestionView->selectionModel();
197-
auto currentIndex = selectionModel->currentIndex();
198-
int lastRow = m_suggestionModel.rowCount() - 1;
196+
const int currentRow = m_suggestionView->selectionModel()->currentIndex().row();
197+
const int lastRow = m_suggestionModel.rowCount() - 1;
199198

200-
if ((key == Qt::Key_Down || key == Qt::Key_PageDown) && currentIndex.row() == lastRow)
199+
if (isAtLastRowAndPressingDown(key, currentRow, lastRow))
201200
{
202-
if (m_moreSuggestionsAreAvailable)
203-
{
204-
fetchMoreSuggestions();
205-
}
206-
return true;
201+
return handleEndOfListKeyPress();
207202
}
208203

209-
if ((key == Qt::Key_Up || key == Qt::Key_PageUp) && currentIndex.row() == 0)
204+
if (isAtFirstRowAndPressingUp(key, currentRow))
210205
{
211206
return true;
212207
}
213208

214-
if ((key == Qt::Key_Down || key == Qt::Key_PageDown) &&
215-
m_moreSuggestionsAreAvailable &&
216-
currentIndex.row() >= lastRow - 2)
209+
if (shouldPreloadMoreSuggestions(key, currentRow, lastRow))
217210
{
218-
bool result = QLineEdit::eventFilter(m_suggestionView, keyEvent);
219-
fetchMoreSuggestions();
220-
ensureCurrentIndexVisible();
221-
return result;
211+
return handleNearEndNavigation(keyEvent);
222212
}
223213

224-
if (key == Qt::Key_Down || key == Qt::Key_Up ||
225-
key == Qt::Key_PageDown || key == Qt::Key_PageUp)
214+
if (isNavigationKey(key))
226215
{
227-
bool result = QLineEdit::eventFilter(m_suggestionView, keyEvent);
228-
ensureCurrentIndexVisible();
229-
return result;
216+
return handleStandardNavigation(keyEvent);
230217
}
231218

232219
return false;
233220
}
234221

222+
bool SearchBarLineEdit::isAtLastRowAndPressingDown(int key, int row, int lastRow) const
223+
{
224+
return (key == Qt::Key_Down || key == Qt::Key_PageDown) && row == lastRow;
225+
}
226+
227+
bool SearchBarLineEdit::isAtFirstRowAndPressingUp(int key, int row) const
228+
{
229+
return (key == Qt::Key_Up || key == Qt::Key_PageUp) && row == 0;
230+
}
231+
232+
bool SearchBarLineEdit::shouldPreloadMoreSuggestions(int key, int row, int lastRow) const
233+
{
234+
return (key == Qt::Key_Down || key == Qt::Key_PageDown) &&
235+
m_moreSuggestionsAreAvailable &&
236+
row >= lastRow - 2;
237+
}
238+
239+
bool SearchBarLineEdit::isNavigationKey(int key) const
240+
{
241+
return key == Qt::Key_Down || key == Qt::Key_Up ||
242+
key == Qt::Key_PageDown || key == Qt::Key_PageUp;
243+
}
244+
245+
bool SearchBarLineEdit::handleEndOfListKeyPress()
246+
{
247+
if (m_moreSuggestionsAreAvailable)
248+
{
249+
fetchMoreSuggestions();
250+
}
251+
return true;
252+
}
253+
254+
bool SearchBarLineEdit::handleNearEndNavigation(QKeyEvent *event)
255+
{
256+
bool result = QLineEdit::eventFilter(m_suggestionView, event);
257+
fetchMoreSuggestions();
258+
ensureCurrentIndexVisible();
259+
return result;
260+
}
261+
262+
bool SearchBarLineEdit::handleStandardNavigation(QKeyEvent *event)
263+
{
264+
bool result = QLineEdit::eventFilter(m_suggestionView, event);
265+
ensureCurrentIndexVisible();
266+
return result;
267+
}
268+
235269
bool SearchBarLineEdit::handleSuggestionKeyRelease(QKeyEvent *keyEvent)
236270
{
237271
int key = keyEvent->key();

src/searchbar.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public slots:
6464
bool handleSuggestionKeyPress(QKeyEvent *keyEvent);
6565
bool handleSuggestionKeyRelease(QKeyEvent *keyEvent);
6666
void ensureCurrentIndexVisible();
67+
68+
// Helper methods for handleSuggestionKeyPress
69+
bool isAtLastRowAndPressingDown(int key, int row, int lastRow) const;
70+
bool isAtFirstRowAndPressingUp(int key, int row) const;
71+
bool shouldPreloadMoreSuggestions(int key, int row, int lastRow) const;
72+
bool isNavigationKey(int key) const;
73+
bool handleEndOfListKeyPress();
74+
bool handleNearEndNavigation(QKeyEvent *event);
75+
bool handleStandardNavigation(QKeyEvent *event);
6776

6877
private slots:
6978
void updateCompletion();

0 commit comments

Comments
 (0)