Skip to content

Commit a1e0bd5

Browse files
[Peek]Fix IsDevFilePreview and white flash (#28734)
1 parent 28bd068 commit a1e0bd5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed partial class BrowserControl : UserControl, IDisposable
2424
/// </summary>
2525
private Uri? _navigatedUri;
2626

27-
private Color originalBackgroundColor;
27+
private Color? _originalBackgroundColor;
2828

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

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

55+
// Will actually be true for Markdown files as well.
5556
public bool IsDevFilePreview
5657
{
5758
get
@@ -101,6 +102,11 @@ public void Navigate()
101102
private void SourcePropertyChanged()
102103
{
103104
OpenUriDialog.Hide();
105+
106+
// Setting the background color to transparent.
107+
// This ensures that non-HTML files are displayed with a transparent background.
108+
PreviewBrowser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0);
109+
104110
Navigate();
105111
}
106112

@@ -113,6 +119,10 @@ private void OnIsDevFilePreviewChanged()
113119
{
114120
PreviewBrowser.CoreWebView2.SetVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.MonacoDirectory, CoreWebView2HostResourceAccessKind.Allow);
115121
}
122+
else
123+
{
124+
PreviewBrowser.CoreWebView2.ClearVirtualHostNameToFolderMapping(Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName);
125+
}
116126
}
117127
}
118128

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

125135
// Storing the original background color so it can be reset later for specific file types like HTML.
126-
originalBackgroundColor = PreviewBrowser.DefaultBackgroundColor;
136+
if (!_originalBackgroundColor.HasValue)
137+
{
138+
_originalBackgroundColor = PreviewBrowser.DefaultBackgroundColor;
139+
}
127140

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

146163
PreviewBrowser.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
147164
PreviewBrowser.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
@@ -158,11 +175,16 @@ private void CoreWebView2_DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMC
158175
{
159176
// If the file being previewed is HTML or HTM, reset the background color to its original state.
160177
// This is done to ensure that HTML and HTM files are displayed as intended, with their own background settings.
161-
if (Source?.ToString().EndsWith(".html", StringComparison.OrdinalIgnoreCase) == true ||
162-
Source?.ToString().EndsWith(".htm", StringComparison.OrdinalIgnoreCase) == true)
178+
// This shouldn't be done for dev file previewer.
179+
if (!IsDevFilePreview &&
180+
(Source?.ToString().EndsWith(".html", StringComparison.OrdinalIgnoreCase) == true ||
181+
Source?.ToString().EndsWith(".htm", StringComparison.OrdinalIgnoreCase) == true))
163182
{
164183
// Reset to default behavior for HTML files
165-
PreviewBrowser.DefaultBackgroundColor = originalBackgroundColor;
184+
if (_originalBackgroundColor.HasValue)
185+
{
186+
PreviewBrowser.DefaultBackgroundColor = _originalBackgroundColor.Value;
187+
}
166188
}
167189

168190
DOMContentLoaded?.Invoke(sender, args);

src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ await Dispatcher.RunOnUiThread(async () =>
118118
}
119119
else
120120
{
121+
// Simple html file to preview. Shouldn't do things like enabling scripts or using a virtual mapped directory.
122+
IsDevFilePreview = false;
121123
Preview = new Uri(File.Path);
122124
}
123125
});

0 commit comments

Comments
 (0)