Skip to content

Commit

Permalink
Add Edge WebView2 as an alternative.
Browse files Browse the repository at this point in the history
WebView2 is the default. To use old set useOldViewer to true in config file.
WebView2 will also preview svg, txt and pdf files.
  • Loading branch information
mooflu committed Jan 10, 2021
1 parent cc890aa commit 99c8e4d
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 3 deletions.
18 changes: 18 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/BrowserPanel.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<UserControl x:Class="QuickLook.Plugin.HtmlViewer.BrowserPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:local="clr-namespace:QuickLook.Plugin.HtmlViewer"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<wv2:CoreWebView2CreationProperties x:Key="EvergreenWebView2CreationProperties" />
</UserControl.Resources>
<wv2:WebView2
x:Name="webView"
CreationProperties="{StaticResource EvergreenWebView2CreationProperties}"
NavigationStarting="NavigationStarting"
/>
</UserControl>
59 changes: 59 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/BrowserPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.Web.WebView2.Core;
using System;
using System.IO;
using System.Windows.Controls;

namespace QuickLook.Plugin.HtmlViewer
{
/// <summary>
/// Interaction logic for BrowserPanel.xaml
/// </summary>
public partial class BrowserPanel : UserControl
{
private Uri _currentUri;

public BrowserPanel()
{
InitializeComponent();
}

public void LoadFile(string path)
{
if (Path.IsPathRooted(path))
path = Helper.FilePathToFileUrl(path);

Navigate(path);
}

public void Navigate(string uri)
{
if (!string.IsNullOrWhiteSpace(uri) && Uri.IsWellFormedUriString(uri, UriKind.Absolute))
try
{
webView.Source = new Uri(uri);
}
catch (UriFormatException)
{
// just don't crash because of a malformed url
}
else
webView.Source = null;

_currentUri = webView.Source;
}

public void Dispose()
{
webView.Dispose();
}

void NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
var newUri = new Uri(e.Uri);
if (newUri != _currentUri)
{
e.Cancel = true;
}
}
}
}
90 changes: 87 additions & 3 deletions QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,61 @@
using System.Windows;
using System.Windows.Threading;
using QuickLook.Common.Plugin;
using QuickLook.Common.Helpers;

namespace QuickLook.Plugin.HtmlViewer
{
public class Plugin : IViewer
internal class WebviewPluginImpl : IViewer
{
private static readonly string[] Extensions = { ".mht", ".mhtml", ".htm", ".html" };
public int Priority => 0;
private static readonly string[] Extensions = { ".mht", ".mhtml", ".htm", ".html", ".svg", ".txt", ".pdf" };
private static readonly string[] SupportedProtocols = { "http", "https" };

private WebpagePanel _panel;
private BrowserPanel _panel;

public void Init()
{
}

public bool CanHandle(string path)
{
return !Directory.Exists(path) && (Extensions.Any(path.ToLower().EndsWith) || (path.ToLower().EndsWith(".url") && SupportedProtocols.Contains(Helper.GetUrlPath(path).Split(':')[0].ToLower())));
}

public void Prepare(string path, ContextObject context)
{
var desiredSize = new Size(1200, 1600);
context.SetPreferredSizeFit(desiredSize, 0.8);
}

public void View(string path, ContextObject context)
{
_panel = new BrowserPanel();
context.ViewerContent = _panel;
context.Title = Path.IsPathRooted(path) ? Path.GetFileName(path) : path;

if (path.ToLower().EndsWith(".url"))
{
path = Helper.GetUrlPath(path);
}
_panel.LoadFile(path);
_panel.Dispatcher.Invoke(() => { context.IsBusy = false; }, DispatcherPriority.Loaded);
}

public void Cleanup()
{
_panel?.Dispose();
_panel = null;
}

}
internal class OldWebpagePluginImpl : IViewer
{
public int Priority => 0;
private static readonly string[] Extensions = { ".mht", ".mhtml", ".htm", ".html" };
private static readonly string[] SupportedProtocols = { "http", "https" };

private WebpagePanel _panel;

public void Init()
{
Expand Down Expand Up @@ -68,4 +112,44 @@ public void Cleanup()
_panel = null;
}
}
public class Plugin : IViewer
{
public int Priority => 0;
private static IViewer _impl;

public void Init()
{
var useOldViewer = SettingHelper.Get("useOldViewer", false);
if (useOldViewer)
{
_impl = new OldWebpagePluginImpl();
}
else
{
_impl = new WebviewPluginImpl();

}
_impl.Init();
}

public bool CanHandle(string path)
{
return _impl.CanHandle(path);
}

public void Prepare(string path, ContextObject context)
{
_impl.Prepare(path, context);
}

public void View(string path, ContextObject context)
{
_impl.View(path, context);
}

public void Cleanup()
{
_impl.Cleanup();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.WinForms.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.664.37, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Web.WebView2.1.0.664.37\lib\net462\Microsoft.Web.WebView2.Wpf.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
Expand All @@ -72,6 +81,9 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="BrowserPanel.xaml.cs">
<DependentUpon>BrowserPanel.xaml</DependentUpon>
</Compile>
<Compile Include="WebpagePanel.cs" />
<Compile Include="WpfBrowserWrapper.cs" />
<Compile Include="..\..\GitVersion.cs">
Expand Down Expand Up @@ -108,5 +120,21 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Page Include="BrowserPanel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets" Condition="Exists('..\..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<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>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Web.WebView2.1.0.664.37\build\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
4 changes: 4 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.WebView2" version="1.0.664.37" targetFramework="net462" />
</packages>

0 comments on commit 99c8e4d

Please # to comment.