From 38c01c65eab5b0ef0899be82774f13dc0fd3d3dd Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Fri, 10 Jan 2025 23:06:18 +0800
Subject: [PATCH 1/2] Add support for customizing equal rules for results
---
Flow.Launcher.Plugin/Result.cs | 23 ++++++++++++++++++---
Flow.Launcher/Storage/TopMostRecord.cs | 10 +++++++--
Flow.Launcher/Storage/UserSelectedRecord.cs | 9 ++++----
3 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs
index f2050cc9f78..baa65866c66 100644
--- a/Flow.Launcher.Plugin/Result.cs
+++ b/Flow.Launcher.Plugin/Result.cs
@@ -1,5 +1,4 @@
using System;
-using System.Runtime;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
@@ -13,7 +12,6 @@ namespace Flow.Launcher.Plugin
///
public class Result
{
-
private string _pluginDirectory;
private string _icoPath;
@@ -103,7 +101,6 @@ public string IcoPath
///
public GlyphInfo Glyph { get; init; }
-
///
/// An action to take in the form of a function call when the result has been selected.
///
@@ -273,6 +270,26 @@ public ValueTask ExecuteAsync(ActionContext context)
///
public const int MaxScore = int.MaxValue;
+ ///
+ /// An action to get the key of the title. 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 of the result dynamically.
+ ///
+ ///
+ /// The function is invoked with the title of the result as the only parameter.
+ /// Its result determines the key of the title.
+ ///
+ public Func GetTitleKey { get; set; } = null;
+
+ ///
+ /// An action to get the key of the subtitle. 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 subtitle of the result dynamically.
+ ///
+ ///
+ /// The function is invoked with the subtitle of the result as the only parameter.
+ /// Its result determines the key of the subtitle.
+ ///
+ public Func GetSubTitleKey { get; set; } = null;
+
///
/// Info of the preview section of a
///
diff --git a/Flow.Launcher/Storage/TopMostRecord.cs b/Flow.Launcher/Storage/TopMostRecord.cs
index cbd0b88fc7e..2e275c304c9 100644
--- a/Flow.Launcher/Storage/TopMostRecord.cs
+++ b/Flow.Launcher/Storage/TopMostRecord.cs
@@ -52,8 +52,14 @@ public class Record
public bool Equals(Result r)
{
- return Title == r.Title
- && SubTitle == r.SubTitle
+ var titleEqual = r.GetTitleKey != null ?
+ r.GetTitleKey(Title) == r.GetTitleKey(r.Title) :
+ Title == r.Title;
+ var subTitleEqual = r.GetSubTitleKey != null ?
+ r.GetSubTitleKey(SubTitle) == r.GetSubTitleKey(r.SubTitle) :
+ SubTitle == r.SubTitle;
+ return titleEqual
+ && subTitleEqual
&& PluginID == r.PluginID;
}
}
diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs
index d6405005dba..b9dc443f4b3 100644
--- a/Flow.Launcher/Storage/UserSelectedRecord.cs
+++ b/Flow.Launcher/Storage/UserSelectedRecord.cs
@@ -15,7 +15,6 @@ public class UserSelectedRecord
[JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary records { get; private set; }
-
public UserSelectedRecord()
{
recordsWithQuery = new Dictionary();
@@ -45,8 +44,8 @@ private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL)
private static int GenerateResultHashCode(Result result)
{
- int hashcode = GenerateStaticHashCode(result.Title);
- return GenerateStaticHashCode(result.SubTitle, hashcode);
+ int hashcode = GenerateStaticHashCode(result.GetTitleKey != null ? result.GetTitleKey(result.Title) : result.Title);
+ return GenerateStaticHashCode(result.GetSubTitleKey != null ? result.GetSubTitleKey(result.SubTitle) : result.SubTitle, hashcode);
}
private static int GenerateQueryAndResultHashCode(Query query, Result result)
@@ -58,8 +57,8 @@ private static int GenerateQueryAndResultHashCode(Query query, Result result)
int hashcode = GenerateStaticHashCode(query.ActionKeyword);
hashcode = GenerateStaticHashCode(query.Search, hashcode);
- hashcode = GenerateStaticHashCode(result.Title, hashcode);
- hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
+ hashcode = GenerateStaticHashCode(result.GetTitleKey != null ? result.GetTitleKey(result.Title) : result.Title, hashcode);
+ hashcode = GenerateStaticHashCode(result.GetSubTitleKey != null ? result.GetSubTitleKey(result.SubTitle) : result.SubTitle, hashcode);
return hashcode;
}
From 32adfca7ed3d4df8a1624b1241f41a8f43dd4a85 Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Fri, 10 Jan 2025 23:14:34 +0800
Subject: [PATCH 2/2] Fix result clone issue
---
Flow.Launcher.Plugin/Result.cs | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs
index baa65866c66..ead977cb165 100644
--- a/Flow.Launcher.Plugin/Result.cs
+++ b/Flow.Launcher.Plugin/Result.cs
@@ -203,6 +203,17 @@ public Result Clone()
TitleHighlightData = TitleHighlightData,
OriginQuery = OriginQuery,
PluginDirectory = PluginDirectory,
+ ContextData = ContextData,
+ PluginID = PluginID,
+ TitleToolTip = TitleToolTip,
+ SubTitleToolTip = SubTitleToolTip,
+ PreviewPanel = PreviewPanel,
+ ProgressBar = ProgressBar,
+ ProgressBarColor = ProgressBarColor,
+ Preview = Preview,
+ AddSelectedCount = AddSelectedCount,
+ GetTitleKey = GetTitleKey,
+ GetSubTitleKey = GetSubTitleKey,
};
}