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:
Nathan Gill
2025-02-18 22:01:03 +00:00
committed by GitHub
parent ec136d7bb7
commit cb5baad677
3 changed files with 52 additions and 0 deletions

View File

@@ -31,5 +31,10 @@
VerticalAlignment="Stretch"
Loaded="Browser_Loaded" />
</Border>
<ContentDialog
x:Name="OpenUriDialog"
x:Uid="OpenUriDialog"
PrimaryButtonStyle="{ThemeResource AccentButtonStyle}"
SecondaryButtonClick="OpenUriDialog_SecondaryButtonClick" />
</Grid>
</UserControl>

View File

@@ -13,6 +13,8 @@ using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Windows.ApplicationModel.DataTransfer;
using Windows.System;
using Windows.UI;
namespace RegistryPreviewUILib
@@ -66,6 +68,7 @@ namespace RegistryPreviewUILib
Browser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0);
Browser.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
Browser.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
Browser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
Browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
Browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
Browser.CoreWebView2.Settings.AreHostObjectsAllowed = false;
@@ -89,6 +92,16 @@ namespace RegistryPreviewUILib
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)
{
if (args.PermissionKind == CoreWebView2PermissionKind.ClipboardRead)
@@ -165,5 +178,23 @@ namespace RegistryPreviewUILib
{
_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);
}
}
}

View File

@@ -178,6 +178,22 @@
<data name="OpenDialogTitle" xml:space="preserve">
<value>Open Registry file</value>
</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">
<value>Reload</value>
</data>