-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.cs
274 lines (242 loc) · 9.91 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// 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 ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using System.Windows.Controls;
using Wox.Plugin;
using static Microsoft.PowerToys.Settings.UI.Library.PluginAdditionalOption;
using System.IO;
using System.Reflection;
using Wox.Plugin.Logger;
using Wox.Infrastructure;
using BrowserInfo = Wox.Plugin.Common.DefaultBrowserInfo;
using Community.PowerToys.Run.Plugin.FastWeb.Classes;
using Community.PowerToys.Run.Plugin.FastWeb.Models;
using PR = Community.PowerToys.Run.Plugin.FastWeb.Properties.Resources;
namespace Community.PowerToys.Run.Plugin.FastWeb
{
public class Main : IPlugin, IPluginI18n, ISettingProvider, IReloadable, IDisposable, IContextMenu
{
private PluginInitContext? _context;
public static Dictionary<string, string> IconPath => new()
{
{ "FastWeb", @"Images\FastWeb.light.png" },
{ "AddKeyword", @"Images\AddKeyword.light.png" },
{ "DeleteKeyword", @"Images\DeleteKeyword.light.png" }
};
private bool _disposed;
public string Name => PR.plugin_name;
public string Description => PR.plugin_description;
public static string PluginID => "9f3525da-af82-4733-9654-860eaf2e756d";
public static string PluginDirectory => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
public static bool IsPluginDirectoryValid => !string.IsNullOrEmpty(PluginDirectory);
private DataHandler DH;
// setting
private static string CurrentSettingFileName;
private static List<string> SettingFileNames = new(
Directory.GetFiles(Path.Combine(PluginDirectory, "Settings"), "*.json")
.Select(x => Path.GetFileNameWithoutExtension(x)));
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = "CurrentSettingFile",
DisplayDescription = "The JSON file that will be applied as setting, Default is Default.json",
DisplayLabel = "Current Setting File",
PluginOptionType = AdditionalOptionType.Combobox,
ComboBoxOptions = SettingFileNames,
ComboBoxValue = 0,
ComboBoxItems = SettingFileNames.Select((val, idx) =>
{
return new KeyValuePair<string, string>(val, idx.ToString());
}).ToList()
}
};
public List<Result> Query(Query query)
{
ArgumentNullException.ThrowIfNull(query);
List<Result> results = [];
if (DH.WebDatas.Count() == 0)
{
results.Add(
new Result()
{
Title = "There are no keywords in your setting file",
SubTitle = $"use /w+ command to add new keyword ({DH.FileName}.json).",
IcoPath = IconPath["FastWeb"],
Action = action => { return true; }
}
);
}
results.AddRange(DH.GetMatchingKeywords(query.Search));
if (results.Count() == 0 || query.Terms.Count > 1)
{
results.Add(DH.GetAddDataResult(new(query.Terms)));
}
return results;
}
public void Init(PluginInitContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(_context.API.GetCurrentTheme());
DH = new(CurrentSettingFileName);
}
public string GetTranslatedPluginTitle()
{
return PR.plugin_name;
}
public string GetTranslatedPluginDescription()
{
return PR.plugin_description;
}
private void OnThemeChanged(Theme oldtheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
private void UpdateIconPath(Theme theme)
{
bool isLightTheme = theme == Theme.Light || theme == Theme.HighContrastWhite;
foreach (string key in IconPath.Keys)
{
IconPath[key] = IconPath[key].Replace(isLightTheme ? "dark" : "light",
isLightTheme ? "light" : "dark");
}
}
public Control CreateSettingPanel()
{
throw new NotImplementedException();
}
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var GetSetting = (string key) => settings.AdditionalOptions.FirstOrDefault(set => set.Key == key);
int defaultSettingIndex = GetSetting("CurrentSettingFile").ComboBoxValue;
if (SettingFileNames.Count == 0)
{
defaultSettingIndex = 0;
SettingFileNames.Add(PR.default_json_name);
}
CurrentSettingFileName = SettingFileNames[defaultSettingIndex];
DH = new(CurrentSettingFileName);
}
public void ReloadData()
{
if (_context is null)
{
return;
}
UpdateIconPath(_context.API.GetCurrentTheme());
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
if (_context != null && _context.API != null)
{
_context.API.ThemeChanged -= OnThemeChanged;
}
_disposed = true;
}
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
if (selectedResult.Title == "Add new keyword" && selectedResult.SubTitle != "Usage: /w+ Keyword URL")
{
string[] words = selectedResult.SubTitle.Split(' ');
string keyword = words[1][..^1], url = words[^1];
return
[
new ContextMenuResult()
{
PluginName = PR.plugin_name,
Title = "Add this keyword to new file (Shift+Enter)",
Glyph = "\xE82E",
FontFamily = "Segoe Fluent Icons, Segoe MDL2 Assets",
AcceleratorKey = System.Windows.Input.Key.Enter,
AcceleratorModifiers = System.Windows.Input.ModifierKeys.Shift,
Action = _ =>
{
DH = new(keyword);
DH.WebDatas.Add(new(keyword, url));
DH.DumpWebDatasToJSON();
return true;
}
},
new ContextMenuResult()
{
PluginName = PR.plugin_name,
Title = "Add this keyword to current file (Enter)",
Glyph = "\xE710",
FontFamily = "Segoe Fluent Icons, Segoe MDL2 Assets",
Action = _ =>
{
DH.WebDatas.Add(new(keyword, url));
DH.DumpWebDatasToJSON();
return true;
}
}
];
}
if (!DH.WebDatas.Any(w => w.Keyword == selectedResult.Title && w.URL == selectedResult.SubTitle))
{
return [];
}
return
[
new ContextMenuResult()
{
PluginName = PR.plugin_name,
Title = "Remove this keyword (Ctrl+D)",
Glyph = "\xE74D",
FontFamily = "Segoe Fluent Icons, Segoe MDL2 Assets",
AcceleratorKey = System.Windows.Input.Key.D,
AcceleratorModifiers = System.Windows.Input.ModifierKeys.Control,
Action = _ =>
{
return DH.RemoveKeyword(selectedResult.Title);
}
},
new ContextMenuResult()
{
PluginName = PR.plugin_name,
Title = "Open in new window (Shift+Enter)",
Glyph = "\xE8A7",
FontFamily = "Segoe Fluent Icons, Segoe MDL2 Assets",
AcceleratorKey = System.Windows.Input.Key.Return,
AcceleratorModifiers = System.Windows.Input.ModifierKeys.Shift,
Action = _ =>
{
if (!Helper.OpenInShell(BrowserInfo.Path, $"--new-window {selectedResult.SubTitle}"))
{
Log.Error($"Plugin: {PR.plugin_name}\nCannot open {selectedResult.SubTitle}", typeof(WebData));
return false;
}
return true;
}
},
new ContextMenuResult()
{
PluginName = PR.plugin_name,
Title = "Open in new tab (Enter)",
Glyph = "\xE8AD",
FontFamily = "Segoe Fluent Icons, Segoe MDL2 Assets",
Action = _ =>
{
if (!Helper.OpenInShell(selectedResult.SubTitle))
{
Log.Error($"Plugin: {PR.plugin_name}\nCannot open {selectedResult.SubTitle}", typeof(WebData));
return false;
}
return true;
}
}
];
}
}
}