Add run as admin context menu item for application results returned by the Indexer Plugin (#4807)

* Added run as admin context menu item to apps returned by indexer plugin

* Added a test and localized strings

* localize strings

* Add more tests for folder and other file types

* fixed run as admin -> run as administrator

* resolved merge conflict

* refactored tests

* moved common code to helper and added logs

* moved start process to the helper class

* added more info in a comment

* fixed count in tests as open in console was added

* removed additional code that was added while fixing merge conflicts
This commit is contained in:
Alekhya
2020-07-08 13:05:57 -07:00
committed by GitHub
parent 638cd1dd48
commit 411140c3ea
11 changed files with 162 additions and 40 deletions

View File

@@ -6,8 +6,9 @@ using System.Windows;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
using Microsoft.Plugin.Indexer.SearchHelper;
using System.Windows.Input;
using System.Reflection;
using System.Windows.Input;
using System.Reflection;
using System.Threading.Tasks;
using Wox.Infrastructure;
namespace Microsoft.Plugin.Indexer
@@ -22,6 +23,9 @@ namespace Microsoft.Plugin.Indexer
File
}
// Extensions for adding run as admin context menu item for applications
private readonly string[] appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
public ContextMenuLoader(PluginInitContext context)
{
_context = context;
@@ -39,6 +43,12 @@ namespace Microsoft.Plugin.Indexer
contextMenus.Add(CreateOpenContainingFolderResult(record));
}
// Test to check if File can be Run as admin, if yes, we add a 'run as admin' context menu item
if(CanFileBeRunAsAdmin(record.Path))
{
contextMenus.Add(CreateRunAsAdminContextMenu(record));
}
contextMenus.Add(new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
@@ -98,8 +108,51 @@ namespace Microsoft.Plugin.Indexer
}
return contextMenus;
}
}
// Function to add the context menu item to run as admin
private ContextMenuResult CreateRunAsAdminContextMenu(SearchResult record)
{
return new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_run_as_administrator"),
Glyph = "\xE7EF",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.Enter,
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift),
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsAdmin(record.Path));
return true;
}
catch(Exception e)
{
Log.Exception($"|Microsoft.Plugin.Indexer.ContextMenu| Failed to run {record.Path} as admin, {e.Message}", e);
return false;
}
}
};
}
// Function to test if the file can be run as admin
private bool CanFileBeRunAsAdmin(string path)
{
string fileExtension = Path.GetExtension(path);
foreach(string extension in appExtensions)
{
if(extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record)
{
return new ContextMenuResult

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -4,6 +4,7 @@
<system:String x:Key="Microsoft_plugin_indexer_copy_path">Copy path (Ctrl+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_containing_folder">Open containing folder (Ctrl+Shift+E)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_run_as_administrator">Run As Administrator (Ctrl+Shift+Enter)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>

View File

@@ -57,12 +57,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Wox.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<Content Include="Languages\de.xaml">
@@ -101,4 +95,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Wox.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>