Skip to content

Commit

Permalink
better and faster window resize; Add "AllowsTransparency" to plugin i…
Browse files Browse the repository at this point in the history
…nterface; Use IPreviewHandler for Office files
  • Loading branch information
xupefei committed May 21, 2017
1 parent e0a7f38 commit e450971
Show file tree
Hide file tree
Showing 36 changed files with 533 additions and 409 deletions.
8 changes: 0 additions & 8 deletions QuickLook.Installer/QuickLook.Installer.wixproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@
<RefProjectOutputGroups>Binaries;Content;Satellites</RefProjectOutputGroups>
<RefTargetDir>INSTALLFOLDER</RefTargetDir>
</ProjectReference>
<ProjectReference Include="..\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\QuickLook.Plugin.OfficeViewer.csproj">
<Name>QuickLook.Plugin.OfficeViewer</Name>
<Project>{e37675ea-d957-4495-8655-2609bf86756c}</Project>
<Private>True</Private>
<DoNotHarvest>True</DoNotHarvest>
<RefProjectOutputGroups>Binaries;Content;Satellites</RefProjectOutputGroups>
<RefTargetDir>INSTALLFOLDER</RefTargetDir>
</ProjectReference>
<ProjectReference Include="..\QuickLook.Plugin\QuickLook.Plugin.PDFViewer\QuickLook.Plugin.PdfViewer.csproj">
<Name>QuickLook.Plugin.PdfViewer</Name>
<Project>{a82ac69c-edf5-4f0d-8cbd-8e5e3c06e64d}</Project>
Expand Down
2 changes: 1 addition & 1 deletion QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Windows;
using SharpCompress.Archives;

