From f5f256809c057bea58840c3baf93baaba39ac0e2 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 21 Jul 2025 13:52:11 +0800 Subject: [PATCH] Add IResultUpdateRegister interface --- .../Plugin/IResultUpdateRegister.cs | 12 +++ Flow.Launcher/ViewModel/MainViewModel.cs | 74 +++++++++---------- 2 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 Flow.Launcher.Core/Plugin/IResultUpdateRegister.cs diff --git a/Flow.Launcher.Core/Plugin/IResultUpdateRegister.cs b/Flow.Launcher.Core/Plugin/IResultUpdateRegister.cs new file mode 100644 index 000000000..1da04bf01 --- /dev/null +++ b/Flow.Launcher.Core/Plugin/IResultUpdateRegister.cs @@ -0,0 +1,12 @@ +using Flow.Launcher.Plugin; + +namespace Flow.Launcher.Core.Plugin; + +public interface IResultUpdateRegister +{ + /// + /// Register a plugin to receive results updated event. + /// + /// + void RegisterResultsUpdatedEvent(PluginPair pair); +} diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 5eae23c27..5816f1a85 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -27,7 +27,7 @@ using ModernWpf; namespace Flow.Launcher.ViewModel { - public partial class MainViewModel : BaseModel, ISavable, IDisposable + public partial class MainViewModel : BaseModel, ISavable, IDisposable, IResultUpdateRegister { #region Private Fields @@ -274,52 +274,50 @@ namespace Flow.Launcher.ViewModel } } - public void RegisterResultsUpdatedEvent() + public void RegisterResultsUpdatedEvent(PluginPair pair) { - foreach (var pair in PluginManager.GetResultUpdatePlugin()) + if (pair.Plugin is not IResultUpdated plugin) return; + + plugin.ResultsUpdated += (s, e) => { - var plugin = (IResultUpdated)pair.Plugin; - plugin.ResultsUpdated += (s, e) => + if (e.Query.RawQuery != QueryText || e.Token.IsCancellationRequested) { - if (e.Query.RawQuery != QueryText || e.Token.IsCancellationRequested) + return; + } + + var token = e.Token == default ? _updateToken : e.Token; + + IReadOnlyList resultsCopy; + if (e.Results == null) + { + resultsCopy = _emptyResult; + } + else + { + // make a clone to avoid possible issue that plugin will also change the list and items when updating view model + resultsCopy = DeepCloneResults(e.Results, false, token); + } + + foreach (var result in resultsCopy) + { + if (string.IsNullOrEmpty(result.BadgeIcoPath)) { - return; + result.BadgeIcoPath = pair.Metadata.IcoPath; } + } - var token = e.Token == default ? _updateToken : e.Token; + PluginManager.UpdatePluginMetadata(resultsCopy, pair.Metadata, e.Query); - IReadOnlyList resultsCopy; - if (e.Results == null) - { - resultsCopy = _emptyResult; - } - else - { - // make a clone to avoid possible issue that plugin will also change the list and items when updating view model - resultsCopy = DeepCloneResults(e.Results, false, token); - } + if (token.IsCancellationRequested) return; - foreach (var result in resultsCopy) - { - if (string.IsNullOrEmpty(result.BadgeIcoPath)) - { - result.BadgeIcoPath = pair.Metadata.IcoPath; - } - } + App.API.LogDebug(ClassName, $"Update results for plugin <{pair.Metadata.Name}>"); - PluginManager.UpdatePluginMetadata(resultsCopy, pair.Metadata, e.Query); - - if (token.IsCancellationRequested) return; - - App.API.LogDebug(ClassName, $"Update results for plugin <{pair.Metadata.Name}>"); - - if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query, - token))) - { - App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); - } - }; - } + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query, + token))) + { + App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); + } + }; } private async Task RegisterClockAndDateUpdateAsync()