Files
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.System/Main.cs
Jaime Bernardo 864b862952 [General]Reduce installer size by flattening application paths (#27451)
* Flatten everything and succeed build

* Figure out Settings assets

* Remove UseCommonOutputDirectory tag

* Proper settings app path

* [VCM] Fix assets location

* Fix some runtime paths

* [RegistryPreview]Use MRTCore specific pri file

* [Hosts]Use MRTCore specific pri file

* [Settings]Use MRTCore specific pri file

* [Peek]Use MRTCore specific pri file

* [FileLocksmith]Use MRTCore specific pri file

* [ScreenRuler]Use MRTCore specific pri file

* [PowerRename]Use MRTCore specific pri file

* [Peek]Move assets to own folder

* [FileLocksmith] Use own Assets path

* [Hosts]Use own assets folder

* [PowerRename]Use own assets dir

* [MeasureTool] Use its own assets folder

* [ImageResizer]Use its own assets path

* Fix spellcheck

* Fix tab instead of space in project files

* Normalize target frameworks and platforms

* Remove WINRT_NO_MAKE_DETECTION flag. No longer needed?

* Fix AOT and Hosts locations

* Fix Dll version differences on dependency

* Add Common.UI.csproj as refernce to fix dll versions

* Update ControlzEx to normalize dll versions

* Update System.Management version to 7.0.2

* Add GPOWrapper to Registry Preview to fix dll versions

* [PTRun]Reference Microsoft.Extensions.Hosting to fix dll versions

* Fix remaining output paths / dll version conflicts

* [KeyboardManager]Executables still on their own directories

* Fix Monaco paths

* WinAppSDK apps get to play outside again

* Enable VCM settings again

* Fix KBM Editor path again

* [Monaco]Set to own Assets dir

* Fix installer preamble; Fix publish. remove unneeded publishes

* Remove Hardlink functions

* Installer builds again (still needs work to work)

* Readd Monaco to spellcheck excludes

* Fix spellcheck and call publish.cmd again

* [Installer] Remove components that don't need own dirs

* [Installer] Refactor Power Launcher

* [Installer] Refactor Color Picker

* [Installer] Refactor Monaco assets

* [Installer]Generate File script no longer needs to remove files

* [Installer]Refactor FileLocksmith

* [Installer] Refactor Hosts

* [Installer]Refactor Image Resizer

* [Installer]Refactor MouseUtils

* [Installer]Refactor MWB

* [Installer]Refactor MeasureTool

* [Installer]Refactor Peek

* [Installer]Refactor PowerRename and registry fixes

* [Installer]Refactor RegistryPreview

* [Installer]Refactor ShortcutGuide

* [Installer]Refactor Settings

* [Installer]Clean up some unused stuff

* [Installer]Clean up stuff for user install

* [Installer]Fix WinUi3Apps wxs

* [Installer]Fix misplaced folders

* [Installer]Move x86 VCM dll to right path

* Fix monaco language list location

* [Installer]Fix VCM directory reference

* [CI]Fix signing

* [Installer] Fix resources folder for release CI

* [ci]Looks like we still ship NLog on PowerToys Run

* [Settings]Add dependency to avoid dll collision with Experimentation

* [RegistryPreview]Move XAML files to own path

* [RegistryPreview]Fix app icon

* [Hosts]Move XAML files to their own path

* [FileLocksmith]Move XAML files to their own path

* [Peek]Move XAML files to own path

* [ScreenRuler]Move XAML files to its own path

* [Settings]Move XAML to its own path

* [ColorPicker]Move Resources to Assets

* [ShortcutGuide]Move svgs to own Assets path

* [Awake]Move images to assets path

* [Runner]Remove debug checks for PowerToys Run assets

* [PTRun]Move images to its own assets path

* [ImageResizer]Icon for context menu on own assets path

* [PowerRename]Move ico to its own path

* Remove unneeded intermediary directories

* Remove further int dirs

* Move tests to its own output path

* Fix spellcheck

* spellcheck: remove warnings

* [CppAnalyzers]Ignore rule in a tool

* [CI]Check if all deps.json files reference same versions

* fix spellcheck

* [ci]Fix task identation

* [ci]Add script to guard against asset conflicts

* [ci]Add more deps.json audit steps in the release build

* Add xbf to spellcheck expects

* Fix typo in asset conflict check scripts

* Fix some more dependency conflicts

* Downgrade CsWinRT to have the same dll version as sdk

* [ci]Do a recursive check for every deps.json

* Fix spellcheck error inside comment

* [ci]Fix asset script error

* [ci]Name deps.json verify tasks a bit better

* [ci]Improve deps json verify script output

* [ci]Update WinRT version to the same running in CI

* Also upgrade CsWinRT in NOTICE.MD

* [PowerRename]Move XAML files to own path

* [Common]Fix Settings executable path

* [ci]Verify there's no xbf files in app directories

* [installer]Fix firewall path

* [Monaco]Move new files to their proper assets path

* [Monaco]Fix paths for new files after merge

* [Feedback]Removed unneeded build conditions

* [Feedback]Clear vcxproj direct reference to frameworks

* [Feedback]RunPlugins name to hold PTRun plugins

* [Feedback]Remove unneeded foreach

* [Feedback]Shortcut Guide svgs with ** in project file

* [Feedback]Fix spellcheck
2023-07-20 00:12:46 +01:00

246 lines
10 KiB
C#

// 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;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.System.Components;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;
namespace Microsoft.PowerToys.Run.Plugin.System
{
public class Main : IPlugin, IPluginI18n, ISettingProvider, IContextMenu, IDelayedExecutionPlugin
{
private PluginInitContext _context;
private bool _confirmSystemCommands;
private bool _showSuccessOnEmptyRB;
private bool _localizeSystemCommands;
private bool _reduceNetworkResultScore;
private bool _separateEmptyRB;
public string Name => Resources.Microsoft_plugin_sys_plugin_name;
public string Description => Resources.Microsoft_plugin_sys_plugin_description;
public string IconTheme { get; set; }
public bool IsBootedInUefiMode { get; set; }
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = "ConfirmSystemCommands",
DisplayLabel = Resources.confirm_system_commands,
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,
Value = true,
},
new PluginAdditionalOption()
{
Key = "SeparateResultEmptyRB",
DisplayLabel = Resources.Microsoft_plugin_sys_RecycleBin_ShowEmptySeparate,
Value = false,
},
new PluginAdditionalOption()
{
Key = "ReduceNetworkResultScore",
DisplayLabel = Resources.Reduce_Network_Result_Score,
DisplayDescription = Resources.Reduce_Network_Result_Score_Description,
Value = true,
},
};
public void Init(PluginInitContext context)
{
_context = context;
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconTheme(_context.API.GetCurrentTheme());
IsBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi;
// Log info if the system hasn't boot in uefi mode.
// (Because this is only going into the log we can ignore the fact that normally UEFI and BIOS are written upper case. No need to convert the enumeration value to upper case.)
if (!IsBootedInUefiMode)
{
Wox.Plugin.Logger.Log.Info($"The UEFI command will not show to the user. The system has not booted in UEFI mode or the system does not have an UEFI firmware! (Detected type: {Win32Helpers.GetSystemFirmwareType()})", typeof(Main));
}
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
CultureInfo culture = _localizeSystemCommands ? CultureInfo.CurrentUICulture : new CultureInfo("en-US");
if (query == null)
{
return results;
}
// normal system commands are fast and can be returned immediately
var systemCommands = Commands.GetSystemCommands(IsBootedInUefiMode, _separateEmptyRB, _confirmSystemCommands, _showSuccessOnEmptyRB, IconTheme, culture);
foreach (var c in systemCommands)
{
var resultMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
if (resultMatch.Score > 0)
{
c.Score = resultMatch.Score;
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.
// On global queries the first word/part has to be 'ip', 'mac' or 'address' for network results
// string[] keywordList = Resources.ResourceManager.GetString("Microsoft_plugin_sys_Search_NetworkKeywordList", culture).Split("; ");
// if (!string.IsNullOrEmpty(query.ActionKeyword) || keywordList.Any(x => query.Search.StartsWith(x, StringComparison.CurrentCultureIgnoreCase)))
// {
// results.Add(new Result()
// {
// Title = "Getting network informations. Please wait ...",
// IcoPath = $"Images\\networkAdapter.{IconTheme}.png",
// Score = StringMatcher.FuzzySearch("address", "ip address").Score,
// });
// }
return results;
}
public List<Result> Query(Query query, bool delayedExecution)
{
List<Result> results = new List<Result>();
CultureInfo culture = _localizeSystemCommands ? CultureInfo.CurrentUICulture : new CultureInfo("en-US");
if (query == null)
{
return results;
}
// Network (ip and mac) results are slow with many network cards and returned delayed.
// On global queries the first word/part has to be 'ip', 'mac' or 'address' for network results
string[] keywordList = Resources.ResourceManager.GetString("Microsoft_plugin_sys_Search_NetworkKeywordList", culture).Split("; ");
if (!string.IsNullOrEmpty(query.ActionKeyword) || keywordList.Any(x => query.Search.StartsWith(x, StringComparison.CurrentCultureIgnoreCase)))
{
var networkConnectionResults = Commands.GetNetworkConnectionResults(IconTheme, culture);
foreach (var r in networkConnectionResults)
{
var resultMatch = StringMatcher.FuzzySearch(query.Search, r.SubTitle);
if (resultMatch.Score > 0)
{
r.Score = _reduceNetworkResultScore ? (int)(resultMatch.Score * 65 / 100) : resultMatch.Score; // Adjust score to improve user experience and priority order
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);
}
}
}
}
return results;
}
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
return ResultHelper.GetContextMenuForResult(selectedResult, _showSuccessOnEmptyRB);
}
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()
{
return Resources.Microsoft_plugin_sys_plugin_description;
}
public string GetTranslatedPluginTitle()
{
return Resources.Microsoft_plugin_sys_plugin_name;
}
public Control CreateSettingPanel()
{
throw new NotImplementedException();
}
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var confirmSystemCommands = false;
var showSuccessOnEmptyRB = false;
var localizeSystemCommands = true;
var reduceNetworkResultScore = true;
var separateEmptyRB = false;
if (settings != null && settings.AdditionalOptions != null)
{
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;
var optionNetworkScore = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ReduceNetworkResultScore");
reduceNetworkResultScore = optionNetworkScore?.Value ?? reduceNetworkResultScore;
var optionSeparateEmptyRB = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "SeparateResultEmptyRB");
separateEmptyRB = optionSeparateEmptyRB?.Value ?? separateEmptyRB;
}
_confirmSystemCommands = confirmSystemCommands;
_showSuccessOnEmptyRB = showSuccessOnEmptyRB;
_localizeSystemCommands = localizeSystemCommands;
_reduceNetworkResultScore = reduceNetworkResultScore;
_separateEmptyRB = separateEmptyRB;
}
}
}