Skip to content

Commit 50924e4

Browse files
committed
Improve code quality
1 parent cc68183 commit 50924e4

File tree

1 file changed

+81
-28
lines changed

1 file changed

+81
-28
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,7 @@ public static class PluginManager
5050
Constant.PreinstalledDirectory, DataLocation.PluginsDirectory
5151
];
5252

53-
private static void DeletePythonBinding()
54-
{
55-
const string binding = "flowlauncher.py";
56-
foreach (var subDirectory in Directory.GetDirectories(DataLocation.PluginsDirectory))
57-
{
58-
File.Delete(Path.Combine(subDirectory, binding));
59-
}
60-
}
61-
62-
public static List<PluginPair> GetAllPlugins()
63-
{
64-
return [.. _allPlugins.Values];
65-
}
66-
67-
public static Dictionary<string, PluginPair> GetNonGlobalPlugins()
68-
{
69-
return _nonGlobalPlugins.ToDictionary();
70-
}
53+
#region Save & Dispose & Reload Plugin
7154

7255
/// <summary>
7356
/// Save json and ISavable
@@ -129,6 +112,10 @@ public static async Task ReloadDataAsync()
129112
})]);
130113
}
131114

115+
#endregion
116+
117+
#region External Preview
118+
132119
public static async Task OpenExternalPreviewAsync(string path, bool sendFailToast = true)
133120
{
134121
await Task.WhenAll([.. GetAllPlugins().Select(plugin => plugin.Plugin switch
@@ -171,6 +158,15 @@ public static bool AllowAlwaysPreview()
171158
return ((IAsyncExternalPreview)plugin.Plugin).AllowAlwaysPreview();
172159
}
173160

161+
private static IList<PluginPair> GetExternalPreviewPlugins()
162+
{
163+
return [.. _externalPreviewPlugins.Where(p => !PluginModified(p.Metadata.ID))];
164+
}
165+
166+
#endregion
167+
168+
#region Constructor
169+
174170
static PluginManager()
175171
{
176172
// validate user directory
@@ -179,6 +175,19 @@ static PluginManager()
179175
DeletePythonBinding();
180176
}
181177

178+
private static void DeletePythonBinding()
179+
{
180+
const string binding = "flowlauncher.py";
181+
foreach (var subDirectory in Directory.GetDirectories(DataLocation.PluginsDirectory))
182+
{
183+
File.Delete(Path.Combine(subDirectory, binding));
184+
}
185+
}
186+
187+
#endregion
188+
189+
#region Load & Initialize Plugins
190+
182191
/// <summary>
183192
/// Load plugins from the directories specified in Directories.
184193
/// </summary>
@@ -342,6 +351,10 @@ private static void AddPluginToLists(PluginPair pair)
342351
_allPlugins.TryAdd(pair.Metadata.ID, pair);
343352
}
344353

354+
#endregion
355+
356+
#region Validate & Query Plugins
357+
345358
public static ICollection<PluginPair> ValidPluginsForQuery(Query query, bool dialogJump)
346359
{
347360
if (query is null)
@@ -350,9 +363,9 @@ public static ICollection<PluginPair> ValidPluginsForQuery(Query query, bool dia
350363
if (!_nonGlobalPlugins.TryGetValue(query.ActionKeyword, out var plugin))
351364
{
352365
if (dialogJump)
353-
return [.. _globalPlugins.Values.Where(p => p.Plugin is IAsyncDialogJump && !PluginModified(p.Metadata.ID))];
366+
return [.. GetGlobalPlugins().Where(p => p.Plugin is IAsyncDialogJump && !PluginModified(p.Metadata.ID))];
354367
else
355-
return [.. _globalPlugins.Values.Where(p => !PluginModified(p.Metadata.ID))];
368+
return [.. GetGlobalPlugins().Where(p => !PluginModified(p.Metadata.ID))];
356369
}
357370

358371
if (dialogJump && plugin.Plugin is not IAsyncDialogJump)
@@ -473,6 +486,10 @@ public static async Task<List<DialogJumpResult>> QueryDialogJumpForPluginAsync(P
473486
return results;
474487
}
475488

489+
#endregion
490+
491+
#region Update Metadata & Get Plugin
492+
476493
public static void UpdatePluginMetadata(IReadOnlyList<Result> results, PluginMetadata metadata, Query query)
477494
{
478495
foreach (var r in results)
@@ -498,12 +515,35 @@ public static PluginPair GetPluginForId(string id)
498515
return GetAllPlugins().FirstOrDefault(o => o.Metadata.ID == id);
499516
}
500517

501-
public static IList<PluginPair> GetTranslationPlugins()
518+
#endregion
519+
520+
#region Get Plugin List
521+
522+
public static List<PluginPair> GetAllPlugins()
523+
{
524+
return [.. _allPlugins.Values];
525+
}
526+
527+
public static List<PluginPair> GetGlobalPlugins()
528+
{
529+
return [.. _globalPlugins.Values];
530+
}
531+
532+
public static Dictionary<string, PluginPair> GetNonGlobalPlugins()
533+
{
534+
return _nonGlobalPlugins.ToDictionary();
535+
}
536+
537+
public static List<PluginPair> GetTranslationPlugins()
502538
{
503539
return [.. _translationPlugins.Where(p => !PluginModified(p.Metadata.ID))];
504540
}
505541

506-
public static List<Result> GetContextMenusForPlugin(Result result)
542+
#endregion
543+
544+
#region Get Context Menus
545+
546+
public static IList<Result> GetContextMenusForPlugin(Result result)
507547
{
508548
var results = new List<Result>();
509549
var pluginPair = _contextMenuPlugins.Where(p => !PluginModified(p.Metadata.ID)).FirstOrDefault(o => o.Metadata.ID == result.PluginID);
@@ -532,15 +572,18 @@ public static List<Result> GetContextMenusForPlugin(Result result)
532572
return results;
533573
}
534574

575+
#endregion
576+
577+
#region Check Home Plugin
578+
535579
public static bool IsHomePlugin(string id)
536580
{
537581
return _homePlugins.Where(p => !PluginModified(p.Metadata.ID)).Any(p => p.Metadata.ID == id);
538582
}
539583

540-
private static List<PluginPair> GetExternalPreviewPlugins()
541-
{
542-
return [.. _externalPreviewPlugins.Where(p => !PluginModified(p.Metadata.ID))];
543-
}
584+
#endregion
585+
586+
#region Plugin Action Keyword
544587

545588
public static bool ActionKeywordRegistered(string actionKeyword)
546589
{
@@ -623,6 +666,12 @@ public static void RemoveActionKeyword(string id, string oldActionkeyword)
623666
}
624667
}
625668

669+
#endregion
670+
671+
#region Plugin Install & Uninstall & Update
672+
673+
#region Private Functions
674+
626675
private static string GetContainingFolderPathAfterUnzip(string unzippedParentFolderPath)
627676
{
628677
var unzippedFolderCount = Directory.GetDirectories(unzippedParentFolderPath).Length;
@@ -653,7 +702,9 @@ private static bool SameOrLesserPluginVersionExists(string metadataPath)
653702
&& newVersion <= version);
654703
}
655704

656-
#region Public functions
705+
#endregion
706+
707+
#region Public Functions
657708

658709
public static bool PluginModified(string id)
659710
{
@@ -691,7 +742,7 @@ public static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, bool
691742

692743
#endregion
693744

694-
#region Internal functions
745+
#region Internal Functions
695746

696747
internal static bool InstallPlugin(UserPlugin plugin, string zipFilePath, bool checkModified)
697748
{
@@ -854,5 +905,7 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
854905
}
855906

856907
#endregion
908+
909+
#endregion
857910
}
858911
}

0 commit comments

Comments
 (0)