mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Use system default web browser when opening links through Monaco in RegistryPreview (#37466)
* Handled NewWindowRequested WebView2 event, to allow links opened through Registry Preview to open in the system default web browser, rather than a new WebView2 window. * Modified RegistryPreview implementatiion to use the open URI dialog that is currently used in Peek.
This commit is contained in:
@@ -31,5 +31,10 @@
|
|||||||
VerticalAlignment="Stretch"
|
VerticalAlignment="Stretch"
|
||||||
Loaded="Browser_Loaded" />
|
Loaded="Browser_Loaded" />
|
||||||
</Border>
|
</Border>
|
||||||
|
<ContentDialog
|
||||||
|
x:Name="OpenUriDialog"
|
||||||
|
x:Uid="OpenUriDialog"
|
||||||
|
PrimaryButtonStyle="{ThemeResource AccentButtonStyle}"
|
||||||
|
SecondaryButtonClick="OpenUriDialog_SecondaryButtonClick" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
|||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Microsoft.Web.WebView2.Core;
|
using Microsoft.Web.WebView2.Core;
|
||||||
|
using Windows.ApplicationModel.DataTransfer;
|
||||||
|
using Windows.System;
|
||||||
using Windows.UI;
|
using Windows.UI;
|
||||||
|
|
||||||
namespace RegistryPreviewUILib
|
namespace RegistryPreviewUILib
|
||||||
@@ -66,6 +68,7 @@ namespace RegistryPreviewUILib
|
|||||||
Browser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0);
|
Browser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0);
|
||||||
Browser.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
|
Browser.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
|
||||||
Browser.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
|
Browser.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
|
||||||
|
Browser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
|
||||||
Browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
|
Browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
|
||||||
Browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
|
Browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
|
||||||
Browser.CoreWebView2.Settings.AreHostObjectsAllowed = false;
|
Browser.CoreWebView2.Settings.AreHostObjectsAllowed = false;
|
||||||
@@ -89,6 +92,16 @@ namespace RegistryPreviewUILib
|
|||||||
Browser.CoreWebView2.Navigate(index);
|
Browser.CoreWebView2.Navigate(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void CoreWebView2_NewWindowRequested(CoreWebView2 sender, CoreWebView2NewWindowRequestedEventArgs args)
|
||||||
|
{
|
||||||
|
// Monaco opens URI in a new window. We open the URI in the default web browser.
|
||||||
|
if (args.Uri != null && args.IsUserInitiated)
|
||||||
|
{
|
||||||
|
args.Handled = true;
|
||||||
|
await ShowOpenUriDialogAsync(new Uri(args.Uri));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void CoreWebView2_PermissionRequested(CoreWebView2 sender, CoreWebView2PermissionRequestedEventArgs args)
|
private void CoreWebView2_PermissionRequested(CoreWebView2 sender, CoreWebView2PermissionRequestedEventArgs args)
|
||||||
{
|
{
|
||||||
if (args.PermissionKind == CoreWebView2PermissionKind.ClipboardRead)
|
if (args.PermissionKind == CoreWebView2PermissionKind.ClipboardRead)
|
||||||
@@ -165,5 +178,23 @@ namespace RegistryPreviewUILib
|
|||||||
{
|
{
|
||||||
_textChangedThrottle?.Dispose();
|
_textChangedThrottle?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task ShowOpenUriDialogAsync(Uri uri)
|
||||||
|
{
|
||||||
|
OpenUriDialog.Content = uri.ToString();
|
||||||
|
var result = await OpenUriDialog.ShowAsync();
|
||||||
|
|
||||||
|
if (result == ContentDialogResult.Primary)
|
||||||
|
{
|
||||||
|
await Launcher.LaunchUriAsync(uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OpenUriDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||||
|
{
|
||||||
|
var dataPackage = new DataPackage();
|
||||||
|
dataPackage.SetText(sender.Content.ToString());
|
||||||
|
Clipboard.SetContent(dataPackage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,6 +178,22 @@
|
|||||||
<data name="OpenDialogTitle" xml:space="preserve">
|
<data name="OpenDialogTitle" xml:space="preserve">
|
||||||
<value>Open Registry file</value>
|
<value>Open Registry file</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="OpenUriDialog.CloseButtonText" xml:space="preserve">
|
||||||
|
<value>Cancel</value>
|
||||||
|
<comment>Dialog showed when an URI is clicked. Button to close the dialog.</comment>
|
||||||
|
</data>
|
||||||
|
<data name="OpenUriDialog.PrimaryButtonText" xml:space="preserve">
|
||||||
|
<value>Open</value>
|
||||||
|
<comment>Dialog showed when an URI is clicked. Button to open the URI.</comment>
|
||||||
|
</data>
|
||||||
|
<data name="OpenUriDialog.SecondaryButtonText" xml:space="preserve">
|
||||||
|
<value>Copy</value>
|
||||||
|
<comment>Dialog showed when an URI is clicked. Button to copy the URI.</comment>
|
||||||
|
</data>
|
||||||
|
<data name="OpenUriDialog.Title" xml:space="preserve">
|
||||||
|
<value>Do you want RegistryPreview to open the external application?</value>
|
||||||
|
<comment>Title of the dialog showed when an URI is clicked,"RegistryPreview" is the name of the utility. </comment>
|
||||||
|
</data>
|
||||||
<data name="RefreshButton.Label" xml:space="preserve">
|
<data name="RefreshButton.Label" xml:space="preserve">
|
||||||
<value>Reload</value>
|
<value>Reload</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
Reference in New Issue
Block a user