Merge pull request #3257 from Jack251970/result_copy

Fix Issue that Plugin Cannot Cache Records
This commit is contained in:
Jack Ye 2025-02-22 17:59:45 +08:00 committed by GitHub
commit 9665619adc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 10 deletions

View file

@ -281,7 +281,7 @@ namespace Flow.Launcher.Core.Plugin
return results;
}
public static void UpdatePluginMetadata(List<Result> results, PluginMetadata metadata, Query query)
public static void UpdatePluginMetadata(IReadOnlyList<Result> results, PluginMetadata metadata, Query query)
{
foreach (var r in results)
{

View file

@ -266,8 +266,9 @@ namespace Flow.Launcher.Plugin
/// The key to identify the record. This is used when FL checks whether the result is the topmost record. Or FL calculates the hashcode of the result for user selected records.
/// This can be useful when your plugin will change the Title or SubTitle of the result dynamically.
/// If the plugin does not specific this, FL just uses Title and SubTitle to identify this result.
/// Note: Because old data does not have this key, we should use null as the default value for consistency.
/// </summary>
public string RecordKey { get; set; } = string.Empty;
public string RecordKey { get; set; } = null;
/// <summary>
/// Info of the preview section of a <see cref="Result"/>

View file

@ -231,8 +231,8 @@ namespace Flow.Launcher.ViewModel
var token = e.Token == default ? _updateToken : e.Token;
// make a copy of results to avoid plugin change the result when updating view model
var resultsCopy = e.Results.ToList();
// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
var resultsCopy = DeepCloneResults(e.Results, token);
PluginManager.UpdatePluginMetadata(resultsCopy, pair.Metadata, e.Query);
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
@ -414,6 +414,22 @@ namespace Flow.Launcher.ViewModel
}
}
private static IReadOnlyList<Result> DeepCloneResults(IReadOnlyList<Result> results, CancellationToken token = default)
{
var resultsCopy = new List<Result>();
foreach (var result in results.ToList())
{
if (token.IsCancellationRequested)
{
break;
}
var resultCopy = result.Clone();
resultsCopy.Add(resultCopy);
}
return resultsCopy;
}
#endregion
#region BasicCommands
@ -1180,9 +1196,18 @@ namespace Flow.Launcher.ViewModel
currentCancellationToken.ThrowIfCancellationRequested();
results ??= _emptyResult;
IReadOnlyList<Result> resultsCopy;
if (results == null)
{
resultsCopy = _emptyResult;
}
else
{
// make a copy of results to avoid possible issue that FL changes some properties of the records, like score, etc.
resultsCopy = DeepCloneResults(results);
}
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(results, plugin.Metadata, query,
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
currentCancellationToken, reSelect)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
@ -1471,12 +1496,31 @@ namespace Flow.Launcher.ViewModel
{
result.Score = Result.MaxScore;
}
else if (result.Score != Result.MaxScore)
else
{
var priorityScore = metaResults.Metadata.Priority * 150;
result.Score += result.AddSelectedCount ?
_userSelectedRecord.GetSelectedCount(result) + priorityScore :
priorityScore;
if (result.AddSelectedCount)
{
if ((long)result.Score + _userSelectedRecord.GetSelectedCount(result) + priorityScore > Result.MaxScore)
{
result.Score = Result.MaxScore;
}
else
{
result.Score += _userSelectedRecord.GetSelectedCount(result) + priorityScore;
}
}
else
{
if ((long)result.Score + priorityScore > Result.MaxScore)
{
result.Score = Result.MaxScore;
}
else
{
result.Score += priorityScore;
}
}
}
}
}