[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
12 changed files with 253 additions and 51 deletions

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,
}
}