[PTRun][System] Recycle Bin command: Allow opening RB + Improvements (#23045)

This commit is contained in:
Heiko 2023-01-10 15:09:23 +01:00 committed by GitHub
parent 25e9241d42
commit 196db19be8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 253 additions and 51 deletions

View File

@ -475,7 +475,6 @@ EFile
ekus
emmintrin
Emoji
emptyrecyclebin
ENABLEDELAYEDEXPANSION
enabledisable
ENABLEDPOPUP
@ -1610,6 +1609,7 @@ shellex
SHELLEXECUTEINFO
SHELLEXECUTEINFOW
shellscalingapi
shemptyrecyclebina
SHFILEINFO
SHGFI
Shl

View File

@ -13,7 +13,7 @@ Available commands:
* Lock
* Sleep
* Hibernate
* Empty Recycle Bin
* Open / Empty Recycle Bin
* UEFI Firmware Settings (Only available on systems, that boot in UEFI mode.)
* IP / MAC / Address => Show informations about network connections.
@ -42,6 +42,7 @@ Available commands:
### [`ResultHelper.cs`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Components/ResultHelper.cs)
- The [`ResultHelper`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Components/ResultHelper.cs) class contains methods for working with the results and some of the result features (tool tip, copy to clipboard, execute command).
- **Recycle Bin command:** The context menu action to empty the Recycle Bin is executed as an async task to not block PowerToys Run. (While the task is running the static class variable `executingEmptyRecycleBinTask` is set to true, to block multiple executions at the same time)
### [`NetworkConnectionProperties.cs`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Components/NetworkConnectionProperties.cs)
- The [`NetworkConnectionProperties`](/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Components/NetworkConnectionProperties.cs) class contains methods to get the properties of a network interface/connection.

View File

@ -26,7 +26,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests
[DataRow("lock", "Images\\lock.dark.png")]
[DataRow("sleep", "Images\\sleep.dark.png")]
[DataRow("hibernate", "Images\\sleep.dark.png")]
[DataRow("empty recycle", "Images\\recyclebin.dark.png")]
[DataRow("recycle bin", "Images\\recyclebin.dark.png")]
[DataRow("uefi firmware settings", "Images\\firmwareSettings.dark.png")]
[DataRow("ip v4 addr", "Images\\networkAdapter.dark.png", true)]
[DataRow("ip v6 addr", "Images\\networkAdapter.dark.png", true)]
@ -53,7 +53,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests
[DataRow("lock", "Images\\lock.light.png")]
[DataRow("sleep", "Images\\sleep.light.png")]
[DataRow("hibernate", "Images\\sleep.light.png")]
[DataRow("empty recycle", "Images\\recyclebin.light.png")]
[DataRow("recycle bin", "Images\\recyclebin.light.png")]
[DataRow("uefi firmware settings", "Images\\firmwareSettings.light.png")]
[DataRow("ipv4 addr", "Images\\networkAdapter.light.png", true)]
[DataRow("ipv6 addr", "Images\\networkAdapter.light.png", true)]

View File

@ -27,7 +27,8 @@ namespace Microsoft.PowerToys.Run.Plugin.System.UnitTests
[DataRow("lock", "Lock computer")]
[DataRow("sleep", "Put computer to sleep")]
[DataRow("hibernate", "Hibernate computer")]
[DataRow("empty recycle", "Empty Recycle Bin")]
[DataRow("recycle b", "Open the Recycle Bin")]
[DataRow("empty recycle", "Open the Recycle Bin")]
public void EnvironmentIndependentQueryResults(string typedString, string expectedResult)
{
// Setup

View File

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime;
using System.Windows;
using System.Windows.Interop;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
@ -107,25 +108,13 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Components
},
new Result
{
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin_description", culture),
Title = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin", culture),
SubTitle = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_description", culture),
IcoPath = $"Images\\recyclebin.{iconTheme}.png",
ContextData = new SystemPluginContext { Type = ResultContextType.RecycleBinCommand, SearchTag = Resources.ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_searchTag", culture) },
Action = c =>
{
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html
// FYI, couldn't find documentation for this but if the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
// 0 for nothing
var result = NativeMethods.SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint)HRESULT.S_OK && result != 0x8000FFFF)
{
var name = "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name;
var message = $"Error emptying recycle bin, error code: {result}\n" +
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137";
Log.Error(message, typeof(Commands));
_ = MessageBox.Show(message, name);
}
return true;
return Helper.OpenInShell("explorer.exe", "shell:RecycleBinFolder");
},
},
});

