mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[PT Run] Support for application URI (#14391)
* [RUN] Add support for uri with scheme only * Fix typo * Add full support for application URI * Apply suggestions from code review and add tests * [PT Run] Add support for application uri * Update error message * Adapt the icon if the result is web URI * Update icons for application URI * Update icons for application URI (dark mode) * Update icon
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 3.7 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.8 KiB |
@@ -6,6 +6,6 @@ namespace Microsoft.Plugin.Uri.Interfaces
|
||||
{
|
||||
public interface IUriParser
|
||||
{
|
||||
bool TryParse(string input, out System.Uri result);
|
||||
bool TryParse(string input, out System.Uri result, out bool isWebUri);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,17 +63,15 @@ namespace Microsoft.Plugin.Uri
|
||||
{
|
||||
results.Add(new Result
|
||||
{
|
||||
Title = Properties.Resources.Microsoft_plugin_uri_default_browser,
|
||||
Title = Properties.Resources.Microsoft_plugin_uri_open,
|
||||
SubTitle = BrowserPath,
|
||||
IcoPath = _uriSettings.ShowBrowserIcon
|
||||
? BrowserIconPath
|
||||
: DefaultIconPath,
|
||||
IcoPath = DefaultIconPath,
|
||||
Action = action =>
|
||||
{
|
||||
if (!Helper.OpenInShell(BrowserPath))
|
||||
{
|
||||
var title = $"Plugin: {Properties.Resources.Microsoft_plugin_uri_plugin_name}";
|
||||
var message = $"{Properties.Resources.Microsoft_plugin_default_browser_open_failed}: ";
|
||||
var message = $"{Properties.Resources.Microsoft_plugin_uri_open_failed}: ";
|
||||
Context.API.ShowMsg(title, message);
|
||||
return false;
|
||||
}
|
||||
@@ -85,16 +83,19 @@ namespace Microsoft.Plugin.Uri
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(query?.Search)
|
||||
&& _uriParser.TryParse(query.Search, out var uriResult)
|
||||
&& _uriParser.TryParse(query.Search, out var uriResult, out var isWebUri)
|
||||
&& _uriResolver.IsValidHost(uriResult))
|
||||
{
|
||||
var uriResultString = uriResult.ToString();
|
||||
var isWebUriBool = isWebUri;
|
||||
|
||||
results.Add(new Result
|
||||
{
|
||||
Title = uriResultString,
|
||||
SubTitle = Properties.Resources.Microsoft_plugin_uri_website,
|
||||
IcoPath = _uriSettings.ShowBrowserIcon
|
||||
SubTitle = isWebUriBool
|
||||
? Properties.Resources.Microsoft_plugin_uri_website
|
||||
: Properties.Resources.Microsoft_plugin_uri_open,
|
||||
IcoPath = isWebUriBool
|
||||
? BrowserIconPath
|
||||
: DefaultIconPath,
|
||||
Action = action =>
|
||||
@@ -118,7 +119,7 @@ namespace Microsoft.Plugin.Uri
|
||||
private static bool IsActivationKeyword(Query query)
|
||||
{
|
||||
return !string.IsNullOrEmpty(query?.ActionKeyword)
|
||||
&& query?.ActionKeyword == query?.RawQuery;
|
||||
&& query?.ActionKeyword == query?.RawQuery;
|
||||
}
|
||||
|
||||
private bool IsDefaultBrowserSet()
|
||||
@@ -155,16 +156,23 @@ namespace Microsoft.Plugin.Uri
|
||||
UpdateBrowserIconPath(newTheme);
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive but will log the exception")]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage(
|
||||
"Design",
|
||||
"CA1031:Do not catch general exception types",
|
||||
Justification = "We want to keep the process alive but will log the exception")]
|
||||
private void UpdateBrowserIconPath(Theme newTheme)
|
||||
{
|
||||
try
|
||||
{
|
||||
var progId = _registryWrapper.GetRegistryValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", "ProgId");
|
||||
var progId = _registryWrapper.GetRegistryValue(
|
||||
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
|
||||
"ProgId");
|
||||
var programLocation =
|
||||
|
||||
// Resolve App Icon (UWP)
|
||||
_registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\Application", "ApplicationIcon")
|
||||
_registryWrapper.GetRegistryValue(
|
||||
"HKEY_CLASSES_ROOT\\" + progId + "\\Application",
|
||||
"ApplicationIcon")
|
||||
|
||||
// Resolves default file association icon (UWP + Normal)
|
||||
?? _registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\DefaultIcon", null);
|
||||
@@ -174,14 +182,21 @@ namespace Microsoft.Plugin.Uri
|
||||
if (programLocation.StartsWith("@", StringComparison.Ordinal))
|
||||
{
|
||||
var directProgramLocationStringBuilder = new StringBuilder(128);
|
||||
if (NativeMethods.SHLoadIndirectString(programLocation, directProgramLocationStringBuilder, (uint)directProgramLocationStringBuilder.Capacity, IntPtr.Zero) ==
|
||||
if (NativeMethods.SHLoadIndirectString(
|
||||
programLocation,
|
||||
directProgramLocationStringBuilder,
|
||||
(uint)directProgramLocationStringBuilder.Capacity,
|
||||
IntPtr.Zero) ==
|
||||
NativeMethods.Hresult.Ok)
|
||||
{
|
||||
// Check if there's a postfix with contract-white/contrast-black icon is available and use that instead
|
||||
var directProgramLocation = directProgramLocationStringBuilder.ToString();
|
||||
var themeIcon = newTheme == Theme.Light || newTheme == Theme.HighContrastWhite ? "contrast-white" : "contrast-black";
|
||||
var themeIcon = newTheme == Theme.Light || newTheme == Theme.HighContrastWhite
|
||||
? "contrast-white"
|
||||
: "contrast-black";
|
||||
var extension = Path.GetExtension(directProgramLocation);
|
||||
var themedProgLocation = $"{directProgramLocation.Substring(0, directProgramLocation.Length - extension.Length)}_{themeIcon}{extension}";
|
||||
var themedProgLocation =
|
||||
$"{directProgramLocation.Substring(0, directProgramLocation.Length - extension.Length)}_{themeIcon}{extension}";
|
||||
BrowserIconPath = File.Exists(themedProgLocation)
|
||||
? themedProgLocation
|
||||
: directProgramLocation;
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace Microsoft.Plugin.Uri.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Open Default Browser.
|
||||
/// Looks up a localized string similar to Open default browser.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_uri_default_browser {
|
||||
get {
|
||||
@@ -79,7 +79,16 @@ namespace Microsoft.Plugin.Uri.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Failed to open URL.
|
||||
/// Looks up a localized string similar to Open URI.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_uri_open {
|
||||
get {
|
||||
return ResourceManager.GetString("Microsoft_plugin_uri_open", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Failed to open URI.
|
||||
/// </summary>
|
||||
public static string Microsoft_plugin_uri_open_failed {
|
||||
get {
|
||||
|
||||
@@ -123,8 +123,11 @@
|
||||
<data name="Microsoft_plugin_uri_default_browser" xml:space="preserve">
|
||||
<value>Open default browser</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_uri_open" xml:space="preserve">
|
||||
<value>Open URI</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_uri_open_failed" xml:space="preserve">
|
||||
<value>Failed to open URL</value>
|
||||
<value>Failed to open URI</value>
|
||||
</data>
|
||||
<data name="Microsoft_plugin_uri_plugin_description" xml:space="preserve">
|
||||
<value>Opens URLs and UNC network shares.</value>
|
||||
@@ -135,4 +138,4 @@
|
||||
<data name="Microsoft_plugin_uri_website" xml:space="preserve">
|
||||
<value>Open in default browser</value>
|
||||
</data>
|
||||
</root>
|
||||
</root>
|
||||
@@ -10,22 +10,37 @@ namespace Microsoft.Plugin.Uri.UriHelper
|
||||
{
|
||||
public class ExtendedUriParser : IUriParser
|
||||
{
|
||||
public bool TryParse(string input, out System.Uri result)
|
||||
public bool TryParse(string input, out System.Uri result, out bool isWebUri)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handling URL with only scheme, typically mailto or application uri.
|
||||
// Do nothing, return the result without urlBuilder
|
||||
if (input.EndsWith(":", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.Contains("/", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.All(char.IsDigit))
|
||||
{
|
||||
result = new System.Uri(input);
|
||||
isWebUri = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Handle common cases UriBuilder does not handle
|
||||
// Using CurrentCulture since this is a user typed string
|
||||
if (input.EndsWith(":", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith(".", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith(":/", StringComparison.CurrentCulture)
|
||||
|| input.EndsWith("://", StringComparison.CurrentCulture)
|
||||
|| input.All(char.IsDigit))
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -35,27 +50,31 @@ namespace Microsoft.Plugin.Uri.UriHelper
|
||||
var hadDefaultPort = urlBuilder.Uri.IsDefaultPort;
|
||||
urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port;
|
||||
|
||||
if (input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase))
|
||||
if (input.StartsWith("HTTP://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
urlBuilder.Scheme = System.Uri.UriSchemeHttp;
|
||||
isWebUri = true;
|
||||
}
|
||||
else if (input.Contains(":", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.Contains("http", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.StartsWith("http", StringComparison.OrdinalIgnoreCase) &&
|
||||
!input.Contains("[", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Do nothing, leave unchanged
|
||||
isWebUri = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
urlBuilder.Scheme = System.Uri.UriSchemeHttps;
|
||||
isWebUri = true;
|
||||
}
|
||||
|
||||
result = urlBuilder.Uri;
|
||||
return true;
|
||||
}
|
||||
catch (System.UriFormatException)
|
||||
catch (UriFormatException)
|
||||
{
|
||||
result = default;
|
||||
isWebUri = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user