Skip to content

Commit 20fbe4e

Browse files
committed
No longer keep WebView2 around across previews.
This should fix crash on subsequent previews in newer WebView2s. I think due to changes in WebView2 where "WebView2 controller will be auto closed if the parent hwnd is destroyed". Also fixes "Multiple windows are not supported" as each preview is now its own instance. Previews will be slower due to this :(
1 parent 7787fad commit 20fbe4e

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

Plugin.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ namespace QuickLook.Plugin.WebViewPlus
1010
{
1111
public class Plugin : IViewer
1212
{
13-
private static WebpagePanel _panel;
13+
private WebpagePanel _panel;
1414

1515
public int Priority => 1;
1616
private static double _width = 1000;
1717
private static double _height = 1200;
1818

1919
public void Init()
2020
{
21-
_panel = new WebpagePanel();
2221
}
2322

2423
public bool CanHandle(string path)
2524
{
2625
var extension = Path.GetExtension(path).ToLower().Substring(1);
27-
return !Directory.Exists(path) && _panel.Extensions.Any(extension.Equals);
26+
return !Directory.Exists(path) && WebpagePanel.Extensions.Any(extension.Equals);
2827
}
2928

3029
public void Prepare(string path, ContextObject context)
@@ -35,6 +34,7 @@ public void Prepare(string path, ContextObject context)
3534

3635
public void View(string path, ContextObject context)
3736
{
37+
_panel = new WebpagePanel();
3838
context.ViewerContent = _panel;
3939
context.Title = Path.IsPathRooted(path) ? Path.GetFileName(path) : path;
4040

@@ -47,11 +47,8 @@ public void Cleanup()
4747
_width = _panel.ActualWidth;
4848
_height = _panel.ActualHeight;
4949

50-
// starting webview2 is expensive, so keep it running and just unload the webapp data
51-
_panel.UnloadData();
52-
53-
//_panel?.Dispose();
54-
//_panel = null;
50+
_panel?.Dispose();
51+
_panel = null;
5552
}
5653
}
5754
}

QuickLook.Plugin.WebViewPlus.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@
5858
</AssemblyOriginatorKeyFile>
5959
</PropertyGroup>
6060
<ItemGroup>
61-
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.2651.64, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
62-
<HintPath>packages\Microsoft.Web.WebView2.1.0.2651.64\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
61+
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.3065.39, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
62+
<HintPath>packages\Microsoft.Web.WebView2.1.0.3065.39\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
6363
</Reference>
64-
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.2651.64, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
65-
<HintPath>packages\Microsoft.Web.WebView2.1.0.2651.64\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
64+
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.3065.39, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
65+
<HintPath>packages\Microsoft.Web.WebView2.1.0.3065.39\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
6666
</Reference>
67-
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.2651.64, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
68-
<HintPath>packages\Microsoft.Web.WebView2.1.0.2651.64\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
67+
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.3065.39, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
68+
<HintPath>packages\Microsoft.Web.WebView2.1.0.3065.39\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
6969
</Reference>
7070
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
7171
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
@@ -109,11 +109,11 @@
109109
</ItemGroup>
110110
<Copy SourceFiles="@(WebApp)" DestinationFiles="@(WebApp->'$(OutDir)\webviewplus\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
111111
</Target>
112-
<Import Project="packages\Microsoft.Web.WebView2.1.0.2651.64\build\Microsoft.Web.WebView2.targets" Condition="Exists('packages\Microsoft.Web.WebView2.1.0.2651.64\build\Microsoft.Web.WebView2.targets')" />
112+
<Import Project="packages\Microsoft.Web.WebView2.1.0.3065.39\build\Microsoft.Web.WebView2.targets" Condition="Exists('packages\Microsoft.Web.WebView2.1.0.3065.39\build\Microsoft.Web.WebView2.targets')" />
113113
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
114114
<PropertyGroup>
115115
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
116116
</PropertyGroup>
117-
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.2651.64\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.2651.64\build\Microsoft.Web.WebView2.targets'))" />
117+
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.3065.39\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.3065.39\build\Microsoft.Web.WebView2.targets'))" />
118118
</Target>
119119
</Project>

WebpagePanel.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class WebpagePanel : UserControl
4747
{
4848
public static readonly string DefaultExtensions =
4949
"html,htm,mht,mhtml,pdf,epub,csv,xlsx,svg,md,markdown,gltf,glb,c++,h++,bat,c,cmake,cpp,cs,css,d,go,h,hpp,java,js,json,jsx,kt,lua,m,mm,makefile,pas,perl,php,pl,ps1,psm1,py,r,rb,rs,sass,scala,scss,sh,sql,swift,tex,ts,tsx,txt,webp,xml,yaml,yml";
50-
public string[] Extensions = { };
50+
public static string[] Extensions = SettingHelper.Get("ExtensionList", WebpagePanel.DefaultExtensions, "QuickLook.Plugin.WebViewPlus").Split(',');
5151

5252
// These should match the ones in the web app openFile.ts:BINARY_EXTENSIONS
5353
private static readonly string[] _binExtensions = "pdf,epub,xlsx,xls,ods,gltf,glb,fbx,obj,webp,jpg,jpeg,png,apng,gif,bmp,avif,ttf,otf,woff,woff2".Split(',');
@@ -61,7 +61,6 @@ public class WebpagePanel : UserControl
6161

6262
public WebpagePanel()
6363
{
64-
Extensions = SettingHelper.Get("ExtensionList", WebpagePanel.DefaultExtensions, "QuickLook.Plugin.WebViewPlus").Split(',');
6564
DetectEncoding = SettingHelper.Get<bool>("DetectEncoding", false, "QuickLook.Plugin.WebViewPlus");
6665

6766
var unavailReason = WebpagePanel.WebView2UnavailableReason();
@@ -327,9 +326,11 @@ private void FrameNavigationStarting(object sender, CoreWebView2NavigationStarti
327326
_webView.CoreWebView2.PostWebMessageAsString("frameNavigationRejected");
328327
}
329328
}
330-
}
331-
332-
public void UnloadData()
329+
}
330+
331+
// No longer used - Changes in webview2 caused crashes due to
332+
// "WebView2 controller will be auto closed if the parent hwnd is destroyed"
333+
public void UnloadData()
333334
{
334335
_activeFileInfo = null;
335336
_sharedBuffer?.Dispose();
@@ -340,7 +341,7 @@ public void UnloadData()
340341
}
341342
}
342343

343-
public void Dispose() // Not used - unloading only; see above
344+
public void Dispose()
344345
{
345346
_activeFileInfo = null;
346347
_sharedBuffer?.Dispose();

packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Web.WebView2" version="1.0.2651.64" targetFramework="net462" />
3+
<package id="Microsoft.Web.WebView2" version="1.0.3065.39" targetFramework="net462" />
44
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net462" />
55
<package id="UTF.Unknown" version="2.5.1" targetFramework="net462" />
66
</packages>

0 commit comments

Comments
 (0)