Skip to content

Commit

Permalink
Added context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrot committed Mar 14, 2023
1 parent fb330cf commit e345036
Showing 1 changed file with 97 additions and 13 deletions.
110 changes: 97 additions & 13 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright (c) Microsoft Corporation
// 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.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Xml.Linq;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure;
Expand Down Expand Up @@ -46,13 +48,35 @@ public partial class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider
{
Key = NotGlobalIfUri,
DisplayLabel = Properties.Resources.plugin_global_if_uri,
Value = false,
Value = true,
},
};

public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
private static string installed;

// constructor
public Main()
{
return new List<ContextMenuResult>(0);
Process process = new Process();

process.StartInfo.FileName = "winget";
process.StartInfo.Arguments = "list";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();

string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

// UTF16 to UTF8
output = System.Text.Encoding.UTF8.GetString(
System.Text.Encoding.Convert(
System.Text.Encoding.Unicode,
System.Text.Encoding.UTF8,
System.Text.Encoding.Unicode.GetBytes(output)));

installed = output;
}

public List<Result> Query(Query query)
Expand Down Expand Up @@ -104,8 +128,6 @@ public List<Result> Query(Query query)
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

var bytes = System.Text.Encoding.Default.GetBytes(output);

// UTF16 to UTF8
output = System.Text.Encoding.UTF8.GetString(
System.Text.Encoding.Convert(
Expand Down Expand Up @@ -179,12 +201,6 @@ public List<Result> Query(Query query)
{
match = string.Empty;
}

// name = ";" + lines[0].Split("ID")[0].Length.ToString() + ";"; // matches[1].Index;;
// idStr = idChars.ToString();
// version = versionChars.ToString();
// match = matchChars.ToString();
// source = sourceChars.ToString();
}
catch (Exception e)
{
Expand Down Expand Up @@ -245,6 +261,74 @@ public void Init(PluginInitContext context)
};
}

private static List<ContextMenuResult> GetContextMenu(in Result result, in string assemblyName)
{
if (result?.Title == Properties.Resources.plugin_description)
{
return new List<ContextMenuResult>(0);
}

var idStr = result?.ProgramArguments;
var name = result?.QueryTextDisplay.Replace("winget ", string.Empty);

List<ContextMenuResult> list = new List<ContextMenuResult>(1)
{
new ContextMenuResult
{
AcceleratorKey = Key.I,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Helper.OpenInShell("winget", "install " + idStr + " -i --force --wait", "/");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE70F", // Symbol: Edit
PluginName = assemblyName,
Title = "Forced interactive install (Ctrl+I)",
},
};

if (installed.ToLower().Contains(name.ToLower()))
{
list.Add(new ContextMenuResult
{
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Helper.OpenInShell("winget", "upgrade " + idStr + " --wait", "/");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE777", // Symbol: UpdateRestore
PluginName = assemblyName,
Title = "Upgrade (Ctrl+U)",
});
list.Add(new ContextMenuResult
{
AcceleratorKey = Key.D,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ =>
{
Helper.OpenInShell("winget", "uninstall " + idStr + " --wait", "/");
return true;
},
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE74D", // Symbol: Delete
PluginName = assemblyName,
Title = "Delete (Ctrl+D)",
});
}

return list;
}

public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
return GetContextMenu(selectedResult, "someassemblyname");
}

public string GetTranslatedPluginTitle()
{
return Properties.Resources.plugin_name;
Expand Down

0 comments on commit e345036

Please sign in to comment.