Skip to content

Commit 54ebe68

Browse files
authored
Merge pull request #189 from taooceros/ProgramPluginImprovement
Program plugin improvement
2 parents 8010179 + 2cc4e84 commit 54ebe68

File tree

4 files changed

+103
-159
lines changed

4 files changed

+103
-159
lines changed

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,17 @@ public List<Result> Query(Query query)
7171
Win32[] win32;
7272
UWP.Application[] uwps;
7373

74-
lock (IndexLock)
75-
{ // just take the reference inside the lock to eliminate query time issues.
76-
win32 = _win32s;
77-
uwps = _uwps;
78-
}
79-
80-
var results1 = win32.AsParallel()
81-
.Where(p => p.Enabled)
82-
.Select(p => p.Result(query.Search, _context.API));
74+
win32 = _win32s;
75+
uwps = _uwps;
8376

84-
var results2 = uwps.AsParallel()
85-
.Where(p => p.Enabled)
86-
.Select(p => p.Result(query.Search, _context.API));
77+
var result = win32.Cast<IProgram>()
78+
.Concat(uwps)
79+
.AsParallel()
80+
.Where(p => p.Enabled)
81+
.Select(p => p.Result(query.Search, _context.API))
82+
.Where(r => r?.Score > 0)
83+
.ToList();
8784

88-
var result = results1.Concat(results2).Where(r => r != null && r.Score > 0).ToList();
8985
return result;
9086
}
9187

@@ -97,10 +93,9 @@ public void Init(PluginInitContext context)
9793
public static void IndexWin32Programs()
9894
{
9995
var win32S = Win32.All(_settings);
100-
lock (IndexLock)
101-
{
102-
_win32s = win32S;
103-
}
96+
97+
_win32s = win32S;
98+
10499
}
105100

106101
public static void IndexUWPPrograms()
@@ -109,10 +104,9 @@ public static void IndexUWPPrograms()
109104
var support = Environment.OSVersion.Version.Major >= windows10.Major;
110105

111106
var applications = support ? UWP.All() : new UWP.Application[] { };
112-
lock (IndexLock)
113-
{
114-
_uwps = applications;
115-
}
107+
108+
_uwps = applications;
109+
116110
}
117111

118112
public static void IndexPrograms()

Plugins/Flow.Launcher.Plugin.Program/Programs/IProgram.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface IProgram
99
string UniqueIdentifier { get; set; }
1010
string Name { get; }
1111
string Location { get; }
12+
bool Enabled { get; }
1213
}
1314
}

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -265,27 +265,30 @@ public class Application : IProgram
265265

266266
public Application(){}
267267

268-
private int Score(string query)
269-
{
270-
var displayNameMatch = StringMatcher.FuzzySearch(query, DisplayName);
271-
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
272-
var score = new[] { displayNameMatch.Score, descriptionMatch.Score }.Max();
273-
return score;
274-
}
275268

276269
public Result Result(string query, IPublicAPI api)
277270
{
278-
var score = Score(query);
279-
if (score <= 0)
280-
{ // no need to create result if score is 0
271+
var title = (Name, Description) switch
272+
{
273+
(var n, null) => n,
274+
(var n, var d) when d.StartsWith(n) => d,
275+
(var n, var d) when n.StartsWith(d) => n,
276+
(var n, var d) when !string.IsNullOrEmpty(d) => $"{n}: {d}",
277+
_ => Name
278+
};
279+
280+
var matchResult = StringMatcher.FuzzySearch(query, title);
281+
282+
if (!matchResult.Success)
281283
return null;
282-
}
283284

284285
var result = new Result
285286
{
287+
Title = title,
286288
SubTitle = Package.Location,
287289
Icon = Logo,
288-
Score = score,
290+
Score = matchResult.Score,
291+
TitleHighlightData = matchResult.MatchData,
289292
ContextData = this,
290293
Action = e =>
291294
{
@@ -294,23 +297,7 @@ public Result Result(string query, IPublicAPI api)
294297
}
295298
};
296299

297-
if (Description.Length >= DisplayName.Length &&
298-
Description.Substring(0, DisplayName.Length) == DisplayName)
299-
{
300-
result.Title = Description;
301-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Description).MatchData;
302-
}
303-
else if (!string.IsNullOrEmpty(Description))
304-
{
305-
var title = $"{DisplayName}: {Description}";
306-
result.Title = title;
307-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, title).MatchData;
308-
}
309-
else
310-
{
311-
result.Title = DisplayName;
312-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, DisplayName).MatchData;
313-
}
300+
314301
return result;
315302
}
316303

