mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
[Peek][Monaco Preview] Open Monaco URI in default browser (#27774)
* open Monaco URI in default browser * added dialog when URI is opened
This commit is contained in:
committed by
GitHub
parent
a46b80c76f
commit
7c7f6cabf7
@@ -12,9 +12,15 @@
|
|||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
<controls:WebView2 x:Name="PreviewBrowser"
|
<controls:WebView2
|
||||||
Loaded="PreviewWV2_Loaded"
|
x:Name="PreviewBrowser"
|
||||||
NavigationStarting="PreviewBrowser_NavigationStarting"
|
Loaded="PreviewWV2_Loaded"
|
||||||
NavigationCompleted="PreviewWV2_NavigationCompleted"/>
|
NavigationStarting="PreviewBrowser_NavigationStarting"
|
||||||
|
NavigationCompleted="PreviewWV2_NavigationCompleted" />
|
||||||
|
<ContentDialog
|
||||||
|
x:Name="OpenUriDialog"
|
||||||
|
x:Uid="OpenUriDialog"
|
||||||
|
PrimaryButtonStyle="{ThemeResource AccentButtonStyle}"
|
||||||
|
SecondaryButtonClick="OpenUriDialog_SecondaryButtonClick" />
|
||||||
</Grid>
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -3,11 +3,13 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
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 Peek.Common.Constants;
|
using Peek.Common.Constants;
|
||||||
using Peek.Common.Helpers;
|
using Peek.Common.Helpers;
|
||||||
|
using Windows.ApplicationModel.DataTransfer;
|
||||||
using Windows.System;
|
using Windows.System;
|
||||||
using Windows.UI;
|
using Windows.UI;
|
||||||
|
|
||||||
@@ -96,6 +98,7 @@ namespace Peek.FilePreviewer.Controls
|
|||||||
|
|
||||||
private void SourcePropertyChanged()
|
private void SourcePropertyChanged()
|
||||||
{
|
{
|
||||||
|
OpenUriDialog.Hide();
|
||||||
Navigate();
|
Navigate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,6 +138,7 @@ namespace Peek.FilePreviewer.Controls
|
|||||||
}
|
}
|
||||||
|
|
||||||
PreviewBrowser.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
|
PreviewBrowser.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
|
||||||
|
PreviewBrowser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -149,6 +153,16 @@ namespace Peek.FilePreviewer.Controls
|
|||||||
DOMContentLoaded?.Invoke(sender, args);
|
DOMContentLoaded?.Invoke(sender, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 async void PreviewBrowser_NavigationStarting(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs args)
|
private async void PreviewBrowser_NavigationStarting(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs args)
|
||||||
{
|
{
|
||||||
if (_navigatedUri == null)
|
if (_navigatedUri == null)
|
||||||
@@ -157,10 +171,11 @@ namespace Peek.FilePreviewer.Controls
|
|||||||
}
|
}
|
||||||
|
|
||||||
// In case user starts or tries to navigate from within the HTML file we launch default web browser for navigation.
|
// In case user starts or tries to navigate from within the HTML file we launch default web browser for navigation.
|
||||||
if (args.Uri != null && args.Uri != _navigatedUri?.ToString() && args.IsUserInitiated)
|
// TODO: && args.IsUserInitiated - always false for PDF files, revert the workaround when fixed in WebView2: https://github.com/microsoft/PowerToys/issues/27403
|
||||||
|
if (args.Uri != null && args.Uri != _navigatedUri?.ToString())
|
||||||
{
|
{
|
||||||
args.Cancel = true;
|
args.Cancel = true;
|
||||||
await Launcher.LaunchUriAsync(new Uri(args.Uri));
|
await ShowOpenUriDialogAsync(new Uri(args.Uri));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +186,29 @@ namespace Peek.FilePreviewer.Controls
|
|||||||
_navigatedUri = Source;
|
_navigatedUri = Source;
|
||||||
}
|
}
|
||||||
|
|
||||||
NavigationCompleted?.Invoke(sender, args);
|
// Don't raise NavigationCompleted event if NavigationStarting has been cancelled
|
||||||
|
if (args.WebErrorStatus != CoreWebView2WebErrorStatus.OperationCanceled)
|
||||||
|
{
|
||||||
|
NavigationCompleted?.Invoke(sender, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -237,4 +237,20 @@
|
|||||||
<value>{0} bytes</value>
|
<value>{0} bytes</value>
|
||||||
<comment>Abbreviation for the size bytes</comment>
|
<comment>Abbreviation for the size bytes</comment>
|
||||||
</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 Peek to open the external application?</value>
|
||||||
|
<comment>Title of the dialog showed when an URI is clicked,"Peek" is the name of the utility. </comment>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
@@ -157,6 +157,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
|||||||
_webView.NavigationCompleted += WebView2Init;
|
_webView.NavigationCompleted += WebView2Init;
|
||||||
_webView.Height = this.Height;
|
_webView.Height = this.Height;
|
||||||
_webView.Width = this.Width;
|
_webView.Width = this.Width;
|
||||||
|
_webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
|
||||||
Controls.Add(_webView);
|
Controls.Add(_webView);
|
||||||
_webView.SendToBack();
|
_webView.SendToBack();
|
||||||
_loadingBar.Value = 100;
|
_loadingBar.Value = 100;
|
||||||
@@ -218,6 +219,16 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
|
||||||
|
{
|
||||||
|
// Monaco opens URI in a new window. We open the URI in the default web browser.
|
||||||
|
if (e.Uri != null && e.IsUserInitiated)
|
||||||
|
{
|
||||||
|
e.Handled = true;
|
||||||
|
await Launcher.LaunchUriAsync(new Uri(e.Uri));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This event sets the height and width of the webview to the size of the form
|
/// This event sets the height and width of the webview to the size of the form
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user