[PT Run] Localized file paths (Part 1): Update helper class and Program plugin (#20024)

* make helper non-static and add cache

* uwp app: add localized path

* Win32Program: Rename variable

* spell fix

* Win32Program: Localized paths

* fix invalid var name

* spell fix

* fix build

* test new shell localization helper

* fixes

* fix crash

* replace old helper class

* replace old helper class 2

* Helper improvements

* last changes

* add docs info

* remove left-over

* remove second left-over
This commit is contained in:
Heiko
2023-02-13 17:30:18 +01:00
committed by GitHub
parent 4c3b470ec0
commit 6e4a2898ab
11 changed files with 178 additions and 95 deletions

View File

@@ -13,6 +13,7 @@ using Microsoft.Plugin.Program.Programs;
using Microsoft.Plugin.Program.Storage;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Common;
using Stopwatch = Wox.Infrastructure.Stopwatch;
namespace Microsoft.Plugin.Program
@@ -30,6 +31,8 @@ namespace Microsoft.Plugin.Program
internal static ProgramPluginSettings Settings { get; set; }
internal static readonly ShellLocalization ShellLocalizationHelper = new();
public string Name => Properties.Resources.wox_plugin_program_plugin_name;
public string Description => Properties.Resources.wox_plugin_program_plugin_description;

View File

@@ -36,6 +36,9 @@ namespace Microsoft.Plugin.Program.Programs
public string Location { get; set; }
// Localized path based on windows display language
public string LocationLocalized { get; set; }
public IList<UWPApplication> Apps { get; private set; }
public PackageVersion Version { get; set; }
@@ -57,6 +60,7 @@ namespace Microsoft.Plugin.Program.Programs
public void InitializeAppInfo(string installedLocation)
{
Location = installedLocation;
LocationLocalized = Main.ShellLocalizationHelper.GetLocalizedPath(installedLocation);
var path = Path.Combine(installedLocation, "AppxManifest.xml");
var namespaces = XmlNamespaces(path);

View File

@@ -55,6 +55,9 @@ namespace Microsoft.Plugin.Program.Programs
public string Location => Package.Location;
// Localized path based on windows display language
public string LocationLocalized => Package.LocationLocalized;
public bool Enabled { get; set; }
public bool CanRunElevated { get; set; }
@@ -119,7 +122,7 @@ namespace Microsoft.Plugin.Program.Programs
// Using CurrentCulture since this is user facing
var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, Package.Location);
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, LocationLocalized);
result.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
return result;

View File

