-
-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathMain.cs
More file actions
134 lines (116 loc) · 4.8 KB
/
Main.cs
File metadata and controls
134 lines (116 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Flow.Launcher.Plugin.Explorer.Helper;
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.Everything;
using Flow.Launcher.Plugin.Explorer.ViewModels;
using Flow.Launcher.Plugin.Explorer.Views;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using Flow.Launcher.Plugin.Explorer.Exceptions;
using System.Linq;
using System.Globalization;
namespace Flow.Launcher.Plugin.Explorer
{
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, IAsyncDialogJump
{
internal static PluginInitContext Context { get; set; }
internal static Settings Settings { get; set; }
private SettingsViewModel viewModel;
private ContextMenu contextMenu;
private SearchManager searchManager;
private static readonly List<DialogJumpResult> _emptyDialogJumpResultList = new();
public Control CreateSettingPanel()
{
return new ExplorerSettings(viewModel);
}
public Task InitAsync(PluginInitContext context)
{
Context = context;
Settings = context.API.LoadSettingJsonStorage<Settings>();
FillQuickAccessLinkNames();
viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context, Settings);
searchManager = new SearchManager(Settings, Context);
ResultManager.Init(Context, Settings);
EverythingApiDllImport.Load(Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "EverythingSDK",
Environment.Is64BitProcess ? "x64" : "x86"));
Everything3ApiDllImport.Load(Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "EverythingSDK",
Environment.Is64BitProcess ? "x64" : "x86"));
return Task.CompletedTask;
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return contextMenu.LoadContextMenus(selectedResult);
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
try
{
return await searchManager.SearchAsync(query, token);
}
catch (Exception e) when (e is SearchException or EngineNotAvailableException)
{
return new List<Result>
{
new()
{
Title = e.Message,
SubTitle = e is EngineNotAvailableException { Resolution: { } resolution }
? resolution
: "Enter to copy the message to clipboard",
Score = 501,
IcoPath = e is EngineNotAvailableException { ErrorIcon: { } iconPath }
? iconPath
: Constants.GeneralSearchErrorImagePath,
AsyncAction = e is EngineNotAvailableException {Action: { } action}
? action
: _ =>
{
Context.API.CopyToClipboard(e.ToString());
return new ValueTask<bool>(true);
}
}
};
}
}
public string GetTranslatedPluginTitle()
{
return Localize.plugin_explorer_plugin_name();
}
public string GetTranslatedPluginDescription()
{
return Localize.plugin_explorer_plugin_description();
}
public void OnCultureInfoChanged(CultureInfo newCulture)
{
// Update labels for setting view model
EverythingSortOptionLocalized.UpdateLabels(viewModel.AllEverythingSortOptions);
}
private static void FillQuickAccessLinkNames()
{
// Legacy version does not have names for quick access links, so we fill them with the path name.
foreach (var link in Settings.QuickAccessLinks)
{
if (string.IsNullOrWhiteSpace(link.Name))
{
link.Name = link.Path.GetPathName();
}
}
}
public async Task<List<DialogJumpResult>> QueryDialogJumpAsync(Query query, CancellationToken token)
{
try
{
var results = await searchManager.SearchAsync(query, token);
return results.Select(r => DialogJumpResult.From(r, r.CopyText)).ToList();
}
catch (Exception e) when (e is SearchException or EngineNotAvailableException)
{
return _emptyDialogJumpResultList;
}
}
}
}