-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
208 lines (180 loc) · 7.32 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
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Community.PowerToys.Run.Plugin.JohnnyDecimal
{
/// <summary>
/// Main class of this plugin that implement all used interfaces.
/// </summary>
public class Main : IPlugin, IContextMenu, ISettingProvider, IDisposable
{
/// <summary>
/// ID of the plugin.
/// </summary>
public static string PluginID => "FEAFCFE212C04174A6CA07DB644DDB86";
/// <summary>
/// Name of the plugin.
/// </summary>
public string Name => "JohnnyDecimal";
/// <summary>
/// Description of the plugin.
/// </summary>
public string Description => "Navigate to the target Johnny.Decimal folder";
/// <summary>
/// Additional options for the plugin.
/// </summary>
public IEnumerable<PluginAdditionalOption> AdditionalOptions => [
new()
{
Key = nameof(RootFolder),
DisplayLabel = "Root folder",
DisplayDescription = "The path to the Johnny.Decimal folder",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = RootFolder,
},
];
private string? RootFolder { get; set; }
private PluginInitContext? Context { get; set; }
private string? IconPath { get; set; }
private bool Disposed { get; set; }
/// <summary>
/// Return a filtered list, based on the given query.
/// </summary>
/// <param name="query">The query to filter the list.</param>
/// <returns>A filtered list, can be empty when nothing was found.</returns>
public List<Result> Query(Query query)
{
Log.Info("Query: " + query.Search, GetType());
Result CreateResult(string title, string subTitle, string? contextData = null) =>
new()
{
QueryTextDisplay = query.Search,
IcoPath = IconPath,
Title = title,
SubTitle = subTitle,
ContextData = contextData,
};
if (!JohnnyDecimalId.TryParse(query.Search, out var johnnyDecimalId))
{
return [CreateResult("No results found", "Please insert a valid JohnnyDecimal ID")];
}
var resolver = new JohnnyDecimalPathResolver(RootFolder);
var pathsResult = resolver.FindPaths(johnnyDecimalId);
return string.IsNullOrEmpty(pathsResult.ErrorMessage)
? pathsResult.Directories.Select(x => CreateResult(x.Name, x.FullName, x.FullName)).ToList()
: [CreateResult("Error", pathsResult.ErrorMessage)];
}
/// <summary>
/// Initialize the plugin with the given <see cref="PluginInitContext"/>.
/// </summary>
/// <param name="context">The <see cref="PluginInitContext"/> for this plugin.</param>
public void Init(PluginInitContext context)
{
Log.Info("Init", GetType());
Context = context ?? throw new ArgumentNullException(nameof(context));
Context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(Context.API.GetCurrentTheme());
}
/// <summary>
/// Return a list context menu entries for a given <see cref="Result"/> (shown at the right side of the result).
/// </summary>
/// <param name="selectedResult">The <see cref="Result"/> for the list with context menu entries.</param>
/// <returns>A list context menu entries.</returns>
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
Log.Info("LoadContextMenus", GetType());
if (selectedResult?.ContextData is string path) {
return [
new ContextMenuResult
{
PluginName = Name,
Title = "Open (Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE838", // FolderOpen
AcceleratorKey = Key.Enter,
Action = _ => OpenFolder(path),
},
new ContextMenuResult
{
PluginName = Name,
Title = "Copy (Ctrl+Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE8C8", // Copy
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => CopyToClipboard(path),
},
];
}
return [];
}
/// <summary>
/// Creates setting panel.
/// </summary>
/// <returns>The control.</returns>
/// <exception cref="NotImplementedException">method is not implemented.</exception>
public Control CreateSettingPanel() => throw new NotImplementedException();
/// <summary>
/// Updates settings.
/// </summary>
/// <param name="settings">The plugin settings.</param>
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
Log.Info("UpdateSettings", GetType());
RootFolder = settings.AdditionalOptions.SingleOrDefault(x => x.Key == nameof(RootFolder))?.TextValue ?? string.Empty;
}
/// <inheritdoc/>
public void Dispose()
{
Log.Info("Dispose", GetType());
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Wrapper method for <see cref="Dispose()"/> that dispose additional objects and events form the plugin itself.
/// </summary>
/// <param name="disposing">Indicate that the plugin is disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (Disposed || !disposing)
{
return;
}
if (Context?.API != null)
{
Context.API.ThemeChanged -= OnThemeChanged;
}
Disposed = true;
}
private void UpdateIconPath(Theme theme) => IconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? Context?.CurrentPluginMetadata.IcoPathLight : Context?.CurrentPluginMetadata.IcoPathDark;
private void OnThemeChanged(Theme currentTheme, Theme newTheme) => UpdateIconPath(newTheme);
private static bool OpenFolder(string? value)
{
if (value != null)
{
Process.Start(new ProcessStartInfo
{
FileName = value,
UseShellExecute = true,
Verb = "open"
});
}
return true;
}
private static bool CopyToClipboard(string? value)
{
if (value != null)
{
Clipboard.SetText(value);
}
return true;
}
}
}