1- using System ;
1+ using System ;
22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
44using System . IO ;
1010using CommunityToolkit . Mvvm . DependencyInjection ;
1111using Flow . Launcher . Core . ExternalPlugins ;
1212using Flow . Launcher . Infrastructure ;
13+ using Flow . Launcher . Infrastructure . DialogJump ;
1314using Flow . Launcher . Infrastructure . Hotkey ;
1415using Flow . Launcher . Infrastructure . UserSettings ;
1516using Flow . Launcher . Plugin ;
@@ -48,6 +49,9 @@ public static class PluginManager
4849 private static readonly Dictionary < PluginPair , List < BasePluginHotkey > > _pluginHotkeyInfo = new ( ) ;
4950 private static readonly Dictionary < HotkeyModel , List < ( PluginMetadata , SearchWindowPluginHotkey ) > > _windowPluginHotkeys = new ( ) ;
5051
52+ private static readonly List < DialogJumpExplorerPair > _dialogJumpExplorerPlugins = new ( ) ;
53+ private static readonly List < DialogJumpDialogPair > _dialogJumpDialogPlugins = new ( ) ;
54+
5155 /// <summary>
5256 /// Directories that will hold Flow Launcher plugin directory
5357 /// </summary>
@@ -195,6 +199,24 @@ public static void LoadPlugins(PluginsSettings settings)
195199 _resultUpdatePlugin = GetPluginsForInterface < IResultUpdated > ( ) ;
196200 _translationPlugins = GetPluginsForInterface < IPluginI18n > ( ) ;
197201 _hotkeyPlugins = GetPluginsForInterface < IPluginHotkey > ( ) ;
202+
203+ // Initialize Dialog Jump plugin pairs
204+ foreach ( var pair in GetPluginsForInterface < IDialogJumpExplorer > ( ) )
205+ {
206+ _dialogJumpExplorerPlugins . Add ( new DialogJumpExplorerPair
207+ {
208+ Plugin = ( IDialogJumpExplorer ) pair . Plugin ,
209+ Metadata = pair . Metadata
210+ } ) ;
211+ }
212+ foreach ( var pair in GetPluginsForInterface < IDialogJumpDialog > ( ) )
213+ {
214+ _dialogJumpDialogPlugins . Add ( new DialogJumpDialogPair
215+ {
216+ Plugin = ( IDialogJumpDialog ) pair . Plugin ,
217+ Metadata = pair . Metadata
218+ } ) ;
219+ }
198220 }
199221
200222 private static void UpdatePluginDirectory ( List < PluginMetadata > metadatas )
@@ -301,20 +323,24 @@ public static async Task InitializePluginsAsync()
301323 }
302324 }
303325
304- public static ICollection < PluginPair > ValidPluginsForQuery ( Query query )
326+ public static ICollection < PluginPair > ValidPluginsForQuery ( Query query , bool dialogJump )
305327 {
306328 if ( query is null )
307329 return Array . Empty < PluginPair > ( ) ;
308330
309331 if ( ! NonGlobalPlugins . TryGetValue ( query . ActionKeyword , out var plugin ) )
310332 {
311- return GlobalPlugins . Where ( p => ! PluginModified ( p . Metadata . ID ) ) . ToList ( ) ;
333+ if ( dialogJump )
334+ return GlobalPlugins . Where ( p => p . Plugin is IAsyncDialogJump && ! PluginModified ( p . Metadata . ID ) ) . ToList ( ) ;
335+ else
336+ return GlobalPlugins . Where ( p => ! PluginModified ( p . Metadata . ID ) ) . ToList ( ) ;
312337 }
313338
339+ if ( dialogJump && plugin . Plugin is not IAsyncDialogJump )
340+ return Array . Empty < PluginPair > ( ) ;
341+
314342 if ( API . PluginModified ( plugin . Metadata . ID ) )
315- {
316343 return Array . Empty < PluginPair > ( ) ;
317- }
318344
319345 return new List < PluginPair >
320346 {
@@ -401,6 +427,36 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
401427 return results ;
402428 }
403429
430+ public static async Task < List < DialogJumpResult > > QueryDialogJumpForPluginAsync ( PluginPair pair , Query query , CancellationToken token )
431+ {
432+ var results = new List < DialogJumpResult > ( ) ;
433+ var metadata = pair . Metadata ;
434+
435+ try
436+ {
437+ var milliseconds = await API . StopwatchLogDebugAsync ( ClassName , $ "Cost for { metadata . Name } ",
438+ async ( ) => results = await ( ( IAsyncDialogJump ) pair . Plugin ) . QueryDialogJumpAsync ( query , token ) . ConfigureAwait ( false ) ) ;
439+
440+ token . ThrowIfCancellationRequested ( ) ;
441+ if ( results == null )
442+ return null ;
443+ UpdatePluginMetadata ( results , metadata , query ) ;
444+
445+ token . ThrowIfCancellationRequested ( ) ;
446+ }
447+ catch ( OperationCanceledException )
448+ {
449+ // null will be fine since the results will only be added into queue if the token hasn't been cancelled
450+ return null ;
451+ }
452+ catch ( Exception e )
453+ {
454+ API . LogException ( ClassName , $ "Failed to query Dialog Jump for plugin: { metadata . Name } ", e ) ;
455+ return null ;
456+ }
457+ return results ;
458+ }
459+
404460 public static void UpdatePluginMetadata ( IReadOnlyList < Result > results , PluginMetadata metadata , Query query )
405461 {
406462 foreach ( var r in results )
@@ -481,6 +537,16 @@ public static bool IsHomePlugin(string id)
481537 return _homePlugins . Where ( p => ! PluginModified ( p . Metadata . ID ) ) . Any ( p => p . Metadata . ID == id ) ;
482538 }
483539
540+ public static IList < DialogJumpExplorerPair > GetDialogJumpExplorers ( )
541+ {
542+ return _dialogJumpExplorerPlugins . Where ( p => ! PluginModified ( p . Metadata . ID ) ) . ToList ( ) ;
543+ }
544+
545+ public static IList < DialogJumpDialogPair > GetDialogJumpDialogs ( )
546+ {
547+ return _dialogJumpDialogPlugins . Where ( p => ! PluginModified ( p . Metadata . ID ) ) . ToList ( ) ;
548+ }
549+
484550 public static IDictionary < PluginPair , List < BasePluginHotkey > > GetPluginHotkeyInfo ( )
485551 {
486552 return _pluginHotkeyInfo . Where ( p => ! PluginModified ( p . Key . Metadata . ID ) )
@@ -862,7 +928,7 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
862928 catch ( Exception e )
863929 {
864930 API . LogException ( ClassName , $ "Failed to delete plugin settings folder for { plugin . Name } ", e ) ;
865- API . ShowMsg ( API . GetTranslation ( "failedToRemovePluginSettingsTitle" ) ,
931+ API . ShowMsgError ( API . GetTranslation ( "failedToRemovePluginSettingsTitle" ) ,
866932 string . Format ( API . GetTranslation ( "failedToRemovePluginSettingsMessage" ) , plugin . Name ) ) ;
867933 }
868934 }
@@ -878,7 +944,7 @@ internal static async Task<bool> UninstallPluginAsync(PluginMetadata plugin, boo
878944 catch ( Exception e )
879945 {
880946 API . LogException ( ClassName , $ "Failed to delete plugin cache folder for { plugin . Name } ", e ) ;
881- API . ShowMsg ( API . GetTranslation ( "failedToRemovePluginCacheTitle" ) ,
947+ API . ShowMsgError ( API . GetTranslation ( "failedToRemovePluginCacheTitle" ) ,
882948 string . Format ( API . GetTranslation ( "failedToRemovePluginCacheMessage" ) , plugin . Name ) ) ;
883949 }
884950 Settings . RemovePluginSettings ( plugin . ID ) ;
0 commit comments