Skip to content

Commit

Permalink
ui: Delay loading a page until user stops typing a query
Browse files Browse the repository at this point in the history
Based on work by Artur Spychaj submitted in #460.
Related #265, #523.
Closes #564.
  • Loading branch information
trollixx committed Jul 19, 2016
1 parent 6f93398 commit fd11923
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <QShortcut>
#include <QSystemTrayIcon>
#include <QTabBar>
#include <QTimer>

#ifdef USE_WEBENGINE
#include <QWebEngineHistory>
Expand Down Expand Up @@ -174,7 +175,8 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent) :
m_application(app),
m_settings(app->settings()),
m_zealListModel(new ListModel(app->docsetRegistry(), this)),
m_globalShortcut(new QxtGlobalShortcut(m_settings->showShortcut, this))
m_globalShortcut(new QxtGlobalShortcut(m_settings->showShortcut, this)),
m_openDocsetTimer(new QTimer(this))
{
ui->setupUi(this);

Expand Down Expand Up @@ -397,6 +399,20 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent) :
}
});

// Setup delayed navigation to a page until user makes a pause in typing a search query.
m_openDocsetTimer->setInterval(400);
m_openDocsetTimer->setSingleShot(true);
connect(m_openDocsetTimer, &QTimer::timeout, this, [this]() {
QModelIndex index = m_openDocsetTimer->property("index").toModelIndex();
if (!index.isValid())
return;

openDocset(index);

// Get focus back. QWebPageEngine::load() always steals focus.
ui->lineEdit->setFocus(Qt::MouseFocusReason);
});

ui->actionNewTab->setShortcut(QKeySequence::AddTab);
connect(ui->actionNewTab, &QAction::triggered, this, [this]() { createTab(); });
addAction(ui->actionNewTab);
Expand Down Expand Up @@ -500,13 +516,14 @@ QIcon MainWindow::docsetIcon(const QString &docsetName) const

void MainWindow::queryCompleted()
{
m_openDocsetTimer->stop();

syncTreeView();

ui->treeView->setCurrentIndex(currentTabState()->searchModel->index(0, 0, QModelIndex()));
openDocset(ui->treeView->currentIndex());

// Get focus back. QWebPageEngine::load() always steals focus.
ui->lineEdit->setFocus(Qt::MouseFocusReason);
m_openDocsetTimer->setProperty("index", ui->treeView->currentIndex());
m_openDocsetTimer->start();
}

void MainWindow::closeTab(int index)
Expand Down
3 changes: 3 additions & 0 deletions src/ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class QxtGlobalShortcut;
class QModelIndex;
class QSystemTrayIcon;
class QTabBar;
class QTimer;

namespace Ui {
class MainWindow;
Expand Down Expand Up @@ -128,6 +129,8 @@ private slots:

QSystemTrayIcon *m_trayIcon = nullptr;

QTimer *m_openDocsetTimer = nullptr;

#ifdef USE_APPINDICATOR
bool m_useAppIndicator = false;
_AppIndicator *m_appIndicator = nullptr;
Expand Down

0 comments on commit fd11923

Please sign in to comment.