Skip to content

Commit 1984426

Browse files
committed
Add support for record key
1 parent 0b8db59 commit 1984426

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ public Result Clone()
185185
TitleHighlightData = TitleHighlightData,
186186
OriginQuery = OriginQuery,
187187
PluginDirectory = PluginDirectory,
188+
ContextData = ContextData,
189+
PluginID = PluginID,
190+
TitleToolTip = TitleToolTip,
191+
SubTitleToolTip = SubTitleToolTip,
192+
PreviewPanel = PreviewPanel,
193+
ProgressBar = ProgressBar,
194+
ProgressBarColor = ProgressBarColor,
195+
Preview = Preview,
196+
AddSelectedCount = AddSelectedCount,
197+
RecordKey = RecordKey
188198
};
189199
}
190200

@@ -252,6 +262,13 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
252262
/// </summary>
253263
public const int MaxScore = int.MaxValue;
254264

265+
/// <summary>
266+
/// 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.
267+
/// This can be useful when your plugin will change the Title or SubTitle of the result dynamically.
268+
/// If the plugin does not specific this, FL just uses Title and SubTitle to identify this result.
269+
/// </summary>
270+
public string RecordKey { get; set; } = string.Empty;
271+
255272
/// <summary>
256273
/// Info of the preview section of a <see cref="Result"/>
257274
/// </summary>

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ internal void AddOrUpdate(Result result)
3333
{
3434
PluginID = result.PluginID,
3535
Title = result.Title,
36-
SubTitle = result.SubTitle
36+
SubTitle = result.SubTitle,
37+
RecordKey = result.RecordKey
3738
};
3839
records.AddOrUpdate(result.OriginQuery.RawQuery, record, (key, oldValue) => record);
3940
}
@@ -49,12 +50,21 @@ public class Record
4950
public string Title { get; set; }
5051
public string SubTitle { get; set; }
5152
public string PluginID { get; set; }
53+
public string RecordKey { get; set; }
5254

5355
public bool Equals(Result r)
5456
{
55-
return Title == r.Title
56-
&& SubTitle == r.SubTitle
57-
&& PluginID == r.PluginID;
57+
if (string.IsNullOrEmpty(RecordKey) || string.IsNullOrEmpty(r.RecordKey))
58+
{
59+
return Title == r.Title
60+
&& SubTitle == r.SubTitle
61+
&& PluginID == r.PluginID;
62+
}
63+
else
64+
{
65+
return RecordKey == r.RecordKey
66+
&& PluginID == r.PluginID;
67+
}
5868
}
5969
}
6070
}

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 19 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,15 @@ 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+
if (string.IsNullOrEmpty(result.RecordKey))
48+
{
49+
int hashcode = GenerateStaticHashCode(result.Title);
50+
return GenerateStaticHashCode(result.SubTitle, hashcode);
51+
}
52+
else
53+
{
54+
return GenerateStaticHashCode(result.RecordKey);
55+
}
5056
}
5157

5258
private static int GenerateQueryAndResultHashCode(Query query, Result result)
@@ -58,8 +64,16 @@ private static int GenerateQueryAndResultHashCode(Query query, Result result)
5864

5965
int hashcode = GenerateStaticHashCode(query.ActionKeyword);
6066
hashcode = GenerateStaticHashCode(query.Search, hashcode);
61-
hashcode = GenerateStaticHashCode(result.Title, hashcode);
62-
hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
67+
68+
if (string.IsNullOrEmpty(result.RecordKey))
69+
{
70+
hashcode = GenerateStaticHashCode(result.Title, hashcode);
71+
hashcode = GenerateStaticHashCode(result.SubTitle, hashcode);
72+
}
73+
else
74+
{
75+
hashcode = GenerateStaticHashCode(result.RecordKey, hashcode);
76+
}
6377

6478
return hashcode;
6579
}

0 commit comments

Comments
 (0)