Skip to content

Commit

Permalink
Add a small delay before processing the key input of search boxes
Browse files Browse the repository at this point in the history
PR #20465.
Closes #20025.
Closes #20235.
  • Loading branch information
Chocobo1 authored Feb 27, 2024
1 parent 46e8ee5 commit 15697f9
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/gui/addnewtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <functional>

#include <QAction>
#include <QByteArray>
#include <QDateTime>
#include <QDebug>
#include <QDir>
Expand All @@ -41,6 +42,7 @@
#include <QMessageBox>
#include <QPushButton>
#include <QShortcut>
#include <QSize>
#include <QString>
#include <QUrl>
#include <QVector>
Expand Down
4 changes: 2 additions & 2 deletions src/gui/addnewtorrentdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
#include "base/path.h"
#include "base/settingvalue.h"

class LineEdit;

namespace Ui
{
class AddNewTorrentDialog;
}

class LineEdit;

class AddNewTorrentDialog final : public QDialog
{
Q_OBJECT
Expand Down
21 changes: 21 additions & 0 deletions src/gui/lineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,41 @@

#include "lineedit.h"

#include <chrono>

#include <QAction>
#include <QKeyEvent>
#include <QTimer>

#include "base/global.h"
#include "uithememanager.h"

using namespace std::chrono_literals;

namespace
{
const std::chrono::milliseconds FILTER_INPUT_DELAY {400};
}

LineEdit::LineEdit(QWidget *parent)
: QLineEdit(parent)
, m_delayedTextChangedTimer {new QTimer(this)}
{
auto *action = new QAction(UIThemeManager::instance()->getIcon(u"edit-find"_s), QString(), this);
addAction(action, QLineEdit::LeadingPosition);

setClearButtonEnabled(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

m_delayedTextChangedTimer->setSingleShot(true);
connect(m_delayedTextChangedTimer, &QTimer::timeout, this, [this]
{
emit textChanged(text());
});
connect(this, &QLineEdit::textChanged, this, [this]
{
m_delayedTextChangedTimer->start(FILTER_INPUT_DELAY);
});
}

void LineEdit::keyPressEvent(QKeyEvent *event)
Expand Down
8 changes: 8 additions & 0 deletions src/gui/lineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

#include <QLineEdit>

class QKeyEvent;
class QTimer;

class LineEdit final : public QLineEdit
{
Q_OBJECT
Expand All @@ -39,6 +42,11 @@ class LineEdit final : public QLineEdit
public:
explicit LineEdit(QWidget *parent = nullptr);

signals:
void textChanged(const QString &text);

private:
void keyPressEvent(QKeyEvent *event) override;

QTimer *m_delayedTextChangedTimer = nullptr;
};
1 change: 1 addition & 0 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <QShortcut>
#include <QSplitter>
#include <QStatusBar>
#include <QString>
#include <QTimer>

#include "base/bittorrent/session.h"
Expand Down
1 change: 1 addition & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class QCloseEvent;
class QComboBox;
class QFileSystemWatcher;
class QSplitter;
class QString;
class QTabWidget;
class QTimer;

Expand Down
2 changes: 1 addition & 1 deletion src/gui/search/searchjobwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ SearchJobWidget::SearchJobWidget(SearchHandler *searchHandler, IGUIApplication *
m_lineEditSearchResultsFilter->setPlaceholderText(tr("Filter search results..."));
m_lineEditSearchResultsFilter->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_lineEditSearchResultsFilter, &QWidget::customContextMenuRequested, this, &SearchJobWidget::showFilterContextMenu);
connect(m_lineEditSearchResultsFilter, &LineEdit::textChanged, this, &SearchJobWidget::filterSearchResults);
m_ui->horizontalLayout->insertWidget(0, m_lineEditSearchResultsFilter);

connect(m_lineEditSearchResultsFilter, &LineEdit::textChanged, this, &SearchJobWidget::filterSearchResults);
connect(m_ui->filterMode, qOverload<int>(&QComboBox::currentIndexChanged)
, this, &SearchJobWidget::updateFilter);
connect(m_ui->minSeeds, &QAbstractSpinBox::editingFinished, this, &SearchJobWidget::updateFilter);
Expand Down
20 changes: 9 additions & 11 deletions src/webui/www/private/scripts/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ window.addEventListener("DOMContentLoaded", function() {
$('error_div').set('html', '');
if (response) {
clearTimeout(torrentsFilterInputTimer);
torrentsFilterInputTimer = -1;

let torrentsTableSelectedRows;
let update_categories = false;
let updateTags = false;
Expand Down Expand Up @@ -1449,18 +1451,14 @@ window.addEventListener("DOMContentLoaded", function() {
$('torrentFilesFilterToolbar').addClass("invisible");
};

let prevTorrentsFilterValue;
let torrentsFilterInputTimer = null;
// listen for changes to torrentsFilterInput
$('torrentsFilterInput').addEvent('input', function() {
const value = $('torrentsFilterInput').get("value");
if (value !== prevTorrentsFilterValue) {
prevTorrentsFilterValue = value;
clearTimeout(torrentsFilterInputTimer);
torrentsFilterInputTimer = setTimeout(function() {
torrentsTable.updateTable(false);
}, 400);
}
let torrentsFilterInputTimer = -1;
$('torrentsFilterInput').addEvent('input', () => {
clearTimeout(torrentsFilterInputTimer);
torrentsFilterInputTimer = setTimeout(() => {
torrentsFilterInputTimer = -1;
torrentsTable.updateTable();
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY);
});

$('transfersTabLink').addEvent('click', showTransfersTab);
Expand Down
2 changes: 2 additions & 0 deletions src/webui/www/private/scripts/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ window.qBittorrent.ContextMenu = (function() {

const touchstartEvent = e;
this.touchstartTimer = setTimeout(function() {
this.touchstartTimer = -1;
this.triggerMenu(touchstartEvent, elem);
}.bind(this), this.options.touchTimer);
}.bind(this));
elem.addEvent('touchend', function(e) {
e.preventDefault();
clearTimeout(this.touchstartTimer);
this.touchstartTimer = -1;
}.bind(this));
},

Expand Down
5 changes: 1 addition & 4 deletions src/webui/www/private/scripts/dynamicTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,7 @@ window.qBittorrent.DynamicTable = (function() {
return null;
},

updateTable: function(fullUpdate) {
if (fullUpdate === undefined)
fullUpdate = false;

updateTable: function(fullUpdate = false) {
const rows = this.getFilteredAndSortedRows();

for (let i = 0; i < this.selectedRows.length; ++i)
Expand Down
2 changes: 2 additions & 0 deletions src/webui/www/private/scripts/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ window.qBittorrent.Misc = (function() {
toFixedPointString: toFixedPointString,
containsAllTerms: containsAllTerms,
sleep: sleep,
// variables
FILTER_INPUT_DELAY: 400,
MAX_ETA: 8640000
};
};
Expand Down
36 changes: 19 additions & 17 deletions src/webui/www/private/scripts/prop-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ window.qBittorrent.PropFiles = (function() {
},
onSuccess: function(files) {
clearTimeout(torrentFilesFilterInputTimer);
torrentFilesFilterInputTimer = -1;

if (files.length === 0) {
torrentFilesTable.clear();
Expand Down Expand Up @@ -640,26 +641,27 @@ window.qBittorrent.PropFiles = (function() {
if (torrentFilesTable.getSortedColumn() === null)
torrentFilesTable.setSortedColumn('name');

let prevTorrentFilesFilterValue;
let torrentFilesFilterInputTimer = null;
// listen for changes to torrentFilesFilterInput
$('torrentFilesFilterInput').addEvent('input', function() {
let torrentFilesFilterInputTimer = -1;
$('torrentFilesFilterInput').addEvent('input', () => {
clearTimeout(torrentFilesFilterInputTimer);

const value = $('torrentFilesFilterInput').get("value");
if (value !== prevTorrentFilesFilterValue) {
prevTorrentFilesFilterValue = value;
torrentFilesTable.setFilter(value);
clearTimeout(torrentFilesFilterInputTimer);
torrentFilesFilterInputTimer = setTimeout(function() {
if (current_hash === "")
return;
torrentFilesTable.updateTable(false);
torrentFilesTable.setFilter(value);

if (value.trim() === "")
collapseAllNodes();
else
expandAllNodes();
}, 400);
}
torrentFilesFilterInputTimer = setTimeout(() => {
torrentFilesFilterInputTimer = -1;

if (current_hash === "")
return;

torrentFilesTable.updateTable();

if (value.trim() === "")
collapseAllNodes();
else
expandAllNodes();
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY);
});

/**
Expand Down
9 changes: 7 additions & 2 deletions src/webui/www/private/views/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
};

let customSyncLogDataInterval = null;
let logFilterTimer;
let logFilterTimer = -1;
let inputtedFilterText = "";
let selectBox;
let selectedLogLevels = JSON.parse(LocalPreferences.get('qbt_selected_log_levels')) || ['1', '2', '4', '8'];
Expand Down Expand Up @@ -298,9 +298,11 @@
const logFilterChanged = () => {
clearTimeout(logFilterTimer);
logFilterTimer = setTimeout((curTab) => {
logFilterTimer = -1;

tableInfo[curTab].instance.updateTable(false);
updateLabelCount(curTab);
}, 400, currentSelectedTab);
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY, currentSelectedTab);
};

const setCurrentTab = (tab) => {
Expand All @@ -322,6 +324,7 @@
}

clearTimeout(logFilterTimer);
logFilterTimer = -1;
load();

if (tableInfo[currentSelectedTab].instance.filterText !== getFilterText()) {
Expand Down Expand Up @@ -378,6 +381,8 @@

if (response.length > 0) {
clearTimeout(logFilterTimer);
logFilterTimer = -1;

for (let i = 0; i < response.length; ++i) {
let row;
if (curTab === 'main') {
Expand Down
22 changes: 10 additions & 12 deletions src/webui/www/private/views/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
max: 0.00,
maxUnit: 3
};
let prevNameFilterValue;
let selectedCategory = "QBT_TR(All categories)QBT_TR[CONTEXT=SearchEngineWidget]";
let selectedPlugin = "all";
let prevSelectedPlugin;
Expand All @@ -255,18 +254,17 @@
searchResultsTable.setup('searchResultsTableDiv', 'searchResultsTableFixedHeaderDiv', searchResultsTableContextMenu);
getPlugins();

let searchInNameFilterTimer = null;
// listen for changes to searchInNameFilter
$('searchInNameFilter').addEvent('input', function() {
const value = $('searchInNameFilter').get("value");
if (value !== prevNameFilterValue) {
prevNameFilterValue = value;
clearTimeout(searchInNameFilterTimer);
searchInNameFilterTimer = setTimeout(function() {
searchText.filterPattern = value;
searchFilterChanged();
}, 400);
}
let searchInNameFilterTimer = -1;
$('searchInNameFilter').addEvent('input', () => {
clearTimeout(searchInNameFilterTimer);
searchInNameFilterTimer = setTimeout(() => {
searchInNameFilterTimer = -1;

const value = $('searchInNameFilter').get("value");
searchText.filterPattern = value;
searchFilterChanged();
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY);
});

new Keyboard({
Expand Down

0 comments on commit 15697f9

Please sign in to comment.