[PT Run] [Program plugin] Add localized name (#19149)

* create common localization class

* add loc name to prog plugin

* fixes

* Tool tip fixes and comments

* cleanup and highlight fix

* change

* Improvements

* Add GetLocalizedPath()

* smal code improvements
This commit is contained in:
Heiko 2022-07-20 16:11:33 +02:00 committed by GitHub
parent 28751d2d36
commit e11bafcc93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 3 deletions

View File

@ -460,6 +460,7 @@ DNLEN
Dns
doctype
DONOTROUND
DONTRESOLVEDLLREFERENCES
DONTVALIDATEPATH
dotnet
DPICHANGED
@ -1081,6 +1082,7 @@ lmcons
LMEM
LMENU
lnk
LOADLIBRARYASDATAFILE
LOADSTRING
LOBYTE
LOCALAPPDATA

View File

@ -19,6 +19,7 @@ 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;
@ -46,6 +47,9 @@ namespace Microsoft.Plugin.Program.Programs
public string LnkResolvedExecutableName { get; set; }
// Localized name based on windows display language
public string LocalizedName { get; set; } = string.Empty;
public string ParentDirectory { get; set; }
public string ExecutableName { get; set; }
@ -97,10 +101,11 @@ namespace Microsoft.Plugin.Program.Programs
private int Score(string query)
{
var nameMatch = StringMatcher.FuzzySearch(query, Name);
var locNameMatch = StringMatcher.FuzzySearch(query, LocalizedName);
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
var executableNameMatch = StringMatcher.FuzzySearch(query, ExecutableName);
var lnkResolvedExecutableNameMatch = StringMatcher.FuzzySearch(query, LnkResolvedExecutableName);
var score = new[] { nameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, lnkResolvedExecutableNameMatch.Score }.Max();
var score = new[] { nameMatch.Score, locNameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, lnkResolvedExecutableNameMatch.Score }.Max();
return score;
}
@ -221,7 +226,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 = Name,
Title = !string.IsNullOrEmpty(LocalizedName) ? LocalizedName : Name,
SubTitle = GetSubtitle(),
IcoPath = IcoPath,
Score = score,
@ -237,7 +242,7 @@ namespace Microsoft.Plugin.Program.Programs
},
};
result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, result.Title).MatchData;
// 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);
@ -370,6 +375,9 @@ 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(),
UniqueIdentifier = path,

View File

@ -0,0 +1,93 @@
// 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.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Wox.Plugin.Common
{
/// <summary>
/// Class to get localized name of shell items like 'My computer'. The localization is based on the 'windows display language'.
/// Reused code from https://stackoverflow.com/questions/41423491/how-to-get-localized-name-of-known-folder for the method <see cref="GetLocalizedName"/>
/// </summary>
public static class ShellLocalization
{
internal const uint DONTRESOLVEDLLREFERENCES = 0x00000001;
internal const uint LOADLIBRARYASDATAFILE = 0x00000002;
[DllImport("shell32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
internal static extern int SHGetLocalizedName(string pszPath, StringBuilder pszResModule, ref int cch, out int pidsRes);
[DllImport("user32.dll", EntryPoint = "LoadStringW", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
internal static extern int LoadString(IntPtr hModule, int resourceID, StringBuilder resourceValue, int len);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "LoadLibraryExW")]
internal static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern int FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", EntryPoint = "ExpandEnvironmentStringsW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern uint ExpandEnvironmentStrings(string lpSrc, StringBuilder lpDst, int nSize);
/// <summary>
/// Returns the localized name of a shell item.
/// </summary>
/// <param name="path">Path to the shell item (e. g. shortcut 'File Explorer.lnk').</param>
/// <returns>The localized name as string or <see cref="string.Empty"/>.</returns>
public static string GetLocalizedName(string path)
{
StringBuilder resourcePath = new StringBuilder(1024);
StringBuilder localizedName = new StringBuilder(1024);
int len, id;
len = resourcePath.Capacity;
// If there is no resource to localize a file name the method returns a non zero value.
if (SHGetLocalizedName(path, resourcePath, ref len, out id) == 0)
{
_ = ExpandEnvironmentStrings(resourcePath.ToString(), resourcePath, resourcePath.Capacity);
IntPtr hMod = LoadLibraryEx(resourcePath.ToString(), IntPtr.Zero, DONTRESOLVEDLLREFERENCES | LOADLIBRARYASDATAFILE);
if (hMod != IntPtr.Zero)
{
if (LoadString(hMod, id, localizedName, localizedName.Capacity) != 0)
{
string lString = localizedName.ToString();
_ = FreeLibrary(hMod);
return lString;
}
_ = FreeLibrary(hMod);
}
}
return string.Empty;
}
/// <summary>
/// This method returns the localized path to a shell item (folder or file)
/// </summary>
/// <param name="path">The path to localize</param>
/// <returns>The localized path or the original path if localized version is not available</returns>
public static string GetLocalizedPath(string path)
{
path = Environment.ExpandEnvironmentVariables(path);
string ext = Path.GetExtension(path);
var pathParts = path.Split("\\");
string[] locPath = new string[pathParts.Length];
for (int i = 0; i < pathParts.Length; i++)
{
int iElements = i + 1;
string lName = GetLocalizedName(string.Join("\\", pathParts[..iElements]));
locPath[i] = !string.IsNullOrEmpty(lName) ? lName : pathParts[i];
}
string newPath = string.Join("\\", locPath);
newPath = !newPath.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase) ? newPath + ext : newPath;
return newPath;
}
}
}