2020-11-26 03:35:48 -06:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-09-07 00:18:14 +02:00
|
|
|
|
using System.Globalization;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
using System.Linq;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
using System.Windows;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Input;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
using ManagedCommon;
|
2021-09-07 00:18:14 +02:00
|
|
|
|
using Microsoft.PowerToys.Run.Plugin.System.Properties;
|
2021-01-08 16:11:30 +01:00
|
|
|
|
using Microsoft.PowerToys.Run.Plugin.System.Win32;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
using Wox.Infrastructure;
|
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
2021-01-08 16:11:30 +01:00
|
|
|
|
namespace Microsoft.PowerToys.Run.Plugin.System
|
2020-11-26 03:35:48 -06:00
|
|
|
|
{
|
2021-07-28 08:37:30 -05:00
|
|
|
|
public class Main : IPlugin, IPluginI18n, ISettingProvider
|
2020-11-26 03:35:48 -06:00
|
|
|
|
{
|
|
|
|
|
|
private PluginInitContext _context;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
private const string ConfirmSystemCommands = nameof(ConfirmSystemCommands);
|
2021-09-07 00:18:14 +02:00
|
|
|
|
private const string LocalizeSystemCommands = nameof(LocalizeSystemCommands);
|
2020-11-26 03:35:48 -06:00
|
|
|
|
|
|
|
|
|
|
internal const int EWXLOGOFF = 0x00000000;
|
|
|
|
|
|
internal const int EWXSHUTDOWN = 0x00000001;
|
|
|
|
|
|
internal const int EWXREBOOT = 0x00000002;
|
|
|
|
|
|
internal const int EWXFORCE = 0x00000004;
|
|
|
|
|
|
internal const int EWXPOWEROFF = 0x00000008;
|
|
|
|
|
|
internal const int EWXFORCEIFHUNG = 0x00000010;
|
|
|
|
|
|
|
|
|
|
|
|
public string IconTheme { get; set; }
|
|
|
|
|
|
|
2021-07-28 08:37:30 -05:00
|
|
|
|
public ICommand Command { get; set; }
|
|
|
|
|
|
|
2021-09-07 00:18:14 +02:00
|
|
|
|
public string Name => Resources.Microsoft_plugin_sys_plugin_name;
|
2021-02-26 13:21:58 +02:00
|
|
|
|
|
2021-09-07 00:18:14 +02:00
|
|
|
|
public string Description => Resources.Microsoft_plugin_sys_plugin_description;
|
2021-02-26 13:21:58 +02:00
|
|
|
|
|
2021-07-28 08:37:30 -05:00
|
|
|
|
private bool _confirmSystemCommands;
|
2021-09-07 00:18:14 +02:00
|
|
|
|
private bool _localizeSystemCommands;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
|
|
|
|
|
|
{
|
|
|
|
|
|
new PluginAdditionalOption()
|
|
|
|
|
|
{
|
|
|
|
|
|
Key = ConfirmSystemCommands,
|
2021-09-07 00:18:14 +02:00
|
|
|
|
DisplayLabel = Resources.confirm_system_commands,
|
2021-07-28 08:37:30 -05:00
|
|
|
|
Value = false,
|
|
|
|
|
|
},
|
2021-09-07 00:18:14 +02:00
|
|
|
|
new PluginAdditionalOption()
|
|
|
|
|
|
{
|
|
|
|
|
|
Key = LocalizeSystemCommands,
|
|
|
|
|
|
DisplayLabel = Resources.Use_localized_system_commands,
|
|
|
|
|
|
Value = true,
|
|
|
|
|
|
},
|
2021-07-28 08:37:30 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
2020-11-26 03:35:48 -06:00
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
|
{
|
2021-07-28 08:37:30 -05:00
|
|
|
|
_context = context;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
_context.API.ThemeChanged += OnThemeChanged;
|
|
|
|
|
|
UpdateIconTheme(_context.API.GetCurrentTheme());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (query == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException(paramName: nameof(query));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var commands = Commands();
|
|
|
|
|
|
var results = new List<Result>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var c in commands)
|
|
|
|
|
|
{
|
|
|
|
|
|
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
|
|
|
|
|
|
if (titleMatch.Score > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Score = titleMatch.Score;
|
2021-06-08 20:23:27 +02:00
|
|
|
|
c.TitleHighlightData = titleMatch.MatchData;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
results.Add(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<Result> Commands()
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
CultureInfo culture = CultureInfo.CurrentUICulture;
|
|
|
|
|
|
|
|
|
|
|
|
if (!_localizeSystemCommands)
|
|
|
|
|
|
{
|
|
|
|
|
|
culture = new CultureInfo("en-US");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-26 03:35:48 -06:00
|
|
|
|
var results = new List<Result>();
|
|
|
|
|
|
results.AddRange(new[]
|
|
|
|
|
|
{
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_shutdown_computer), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_shutdown_computer_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\shutdown.{IconTheme}.png",
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /t 0"));
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_restart_computer), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_restart_computer_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\restart.{IconTheme}.png",
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0"));
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sign_out), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sign_out_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\logoff.{IconTheme}.png",
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0));
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_lock), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_lock_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\lock.{IconTheme}.png",
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation());
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sleep), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sleep_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\sleep.{IconTheme}.png",
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true));
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_hibernate), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_hibernate_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\sleep.{IconTheme}.png", // Icon change needed
|
|
|
|
|
|
Action = c =>
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return ExecuteCommand(Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true));
|
2020-11-26 03:35:48 -06:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
new Result
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_emptyrecyclebin), culture),
|
|
|
|
|
|
SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_emptyrecyclebin_description), culture),
|
2020-11-26 03:35:48 -06:00
|
|
|
|
IcoPath = $"Images\\recyclebin.{IconTheme}.png",
|
|
|
|
|
|
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)NativeMethods.HRESULT.S_OK && result != 0x8000FFFF)
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
var name = "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
var message = $"Error emptying recycle bin, error code: {result}\n" +
|
|
|
|
|
|
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137";
|
|
|
|
|
|
_context.API.ShowMsg(name, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateIconTheme(Theme theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
|
|
|
|
|
{
|
|
|
|
|
|
IconTheme = "light";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
IconTheme = "dark";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnThemeChanged(Theme currentTheme, Theme newTheme)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateIconTheme(newTheme);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return Resources.Microsoft_plugin_sys_plugin_description;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
return Resources.Microsoft_plugin_sys_plugin_name;
|
2020-11-26 03:35:48 -06:00
|
|
|
|
}
|
2021-07-28 08:37:30 -05:00
|
|
|
|
|
|
|
|
|
|
private bool ExecuteCommand(string confirmationMessage, Action command)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_confirmSystemCommands)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBoxResult messageBoxResult = MessageBox.Show(
|
|
|
|
|
|
confirmationMessage,
|
2021-09-07 00:18:14 +02:00
|
|
|
|
Resources.Microsoft_plugin_sys_confirmation,
|
2021-07-28 08:37:30 -05:00
|
|
|
|
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;
|
2021-09-07 00:18:14 +02:00
|
|
|
|
var localizeSystemCommands = true;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
|
|
|
|
|
|
if (settings != null && settings.AdditionalOptions != null)
|
|
|
|
|
|
{
|
2021-09-07 00:18:14 +02:00
|
|
|
|
var optionConfirm = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ConfirmSystemCommands);
|
|
|
|
|
|
confirmSystemCommands = optionConfirm?.Value ?? false;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
|
2021-09-07 00:18:14 +02:00
|
|
|
|
var optionLocalize = settings.AdditionalOptions.FirstOrDefault(x => x.Key == LocalizeSystemCommands);
|
|
|
|
|
|
localizeSystemCommands = optionLocalize?.Value ?? true;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_confirmSystemCommands = confirmSystemCommands;
|
2021-09-07 00:18:14 +02:00
|
|
|
|
_localizeSystemCommands = localizeSystemCommands;
|
2021-07-28 08:37:30 -05:00
|
|
|
|
}
|
2020-11-26 03:35:48 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|