@@ -324,9 +311,14 @@ public List<Result> ContextMenus(IPublicAPI api)
324311

325312
Action = _ =>
326313
{
327-
Main.StartProcess(Process.Start, new ProcessStartInfo(
328-
!string.IsNullOrEmpty(Main._settings.CustomizedExplorer) ? Main._settings.CustomizedExplorer:Settings.Explorer,
329-
Main._settings.CustomizedArgs.Replace("%s",$"\"{Package.Location}\"").Trim()));
314+
Main.StartProcess(Process.Start,
315+
new ProcessStartInfo(
316+
!string.IsNullOrEmpty(Main._settings.CustomizedExplorer)
317+
? Main._settings.CustomizedExplorer
318+
: Settings.Explorer,
319+
Main._settings.CustomizedArgs
320+
.Replace("%s",$"\"{Package.Location}\"")
321+
.Trim()));
330322

331323
return true;
332324
},

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 63 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,30 @@ public class Win32 : IProgram
3333
private const string ApplicationReferenceExtension = "appref-ms";
3434
private const string ExeExtension = "exe";
3535

36-
private int Score(string query)
37-
{
38-
var nameMatch = StringMatcher.FuzzySearch(query, Name);
39-
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
40-
var executableNameMatch = StringMatcher.FuzzySearch(query, ExecutableName);
41-
var score = new[] { nameMatch.Score, descriptionMatch.Score, executableNameMatch.Score }.Max();
42-
return score;
43-
}
44-
4536

4637
public Result Result(string query, IPublicAPI api)
4738
{
48-
var score = Score(query);
49-
if (score <= 0)
50-
{ // no need to create result if this is zero
39+
var title = (Name, Description) switch
40+
{
41+
(var n, null) => n,
42+
(var n, var d) when d.StartsWith(n) => d,
43+
(var n, var d) when n.StartsWith(d) => n,
44+
(var n, var d) when !string.IsNullOrEmpty(d) => $"{n}: {d}",
45+
_ => Name
46+
};
47+
48+
var matchResult = StringMatcher.FuzzySearch(query, title);
49+
50+
if (!matchResult.Success)
5151
return null;
52-
}
5352

5453
var result = new Result
5554
{
55+
Title = title,
5656
SubTitle = FullPath,
5757
IcoPath = IcoPath,
58-
Score = score,
58+
Score = matchResult.Score,
59+
TitleHighlightData = matchResult.MatchData,
5960
ContextData = this,
6061
Action = e =>
6162
{
@@ -72,24 +73,6 @@ public Result Result(string query, IPublicAPI api)
7273
}
7374
};
7475

75-
if (Description.Length >= Name.Length &&
76-
Description.Substring(0, Name.Length) == Name)
77-
{
78-
result.Title = Description;
79-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Description).MatchData;
80-
}
81-
else if (!string.IsNullOrEmpty(Description))
82-
{
83-
var title = $"{Name}: {Description}";
84-
result.Title = title;
85-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, title).MatchData;
86-
}
87-
else
88-
{
89-
result.Title = Name;
90-
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
91-
}
92-
9376
return result;
9477
}
9578

@@ -141,18 +124,20 @@ public List<Result> ContextMenus(IPublicAPI api)
141124
Action = _ =>
142125
{
143126
var args = !string.IsNullOrWhiteSpace(Main._settings.CustomizedArgs)
144-
?Main._settings.CustomizedArgs
127+
? Main._settings.CustomizedArgs
145128
.Replace("%s",$"\"{ParentDirectory}\"")
146-
.Replace("%f",$"\"{FullPath}\""):
147-
Main._settings.CustomizedExplorer==Settings.Explorer
148-
? $"/select,\"{FullPath}\""
149-
: Settings.ExplorerArgs;
150-
151-
Main.StartProcess(Process.Start, new ProcessStartInfo(
152-
!string.IsNullOrWhiteSpace(Main._settings.CustomizedExplorer)
153-
? Main._settings.CustomizedExplorer
154-
: Settings.Explorer,
155-
args));
129+
.Replace("%f",$"\"{FullPath}\"")
130+
: Main._settings.CustomizedExplorer==Settings.Explorer
131+
? $"/select,\"{FullPath}\""
132+
: Settings.ExplorerArgs;
133+
134+
Main.StartProcess(Process.Start,
135+
new ProcessStartInfo(
136+
!string.IsNullOrWhiteSpace(Main._settings.CustomizedExplorer)
137+
? Main._settings.CustomizedExplorer
138+
: Settings.Explorer,
139+
args));
140+
156141
return true;
157142
},
158143
IcoPath = "Images/folder.png"
@@ -264,9 +249,7 @@ private static Win32 ExeProgram(string path)
264249
var program = Win32Program(path);
265250
var info = FileVersionInfo.GetVersionInfo(path);
266251
if (!string.IsNullOrEmpty(info.FileDescription))
267-
{
268252
program.Description = info.FileDescription;
269-
}
270253
return program;
271254
}
272255
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
@@ -282,47 +265,23 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
282265
{
283266
if (!Directory.Exists(directory))
284267
return new string[] { };
285-
var files = new List<string>();
286-
var folderQueue = new Queue<string>();
287-
folderQueue.Enqueue(directory);
288-
do
268+
try
289269
{
290-
var currentDirectory = folderQueue.Dequeue();
291-
try
292-
{
293-
foreach (var suffix in suffixes)
294-
{
295-
try
296-
{
297-
files.AddRange(Directory.EnumerateFiles(currentDirectory, $"*.{suffix}", SearchOption.TopDirectoryOnly));
298-
}
299-
catch (DirectoryNotFoundException e)
300-
{
301-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
302-
"|The directory trying to load the program from does not exist", e);
303-
}
304-
}
305-
}
306-
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
307-
{
308-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
309-
$"|Permission denied when trying to load programs from {currentDirectory}", e);
310-
}
270+
var paths = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)
271+
.Where(x => suffixes.Contains(Extension(x)));
272+
return paths;
311273