View File

@ -4,16 +4,21 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;
using Wox.Plugin.Logger;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
internal static class ResultHelper
{
private static bool executingEmptyRecycleBinTask;
internal static bool ExecuteCommand(bool confirm, string confirmationMessage, Action command)
{
if (confirm)
@ -49,7 +54,18 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Components
}
}
internal static List<ContextMenuResult> GetContextMenuForResult(Result result)
internal static async void EmptyRecycleBinAsync(bool settingEmptyRBSuccesMsg)
{
if (executingEmptyRecycleBinTask)
{
_ = MessageBox.Show(Resources.Microsoft_plugin_sys_RecycleBin_EmptyTaskRunning, "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name, MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
await Task.Run(() => EmptyRecycleBinTask(settingEmptyRBSuccesMsg));
}
internal static List<ContextMenuResult> GetContextMenuForResult(Result result, bool settingEmptyRBSuccesMsg)
{
var contextMenu = new List<ContextMenuResult>();
@ -67,11 +83,62 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Components
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE8C8", // E8C8 => Symbol: Copy
Title = Resources.Microsoft_plugin_sys_CopyDetails,
Action = _ => ResultHelper.CopyToClipBoard(contextData.Data),
Action = _ => CopyToClipBoard(contextData.Data),
});
}
if (contextData.Type == ResultContextType.RecycleBinCommand)
{
contextMenu.Add(new ContextMenuResult()
{
AcceleratorKey = Key.Delete,
AcceleratorModifiers = ModifierKeys.Shift, // Shift+Delete is the common key for deleting without recycle bin
FontFamily = "Segoe MDL2 Assets",
Glyph = "\xE74D", // E74D => Symbol: Delete
Title = Resources.Microsoft_plugin_sys_RecycleBin_contextMenu,
Action = _ =>
{
EmptyRecycleBinAsync(settingEmptyRBSuccesMsg);
return true;
},
});
}
return contextMenu;
}
/// <summary>
/// Method to process the empty recycle bin command in a separate task
/// </summary>
private static void EmptyRecycleBinTask(bool settingEmptyRBSuccesMsg)
{
executingEmptyRecycleBinTask = true;
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shemptyrecyclebina/
// http://www.pinvoke.net/default.aspx/shell32/SHEmptyRecycleBin.html/
// If the recycle bin is already empty, it will return -2147418113 (0x8000FFFF (E_UNEXPECTED))
// If the user canceled the deletion task it will return 2147943623 (0x800704C7 (E_CANCELLED - The operation was canceled by the user.))
// On success it will return 0 (S_OK)
var result = NativeMethods.SHEmptyRecycleBin(IntPtr.Zero, 0);
if (result == (uint)HRESULT.E_UNEXPECTED)
{
_ = MessageBox.Show(Resources.Microsoft_plugin_sys_RecycleBin_IsEmpty, "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name, MessageBoxButton.OK, MessageBoxImage.Information);
}
else if (result != (uint)HRESULT.S_OK && result != (uint)HRESULT.E_CANCELLED)
{
var errorDesc = Win32Helpers.MessageFromHResult((int)result);
var name = "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name;
var message = $"{Resources.Microsoft_plugin_sys_RecycleBin_ErrorMsg} {errorDesc}";
Log.Error(message + " - Please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137 for more information.", typeof(Commands));
_ = MessageBox.Show(message, name, MessageBoxButton.OK, MessageBoxImage.Error);
}
if (result == (uint)HRESULT.S_OK && settingEmptyRBSuccesMsg)
{
_ = MessageBox.Show(Resources.Microsoft_plugin_sys_RecycleBin_EmptySuccessMessage, "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name, MessageBoxButton.OK, MessageBoxImage.Information);
}
executingEmptyRecycleBinTask = false;
}
}
}

