From cb5baad6779e72e2c28772f3601738a29a6fb749 Mon Sep 17 00:00:00 2001 From: Nathan Gill Date: Tue, 18 Feb 2025 22:01:03 +0000 Subject: [PATCH] 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. --- .../MonacoEditorControl.xaml | 5 +++ .../MonacoEditorControl.xaml.cs | 31 +++++++++++++++++++ .../Strings/en-US/Resources.resw | 16 ++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml b/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml index 9635708343fe..e3d65c72c8ca 100644 --- a/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml +++ b/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml @@ -31,5 +31,10 @@ VerticalAlignment="Stretch" Loaded="Browser_Loaded" /> + diff --git a/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml.cs b/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml.cs index eeddfcb0f4d1..551850abbe3e 100644 --- a/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml.cs +++ b/src/modules/registrypreview/RegistryPreviewUILib/MonacoEditorControl.xaml.cs @@ -13,6 +13,8 @@ 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 @@ private async void Browser_Loaded(object sender, RoutedEventArgs e) 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 @@ private async void Browser_Loaded(object sender, RoutedEventArgs e) 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 @@ public void 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); + } } } diff --git a/src/modules/registrypreview/RegistryPreviewUILib/Strings/en-US/Resources.resw b/src/modules/registrypreview/RegistryPreviewUILib/Strings/en-US/Resources.resw index 9401908d216e..73469a3b509b 100644 --- a/src/modules/registrypreview/RegistryPreviewUILib/Strings/en-US/Resources.resw +++ b/src/modules/registrypreview/RegistryPreviewUILib/Strings/en-US/Resources.resw @@ -178,6 +178,22 @@ Open Registry file + + Cancel + Dialog showed when an URI is clicked. Button to close the dialog. + + + Open + Dialog showed when an URI is clicked. Button to open the URI. + + + Copy + Dialog showed when an URI is clicked. Button to copy the URI. + + + Do you want RegistryPreview to open the external application? + Title of the dialog showed when an URI is clicked,"RegistryPreview" is the name of the utility. + Reload