|
1 | | -// Copyright (c) Microsoft Corporation |
| 1 | +// Copyright (c) Microsoft Corporation |
2 | 2 | // The Microsoft Corporation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Diagnostics; |
8 | 8 | using System.Globalization; |
| 9 | +using System.IO; |
9 | 10 | using System.Linq; |
| 11 | +using System.Reflection; |
10 | 12 | using System.Text.RegularExpressions; |
11 | 13 | using System.Windows.Controls; |
| 14 | +using System.Windows.Input; |
12 | 15 | using System.Windows.Shapes; |
13 | | -using System.Xml.Linq; |
14 | 16 | using ManagedCommon; |
15 | 17 | using Microsoft.PowerToys.Settings.UI.Library; |
16 | 18 | using Wox.Infrastructure; |
@@ -46,13 +48,35 @@ public partial class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider |
46 | 48 | { |
47 | 49 | Key = NotGlobalIfUri, |
48 | 50 | DisplayLabel = Properties.Resources.plugin_global_if_uri, |
49 | | - Value = false, |
| 51 | + Value = true, |
50 | 52 | }, |
51 | 53 | }; |
52 | 54 |
|
53 | | - public List<ContextMenuResult> LoadContextMenus(Result selectedResult) |
| 55 | + private static string installed; |
| 56 | + |
| 57 | + // constructor |
| 58 | + public Main() |
54 | 59 | { |
55 | | - return new List<ContextMenuResult>(0); |
| 60 | + Process process = new Process(); |
| 61 | + |
| 62 | + process.StartInfo.FileName = "winget"; |
| 63 | + process.StartInfo.Arguments = "list"; |
| 64 | + process.StartInfo.UseShellExecute = false; |
| 65 | + process.StartInfo.CreateNoWindow = true; |
| 66 | + process.StartInfo.RedirectStandardOutput = true; |
| 67 | + process.Start(); |
| 68 | + |
| 69 | + string output = process.StandardOutput.ReadToEnd(); |
| 70 | + process.WaitForExit(); |
| 71 | + |
| 72 | + // UTF16 to UTF8 |
| 73 | + output = System.Text.Encoding.UTF8.GetString( |
| 74 | + System.Text.Encoding.Convert( |
| 75 | + System.Text.Encoding.Unicode, |
| 76 | + System.Text.Encoding.UTF8, |
| 77 | + System.Text.Encoding.Unicode.GetBytes(output))); |
| 78 | + |
| 79 | + installed = output; |
56 | 80 | } |
57 | 81 |
|
58 | 82 | public List<Result> Query(Query query) |
@@ -104,8 +128,6 @@ public List<Result> Query(Query query) |
104 | 128 | string output = process.StandardOutput.ReadToEnd(); |
105 | 129 | process.WaitForExit(); |
106 | 130 |
|
107 | | - var bytes = System.Text.Encoding.Default.GetBytes(output); |
108 | | - |
109 | 131 | // UTF16 to UTF8 |
110 | 132 | output = System.Text.Encoding.UTF8.GetString( |
111 | 133 | System.Text.Encoding.Convert( |
@@ -179,12 +201,6 @@ public List<Result> Query(Query query) |
179 | 201 | { |
180 | 202 | match = string.Empty; |
181 | 203 | } |
182 | | - |
183 | | - // name = ";" + lines[0].Split("ID")[0].Length.ToString() + ";"; // matches[1].Index;; |
184 | | - // idStr = idChars.ToString(); |
185 | | - // version = versionChars.ToString(); |
186 | | - // match = matchChars.ToString(); |
187 | | - // source = sourceChars.ToString(); |
188 | 204 | } |
189 | 205 | catch (Exception e) |
190 | 206 | { |
@@ -245,6 +261,74 @@ public void Init(PluginInitContext context) |
245 | 261 | }; |
246 | 262 | } |
247 | 263 |
|
| 264 | + private static List<ContextMenuResult> GetContextMenu(in Result result, in string assemblyName) |
| 265 | + { |
| 266 | + if (result?.Title == Properties.Resources.plugin_description) |
| 267 | + { |
| 268 | + return new List<ContextMenuResult>(0); |
| 269 | + } |
| 270 | + |
| 271 | + var idStr = result?.ProgramArguments; |
| 272 | + var name = result?.QueryTextDisplay.Replace("winget ", string.Empty); |
| 273 | + |
| 274 | + List<ContextMenuResult> list = new List<ContextMenuResult>(1) |
| 275 | + { |
| 276 | + new ContextMenuResult |
| 277 | + { |
| 278 | + AcceleratorKey = Key.I, |
| 279 | + AcceleratorModifiers = ModifierKeys.Control, |
| 280 | + Action = _ => |
| 281 | + { |
| 282 | + Helper.OpenInShell("winget", "install " + idStr + " -i --force --wait", "/"); |
| 283 | + return true; |
| 284 | + }, |
| 285 | + FontFamily = "Segoe MDL2 Assets", |
| 286 | + Glyph = "\xE70F", // Symbol: Edit |
| 287 | + PluginName = assemblyName, |
| 288 | + Title = "Forced interactive install (Ctrl+I)", |
| 289 | + }, |
| 290 | + }; |
| 291 | + |
| 292 | + if (installed.ToLower().Contains(name.ToLower())) |
| 293 | + { |
| 294 | + list.Add(new ContextMenuResult |
| 295 | + { |
| 296 | + AcceleratorKey = Key.U, |
| 297 | + AcceleratorModifiers = ModifierKeys.Control, |
| 298 | + Action = _ => |
| 299 | + { |
| 300 | + Helper.OpenInShell("winget", "upgrade " + idStr + " --wait", "/"); |
| 301 | + return true; |
| 302 | + }, |
| 303 | + FontFamily = "Segoe MDL2 Assets", |
| 304 | + Glyph = "\xE777", // Symbol: UpdateRestore |
| 305 | + PluginName = assemblyName, |
| 306 | + Title = "Upgrade (Ctrl+U)", |
| 307 | + }); |
| 308 | + list.Add(new ContextMenuResult |
| 309 | + { |
| 310 | + AcceleratorKey = Key.D, |
| 311 | + AcceleratorModifiers = ModifierKeys.Control, |
| 312 | + Action = _ => |
| 313 | + { |
| 314 | + Helper.OpenInShell("winget", "uninstall " + idStr + " --wait", "/"); |
| 315 | + return true; |
| 316 | + }, |
| 317 | + FontFamily = "Segoe MDL2 Assets", |
| 318 | + Glyph = "\xE74D", // Symbol: Delete |
| 319 | + PluginName = assemblyName, |
| 320 | + Title = "Delete (Ctrl+D)", |
| 321 | + }); |
| 322 | + } |
| 323 | + |
| 324 | + return list; |
| 325 | + } |
| 326 | + |
| 327 | + public List<ContextMenuResult> LoadContextMenus(Result selectedResult) |
| 328 | + { |
| 329 | + return GetContextMenu(selectedResult, "someassemblyname"); |
| 330 | + } |
| 331 | + |
248 | 332 | public string GetTranslatedPluginTitle() |
249 | 333 | { |
250 | 334 | return Properties.Resources.plugin_name; |
|
0 commit comments