mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[Peek][PreviewPane]Show Copy entry in right-click copy menu (#33845)
## Summary of the Pull Request Fixes two bugs: - Peek: Missing "Copy" menu-item for all WebView2 previewers. - PreviewPane: Missing "Copy" menu-item for markdown files only. ## Detailed Description of the Pull Request / Additional comments The issues are: - Peek: - When not using Monaco (markdown, html) - the default WebView2 context menu has been disabled. I have enabled it and then disabled ALL menu-items other than "Copy" (such as "Back"). - When using Monaco + Release (other code files) - current code tries to use the Monaco context menu, but it is somehow disabled at runtime. I spent MANY hours trying to find out why but without success. It works fine when I view the generated html + js files in a browser or in a Debug build or in PreviewPane. But I couldn't find the root cause. Trying to fix it by enabling the WebView2 context menu instead doesn't work as for whatever reason, WebView2 doesn't generate a "Copy" menu-item (it thinks there's no selected text when there is). So in this case, the only thing I could get to work was generating context menu-items via WebView2 callbacks that call JS functions. As a bonus, this way of doing it also allows "Toggle text wrapping" to work. - PreviewPane: - Markdown - the default WebView2 context menu has been disabled. Like for Peek, I have enabled it and then disabled ALL menu-items other than "Copy" (such as "Back"). - Monaco (other code files) - this already just works fine, so I've left it as is. I *could* make it work the same way as I've done for Peek for consistency, but I've chosen to leave it as is since it works.  
This commit is contained in:
@@ -143,7 +143,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Markdown
|
||||
await _browser.EnsureCoreWebView2Async(_webView2Environment).ConfigureAwait(true);
|
||||
_browser.CoreWebView2.SetVirtualHostNameToFolderMapping(VirtualHostName, AssemblyDirectory, CoreWebView2HostResourceAccessKind.Deny);
|
||||
_browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
|
||||
_browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
|
||||
_browser.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
|
||||
_browser.CoreWebView2.Settings.AreDevToolsEnabled = false;
|
||||
_browser.CoreWebView2.Settings.AreHostObjectsAllowed = false;
|
||||
_browser.CoreWebView2.Settings.IsGeneralAutofillEnabled = false;
|
||||
@@ -162,6 +162,23 @@ namespace Microsoft.PowerToys.PreviewHandler.Markdown
|
||||
}
|
||||
};
|
||||
|
||||
_browser.CoreWebView2.ContextMenuRequested += (object sender, CoreWebView2ContextMenuRequestedEventArgs args) =>
|
||||
{
|
||||
var menuItems = args.MenuItems;
|
||||
|
||||
if (!menuItems.IsReadOnly)
|
||||
{
|
||||
var copyMenuItem = menuItems.FirstOrDefault(menuItem => menuItem.Name == "copy");
|
||||
|
||||
menuItems.Clear();
|
||||
|
||||
if (copyMenuItem != null)
|
||||
{
|
||||
menuItems.Add(copyMenuItem);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// WebView2.NavigateToString() limitation
|
||||
// See https://learn.microsoft.com/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatetostring?view=webview2-dotnet-1.0.864.35#remarks
|
||||
// While testing the limit, it turned out it is ~1.5MB, so to be on a safe side we go for 1.5m bytes
|
||||
|
||||
@@ -396,6 +396,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
|
||||
_html = FilePreviewCommon.MonacoHelper.ReadIndexHtml();
|
||||
_html = _html.Replace("[[PT_LANG]]", _vsCodeLangSet, StringComparison.InvariantCulture);
|
||||
_html = _html.Replace("[[PT_WRAP]]", _settings.Wrap ? "1" : "0", StringComparison.InvariantCulture);
|
||||
_html = _html.Replace("[[PT_CONTEXTMENU]]", "1", StringComparison.InvariantCulture);
|
||||
_html = _html.Replace("[[PT_THEME]]", Settings.GetTheme(), StringComparison.InvariantCulture);
|
||||
_html = _html.Replace("[[PT_STICKY_SCROLL]]", _settings.StickyScroll ? "1" : "0", StringComparison.InvariantCulture);
|
||||
_html = _html.Replace("[[PT_FONT_SIZE]]", _settings.FontSize.ToString(CultureInfo.InvariantCulture), StringComparison.InvariantCulture);
|
||||
|
||||
Reference in New Issue
Block a user