View File

@ -14,12 +14,18 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Components
/// <summary>
/// Gets or sets the context data for the command/results
/// </summary>
public string Data { get; set; }
public string Data { get; set; } = string.Empty;
/// <summary>
/// Gets or sets an additional result name for searching
/// </summary>
public string SearchTag { get; set; } = string.Empty;
}
internal enum ResultContextType
{
Command, // Reserved for later usage
NetworkAdapterInfo,
RecycleBinCommand,
}
}

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using ControlzEx.Standard;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.System.Components;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
@ -22,6 +23,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
private PluginInitContext _context;
private bool _confirmSystemCommands;
private bool _showSuccessOnEmptyRB;
private bool _localizeSystemCommands;
private bool _reduceNetworkResultScore;
@ -42,6 +44,12 @@ namespace Microsoft.PowerToys.Run.Plugin.System
Value = false,
},
new PluginAdditionalOption()
{
Key = "ShowSuccessOnEmptyRB",
DisplayLabel = Resources.Microsoft_plugin_sys_RecycleBin_ShowEmptySuccessMessage,
Value = false,
},
new PluginAdditionalOption()
{
Key = "LocalizeSystemCommands",
DisplayLabel = Resources.Use_localized_system_commands,
@ -92,6 +100,15 @@ namespace Microsoft.PowerToys.Run.Plugin.System
c.TitleHighlightData = resultMatch.MatchData;
results.Add(c);
}
else if (c?.ContextData is SystemPluginContext contextData)
{
var searchTagMatch = StringMatcher.FuzzySearch(query.Search, contextData.SearchTag);
if (searchTagMatch.Score > 0)
{
c.Score = resultMatch.Score;
results.Add(c);
}
}
}
// The following information result is not returned because delayed queries doesn't clear output if no results are available.
@ -134,6 +151,15 @@ namespace Microsoft.PowerToys.Run.Plugin.System
r.SubTitleHighlightData = resultMatch.MatchData;
results.Add(r);
}
else if (r?.ContextData is SystemPluginContext contextData)
{
var searchTagMatch = StringMatcher.FuzzySearch(query.Search, contextData.SearchTag);
if (searchTagMatch.Score > 0)
{
r.Score = resultMatch.Score;
results.Add(r);
}
}
}
}
@ -142,7 +168,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
return ResultHelper.GetContextMenuForResult(selectedResult);
return ResultHelper.GetContextMenuForResult(selectedResult, _showSuccessOnEmptyRB);
}
private void UpdateIconTheme(Theme theme)
@ -180,6 +206,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var confirmSystemCommands = false;
var showSuccessOnEmptyRB = false;
var localizeSystemCommands = true;
var reduceNetworkResultScore = true;
@ -188,6 +215,9 @@ namespace Microsoft.PowerToys.Run.Plugin.System
var optionConfirm = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ConfirmSystemCommands");
confirmSystemCommands = optionConfirm?.Value ?? confirmSystemCommands;
var optionEmptyRBSuccessMsg = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ShowSuccessOnEmptyRB");
showSuccessOnEmptyRB = optionEmptyRBSuccessMsg?.Value ?? showSuccessOnEmptyRB;
var optionLocalize = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "LocalizeSystemCommands");
localizeSystemCommands = optionLocalize?.Value ?? localizeSystemCommands;
@ -196,6 +226,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
}
_confirmSystemCommands = confirmSystemCommands;
_showSuccessOnEmptyRB = showSuccessOnEmptyRB;
_localizeSystemCommands = localizeSystemCommands;
_reduceNetworkResultScore = reduceNetworkResultScore;
}

View File

