Skip to content

Commit

Permalink
Use system default web browser when opening links through Monaco in R…
Browse files Browse the repository at this point in the history
…egistryPreview (#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.
  • Loading branch information
OldUser101 authored Feb 18, 2025
1 parent ec136d7 commit cb5baad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit cb5baad

Please sign in to comment.