feat: add (semi) instant searching in mod downloader

It has a delay of 350ms from the last typed character to search, in
order to cache small changes while typing.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-07-15 11:57:27 -03:00
parent 5936c7b65c
commit c3f647dc96
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 15 additions and 0 deletions

View File

@ -56,8 +56,15 @@ ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
, api(api) , api(api)
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch); connect(ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch);
connect(ui->modFilterButton, &QPushButton::clicked, this, &ModPage::filterMods); connect(ui->modFilterButton, &QPushButton::clicked, this, &ModPage::filterMods);
m_search_timer.setTimerType(Qt::TimerType::CoarseTimer);
m_search_timer.setSingleShot(true);
connect(&m_search_timer, &QTimer::timeout, this, &ModPage::triggerSearch);
ui->searchEdit->installEventFilter(this); ui->searchEdit->installEventFilter(this);
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
@ -101,6 +108,11 @@ auto ModPage::eventFilter(QObject* watched, QEvent* event) -> bool
triggerSearch(); triggerSearch();
keyEvent->accept(); keyEvent->accept();
return true; return true;
} else {
if (m_search_timer.isActive())
m_search_timer.stop();
m_search_timer.start(350);
} }
} else if (watched == ui->packView && event->type() == QEvent::KeyPress) { } else if (watched == ui->packView && event->type() == QEvent::KeyPress) {
auto* keyEvent = dynamic_cast<QKeyEvent*>(event); auto* keyEvent = dynamic_cast<QKeyEvent*>(event);

View File

@ -81,4 +81,7 @@ class ModPage : public QWidget, public BasePage {
std::unique_ptr<ModAPI> api; std::unique_ptr<ModAPI> api;
int selectedVersion = -1; int selectedVersion = -1;
// Used to do instant searching with a delay to cache quick changes
QTimer m_search_timer;
}; };