/
Main.cs
213 lines (173 loc) · 7.51 KB
/
Main.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.Plugin.Program.ProgramArgumentParser;
using Microsoft.Plugin.Program.Programs;
using Microsoft.Plugin.Program.Storage;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Common;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Microsoft.Plugin.Program
{
public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IDisposable
{
// The order of this array is important! The Parsers will be checked in order (index 0 to index Length-1) and the first parser which is able to parse the Query will be used
// NoArgumentsArgumentParser does always succeed and therefore should always be last/fallback
private static readonly IProgramArgumentParser[] _programArgumentParsers = new IProgramArgumentParser[]
{
new DoubleDashProgramArgumentParser(),
new InferredProgramArgumentParser(),
new NoArgumentsArgumentParser(),
};
internal static ProgramPluginSettings Settings { get; set; }
internal static readonly ShellLocalization ShellLocalizationHelper = new();
public string Name => Properties.Resources.wox_plugin_program_plugin_name;
public string Description => Properties.Resources.wox_plugin_program_plugin_description;
public static string PluginID => "791FC278BA414111B8D1886DFE447410";
private static PluginInitContext _context;
private readonly PluginJsonStorage<ProgramPluginSettings> _settingsStorage;
private bool _disposed;
private PackageRepository _packageRepository = new PackageRepository(new PackageCatalogWrapper());
private static Win32ProgramFileSystemWatchers _win32ProgramRepositoryHelper;
private static Win32ProgramRepository _win32ProgramRepository;
public Main()
{
_settingsStorage = new PluginJsonStorage<ProgramPluginSettings>();
Settings = _settingsStorage.Load();
// This helper class initializes the file system watchers based on the locations to watch
_win32ProgramRepositoryHelper = new Win32ProgramFileSystemWatchers();
// Initialize the Win32ProgramRepository with the settings object
_win32ProgramRepository = new Win32ProgramRepository(_win32ProgramRepositoryHelper.FileSystemWatchers.Cast<IFileSystemWatcherWrapper>().ToList(), Settings, _win32ProgramRepositoryHelper.PathsToWatch);
}
public void Save()
{
_settingsStorage.Save();
}
public List<Result> Query(Query query)
{
var sources = _programArgumentParsers
.Where(programArgumentParser => programArgumentParser.Enabled);
foreach (var programArgumentParser in sources)
{
if (!programArgumentParser.TryParse(query, out var program, out var programArguments))
{
continue;
}
return Query(program, programArguments).ToList();
}
return new List<Result>(0);
}
private IEnumerable<Result> Query(string program, string programArguments)
{
var result = _win32ProgramRepository
.Concat<IProgram>(_packageRepository)
.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(program, programArguments, _context.API))
.Where(r => r?.Score > 0)
.ToArray();
if (result.Length != 0)
{
var maxScore = result.Max(x => x.Score);
return result
.Where(x => x.Score > Settings.MinScoreThreshold * maxScore);
}
return Enumerable.Empty<Result>();
}
public void Init(PluginInitContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_context.API.ThemeChanged += OnThemeChanged;
var a = Task.Run(() =>
{
Stopwatch.Normal("Microsoft.Plugin.Program.Main - Win32Program index cost", _win32ProgramRepository.IndexPrograms);
});
var b = Task.Run(() =>
{
Stopwatch.Normal("Microsoft.Plugin.Program.Main - Package index cost", _packageRepository.IndexPrograms);
UpdateUWPIconPath(_context.API.GetCurrentTheme());
});
Task.WaitAll(a, b);
Settings.LastIndexTime = DateTime.Today;
}
public void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateUWPIconPath(newTheme);
}
public void UpdateUWPIconPath(Theme theme)
{
foreach (UWPApplication app in _packageRepository)
{
app.UpdatePath(theme);
}
}
public void IndexPrograms()
{
var t1 = Task.Run(() => _win32ProgramRepository.IndexPrograms());
var t2 = Task.Run(() => _packageRepository.IndexPrograms());
Task.WaitAll(t1, t2);
Settings.LastIndexTime = DateTime.Today;
}
public string GetTranslatedPluginTitle()
{
return Properties.Resources.wox_plugin_program_plugin_name;
}
public string GetTranslatedPluginDescription()
{
return Properties.Resources.wox_plugin_program_plugin_description;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
ArgumentNullException.ThrowIfNull(selectedResult);
var menuOptions = new List<ContextMenuResult>();
if (selectedResult.ContextData is IProgram program)
{
menuOptions = program.ContextMenus(selectedResult.ProgramArguments, _context.API);
}
return menuOptions;
}
public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)
{
try
{
ArgumentNullException.ThrowIfNull(runProcess);
ArgumentNullException.ThrowIfNull(info);
runProcess(info);
}
catch (Exception ex)
{
Logger.ProgramLogger.Exception($"Unable to start ", ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, info?.FileName);
var name = "Plugin: " + Properties.Resources.wox_plugin_program_plugin_name;
var message = $"{Properties.Resources.powertoys_run_plugin_program_start_failed}: {info?.FileName}";
_context.API.ShowMsg(name, message, string.Empty);
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_context != null && _context.API != null)
{
_context.API.ThemeChanged -= OnThemeChanged;
}
_win32ProgramRepositoryHelper?.Dispose();
_disposed = true;
}
}
}
}
}