Skip to content

Commit

Permalink
[Peek]Fix IsDevFilePreview and white flash (#28734)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimecbernardo authored Sep 25, 2023
1 parent 28bd068 commit a1e0bd5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed partial class BrowserControl : UserControl, IDisposable
/// </summary>
private Uri? _navigatedUri;

private Color originalBackgroundColor;
private Color? _originalBackgroundColor;

public delegate void NavigationCompletedHandler(WebView2? sender, CoreWebView2NavigationCompletedEventArgs? args);

Expand Down Expand Up @@ -52,6 +52,7 @@ public Uri? Source
typeof(BrowserControl),
new PropertyMetadata(null, new PropertyChangedCallback((d, e) => ((BrowserControl)d).OnIsDevFilePreviewChanged())));

// Will actually be true for Markdown files as well.
public bool IsDevFilePreview
{
get
Expand Down Expand Up @@ -101,6 +102,11 @@ public void Navigate()
private void SourcePropertyChanged()
{
OpenUriDialog.Hide();

// Setting the background color to transparent.
// This ensures that non-HTML files are displayed with a transparent background.
PreviewBrowser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0);

Navigate();
}

Expand All @@ -113,6 +119,10 @@ private void OnIsDevFilePreviewChanged()
{
PreviewBrowser.CoreWebView2.SetVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.MonacoDirectory, CoreWebView2HostResourceAccessKind.Allow);
}
else
{
PreviewBrowser.CoreWebView2.ClearVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName);
}
}
}

Expand All @@ -123,7 +133,10 @@ private async void PreviewWV2_Loaded(object sender, RoutedEventArgs e)
await PreviewBrowser.EnsureCoreWebView2Async();

// Storing the original background color so it can be reset later for specific file types like HTML.
originalBackgroundColor = PreviewBrowser.DefaultBackgroundColor;
if (!_originalBackgroundColor.HasValue)
{
_originalBackgroundColor = PreviewBrowser.DefaultBackgroundColor;
}

// Setting the background color to transparent when initially loading the WebView2 component.
// This ensures that non-HTML files are displayed with a transparent background.
Expand All @@ -142,6 +155,10 @@ private async void PreviewWV2_Loaded(object sender, RoutedEventArgs e)
{
PreviewBrowser.CoreWebView2.SetVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.MonacoDirectory, CoreWebView2HostResourceAccessKind.Allow);
}
else
{
PreviewBrowser.CoreWebView2.ClearVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName);
}

PreviewBrowser.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
PreviewBrowser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
Expand All @@ -158,11 +175,16 @@ private void CoreWebView2_DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMC
{
// If the file being previewed is HTML or HTM, reset the background color to its original state.
// This is done to ensure that HTML and HTM files are displayed as intended, with their own background settings.
if (Source?.ToString().EndsWith(".html", StringComparison.OrdinalIgnoreCase) == true ||
Source?.ToString().EndsWith(".htm", StringComparison.OrdinalIgnoreCase) == true)
// This shouldn't be done for dev file previewer.
if (!IsDevFilePreview &&
(Source?.ToString().EndsWith(".html", StringComparison.OrdinalIgnoreCase) == true ||
Source?.ToString().EndsWith(".htm", StringComparison.OrdinalIgnoreCase) == true))
{
// Reset to default behavior for HTML files
PreviewBrowser.DefaultBackgroundColor = originalBackgroundColor;
if (_originalBackgroundColor.HasValue)
{
PreviewBrowser.DefaultBackgroundColor = _originalBackgroundColor.Value;
}
}

DOMContentLoaded?.Invoke(sender, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ await Dispatcher.RunOnUiThread(async () =>
}
else
{
// Simple html file to preview. Shouldn't do things like enabling scripts or using a virtual mapped directory.
IsDevFilePreview = false;
Preview = new Uri(File.Path);
}
});
Expand Down

0 comments on commit a1e0bd5

Please sign in to comment.