@ -168,24 +168,6 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Empty Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_emptyrecyclebin {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_emptyrecyclebin_description {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_emptyrecyclebin_description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Default Gateway.
/// </summary>
@ -402,6 +384,87 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty Recycle Bin (Shift+Delete).
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_contextMenu {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_contextMenu", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Open the Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_description {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Recycle Bin emptied successfully..
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_EmptySuccessMessage {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_EmptySuccessMessage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The task to empty the Recycle Bin is already running..
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_EmptyTaskRunning {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_EmptyTaskRunning", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Failed to empty the Recycle Bin:.
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_ErrorMsg {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_ErrorMsg", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Recycle Bin is empty..
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_IsEmpty {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_IsEmpty", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Empty Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_searchTag {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_searchTag", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Show a success message after empty the Recycle Bin.
/// </summary>
internal static string Microsoft_plugin_sys_RecycleBin_ShowEmptySuccessMessage {
get {
return ResourceManager.GetString("Microsoft_plugin_sys_RecycleBin_ShowEmptySuccessMessage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Restart.
/// </summary>

View File

@ -155,14 +155,6 @@
<data name="Microsoft_plugin_sys_Dns" xml:space="preserve">
<value>DNS servers</value>
</data>
<data name="Microsoft_plugin_sys_emptyrecyclebin" xml:space="preserve">
<value>Empty Recycle Bin</value>
<comment>This should align to the action in Windows of emptying the recycle bin on your computer.</comment>
</data>
<data name="Microsoft_plugin_sys_emptyrecyclebin_description" xml:space="preserve">
<value>Empty Recycle Bin</value>
<comment>This should align to the action in Windows of emptying the recycle bin on your computer.</comment>
</data>
<data name="Microsoft_plugin_sys_Gateways" xml:space="preserve">
<value>Default Gateway</value>
</data>
@ -248,6 +240,41 @@
<value>Windows System Commands</value>
<comment>Windows operating system commands.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin" xml:space="preserve">
<value>Recycle Bin</value>
<comment>Means the recycle bin folder in Explorer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_contextMenu" xml:space="preserve">
<value>Empty Recycle Bin (Shift+Delete)</value>
<comment>This should align to the action in Windows of emptying the recycle bin on your computer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_description" xml:space="preserve">
<value>Open the Recycle Bin</value>
<comment>This should align to the action in Windows of emptying the recycle bin on your computer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_EmptySuccessMessage" xml:space="preserve">
<value>Recycle Bin emptied successfully.</value>
<comment>Means the recycle bin folder in Explorer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_EmptyTaskRunning" xml:space="preserve">
<value>The task to empty the Recycle Bin is already running.</value>
<comment>Means the recycle bin folder in Explorer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_ErrorMsg" xml:space="preserve">
<value>Failed to empty the Recycle Bin:</value>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_IsEmpty" xml:space="preserve">
<value>Recycle Bin is empty.</value>
<comment>Means the recycle bin folder in Explorer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_searchTag" xml:space="preserve">
<value>Empty Recycle Bin</value>
<comment>This should align to the action in Windows of emptying the recycle bin on your computer.</comment>
</data>
<data name="Microsoft_plugin_sys_RecycleBin_ShowEmptySuccessMessage" xml:space="preserve">
<value>Show a success message after emptying the Recycle Bin</value>
<comment>Means the recycle bin folder in Explorer and "emptying" refers to "Empty Recycle Bin" command.</comment>
</data>
<data name="Microsoft_plugin_sys_restart_computer" xml:space="preserve">
<value>Restart</value>
<comment>This should align to the action in Windows of a restarting your computer.</comment>

View File

@ -202,6 +202,13 @@ namespace Wox.Plugin.Common.Win32
/// One or more arguments are not valid.
/// </summary>
E_INVALIDARG = 0x80070057,
/// <summary>
/// The operation was canceled by the user. (Error source 7 means Win32.)
/// </summary>
/// <SeeAlso href="https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--1000-1299-"/>
/// <SeeAlso href="https://en.wikipedia.org/wiki/HRESULT"/>
E_CANCELLED = 0x800704C7,
}
/// <remarks>

View File

@ -44,5 +44,15 @@ namespace Wox.Plugin.Common.Win32
return NativeMethods.CloseHandle(handle);
}
/// <summary>
/// Gets the description for an HRESULT error code.
/// </summary>
/// <param name="hr">The HRESULT number</param>
/// <returns>A string containing the description.</returns>
public static string MessageFromHResult(int hr)
{
return Marshal.GetExceptionForHR(hr).Message;
}
}
}