Skip to content

Commit e345036

Browse files
committed
Added context menu
1 parent fb330cf commit e345036

File tree

1 file changed

+97
-13
lines changed

1 file changed

+97
-13
lines changed

Main.cs

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
// Copyright (c) Microsoft Corporation
1+
// Copyright (c) Microsoft Corporation
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Globalization;
9+
using System.IO;
910
using System.Linq;
11+
using System.Reflection;
1012
using System.Text.RegularExpressions;
1113
using System.Windows.Controls;
14+
using System.Windows.Input;
1215
using System.Windows.Shapes;
13-
using System.Xml.Linq;
1416
using ManagedCommon;
1517
using Microsoft.PowerToys.Settings.UI.Library;
1618
using Wox.Infrastructure;
@@ -46,13 +48,35 @@ public partial class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider
4648
{
4749
Key = NotGlobalIfUri,
4850
DisplayLabel = Properties.Resources.plugin_global_if_uri,
49-
Value = false,
51+
Value = true,
5052
},
5153
};
5254

53-
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
55+
private static string installed;
56+
57+
// constructor
58+
public Main()
5459
{
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;
5680
}
5781

5882
public List<Result> Query(Query query)
@@ -104,8 +128,6 @@ public List<Result> Query(Query query)
104128
string output = process.StandardOutput.ReadToEnd();
105129
process.WaitForExit();
106130

107-
var bytes = System.Text.Encoding.Default.GetBytes(output);
108-
109131
// UTF16 to UTF8
110132
output = System.Text.Encoding.UTF8.GetString(
111133
System.Text.Encoding.Convert(
@@ -179,12 +201,6 @@ public List<Result> Query(Query query)
179201
{
180202
match = string.Empty;
181203
}
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();
188204
}
189205
catch (Exception e)
190206
{
@@ -245,6 +261,74 @@ public void Init(PluginInitContext context)
245261
};
246262
}
247263

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+
248332
public string GetTranslatedPluginTitle()
249333
{
250334
return Properties.Resources.plugin_name;

0 commit comments

Comments
 (0)