Skip to content

Commit 4f269d3

Browse files
authored
Dialog Jump - Quickly navigate the Open/Save As dialog window (#1018)
1 parent ff2d5e8 commit 4f269d3

29 files changed

+2872
-98
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
@@ -9,6 +9,7 @@
99
using CommunityToolkit.Mvvm.DependencyInjection;
1010
using Flow.Launcher.Core.ExternalPlugins;
1111
using Flow.Launcher.Infrastructure;
12+
using Flow.Launcher.Infrastructure.DialogJump;
1213
using Flow.Launcher.Infrastructure.UserSettings;
1314
using Flow.Launcher.Plugin;
1415
using Flow.Launcher.Plugin.SharedCommands;
@@ -40,6 +41,9 @@ public static class PluginManager
4041
private static IEnumerable<PluginPair> _resultUpdatePlugin;
4142
private static IEnumerable<PluginPair> _translationPlugins;
4243

44+
private static readonly List<DialogJumpExplorerPair> _dialogJumpExplorerPlugins = new();
45+
private static readonly List<DialogJumpDialogPair> _dialogJumpDialogPlugins = new();
46+
4347
/// <summary>
4448
/// Directories that will hold Flow Launcher plugin directory
4549
/// </summary>
@@ -186,6 +190,24 @@ public static void LoadPlugins(PluginsSettings settings)
186190
_homePlugins = GetPluginsForInterface<IAsyncHomeQuery>();
187191
_resultUpdatePlugin = GetPluginsForInterface<IResultUpdated>();
188192
_translationPlugins = GetPluginsForInterface<IPluginI18n>();
193+
194+
// Initialize Dialog Jump plugin pairs
195+
foreach (var pair in GetPluginsForInterface<IDialogJumpExplorer>())
196+
{
197+
_dialogJumpExplorerPlugins.Add(new DialogJumpExplorerPair
198+
{
199+
Plugin = (IDialogJumpExplorer)pair.Plugin,
200+
Metadata = pair.Metadata
201+
});
202+
}
203+
foreach (var pair in GetPluginsForInterface<IDialogJumpDialog>())
204+
{
205+
_dialogJumpDialogPlugins.Add(new DialogJumpDialogPair
206+
{
207+
Plugin = (IDialogJumpDialog)pair.Plugin,
208+
Metadata = pair.Metadata
209+
});
210+
}
189211
}
190212

191213
private static void UpdatePluginDirectory(List<PluginMetadata> metadatas)
@@ -288,20 +310,24 @@ public static async Task InitializePluginsAsync()
288310
}
289311
}
290312

291-
public static ICollection<PluginPair> ValidPluginsForQuery(Query query)
313+
public static ICollection<PluginPair> ValidPluginsForQuery(Query query, bool dialogJump)
292314
{
293315
if (query is null)
294316
return Array.Empty<PluginPair>();
295317

296318
if (!NonGlobalPlugins.TryGetValue(query.ActionKeyword, out var plugin))
297319
{
298-
return GlobalPlugins.Where(p => !PluginModified(p.Metadata.ID)).ToList();
320+
if (dialogJump)
321+
return GlobalPlugins.Where(p => p.Plugin is IAsyncDialogJump && !PluginModified(p.Metadata.ID)).ToList();
322+
else
323+
return GlobalPlugins.Where(p => !PluginModified(p.Metadata.ID)).ToList();
299324
}
300325

326+
if (dialogJump && plugin.Plugin is not IAsyncDialogJump)
327+
return Array.Empty<PluginPair>();
328+
301329
if (API.PluginModified(plugin.Metadata.ID))
302-
{
303330
return Array.Empty<PluginPair>();
304-
}
305331

306332
return new List<PluginPair>
307333
{
@@ -388,6 +414,36 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
388414
return results;
389415
}
390416

417+
public static async Task<List<DialogJumpResult>> QueryDialogJumpForPluginAsync(PluginPair pair, Query query, CancellationToken token)
418+
{
419+
var results = new List<DialogJumpResult>();
420+
var metadata = pair.Metadata;
421+
422+
try
423+
{
424+
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
425+
async () => results = await ((IAsyncDialogJump)pair.Plugin).QueryDialogJumpAsync(query, token).ConfigureAwait(false));
426+
427+
token.ThrowIfCancellationRequested();
428+
if (results == null)
429+
return null;
430+
UpdatePluginMetadata(results, metadata, query);
431+
432+
token.ThrowIfCancellationRequested();
433+
}
434+
catch (OperationCanceledException)
435+
{
436+
// null will be fine since the results will only be added into queue if the token hasn't been cancelled
437+
return null;
438+
}
439+
catch (Exception e)
440+
{
441+
API.LogException(ClassName, $"Failed to query Dialog Jump for plugin: {metadata.Name}", e);
442+
return null;
443+
}
444+
return results;
445+
}
446+
391447
public static void UpdatePluginMetadata(IReadOnlyList<Result> results, PluginMetadata metadata, Query query)
392448
{
393449
foreach (var r in results)
@@ -463,6 +519,16 @@ public static bool IsHomePlugin(string id)
463519
return _homePlugins.Where(p => !PluginModified(p.Metadata.ID)).Any(p => p.Metadata.ID == id);
464520
}
465521

522+
public static IList<DialogJumpExplorerPair> GetDialogJumpExplorers()
523+
{
524+
return _dialogJumpExplorerPlugins.Where(p => !PluginModified(p.Metadata.ID)).ToList();
525+
}
526+
527+
public static IList<DialogJumpDialogPair> GetDialogJumpDialogs()
528+
{
529+
return _dialogJumpDialogPlugins.Where(p => !PluginModified(p.Metadata.ID)).ToList();
530+
}
531+
466532
public static bool ActionKeywordRegistered(string actionKeyword)
467533
{
468534
// this method is only checking for action keywords (defined as not '*') registration

0 commit comments

Comments
 (0)