mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Use localized name for shell link programs
This commit is contained in:
parent
4849e26aca
commit
9b05174a97
2 changed files with 106 additions and 6 deletions
|
|
@ -0,0 +1,92 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace Flow.Launcher.Plugin.Program.Programs
|
||||
{
|
||||
// From PT Run
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -44,6 +44,9 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
|||
public bool Enabled { get; set; }
|
||||
public string Location => ParentDirectory;
|
||||
|
||||
// Localized name based on windows display language
|
||||
public string LocalizedName { get; set; } = string.Empty;
|
||||
|
||||
private const string ShortcutExtension = "lnk";
|
||||
private const string UrlExtension = "url";
|
||||
private const string ExeExtension = "exe";
|
||||
|
|
@ -69,27 +72,30 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
|||
string title;
|
||||
MatchResult matchResult;
|
||||
|
||||
// Name of the result
|
||||
string resultName = string.IsNullOrEmpty(LocalizedName) ? Name : LocalizedName;
|
||||
|
||||
// We suppose Name won't be null
|
||||
if (!Main._settings.EnableDescription || Description == null || Name.StartsWith(Description))
|
||||
if (!Main._settings.EnableDescription || Description == null || resultName.StartsWith(Description))
|
||||
{
|
||||
title = Name;
|
||||
title = resultName;
|
||||
matchResult = StringMatcher.FuzzySearch(query, title);
|
||||
}
|
||||
else if (Description.StartsWith(Name))
|
||||
else if (Description.StartsWith(resultName))
|
||||
{
|
||||
title = Description;
|
||||
matchResult = StringMatcher.FuzzySearch(query, Description);
|
||||
}
|
||||
else
|
||||
{
|
||||
title = $"{Name}: {Description}";
|
||||
var nameMatch = StringMatcher.FuzzySearch(query, Name);
|
||||
title = $"{resultName}: {Description}";
|
||||
var nameMatch = StringMatcher.FuzzySearch(query, resultName);
|
||||
var desciptionMatch = StringMatcher.FuzzySearch(query, Description);
|
||||
if (desciptionMatch.Score > nameMatch.Score)
|
||||
{
|
||||
for (int i = 0; i < desciptionMatch.MatchData.Count; i++)
|
||||
{
|
||||
desciptionMatch.MatchData[i] += Name.Length + 2; // 2 is ": "
|
||||
desciptionMatch.MatchData[i] += resultName.Length + 2; // 2 is ": "
|
||||
}
|
||||
matchResult = desciptionMatch;
|
||||
}
|
||||
|
|
@ -297,6 +303,8 @@ namespace Flow.Launcher.Plugin.Program.Programs
|
|||
}
|
||||
}
|
||||
|
||||
program.LocalizedName = ShellLocalization.GetLocalizedName(path);
|
||||
|
||||
return program;
|
||||
}
|
||||
catch (COMException e)
|
||||
|
|
|
|||
Loading…
Reference in a new issue