[Powertoys Run] Add setting to confirm system commands (#12427)

* HTTPS by default, HTTP only if specified

* Added/Updated unit tests;Added FTPS

* Added confirmation to system messages such as shutdown, reboot, and lock

* Corrected Typo

* Added confirmation to system messages such as shutdown, reboot, and lock

* Corrected Typo

* Made changes requested by @mykhailopylyp

* Further changes per review by mykhailopylyp

* Fixes per code review
This commit is contained in:
Chris
2021-07-28 08:37:30 -05:00
committed by GitHub
parent 9731cdee67
commit 04636339ce
5 changed files with 180 additions and 24 deletions

View File

@@ -4,18 +4,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.System.Win32;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.System
{
public class Main : IPlugin, IPluginI18n
public class Main : IPlugin, IPluginI18n, ISettingProvider
{
private PluginInitContext _context;
private const string ConfirmSystemCommands = nameof(ConfirmSystemCommands);
internal const int EWXLOGOFF = 0x00000000;
internal const int EWXSHUTDOWN = 0x00000001;
@@ -26,13 +33,27 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public string IconTheme { get; set; }
public ICommand Command { get; set; }
public string Name => Properties.Resources.Microsoft_plugin_sys_plugin_name;
public string Description => Properties.Resources.Microsoft_plugin_sys_plugin_description;
private bool _confirmSystemCommands;
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = ConfirmSystemCommands,
DisplayLabel = Properties.Resources.confirm_system_commands,
Value = false,
},
};
public void Init(PluginInitContext context)
{
this._context = context;
_context = context;
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconTheme(_context.API.GetCurrentTheme());
}
@@ -73,8 +94,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\shutdown.{IconTheme}.png",
Action = c =>
{
Helper.OpenInShell("shutdown", "/s /t 0");
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /t 0"));
},
},
new Result
@@ -84,8 +104,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\restart.{IconTheme}.png",
Action = c =>
{
Helper.OpenInShell("shutdown", "/r /t 0");
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0"));
},
},
new Result
@@ -95,8 +114,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\logoff.{IconTheme}.png",
Action = c =>
{
NativeMethods.ExitWindowsEx(EWXLOGOFF, 0);
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0));
},
},
new Result
@@ -106,8 +124,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\lock.{IconTheme}.png",
Action = c =>
{
NativeMethods.LockWorkStation();
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation());
},
},
new Result
@@ -117,8 +134,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\sleep.{IconTheme}.png",
Action = c =>
{
NativeMethods.SetSuspendState(false, true, true);
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true));
},
},
new Result
@@ -128,8 +144,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
IcoPath = $"Images\\sleep.{IconTheme}.png", // Icon change needed
Action = c =>
{
NativeMethods.SetSuspendState(true, true, true);
return true;
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true));
},
},
new Result
@@ -184,5 +199,44 @@ namespace Microsoft.PowerToys.Run.Plugin.System
{
return Properties.Resources.Microsoft_plugin_sys_plugin_name;
}
private bool ExecuteCommand(string confirmationMessage, Action command)
{
if (_confirmSystemCommands)
{
MessageBoxResult messageBoxResult = MessageBox.Show(
confirmationMessage,
Properties.Resources.Microsoft_plugin_sys_confirmation,
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (messageBoxResult == MessageBoxResult.No)
{
return false;
}
}
command();
return true;
}
public Control CreateSettingPanel()
{
throw new NotImplementedException();
}
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var confirmSystemCommands = false;
if (settings != null && settings.AdditionalOptions != null)
{
var option = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ConfirmSystemCommands);
confirmSystemCommands = option == null ? false : option.Value;
}
_confirmSystemCommands = confirmSystemCommands;
}
}
}