From 1490fb300c23129eb68eac4754e32799c8bc74e3 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Sun, 12 Jun 2022 14:12:10 +0100 Subject: [PATCH] [PTRun]Add setting to disable input delay (#18724) * [PTRun]Add setting to disable input delay * Address feedback and allow configuring delay * Address PR feedback --- .../launcher/PowerLauncher/MainWindow.xaml.cs | 53 ++++++++++++++++--- .../launcher/PowerLauncher/SettingsReader.cs | 10 ++++ .../PowerLauncher/ViewModel/MainViewModel.cs | 15 ++++++ .../UserSettings/PowerToysRunSettings.cs | 38 +++++++++++++ .../PowerLauncherProperties.cs | 8 +++ .../ViewModels/PowerLauncherViewModel.cs | 34 ++++++++++++ .../Settings.UI/Strings/en-us/Resources.resw | 11 ++++ .../Settings.UI/Views/PowerLauncherPage.xaml | 23 ++++++++ 8 files changed, 185 insertions(+), 7 deletions(-) diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index ffa1efc692..f7d9292acb 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -36,6 +36,7 @@ namespace PowerLauncher private Timer _firstDeleteTimer = new Timer(); private bool _coldStateHotkeyPressed; private bool _disposedValue; + private IDisposable _reactiveSubscription; public MainWindow(PowerToysRunSettings settings, MainViewModel mainVM) : this() @@ -159,13 +160,18 @@ namespace PowerLauncher SearchBox.QueryTextBox.DataContext = _viewModel; SearchBox.QueryTextBox.PreviewKeyDown += Launcher_KeyDown; - Observable.FromEventPattern( - 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(); + SetupSearchTextBoxReactiveness(_viewModel.GetSearchQueryResultsWithDelaySetting(), _viewModel.GetSearchInputDelaySetting()); + _viewModel.RegisterSettingsChangeListener( + (s, prop_e) => + { + if (prop_e.PropertyName == nameof(PowerToysRunSettings.SearchQueryResultsWithDelay) || prop_e.PropertyName == nameof(PowerToysRunSettings.SearchInputDelay)) + { + Application.Current.Dispatcher.Invoke(() => + { + SetupSearchTextBoxReactiveness(_viewModel.GetSearchQueryResultsWithDelaySetting(), _viewModel.GetSearchInputDelaySetting()); + }); + } + }); // Set initial language flow direction SearchBox_UpdateFlowDirection(); @@ -186,6 +192,32 @@ namespace PowerLauncher BringProcessToForeground(); } + private void SetupSearchTextBoxReactiveness(bool showResultsWithDelay, int searchInputDelayMs) + { + if (_reactiveSubscription != null) + { + _reactiveSubscription.Dispose(); + _reactiveSubscription = null; + } + + SearchBox.QueryTextBox.TextChanged -= QueryTextBox_TextChanged; + + if (showResultsWithDelay) + { + _reactiveSubscription = Observable.FromEventPattern( + add => SearchBox.QueryTextBox.TextChanged += add, + remove => SearchBox.QueryTextBox.TextChanged -= remove) + .Do(@event => ClearAutoCompleteText((TextBox)@event.Sender)) + .Throttle(TimeSpan.FromMilliseconds(searchInputDelayMs)) + .Do(@event => Dispatcher.InvokeAsync(() => PerformSearchQuery((TextBox)@event.Sender))) + .Subscribe(); + } + else + { + SearchBox.QueryTextBox.TextChanged += QueryTextBox_TextChanged; + } + } + private void SuggestionsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var result = ((FrameworkElement)e.OriginalSource).DataContext; @@ -428,6 +460,13 @@ namespace PowerLauncher } } + private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e) + { + var textBox = (TextBox)sender; + ClearAutoCompleteText(textBox); + PerformSearchQuery(textBox); + } + private void ClearAutoCompleteText(TextBox textBox) { var text = textBox.Text; diff --git a/src/modules/launcher/PowerLauncher/SettingsReader.cs b/src/modules/launcher/PowerLauncher/SettingsReader.cs index 588ef793ef..fa06d1031d 100644 --- a/src/modules/launcher/PowerLauncher/SettingsReader.cs +++ b/src/modules/launcher/PowerLauncher/SettingsReader.cs @@ -105,6 +105,16 @@ namespace PowerLauncher _settings.UseCentralizedKeyboardHook = overloadSettings.Properties.UseCentralizedKeyboardHook; } + if (_settings.SearchQueryResultsWithDelay != overloadSettings.Properties.SearchQueryResultsWithDelay) + { + _settings.SearchQueryResultsWithDelay = overloadSettings.Properties.SearchQueryResultsWithDelay; + } + + if (_settings.SearchInputDelay != overloadSettings.Properties.SearchInputDelay) + { + _settings.SearchInputDelay = overloadSettings.Properties.SearchInputDelay; + } + if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults) { _settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults; diff --git a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs index 03e38f3bc5..b66fba458e 100644 --- a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs +++ b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs @@ -132,6 +132,11 @@ namespace PowerLauncher.ViewModel // SetCustomPluginHotkey(); } + public void RegisterSettingsChangeListener(System.ComponentModel.PropertyChangedEventHandler handler) + { + _settings.PropertyChanged += handler; + } + private void RegisterResultsUpdatedEvent() { foreach (var pair in PluginManager.GetPluginsForInterface()) @@ -1102,5 +1107,15 @@ namespace PowerLauncher.ViewModel _hotkeyTimer.Reset(); return recordedTime; } + + public bool GetSearchQueryResultsWithDelaySetting() + { + return _settings.SearchQueryResultsWithDelay; + } + + public int GetSearchInputDelaySetting() + { + return _settings.SearchInputDelay; + } } } diff --git a/src/modules/launcher/Wox.Infrastructure/UserSettings/PowerToysRunSettings.cs b/src/modules/launcher/Wox.Infrastructure/UserSettings/PowerToysRunSettings.cs index 5ee3d9da0f..d7fb04a194 100644 --- a/src/modules/launcher/Wox.Infrastructure/UserSettings/PowerToysRunSettings.cs +++ b/src/modules/launcher/Wox.Infrastructure/UserSettings/PowerToysRunSettings.cs @@ -61,6 +61,44 @@ namespace Wox.Infrastructure.UserSettings } } + private bool _searchQueryResultsWithDelay = true; + + public bool SearchQueryResultsWithDelay + { + get + { + return _searchQueryResultsWithDelay; + } + + set + { + if (_searchQueryResultsWithDelay != value) + { + _searchQueryResultsWithDelay = value; + OnPropertyChanged(nameof(SearchQueryResultsWithDelay)); + } + } + } + + private int _searchInputDelay = 150; + + public int SearchInputDelay + { + get + { + return _searchInputDelay; + } + + set + { + if (_searchInputDelay != value) + { + _searchInputDelay = value; + OnPropertyChanged(nameof(SearchInputDelay)); + } + } + } + public string Language { get; set; } = "en"; public Theme Theme { get; set; } = Theme.System; diff --git a/src/settings-ui/Settings.UI.Library/PowerLauncherProperties.cs b/src/settings-ui/Settings.UI.Library/PowerLauncherProperties.cs index aa42cd5e6a..54ed8cdf94 100644 --- a/src/settings-ui/Settings.UI.Library/PowerLauncherProperties.cs +++ b/src/settings-ui/Settings.UI.Library/PowerLauncherProperties.cs @@ -51,6 +51,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library [JsonPropertyName("use_centralized_keyboard_hook")] public bool UseCentralizedKeyboardHook { get; set; } + [JsonPropertyName("search_query_results_with_delay")] + public bool SearchQueryResultsWithDelay { get; set; } + + [JsonPropertyName("search_input_delay")] + public int SearchInputDelay { get; set; } + public PowerLauncherProperties() { OpenPowerLauncher = new HotkeySettings(false, false, true, false, 32); @@ -65,6 +71,8 @@ namespace Microsoft.PowerToys.Settings.UI.Library Theme = Theme.System; Position = StartupPosition.Cursor; UseCentralizedKeyboardHook = false; + SearchQueryResultsWithDelay = true; + SearchInputDelay = 150; } } } diff --git a/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs b/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs index 2c8e555b73..6dc73c4b90 100644 --- a/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs +++ b/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs @@ -296,6 +296,40 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels } } + public bool SearchQueryResultsWithDelay + { + get + { + return settings.Properties.SearchQueryResultsWithDelay; + } + + set + { + if (settings.Properties.SearchQueryResultsWithDelay != value) + { + settings.Properties.SearchQueryResultsWithDelay = value; + UpdateSettings(); + } + } + } + + public int SearchInputDelay + { + get + { + return settings.Properties.SearchInputDelay; + } + + set + { + if (settings.Properties.SearchInputDelay != value) + { + settings.Properties.SearchInputDelay = value; + UpdateSettings(); + } + } + } + public HotkeySettings OpenFileLocation { get diff --git a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw index 90c811313e..285445c561 100644 --- a/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw +++ b/src/settings-ui/Settings.UI/Strings/en-us/Resources.resw @@ -414,6 +414,17 @@ Clear the previous query on launch + + Delay search + This is about adding a delay to wait for more input before executing a search + + + Add a delay to wait for more input before executing a search + + + Search delay (ms) + ms = milliseconds + To: Keyboard Manager mapping keys view right header diff --git a/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml b/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml index f0b079536f..15ffaf4b57 100644 --- a/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml +++ b/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml @@ -94,6 +94,29 @@ + + + + + + + + + + + + + + + +