Add small delay to the search query before displaying results (#18290)

* Add small delay to the search query before displaying results

* Reduce user input throttle to 150ms and fix issue with autocomplete text updating

* Add System.Reactive.dll to Launcher installer
This commit is contained in:
Jason Shands
2022-05-19 13:23:24 -05:00
committed by GitHub
parent dda0aa237c
commit be1ed8c0d4
3 changed files with 19 additions and 6 deletions

View File

@@ -5,6 +5,7 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Timers;
using System.Windows;
@@ -159,7 +160,14 @@ namespace PowerLauncher
SearchBox.QueryTextBox.DataContext = _viewModel;
SearchBox.QueryTextBox.PreviewKeyDown += Launcher_KeyDown;
SearchBox.QueryTextBox.TextChanged += QueryTextBox_TextChanged;
Observable.FromEventPattern<TextChangedEventHandler, TextChangedEventArgs>(
add => SearchBox.QueryTextBox.TextChanged += add,
remove => SearchBox.QueryTextBox.TextChanged -= remove)
.Do(@event => ClearAutoCompleteText((TextBox)@event.Sender))
.Throttle(TimeSpan.FromMilliseconds(150))
.Do(@event => Dispatcher.InvokeAsync(() => PerformSearchQuery((TextBox)@event.Sender)))
.Subscribe();
// Set initial language flow direction
SearchBox_UpdateFlowDirection();
@@ -413,7 +421,7 @@ namespace PowerLauncher
// To populate the AutoCompleteTextBox as soon as the selection is changed or set.
// Setting it here instead of when the text is changed as there is a delay in executing the query and populating the result
if (_viewModel.Results != null)
if (_viewModel.Results != null && !string.IsNullOrEmpty(SearchBox.QueryTextBox.Text))
{
SearchBox.AutoCompleteTextBlock.Text = MainViewModel.GetAutoCompleteText(
_viewModel.Results.SelectedIndex,
@@ -422,9 +430,8 @@ namespace PowerLauncher
}
}
private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e)
private void ClearAutoCompleteText(TextBox textBox)
{
var textBox = (TextBox)sender;
var text = textBox.Text;
var autoCompleteText = SearchBox.AutoCompleteTextBlock.Text;
@@ -432,6 +439,11 @@ namespace PowerLauncher
{
SearchBox.AutoCompleteTextBlock.Text = string.Empty;
}
}
private void PerformSearchQuery(TextBox textBox)
{
var text = textBox.Text;
if (_isTextSetProgrammatically)
{

View File

@@ -88,6 +88,7 @@
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="ModernWpfUI" Version="0.9.4" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="Microsoft.VCRTForwarders.140" Version="1.0.7" />
<PackageReference Include="System.Data.OleDb" Version="6.0.0" />