Skip to content

Commit

Permalink
Fix selecting specific row on search
Browse files Browse the repository at this point in the history
Fixes #2770
  • Loading branch information
hluk committed Nov 19, 2024
1 parent 922647f commit df487eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
52 changes: 29 additions & 23 deletions src/gui/clipboardbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ bool ClipboardBrowser::isFiltered(int row) const
return true;

const QModelIndex ind = m.index(row);
return m_filterRow != row
&& m_sharedData->itemFactory
return m_sharedData->itemFactory
&& !m_sharedData->itemFactory->matches(ind, *filter);
}

Expand Down Expand Up @@ -1303,36 +1302,43 @@ void ClipboardBrowser::filterItems(const ItemFilterPtr &filter)

d.setItemFilter(filter);

// If search string is a number, highlight item in that row.
bool filterByRowNumber = !m_sharedData->numberSearch;
if (filterByRowNumber) {
m_filterRow = newSearch.toInt(&filterByRowNumber);
if (m_filterRow > 0 && m_sharedData->rowIndexFromOne)
--m_filterRow;
}
if (!filterByRowNumber)
m_filterRow = -1;

int row = 0;
for ( ; row < length() && hideFiltered(row); ++row ) {}

if ( !filter || filter->matchesAll() ) {
for ( ; row < length(); ++row )
hideFiltered(row);
const int firstVisibleRow = row;

for ( ; row < length(); ++row )
hideFiltered(row);

if ( !filter || filter->matchesAll() ) {
scrollTo(currentIndex(), PositionAtCenter);
} else {
for ( ; row < length() && hideFiltered(row); ++row ) {}
const int currentRow = currentRowFromSearch(newSearch, firstVisibleRow);
setCurrent(currentRow);
}

setCurrent(row);
d.updateAllRows();
}

for ( ; row < length(); ++row )
hideFiltered(row);
int ClipboardBrowser::currentRowFromSearch(const QString &search, int fallback)
{
if (m_sharedData->numberSearch)
return fallback;

if ( filterByRowNumber && m_filterRow >= 0 && m_filterRow < m.rowCount() )
setCurrent(m_filterRow);
}
// If search string is a number, highlight item in that row.
bool ok;
int maybeRow = search.toInt(&ok);
if (!ok)
return fallback;

d.updateAllRows();
if (maybeRow > 0 && m_sharedData->rowIndexFromOne)
--maybeRow;

if (maybeRow < 0 || maybeRow >= m.rowCount())
return fallback;

setRowHidden(maybeRow, false);
return maybeRow;
}

void ClipboardBrowser::moveToClipboard(const QModelIndex &ind)
Expand Down
4 changes: 2 additions & 2 deletions src/gui/clipboardbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class ClipboardBrowser final : public QListView

void dragDropScroll();

int currentRowFromSearch(const QString &search, int fallback);

ItemSaverPtr m_itemSaver;

QString m_tabName;
Expand All @@ -400,8 +402,6 @@ class ClipboardBrowser final : public QListView
int m_dragTargetRow;
QPoint m_dragStartPosition;

int m_filterRow = -1;

bool m_selectNewItems = false;
};

Expand Down
2 changes: 0 additions & 2 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3575,9 +3575,7 @@ void MainWindow::enterSearchMode()
if ( !ui->searchBar->text().isEmpty() ) {
auto c = browserOrNull();
if (c) {
const int currentRow = c->currentIndex().row();
c->filterItems( ui->searchBar->filter() );
c->setCurrent(currentRow);
}
}
}
Expand Down

0 comments on commit df487eb

Please sign in to comment.