mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[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:
@@ -0,0 +1,46 @@
|
||||
// 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.Runtime.InteropServices;
|
||||
using Wox.Plugin.Common.Win32;
|
||||
|
||||
namespace Wox.Plugin.Common.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// The following are ShellItem DisplayName types.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum SIGDN : uint
|
||||
{
|
||||
NORMALDISPLAY = 0,
|
||||
PARENTRELATIVEPARSING = 0x80018001,
|
||||
PARENTRELATIVEFORADDRESSBAR = 0x8001c001,
|
||||
DESKTOPABSOLUTEPARSING = 0x80028000,
|
||||
PARENTRELATIVEEDITING = 0x80031001,
|
||||
DESKTOPABSOLUTEEDITING = 0x8004c000,
|
||||
FILESYSPATH = 0x80058000,
|
||||
URL = 0x80068000,
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
|
||||
public interface IShellItem
|
||||
{
|
||||
void BindToHandler(
|
||||
IntPtr pbc,
|
||||
[MarshalAs(UnmanagedType.LPStruct)] Guid bhid,
|
||||
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
|
||||
out IntPtr ppv);
|
||||
|
||||
void GetParent(out IShellItem ppsi);
|
||||
|
||||
void GetDisplayName(SIGDN sigdnName, [MarshalAs(UnmanagedType.LPWStr)] out string ppszName);
|
||||
|
||||
void GetAttributes(uint sfgaoMask, out uint psfgaoAttribs);
|
||||
|
||||
void Compare(IShellItem psi, uint hint, out int piOrder);
|
||||
}
|
||||
}
|
||||
@@ -2,67 +2,51 @@
|
||||
// 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.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Wox.Plugin.Common.Interfaces;
|
||||
using Wox.Plugin.Common.Win32;
|
||||
|
||||
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
|
||||
public 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);
|
||||
// Cache for already localized names. This makes localization of already localized string faster.
|
||||
private Dictionary<string, string> _localizationCache = new Dictionary<string, string>();
|
||||
|
||||
/// <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)
|
||||
public 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)
|
||||
// Checking cache if path is already localized
|
||||
if (_localizationCache.ContainsKey(path.ToLowerInvariant()))
|
||||
{
|
||||
_ = 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 _localizationCache[path.ToLowerInvariant()];
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
Guid shellItemType = ShellItemTypeConstants.ShellItemGuid;
|
||||
int retCode = NativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref shellItemType, out IShellItem shellItem);
|
||||
if (retCode != 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
shellItem.GetDisplayName(SIGDN.NORMALDISPLAY, out string filename);
|
||||
|
||||
if (!_localizationCache.ContainsKey(path.ToLowerInvariant()))
|
||||
{
|
||||
// The if condition is required to not get timing problems when called from an parallel execution.
|
||||
// Without the check we will get "key exists" exceptions.
|
||||
_localizationCache.Add(path.ToLowerInvariant(), filename);
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -70,7 +54,7 @@ namespace Wox.Plugin.Common
|
||||
/// </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)
|
||||
public string GetLocalizedPath(string path)
|
||||
{
|
||||
path = Environment.ExpandEnvironmentVariables(path);
|
||||
string ext = Path.GetExtension(path);
|
||||
@@ -79,6 +63,14 @@ namespace Wox.Plugin.Common
|
||||
|
||||
for (int i = 0; i < pathParts.Length; i++)
|
||||
{
|
||||
if (i == 0 && pathParts[i].EndsWith(':'))
|
||||
{
|
||||
// Skip the drive letter.
|
||||
locPath[0] = pathParts[0];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Localize path.
|
||||
int iElements = i + 1;
|
||||
string lName = GetLocalizedName(string.Join("\\", pathParts[..iElements]));
|
||||
locPath[i] = !string.IsNullOrEmpty(lName) ? lName : pathParts[i];
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Wox.Plugin.Common.Interfaces;
|
||||
using SuppressMessageAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute;
|
||||
|
||||
#pragma warning disable SA1649, CA1051, CA1707, CA1028, CA1714, CA1069, SA1402
|
||||
@@ -114,6 +115,9 @@ namespace Wox.Plugin.Common.Win32
|
||||
|
||||
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
||||
public static extern HRESULT SHCreateStreamOnFileEx(string fileName, STGM grfMode, uint attributes, bool create, System.Runtime.InteropServices.ComTypes.IStream reserved, out System.Runtime.InteropServices.ComTypes.IStream stream);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
public static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string path, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IShellItem shellItem);
|
||||
}
|
||||
|
||||
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "These are the names used by win32.")]
|
||||
@@ -141,6 +145,19 @@ namespace Wox.Plugin.Common.Win32
|
||||
public const int SC_CLOSE = 0xF060;
|
||||
}
|
||||
|
||||
public static class ShellItemTypeConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// Guid for type IShellItem.
|
||||
/// </summary>
|
||||
public static readonly Guid ShellItemGuid = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe");
|
||||
|
||||
/// <summary>
|
||||
/// Guid for type IShellItem2.
|
||||
/// </summary>
|
||||
public static readonly Guid ShellItem2Guid = new Guid("7E9FB0D3-919F-4307-AB2E-9B1860310C93");
|
||||
}
|
||||
|
||||
public enum HRESULT : uint
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user