Skip to content

Commit 38c01c6

Browse files
committed
Add support for customizing equal rules for results
1 parent 95b4c4c commit 38c01c6

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Threading.Tasks;
@@ -13,7 +12,6 @@ namespace Flow.Launcher.Plugin
1312
/// </summary>
1413
public class Result
1514
{
16-
1715
private string _pluginDirectory;
1816

1917
private string _icoPath;
@@ -103,7 +101,6 @@ public string IcoPath
103101
/// </summary>
104102
public GlyphInfo Glyph { get; init; }
105103

106-
107104
/// <summary>
108105
/// An action to take in the form of a function call when the result has been selected.
109106
/// </summary>
@@ -273,6 +270,26 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
273270
/// </summary>
274271
public const int MaxScore = int.MaxValue;
275272

273+
/// <summary>
274+
/// 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.
275+
/// This can be useful when your plugin will change the title of the result dynamically.
276+
/// </summary>
277+
/// <remarks>
278+
/// The function is invoked with the title of the result as the only parameter.
279+
/// Its result determines the key of the title.
280+
/// </remarks>
281+
public Func<string, string> GetTitleKey { get; set; } = null;
282+
283+
/// <summary>
284+
/// 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.
285+
/// This can be useful when your plugin will change the subtitle of the result dynamically.
286+
/// </summary>
287+
/// <remarks>
288+
/// The function is invoked with the subtitle of the result as the only parameter.
289+
/// Its result determines the key of the subtitle.
290+
/// </remarks>
291+
public Func<string, string> GetSubTitleKey { get; set; } = null;
292+
276293
/// <summary>
277294
/// Info of the preview section of a <see cref="Result"/>
278295
/// </summary>

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ public class Record
5252

5353
public bool Equals(Result r)
5454
{
55-
return Title == r.Title
56-
&& SubTitle == r.SubTitle
55+
var titleEqual = r.GetTitleKey != null ?
56+
r.GetTitleKey(Title) == r.GetTitleKey(r.Title) :
57+
Title == r.Title;
58+
var subTitleEqual = r.GetSubTitleKey != null ?
59+
r.GetSubTitleKey(SubTitle) == r.GetSubTitleKey(r.SubTitle) :
60+
SubTitle == r.SubTitle;
61+
return titleEqual
62+
&& subTitleEqual
5763
&& PluginID == r.PluginID;
5864
}
5965
}

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class UserSelectedRecord
1515
[JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1616
public Dictionary<string, int> records { get; private set; }
1717

18-
1918
public UserSelectedRecord()
2019
{
2120
recordsWithQuery = new Dictionary<int, int>();
@@ -45,8 +44,8 @@ private static int GenerateStaticHashCode(string s, int start = HASH_INITIAL)
4544

4645
private static int GenerateResultHashCode(Result result)
4746
{
48-
int hashcode = GenerateStaticHashCode(result.Title);
49-
return GenerateStaticHashCode(result.SubTitle, hashcode);
47+
int hashcode = GenerateStaticHashCode(result.GetTitleKey != null ? result.GetTitleKey(result.Title) : result.Title);
48+
return GenerateStaticHashCode(result.GetSubTitleKey != null ? result.GetSubTitleKey(result.SubTitle) : result.SubTitle, hashcode);
5049
}
5150

5251
private static int GenerateQueryAndResultHashCode(Query query, Result result)
@@ -58,8 +57,8 @@ private static int GenerateQueryAndResultHashCode(Query query, Result result)
5857

5958
int hashcode = GenerateStaticHashCode(query.ActionKeyword);
6059
hashcode = GenerateStaticHashCode(query.Search, hashcode);
61-
hashcode = GenerateStaticHashCode(result.Title, hashcode);
62-
hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
60+
hashcode = GenerateStaticHashCode(result.GetTitleKey != null ? result.GetTitleKey(result.Title) : result.Title, hashcode);
61+
hashcode = GenerateStaticHashCode(result.GetSubTitleKey != null ? result.GetSubTitleKey(result.SubTitle) : result.SubTitle, hashcode);
6362

6463
return hashcode;
6564
}

0 commit comments

Comments
 (0)