Skip to content

Commit 2ac53aa

Browse files
committed
Merge branch 'add_keypress_processing_delay2' into add_keypress_processing_delay
2 parents e46e31a + 8469b33 commit 2ac53aa

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

NppNavigateTo/Forms/FrmNavigateTo.cs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
using System.Net;
1010
using System.Reflection;
1111
using System.Reflection.Emit;
12+
using System.Runtime.CompilerServices;
1213
using System.Text;
1314
using System.Text.RegularExpressions;
15+
using System.Threading;
1416
using System.Windows.Controls;
1517
using System.Windows.Documents;
1618
using System.Windows.Forms;
@@ -67,6 +69,12 @@ partial class FrmNavigateTo : Form
6769

6870
private Glob glob { get; set; }
6971

72+
const int lastKeyPressTimerInterval = 250;
73+
74+
private System.Timers.Timer lastKeyPressTimer;
75+
76+
private long lastKeypressTicks;
77+
7078
/// <summary>
7179
/// function used to filter filenames when not using fuzzy matchin
7280
/// </summary>
@@ -102,6 +110,11 @@ public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
102110
//backgroundWorker.WorkerSupportsCancellation = true;
103111
//backgroundWorker.DoWork += BackgroundWorker_DoWork;
104112
glob = new Glob();
113+
lastKeyPressTimer = new System.Timers.Timer();
114+
lastKeyPressTimer.AutoReset = false;
115+
lastKeyPressTimer.Interval = lastKeyPressTimerInterval;
116+
lastKeyPressTimer.Elapsed += LastKeyPressTimer_Elapsed;
117+
lastKeypressTicks = 0;
105118
nonFuzzyFilterFunc = null;
106119
InitializeComponent();
107120
ReloadFileList();
@@ -576,9 +589,11 @@ private void SearchComboBoxKeyDown(object sender, KeyEventArgs e)
576589
break;
577590

578591
case Keys.Space:
579-
// adding space can't change the results
580-
// unless the space is part of a character class in a glob (e.g. "[ ]" matches literal whitespace)
581-
lastCharCannotChangeResults = searchComboBox.Text.IndexOf('[') == -1;
592+
// adding space can't change the results unless:
593+
// - the space is part of a character class in a glob (e.g. "[ ]" matches literal whitespace)
594+
// - the space would push the number of chars in the search box over the min char limit
595+
int minLength = FrmSettings.Settings.GetIntSetting(Settings.minTypeCharLimit);
596+
lastCharCannotChangeResults = searchComboBox.Text.Length != minLength && searchComboBox.Text.IndexOf('[') == -1;
582597
break;
583598
}
584599
}
@@ -658,21 +673,33 @@ private void SearchComboBoxTextChanged(object sender, EventArgs e)
658673
{
659674
if (lastCharCannotChangeResults)
660675
return;
661-
int textLength = searchComboBox.Text.Length;
662-
bool emptyText = string.IsNullOrWhiteSpace(searchComboBox.Text);
663-
int minLength = FrmSettings.Settings.GetIntSetting(Settings.minTypeCharLimit);
676+
lastKeyPressTimer.Start();
677+
lastKeypressTicks = System.DateTime.UtcNow.Ticks;
678+
}
664679

665-
if (emptyText)
666-
ReloadFileList();
667-
if (textLength > minLength || emptyText)
668-
{
669-
FilterDataGrid(searchComboBox.Text);
670-
}
680+
private void LastKeyPressTimer_Elapsed(object sender, EventArgs e)
681+
{
682+
Invoke(new Action(() =>
683+
{
684+
// allow keypresses more recent than the interval, because we can't rely on precise timing of this event firing
685+
if (System.DateTime.UtcNow.Ticks <= lastKeypressTicks + System.TimeSpan.TicksPerMillisecond * lastKeyPressTimerInterval / 4)
686+
return;
687+
int textLength = searchComboBox.Text.Length;
688+
bool emptyText = string.IsNullOrWhiteSpace(searchComboBox.Text);
689+
int minLength = FrmSettings.Settings.GetIntSetting(Settings.minTypeCharLimit);
690+
691+
if (emptyText)
692+
ReloadFileList();
693+
if (textLength > minLength || emptyText)
694+
{
695+
FilterDataGrid(searchComboBox.Text);
696+
}
671697

672-
if (FrmSettings.Settings.GetBoolSetting(Settings.selectFirstRowOnFilter))
673-
{
674-
SelectFirstRow();
675-
}
698+
if (FrmSettings.Settings.GetBoolSetting(Settings.selectFirstRowOnFilter))
699+
{
700+
SelectFirstRow();
701+
}
702+
}));
676703
}
677704

678705
private void SelectFirstRow()

NppNavigateTo/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929
// Build Number
3030
// Revision
3131
//
32-
[assembly: AssemblyVersion("2.6.5.0")]
33-
[assembly: AssemblyFileVersion("2.6.5.0")]
32+
[assembly: AssemblyVersion("2.6.5.1")]
33+
[assembly: AssemblyFileVersion("2.6.5.1")]

0 commit comments

Comments
 (0)