From 659356a650fb952a4ccda13b4c08b59c69add458 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 30 Mar 2025 22:40:46 +0800 Subject: [PATCH] Support plugin search delay --- .../UserSettings/PluginSettings.cs | 13 +++-- .../UserSettings/Settings.cs | 29 +--------- Flow.Launcher.Plugin/PluginMetadata.cs | 5 +- Flow.Launcher.Plugin/SearchDelaySpeeds.cs | 32 +++++++++++ Flow.Launcher/Flow.Launcher.csproj | 1 - Flow.Launcher/Languages/en.xaml | 8 ++- Flow.Launcher/MainWindow.xaml | 1 + Flow.Launcher/MainWindow.xaml.cs | 56 +------------------ .../Controls/InstalledPluginSearchDelay.xaml | 10 ++-- .../Views/SettingsPaneGeneral.xaml | 4 +- Flow.Launcher/ViewModel/MainViewModel.cs | 21 +++++-- Flow.Launcher/ViewModel/PluginViewModel.cs | 24 ++++---- 12 files changed, 89 insertions(+), 115 deletions(-) create mode 100644 Flow.Launcher.Plugin/SearchDelaySpeeds.cs diff --git a/Flow.Launcher.Infrastructure/UserSettings/PluginSettings.cs b/Flow.Launcher.Infrastructure/UserSettings/PluginSettings.cs index 40999cd26..d1c047495 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/PluginSettings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/PluginSettings.cs @@ -51,7 +51,7 @@ namespace Flow.Launcher.Infrastructure.UserSettings settings.Version = metadata.Version; } settings.DefaultActionKeywords = metadata.ActionKeywords; // metadata provides default values - settings.DefaultSearchDelay = metadata.SearchDelay; // metadata provides default values + settings.DefaultSearchDelaySpeed = metadata.SearchDelaySpeed; // metadata provides default values // update metadata values with settings if (settings.ActionKeywords?.Count > 0) @@ -66,7 +66,7 @@ namespace Flow.Launcher.Infrastructure.UserSettings } metadata.Disabled = settings.Disabled; metadata.Priority = settings.Priority; - metadata.SearchDelay = settings.SearchDelay; + metadata.SearchDelaySpeed = settings.SearchDelaySpeed; } else { @@ -80,8 +80,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings ActionKeywords = metadata.ActionKeywords, // use default value Disabled = metadata.Disabled, Priority = metadata.Priority, - DefaultSearchDelay = metadata.SearchDelay, // metadata provides default values - SearchDelay = metadata.SearchDelay, // use default value + DefaultSearchDelaySpeed = metadata.SearchDelaySpeed, // metadata provides default values + SearchDelaySpeed = metadata.SearchDelaySpeed, // use default value }; } } @@ -120,9 +120,10 @@ namespace Flow.Launcher.Infrastructure.UserSettings public int Priority { get; set; } [JsonIgnore] - public int DefaultSearchDelay { get; set; } + public SearchDelaySpeeds? DefaultSearchDelaySpeed { get; set; } - public int SearchDelay { get; set; } + [JsonConverter(typeof(JsonStringEnumConverter))] + public SearchDelaySpeeds? SearchDelaySpeed { get; set; } /// /// Used only to save the state of the plugin in settings diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index e3f14480f..ab0a364d2 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -330,8 +330,9 @@ namespace Flow.Launcher.Infrastructure.UserSettings OnPropertyChanged(); } } - + SearchDelaySpeeds searchDelaySpeed { get; set; } = SearchDelaySpeeds.Medium; + [JsonConverter(typeof(JsonStringEnumConverter))] public SearchDelaySpeeds SearchDelaySpeed { get => searchDelaySpeed; @@ -342,23 +343,6 @@ namespace Flow.Launcher.Infrastructure.UserSettings } } - [JsonIgnore] - public int SearchDelay => SearchDelaySpeed switch - { - SearchDelaySpeeds.Slow => 250, - SearchDelaySpeeds.ModeratelySlow => 200, - SearchDelaySpeeds.Medium => 150, - SearchDelaySpeeds.ModeratelyFast => 100, - SearchDelaySpeeds.Fast => 50, - _ => 150 - }; - - [JsonIgnore] - public List PluginSearchDelayRange { get; } = new() - { - 0, 30, 60, 90, 120, 150 - }; - [JsonConverter(typeof(JsonStringEnumConverter))] public SearchWindowScreens SearchWindowScreen { get; set; } = SearchWindowScreens.Cursor; @@ -521,13 +505,4 @@ namespace Flow.Launcher.Infrastructure.UserSettings Mica, MicaAlt } - - public enum SearchDelaySpeeds - { - Slow, - ModeratelySlow, - Medium, - ModeratelyFast, - Fast - } } diff --git a/Flow.Launcher.Plugin/PluginMetadata.cs b/Flow.Launcher.Plugin/PluginMetadata.cs index bafce164b..42b623717 100644 --- a/Flow.Launcher.Plugin/PluginMetadata.cs +++ b/Flow.Launcher.Plugin/PluginMetadata.cs @@ -99,9 +99,10 @@ namespace Flow.Launcher.Plugin public bool HideActionKeywordPanel { get; set; } /// - /// Plugin search delay in ms. + /// Plugin search delay speed. Null means use default search delay. /// - public int SearchDelay { get; set; } + [JsonConverter(typeof(JsonStringEnumConverter))] + public SearchDelaySpeeds? SearchDelaySpeed { get; set; } = null; /// /// Plugin icon path. diff --git a/Flow.Launcher.Plugin/SearchDelaySpeeds.cs b/Flow.Launcher.Plugin/SearchDelaySpeeds.cs new file mode 100644 index 000000000..543f8b3f6 --- /dev/null +++ b/Flow.Launcher.Plugin/SearchDelaySpeeds.cs @@ -0,0 +1,32 @@ +namespace Flow.Launcher.Plugin; + +/// +/// Enum for search delay speeds +/// +public enum SearchDelaySpeeds +{ + /// + /// Slow search delay speed. 50ms. + /// + Slow, + + /// + /// Moderately slow search delay speed. 100ms. + /// + ModeratelySlow, + + /// + /// Medium search delay speed. 150ms. Default value. + /// + Medium, + + /// + /// Moderately fast search delay speed. 200ms. + /// + ModeratelyFast, + + /// + /// Fast search delay speed. 250ms. + /// + Fast +} diff --git a/Flow.Launcher/Flow.Launcher.csproj b/Flow.Launcher/Flow.Launcher.csproj index d5567656c..91cab9fa0 100644 --- a/Flow.Launcher/Flow.Launcher.csproj +++ b/Flow.Launcher/Flow.Launcher.csproj @@ -100,7 +100,6 @@ - diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index c1c894995..2f5c2e5a9 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -104,8 +104,8 @@ Shadow effect is not allowed while current theme has blur effect enabled Search Delay Delay for a while to search when typing. This reduces interface jumpiness and result load. - Search Delay Speed - Delay time speed after which search results appear when typing is stopped. Default is medium. + Default Search Delay Speed + Plugins default delay time after which search results appear when typing is stopped. Default is medium. Slow Moderately slow Medium @@ -127,7 +127,8 @@ Current action keyword New action keyword Change Action Keywords - Change Seach Delay Time + Plugin seach delay speed + Change Plugin Seach Delay Speed Current Priority New Priority Priority @@ -143,6 +144,7 @@ Plugins: {0} - Fail to remove plugin settings files, please remove them manually Fail to remove plugin cache Plugins: {0} - Fail to remove plugin cache files, please remove them manually + Default Plugin Store diff --git a/Flow.Launcher/MainWindow.xaml b/Flow.Launcher/MainWindow.xaml index ff9db9a94..31bc2ba50 100644 --- a/Flow.Launcher/MainWindow.xaml +++ b/Flow.Launcher/MainWindow.xaml @@ -252,6 +252,7 @@ PreviewKeyUp="QueryTextBox_KeyUp" Style="{DynamicResource QueryBoxStyle}" Text="{Binding QueryText, Mode=OneWay}" + TextChanged="QueryTextBox_TextChanged1" Visibility="Visible" WindowChrome.IsHitTestVisibleInChrome="True"> diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index b1ee1726c..011d46d6b 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -2,7 +2,6 @@ using System.ComponentModel; using System.Linq; using System.Media; -using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -64,9 +63,6 @@ namespace Flow.Launcher private const double DefaultRightMargin = 66; //* this value from base.xaml private bool _isClockPanelAnimating = false; - // Search Delay - private IDisposable _reactiveSubscription; - // IDisposable private bool _disposed = false; @@ -122,9 +118,6 @@ namespace Flow.Launcher welcomeWindow.Show(); } - // Initialize search delay - SetupSearchTextBoxReactiveness(_settings.SearchQueryResultsWithDelay, _settings.SearchDelay); - // Initialize place holder SetupPlaceholderText(); _viewModel.PlaceholderText = _settings.PlaceholderText; @@ -267,15 +260,6 @@ namespace Flow.Launcher case nameof(Settings.KeepMaxResults): SetupResizeMode(); break; - case nameof(Settings.SearchQueryResultsWithDelay): - SetupSearchTextBoxReactiveness(_settings.SearchQueryResultsWithDelay, _settings.SearchDelay); - break; - case nameof(Settings.SearchDelaySpeed): - if (_settings.SearchQueryResultsWithDelay) - { - SetupSearchTextBoxReactiveness(_settings.SearchQueryResultsWithDelay, _settings.SearchDelay); - } - break; } }; @@ -1126,46 +1110,11 @@ namespace Flow.Launcher #region Search Delay - // Edited from: https://github.com/microsoft/PowerToys - - private void SetupSearchTextBoxReactiveness(bool showResultsWithDelay, int searchDelay) - { - if (_reactiveSubscription != null) - { - _reactiveSubscription.Dispose(); - _reactiveSubscription = null; - } - - QueryTextBox.TextChanged -= QueryTextBox_TextChanged1; - - if (showResultsWithDelay) - { - _reactiveSubscription = Observable.FromEventPattern( - conversion => (sender, eventArg) => conversion(sender, eventArg), - add => QueryTextBox.TextChanged += add, - remove => QueryTextBox.TextChanged -= remove) - // TODO: Remove debug codes. - .Throttle(TimeSpan.FromMilliseconds(_settings.SearchDelay * 10)) - .Do(@event => Dispatcher.Invoke(() => PerformSearchQuery(true, (TextBox)@event.Sender))) - .Subscribe(); - } - else - { - QueryTextBox.TextChanged += QueryTextBox_TextChanged1; - } - } - private void QueryTextBox_TextChanged1(object sender, TextChangedEventArgs e) { var textBox = (TextBox)sender; - PerformSearchQuery(false, textBox); - } - - private void PerformSearchQuery(bool searchDelay, TextBox textBox) - { - var text = textBox.Text; - _viewModel.QueryText = text; - _viewModel.Query(searchDelay); + _viewModel.QueryText = textBox.Text; + _viewModel.Query(_settings.SearchQueryResultsWithDelay); } #endregion @@ -1180,7 +1129,6 @@ namespace Flow.Launcher { _hwndSource?.Dispose(); _notifyIcon?.Dispose(); - _reactiveSubscription?.Dispose(); } _disposed = true; diff --git a/Flow.Launcher/Resources/Controls/InstalledPluginSearchDelay.xaml b/Flow.Launcher/Resources/Controls/InstalledPluginSearchDelay.xaml index 3e3af80c0..bd2f9a7c9 100644 --- a/Flow.Launcher/Resources/Controls/InstalledPluginSearchDelay.xaml +++ b/Flow.Launcher/Resources/Controls/InstalledPluginSearchDelay.xaml @@ -32,18 +32,18 @@ VerticalAlignment="Center" DockPanel.Dock="Left" Style="{DynamicResource SettingTitleLabel}" - Text="{DynamicResource searchDelayTime}" /> + Text="{DynamicResource pluginSearchDelaySpeed}" /> - + ToolTip="{DynamicResource pluginSearchDelaySpeedTooltip}" /> diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml index c0a3118ea..372fdccd7 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml @@ -184,9 +184,9 @@ + Sub="{DynamicResource searchDelaySpeedToolTip}"> 250, + SearchDelaySpeeds.ModeratelySlow => 200, + SearchDelaySpeeds.Medium => 150, + SearchDelaySpeeds.ModeratelyFast => 100, + SearchDelaySpeeds.Fast => 50, + _ => 150 + }; // TODO: Remove debug codes. - System.Diagnostics.Debug.WriteLine($"!!!{plugin.Metadata.Name} Waited {plugin.Metadata.SearchDelay} ms"); + System.Diagnostics.Debug.WriteLine($"!!!{plugin.Metadata.Name} Waiting {searchDelayTime} ms"); + + await Task.Delay(searchDelayTime, token); + + // TODO: Remove debug codes. + System.Diagnostics.Debug.WriteLine($"!!!{plugin.Metadata.Name} Waited {searchDelayTime} ms"); if (token.IsCancellationRequested) return; diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index 7e5a2df17..b856fc58b 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -1,14 +1,11 @@ using System.Linq; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; -using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Infrastructure.Image; -using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Resources.Controls; @@ -63,6 +60,7 @@ namespace Flow.Launcher.ViewModel } set => _image = value; } + public bool PluginState { get => !PluginPair.Metadata.Disabled; @@ -72,6 +70,7 @@ namespace Flow.Launcher.ViewModel PluginSettingsObject.Disabled = !value; } } + public bool IsExpanded { get => _isExpanded; @@ -84,16 +83,13 @@ namespace Flow.Launcher.ViewModel } } - public IEnumerable PluginSearchDelayRange { get; } = - Ioc.Default.GetRequiredService().PluginSearchDelayRange; - - public int PluginSearchDelay + public SearchDelaySpeeds? PluginSearchDelay { - get => PluginPair.Metadata.SearchDelay; + get => PluginPair.Metadata.SearchDelaySpeed; set { - PluginPair.Metadata.SearchDelay = value; - PluginSettingsObject.SearchDelay = value; + PluginPair.Metadata.SearchDelaySpeed = value; + PluginSettingsObject.SearchDelaySpeed = value; } } @@ -131,6 +127,7 @@ namespace Flow.Launcher.ViewModel PluginPair.Metadata.AvgQueryTime + "ms"; public string ActionKeywordsText => string.Join(Query.ActionKeywordSeparator, PluginPair.Metadata.ActionKeywords); public int Priority => PluginPair.Metadata.Priority; + public string SearchDelaySpeedText => PluginPair.Metadata.SearchDelaySpeed == null ? App.API.GetTranslation("default") : App.API.GetTranslation($"SearchDelaySpeed{PluginPair.Metadata.SearchDelaySpeed}"); public Infrastructure.UserSettings.Plugin PluginSettingsObject{ get; init; } public void OnActionKeywordsChanged() @@ -179,5 +176,12 @@ namespace Flow.Launcher.ViewModel ActionKeywords changeKeywordsWindow = new ActionKeywords(this); changeKeywordsWindow.ShowDialog(); } + + [RelayCommand] + private void SetSearchDelaySpeed() + { + /*SearchDelaySpeedWindow searchDelaySpeedWindow = new SearchDelaySpeedWindow(this); + searchDelaySpeedWindow.ShowDialog();*/ + } } }