mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
Fixing stylecop violations (#5629)
This commit is contained in:
@@ -1,114 +1,118 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
using Microsoft.Plugin.Indexer.SearchHelper;
|
||||
using System.Windows.Input;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Infrastructure;
|
||||
// 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.
|
||||
|
||||
namespace Microsoft.Plugin.Indexer
|
||||
{
|
||||
internal class ContextMenuLoader : IContextMenu
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.Plugin.Indexer.SearchHelper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Microsoft.Plugin.Indexer
|
||||
{
|
||||
internal class ContextMenuLoader : IContextMenu
|
||||
{
|
||||
private readonly PluginInitContext _context;
|
||||
|
||||
public enum ResultType
|
||||
{
|
||||
Folder,
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log and show an error message")]
|
||||
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
||||
{
|
||||
var contextMenus = new List<ContextMenuResult>();
|
||||
if (selectedResult.ContextData is SearchResult record)
|
||||
{
|
||||
ResultType type = Path.HasExtension(record.Path) ? ResultType.File : ResultType.Folder;
|
||||
|
||||
if (type == ResultType.File)
|
||||
{
|
||||
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
|
||||
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
||||
{
|
||||
var contextMenus = new List<ContextMenuResult>();
|
||||
if (selectedResult.ContextData is SearchResult record)
|
||||
{
|
||||
ResultType type = Path.HasExtension(record.Path) ? ResultType.File : ResultType.Folder;
|
||||
|
||||
if (type == ResultType.File)
|
||||
{
|
||||
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,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_copy_path"),
|
||||
Glyph = "\xE8C8",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
}
|
||||
|
||||
contextMenus.Add(new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_copy_path"),
|
||||
Glyph = "\xE8C8",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.C,
|
||||
AcceleratorModifiers = ModifierKeys.Control,
|
||||
|
||||
Action = (context) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(record.Path);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var message = "Fail to set text in clipboard";
|
||||
LogException(message, e);
|
||||
_context.API.ShowMsg(message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
contextMenus.Add(new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_open_in_console"),
|
||||
Glyph = "\xE756",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorModifiers = ModifierKeys.Control,
|
||||
|
||||
Action = (context) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(record.Path);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var message = "Fail to set text in clipboard";
|
||||
LogException(message, e);
|
||||
_context.API.ShowMsg(message);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
contextMenus.Add(new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_open_in_console"),
|
||||
Glyph = "\xE756",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.C,
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
|
||||
Action = (context) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (type == ResultType.File)
|
||||
{
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
|
||||
Action = (context) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (type == ResultType.File)
|
||||
{
|
||||
Helper.OpenInConsole(Path.GetDirectoryName(record.Path));
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
Helper.OpenInConsole(record.Path);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|Microsoft.Plugin.Indexer.ContextMenuLoader.LoadContextMenus| Failed to open {record.Path} in console, {e.Message}", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return contextMenus;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|Microsoft.Plugin.Indexer.ContextMenuLoader.LoadContextMenus| Failed to open {record.Path} in console, {e.Message}", e);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return contextMenus;
|
||||
}
|
||||
|
||||
// Function to add the context menu item to run as admin
|
||||
@@ -122,7 +126,7 @@ namespace Microsoft.Plugin.Indexer
|
||||
Glyph = "\xE7EF",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.Enter,
|
||||
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift),
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
Action = _ =>
|
||||
{
|
||||
try
|
||||
@@ -135,9 +139,8 @@ namespace Microsoft.Plugin.Indexer
|
||||
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
|
||||
@@ -151,43 +154,43 @@ namespace Microsoft.Plugin.Indexer
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log and show an error message")]
|
||||
private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record)
|
||||
{
|
||||
return new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"),
|
||||
Glyph = "\xE838",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.E,
|
||||
AcceleratorModifiers = (ModifierKeys.Control | ModifierKeys.Shift),
|
||||
Action = _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start("explorer.exe", $" /select,\"{record.Path}\"");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var message = $"Fail to open file at {record.Path}";
|
||||
LogException(message, e);
|
||||
_context.API.ShowMsg(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static void LogException(string message, Exception e)
|
||||
{
|
||||
Log.Exception($"|Microsoft.Plugin.Folder.ContextMenu|{message}", e);
|
||||
}
|
||||
private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record)
|
||||
{
|
||||
return new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"),
|
||||
Glyph = "\xE838",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.E,
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
Action = _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start("explorer.exe", $" /select,\"{record.Path}\"");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var message = $"Fail to open file at {record.Path}";
|
||||
LogException(message, e);
|
||||
_context.API.ShowMsg(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static void LogException(string message, Exception e)
|
||||
{
|
||||
Log.Exception($"|Microsoft.Plugin.Folder.ContextMenu|{message}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user