@@ -20,7 +20,6 @@ using Microsoft.Win32;
using Wox.Infrastructure;
using Wox.Infrastructure.FileSystemHelper;
using Wox.Plugin;
using Wox.Plugin.Common;
using Wox.Plugin.Logger;
using DirectoryWrapper = Wox.Infrastructure.FileSystemHelper.DirectoryWrapper;
@@ -38,24 +37,35 @@ namespace Microsoft.Plugin.Program.Programs
public string Name { get; set; }
// Localized name based on windows display language
public string NameLocalized { get; set; } = string.Empty;
public string UniqueIdentifier { get; set; }
public string IcoPath { get; set; }
public string Description { get; set; } = string.Empty;
// Path of app executable or lnk target executable
public string FullPath { get; set; }
public string LnkResolvedPath { get; set; }
public string LnkResolvedExecutableName { get; set; }
// Localized name based on windows display language
public string LocalizedName { get; set; } = string.Empty;
// Localized path based on windows display language
public string FullPathLocalized { get; set; } = string.Empty;
public string ParentDirectory { get; set; }
public string ExecutableName { get; set; }
public string Description { get; set; } = string.Empty;
// Localized executable name based on windows display language
public string ExecutableNameLocalized { get; set; } = string.Empty;
// Path to the lnk file on LnkProgram
public string LnkFilePath { get; set; }
public string LnkResolvedExecutableName { get; set; }
// Localized path based on windows display language
public string LnkResolvedExecutableNameLocalized { get; set; } = string.Empty;
public bool Valid { get; set; }
@@ -102,11 +112,13 @@ namespace Microsoft.Plugin.Program.Programs
private int Score(string query)
{
var nameMatch = StringMatcher.FuzzySearch(query, Name);
var locNameMatch = StringMatcher.FuzzySearch(query, LocalizedName);
var locNameMatch = StringMatcher.FuzzySearch(query, NameLocalized);
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
var executableNameMatch = StringMatcher.FuzzySearch(query, ExecutableName);
var locExecutableNameMatch = StringMatcher.FuzzySearch(query, ExecutableNameLocalized);
var lnkResolvedExecutableNameMatch = StringMatcher.FuzzySearch(query, LnkResolvedExecutableName);
var score = new[] { nameMatch.Score, locNameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, lnkResolvedExecutableNameMatch.Score }.Max();
var locLnkResolvedExecutableNameMatch = StringMatcher.FuzzySearch(query, LnkResolvedExecutableNameLocalized);
var score = new[] { nameMatch.Score, locNameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, locExecutableNameMatch.Score, lnkResolvedExecutableNameMatch.Score, locLnkResolvedExecutableNameMatch.Score }.Max();
return score;
}
@@ -227,7 +239,7 @@ namespace Microsoft.Plugin.Program.Programs
var result = new Result
{
// To set the title for the result to always be the name of the application
Title = !string.IsNullOrEmpty(LocalizedName) ? LocalizedName : Name,
Title = !string.IsNullOrEmpty(NameLocalized) ? NameLocalized : Name,
SubTitle = GetSubtitle(),
IcoPath = IcoPath,
Score = score,
@@ -247,7 +259,8 @@ namespace Microsoft.Plugin.Program.Programs
// Using CurrentCulture since this is user facing
var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, FullPath);
string filePath = !string.IsNullOrEmpty(FullPathLocalized) ? FullPathLocalized : FullPath;
var toolTipText = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_path, filePath);
result.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
return result;
@@ -346,7 +359,7 @@ namespace Microsoft.Plugin.Program.Programs
{
return new ProcessStartInfo
{
FileName = LnkResolvedPath ?? FullPath,
FileName = LnkFilePath ?? FullPath,
WorkingDirectory = ParentDirectory,
UseShellExecute = true,
Arguments = programArguments,
@@ -376,17 +389,19 @@ namespace Microsoft.Plugin.Program.Programs
ExecutableName = Path.GetFileName(path),
IcoPath = path,
// Localized name based on windows display language
LocalizedName = ShellLocalization.GetLocalizedName(path),
// Using InvariantCulture since this is user facing
FullPath = path.ToLowerInvariant(),
FullPath = path,
UniqueIdentifier = path,
ParentDirectory = Directory.GetParent(path).FullName,
Description = string.Empty,
Valid = true,
Enabled = true,
AppType = ApplicationType.Win32Application,
// Localized name, path and executable based on windows display language
NameLocalized = Main.ShellLocalizationHelper.GetLocalizedName(path),
FullPathLocalized = Main.ShellLocalizationHelper.GetLocalizedPath(path),
ExecutableNameLocalized = Path.GetFileName(Main.ShellLocalizationHelper.GetLocalizedPath(path)),
};
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
@@ -462,7 +477,7 @@ namespace Microsoft.Plugin.Program.Programs
Name = Path.GetFileNameWithoutExtension(path),
ExecutableName = Path.GetFileName(path),
IcoPath = iconPath,
FullPath = urlPath.ToLowerInvariant(),
FullPath = urlPath,
UniqueIdentifier = path,
ParentDirectory = Directory.GetParent(path).FullName,
Valid = true,
@@ -500,11 +515,13 @@ namespace Microsoft.Plugin.Program.Programs
return InvalidProgram;
}
program.LnkResolvedPath = program.FullPath;
program.LnkFilePath = program.FullPath;
program.LnkResolvedExecutableName = Path.GetFileName(target);
program.LnkResolvedExecutableNameLocalized = Path.GetFileName(Main.ShellLocalizationHelper.GetLocalizedPath(target));
// Using CurrentCulture since this is user facing
program.FullPath = Path.GetFullPath(target).ToLowerInvariant();
program.FullPath = Path.GetFullPath(target);
program.FullPathLocalized = Main.ShellLocalizationHelper.GetLocalizedPath(target);
program.Arguments = ShellLinkHelper.Arguments;

View File

@@ -150,7 +150,7 @@ namespace Microsoft.Plugin.Program.Storage
// Using OrdinalIgnoreCase since this is used internally
if (extension.Equals(LnkExtension, StringComparison.OrdinalIgnoreCase))
{
app = GetAppWithSameLnkResolvedPath(path);
app = GetAppWithSameLnkFilePath(path);
if (app == null)
{
// Cancelled links won't have a resolved path.
@@ -195,12 +195,12 @@ namespace Microsoft.Plugin.Program.Storage
// To mitigate the issue faced (as stated above) when a shortcut application is renamed, the Exe FullPath and executable name must be obtained.
// Unlike the rename event args, since we do not have a newPath, we iterate through all the programs and find the one with the same LnkResolved path.
private Programs.Win32Program GetAppWithSameLnkResolvedPath(string lnkResolvedPath)
private Programs.Win32Program GetAppWithSameLnkFilePath(string lnkFilePath)
{
foreach (Programs.Win32Program app in Items)
{
// Using Invariant / OrdinalIgnoreCase since we're comparing paths
if (lnkResolvedPath.ToUpperInvariant().Equals(app.LnkResolvedPath, StringComparison.OrdinalIgnoreCase))
if (lnkFilePath.ToUpperInvariant().Equals(app.LnkFilePath, StringComparison.OrdinalIgnoreCase))
{
return app;
}