312-
try
313-
{
314-
foreach (var childDirectory in Directory.EnumerateDirectories(currentDirectory, "*", SearchOption.TopDirectoryOnly))
315-
{
316-
folderQueue.Enqueue(childDirectory);
317-
}
318-
}
319-
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
320-
{
321-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
322-
$"|Permission denied when trying to load programs from {currentDirectory}", e);
323-
}
324-
} while (folderQueue.Any());
325-
return files;
274+
}
275+
catch (DirectoryNotFoundException e)
276+
{
277+
ProgramLogger.LogException($"Directory not found {directory}", e);
278+
return new string[] { };
279+
}
280+
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
281+
{
282+
ProgramLogger.LogException($"Permission denied {directory}", e);
283+
return new string[] { };
284+
}
326285
}
327286

328287
private static string Extension(string path)
@@ -340,23 +299,20 @@ private static string Extension(string path)
340299

341300
private static ParallelQuery<Win32> UnregisteredPrograms(List<Settings.ProgramSource> sources, string[] suffixes)
342301
{
343-
var listToAdd = new List<string>();
344-
sources.Where(s => Directory.Exists(s.Location) && s.Enabled)
302+
var paths = sources.Where(s => Directory.Exists(s.Location) && s.Enabled)
345303
.SelectMany(s => ProgramPaths(s.Location, suffixes))
346-
.ToList()
347304
.Where(t1 => !Main._settings.DisabledProgramSources.Any(x => t1 == x.UniqueIdentifier))
348-
.ToList()
349-
.ForEach(x => listToAdd.Add(x));
350-
351-
var paths = listToAdd.Distinct().ToArray();
352-
353-
var programs1 = paths.AsParallel().Where(p => Extension(p) == ExeExtension).Select(ExeProgram);
354-
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
355-
var programs3 = from p in paths.AsParallel()
356-
let e = Extension(p)
357-
where e != ShortcutExtension && e != ExeExtension
358-
select Win32Program(p);
359-
return programs1.Concat(programs2).Concat(programs3);
305+
.Distinct();
306+
307+
var programs = paths.AsParallel().Select(x => Extension(x) switch
308+
{
309+
ExeExtension => ExeProgram(x),
310+
ShortcutExtension => LnkProgram(x),
311+
_ => Win32Program(x)
312+
});
313+
314+
315+
return programs;
360316
}
361317

362318
private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
@@ -369,15 +325,16 @@ private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
369325
var paths2 = ProgramPaths(directory2, suffixes);
370326

371327
var toFilter = paths1.Concat(paths2);
372-
var paths = toFilter
328+
329+
var programs = toFilter
330+
.AsParallel()
373331
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1))
374-
.Select(t1 => t1)
375332
.Distinct()
376-
.ToArray();
377-
378-
var programs1 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
379-
var programs2 = paths.AsParallel().Where(p => Extension(p) == ApplicationReferenceExtension).Select(Win32Program);
380-
var programs = programs1.Concat(programs2).Where(p => p.Valid);
333+
.Select(x => Extension(x) switch
334+
{
335+
ShortcutExtension => LnkProgram(x),
336+
_ => Win32Program(x)
337+
}).Where(x => x.Valid);
381338
return programs;
382339
}
383340

0 commit comments

Comments
 (0)