namespace QuickLook.Plugin.ArchiveViewer
{
Expand All @@ -10,6 +9,7 @@ public class Plugin : IViewer
private ArchiveInfoPanel _panel;

public int Priority => 0;
public bool AllowsTransparency => true;

public bool CanHandle(string path)
{
Expand Down
3 changes: 2 additions & 1 deletion QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Plugin : IViewer
private WebkitPanel _panel;

public int Priority => int.MaxValue;
public bool AllowsTransparency => true;

public bool CanHandle(string path)
{
Expand All @@ -30,7 +31,7 @@ public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size(800, 800);

context.Focusable = true;
context.CanFocus = true;
}

public void View(string path, ContextObject context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.InteropServices;

namespace QuickLook.Plugin.IPreviewHandlers
{
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
internal interface IInitializeWithFile
{
void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QuickLook.Plugin.IPreviewHandlers
{
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
internal interface IPreviewHandler
{
void SetWindow(IntPtr hwnd, ref Rectangle rect);
void SetRect(ref Rectangle rect);
void DoPreview();
void Unload();
void SetFocus();
void QueryFocus(out IntPtr phwnd);

[PreserveSig]
uint TranslateAccelerator(ref Message pmsg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace QuickLook.Plugin.IPreviewHandlers
{
public class PluginInterface : IViewer
{
private PreviewPanel _panel;

public int Priority => int.MaxValue;
public bool AllowsTransparency => false;

public bool CanHandle(string path)
{
if (Directory.Exists(path))
return false;

switch (Path.GetExtension(path).ToLower())
{
case ".doc":
case ".docx":
case ".xls":
case ".xlsx":
case ".xlsm":
// Visio Viewer will not quit after preview, which cause serious memory issue
//case ".vsd":
//case ".vsdx":
case ".ppt":
case ".pptx":
return true;
}

return false;
}

public void Prepare(string path, ContextObject context)
{
context.SetPreferredSizeFit(new Size {Width = 800, Height = 800}, 0.8);
}

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

_panel.Loaded += (sender, e) =>
{
_panel.PreviewFile(path);
SetForegroundWindow(new WindowInteropHelper(context.ViewerWindow).Handle);
};

context.IsBusy = false;
}

public void Cleanup()
{
GC.SuppressFinalize(this);

_panel?.Dispose();
_panel = null;
}


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

~PluginInterface()
{
Cleanup();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Preview Handlers Revisted
// Bradley Smith - 2010/09/17, updated 2013/10/14

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;

namespace QuickLook.Plugin.IPreviewHandlers
{
/// <summary>
/// A Windows Forms host for Preview Handlers.
/// </summary>
public class PreviewHandlerHost : Control
{
/// <summary>
/// The GUID for the IShellItem interface.
/// </summary>
internal const string GuidIshellitem = "43826d1e-e718-42ee-bc55-a1e261c37bfe";

private IPreviewHandler _mCurrentPreviewHandler;

/// <summary>
/// Initialialises a new instance of the PreviewHandlerHost class.
/// </summary>
public PreviewHandlerHost()
{
Size = new Size(320, 240);
}

/// <summary>
/// Gets the GUID of the current preview handler.
/// </summary>
[Browsable(false)]
[ReadOnly(true)]
public Guid CurrentPreviewHandler { get; private set; } = Guid.Empty;

/// <summary>
/// Releases the unmanaged resources used by the PreviewHandlerHost and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
UnloadPreviewHandler();

if (_mCurrentPreviewHandler != null)
{
Marshal.FinalReleaseComObject(_mCurrentPreviewHandler);
_mCurrentPreviewHandler = null;
GC.Collect();
}

base.Dispose(disposing);
}

/// <summary>
/// Returns the GUID of the preview handler associated with the specified file.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private Guid GetPreviewHandlerGUID(string filename)
{
// open the registry key corresponding to the file extension
var ext = Registry.ClassesRoot.OpenSubKey(Path.GetExtension(filename));
if (ext != null)
{
// open the key that indicates the GUID of the preview handler type
var test = ext.OpenSubKey("shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));

// sometimes preview handlers are declared on key for the class
var className = Convert.ToString(ext.GetValue(null));
if (className != null)
{
test = Registry.ClassesRoot.OpenSubKey(
className + "\\shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
}
}

return Guid.Empty;
}

/// <summary>
/// Resizes the hosted preview handler when this PreviewHandlerHost is resized.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);

var r = ClientRectangle;
_mCurrentPreviewHandler?.SetRect(ref r);
}

/// <summary>
/// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public bool Open(string path)
{
UnloadPreviewHandler();

if (string.IsNullOrEmpty(path))
return false;

// try to get GUID for the preview handler
var guid = GetPreviewHandlerGUID(path);

if (guid == Guid.Empty)
return false;

CurrentPreviewHandler = guid;
var o = Activator.CreateInstance(Type.GetTypeFromCLSID(CurrentPreviewHandler, true));

var fileInit = o as IInitializeWithFile;

if (fileInit == null)
return false;

fileInit.Initialize(path, 0);
_mCurrentPreviewHandler = o as IPreviewHandler;
if (_mCurrentPreviewHandler == null)
return false;

// bind the preview handler to the control's bounds and preview the content
var r = ClientRectangle;
_mCurrentPreviewHandler.SetWindow(Handle, ref r);
_mCurrentPreviewHandler.DoPreview();

return true;
}

/// <summary>
/// Unloads the preview handler hosted in this PreviewHandlerHost and closes the file stream.
/// </summary>
public void UnloadPreviewHandler()
{
try
{
_mCurrentPreviewHandler?.Unload();
}
catch (Exception)
{
// ignored
}
}
}

#region COM Interop

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<UserControl x:Class="QuickLook.Plugin.IPreviewHandlers.PreviewPanel"
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"
mc:Ignorable="d"
x:Name="panel"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<WindowsFormsHost x:Name="presenter" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Controls;

namespace QuickLook.Plugin.IPreviewHandlers
{
/// <summary>
/// Interaction logic for PreviewPanel.xaml
/// </summary>
public partial class PreviewPanel : UserControl, IDisposable
{
private PreviewHandlerHost _control = new PreviewHandlerHost();

public PreviewPanel()
{
InitializeComponent();
}

public void Dispose()
{
presenter.Child = null;
presenter?.Dispose();

_control?.Dispose();
_control = null;
}

public void PreviewFile(string file)
{
_control = new PreviewHandlerHost();

presenter.Child = _control;

_control.Open(file);

SetActiveWindow(presenter.Handle);
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetActiveWindow(IntPtr hWnd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("QuickLook.Plugin.OfficeViewer")]
[assembly: AssemblyTitle("QuickLook.Plugin.IPreviewHandlers")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("QuickLook.Plugin.OfficeViewer")]
[assembly: AssemblyProduct("QuickLook.Plugin.IPreviewHandlers")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
Loading

0 comments on commit e450971

Please sign in to comment.