55using System . Linq ;
66using System . Threading ;
77using System . Threading . Tasks ;
8- using CommunityToolkit . Mvvm . DependencyInjection ;
98using Flow . Launcher . Core . ExternalPlugins ;
109using Flow . Launcher . Infrastructure ;
1110using Flow . Launcher . Infrastructure . UserSettings ;
@@ -25,10 +24,6 @@ public static class PluginManager
2524 public static readonly HashSet < PluginPair > GlobalPlugins = new ( ) ;
2625 public static readonly Dictionary < string , PluginPair > NonGlobalPlugins = new ( ) ;
2726
28- // We should not initialize API in static constructor because it will create another API instance
29- private static IPublicAPI api = null ;
30- private static IPublicAPI API => api ??= Ioc . Default . GetRequiredService < IPublicAPI > ( ) ;
31-
3227 private static PluginsSettings Settings ;
3328
3429 private static IEnumerable < PluginPair > _contextMenuPlugins ;
@@ -58,12 +53,12 @@ public static void Save()
5853 }
5954 catch ( Exception e )
6055 {
61- API . LogException ( ClassName , $ "Failed to save plugin { pluginPair . Metadata . Name } ", e ) ;
56+ PublicApi . Instance . LogException ( ClassName , $ "Failed to save plugin { pluginPair . Metadata . Name } ", e ) ;
6257 }
6358 }
6459
65- API . SavePluginSettings ( ) ;
66- API . SavePluginCaches ( ) ;
60+ PublicApi . Instance . SavePluginSettings ( ) ;
61+ PublicApi . Instance . SavePluginCaches ( ) ;
6762 }
6863
6964 public static async ValueTask DisposePluginsAsync ( )
@@ -90,7 +85,7 @@ private static async Task DisposePluginAsync(PluginPair pluginPair)
9085 }
9186 catch ( Exception e )
9287 {
93- API . LogException ( ClassName , $ "Failed to dispose plugin { pluginPair . Metadata . Name } ", e ) ;
88+ PublicApi . Instance . LogException ( ClassName , $ "Failed to dispose plugin { pluginPair . Metadata . Name } ", e ) ;
9489 }
9590 }
9691
@@ -181,7 +176,7 @@ private static void UpdatePluginDirectory(List<PluginMetadata> metadatas)
181176 {
182177 if ( string . IsNullOrEmpty ( metadata . AssemblyName ) )
183178 {
184- API . LogWarn ( ClassName , $ "AssemblyName is empty for plugin with metadata: { metadata . Name } ") ;
179+ PublicApi . Instance . LogWarn ( ClassName , $ "AssemblyName is empty for plugin with metadata: { metadata . Name } ") ;
185180 continue ; // Skip if AssemblyName is not set, which can happen for erroneous plugins
186181 }
187182 metadata . PluginSettingsDirectoryPath = Path . Combine ( DataLocation . PluginSettingsDirectory , metadata . AssemblyName ) ;
@@ -191,7 +186,7 @@ private static void UpdatePluginDirectory(List<PluginMetadata> metadatas)
191186 {
192187 if ( string . IsNullOrEmpty ( metadata . Name ) )
193188 {
194- API . LogWarn ( ClassName , $ "Name is empty for plugin with metadata: { metadata . Name } ") ;
189+ PublicApi . Instance . LogWarn ( ClassName , $ "Name is empty for plugin with metadata: { metadata . Name } ") ;
195190 continue ; // Skip if Name is not set, which can happen for erroneous plugins
196191 }
197192 metadata . PluginSettingsDirectoryPath = Path . Combine ( DataLocation . PluginSettingsDirectory , metadata . Name ) ;
@@ -212,28 +207,28 @@ public static async Task InitializePluginsAsync()
212207 {
213208 try
214209 {
215- var milliseconds = await API . StopwatchLogDebugAsync ( ClassName , $ "Init method time cost for <{ pair . Metadata . Name } >",
216- ( ) => pair . Plugin . InitAsync ( new PluginInitContext ( pair . Metadata , API ) ) ) ;
210+ var milliseconds = await PublicApi . Instance . StopwatchLogDebugAsync ( ClassName , $ "Init method time cost for <{ pair . Metadata . Name } >",
211+ ( ) => pair . Plugin . InitAsync ( new PluginInitContext ( pair . Metadata , PublicApi . Instance ) ) ) ;
217212
218213 pair . Metadata . InitTime += milliseconds ;
219- API . LogInfo ( ClassName ,
214+ PublicApi . Instance . LogInfo ( ClassName ,
220215 $ "Total init cost for <{ pair . Metadata . Name } > is <{ pair . Metadata . InitTime } ms>") ;
221216 }
222217 catch ( Exception e )
223218 {
224- API . LogException ( ClassName , $ "Fail to Init plugin: { pair . Metadata . Name } ", e ) ;
219+ PublicApi . Instance . LogException ( ClassName , $ "Fail to Init plugin: { pair . Metadata . Name } ", e ) ;
225220 if ( pair . Metadata . Disabled && pair . Metadata . HomeDisabled )
226221 {
227222 // If this plugin is already disabled, do not show error message again
228223 // Or else it will be shown every time
229- API . LogDebug ( ClassName , $ "Skipped init for <{ pair . Metadata . Name } > due to error") ;
224+ PublicApi . Instance . LogDebug ( ClassName , $ "Skipped init for <{ pair . Metadata . Name } > due to error") ;
230225 }
231226 else
232227 {
233228 pair . Metadata . Disabled = true ;
234229 pair . Metadata . HomeDisabled = true ;
235230 failedPlugins . Enqueue ( pair ) ;
236- API . LogDebug ( ClassName , $ "Disable plugin <{ pair . Metadata . Name } > because init failed") ;
231+ PublicApi . Instance . LogDebug ( ClassName , $ "Disable plugin <{ pair . Metadata . Name } > because init failed") ;
237232 }
238233 }
239234 } ) ) ;
@@ -258,15 +253,12 @@ public static async Task InitializePluginsAsync()
258253 }
259254 }
260255
261- if ( failedPlugins . Any ( ) )
256+ if ( ! failedPlugins . IsEmpty )
262257 {
263258 var failed = string . Join ( "," , failedPlugins . Select ( x => x . Metadata . Name ) ) ;
264- API . ShowMsg (
265- API . GetTranslation ( "failedToInitializePluginsTitle" ) ,
266- string . Format (
267- API . GetTranslation ( "failedToInitializePluginsMessage" ) ,
268- failed
269- ) ,
259+ PublicApi . Instance . ShowMsg (
260+ Localize . failedToInitializePluginsTitle ( ) ,
261+ Localize . failedToInitializePluginsMessage ( failed ) ,
270262 "" ,
271263 false
272264 ) ;
@@ -301,7 +293,7 @@ public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Quer
301293
302294 try
303295 {
304- var milliseconds = await API . StopwatchLogDebugAsync ( ClassName , $ "Cost for { metadata . Name } ",
296+ var milliseconds = await PublicApi . Instance . StopwatchLogDebugAsync ( ClassName , $ "Cost for { metadata . Name } ",
305297 async ( ) => results = await pair . Plugin . QueryAsync ( query , token ) . ConfigureAwait ( false ) ) ;
306298
307299 token . ThrowIfCancellationRequested ( ) ;
@@ -345,7 +337,7 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
345337
346338 try
347339 {
348- var milliseconds = await API . StopwatchLogDebugAsync ( ClassName , $ "Cost for { metadata . Name } ",
340+ var milliseconds = await PublicApi . Instance . StopwatchLogDebugAsync ( ClassName , $ "Cost for { metadata . Name } ",
349341 async ( ) => results = await ( ( IAsyncHomeQuery ) pair . Plugin ) . HomeQueryAsync ( token ) . ConfigureAwait ( false ) ) ;
350342
351343 token . ThrowIfCancellationRequested ( ) ;
@@ -362,7 +354,7 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
362354 }
363355 catch ( Exception e )
364356 {
365- API . LogException ( ClassName , $ "Failed to query home for plugin: { metadata . Name } ", e ) ;
357+ PublicApi . Instance . LogException ( ClassName , $ "Failed to query home for plugin: { metadata . Name } ", e ) ;
366358 return null ;
367359 }
368360 return results ;
@@ -429,7 +421,7 @@ public static List<Result> GetContextMenusForPlugin(Result result)
429421 }
430422 catch ( Exception e )
431423 {
432- API . LogException ( ClassName ,
424+ PublicApi . Instance . LogException ( ClassName ,
433425 $ "Can't load context menus for plugin <{ pluginPair . Metadata . Name } >",
434426 e ) ;
435427 }
0 commit comments