Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API for Customizing Equal Rules for Results & Fix Result Clone Issue #3178

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Flow.Launcher.Plugin/Result.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -13,7 +12,6 @@ namespace Flow.Launcher.Plugin
/// </summary>
public class Result
{

private string _pluginDirectory;

private string _icoPath;
Expand Down Expand Up @@ -103,7 +101,6 @@ public string IcoPath
/// </summary>
public GlyphInfo Glyph { get; init; }


/// <summary>
/// An action to take in the form of a function call when the result has been selected.
/// </summary>
Expand Down Expand Up @@ -206,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,
Jack251970 marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down Expand Up @@ -273,6 +281,26 @@ public ValueTask<bool> ExecuteAsync(ActionContext context)
/// </summary>
public const int MaxScore = int.MaxValue;

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// The function is invoked with the title of the result as the only parameter.
/// Its result determines the key of the title.
/// </remarks>
public Func<string, string> GetTitleKey { get; set; } = null;

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// The function is invoked with the subtitle of the result as the only parameter.
/// Its result determines the key of the subtitle.
/// </remarks>
public Func<string, string> GetSubTitleKey { get; set; } = null;

/// <summary>
/// Info of the preview section of a <see cref="Result"/>
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions Flow.Launcher/Storage/TopMostRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 4 additions & 5 deletions Flow.Launcher/Storage/UserSelectedRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class UserSelectedRecord
[JsonInclude, JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, int> records { get; private set; }


public UserSelectedRecord()
{
recordsWithQuery = new Dictionary<int, int>();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Jack251970 marked this conversation as resolved.
Show resolved Hide resolved

return hashcode;
}
Expand Down
Loading