forked from QL-Win/QuickLook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better and faster window resize; Add "AllowsTransparency" to plugin i…
…nterface; Use IPreviewHandler for Office files
- Loading branch information
Showing
36 changed files
with
533 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IInitializeWithFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IPreviewHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PluginInterface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewHandlerHost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
12 changes: 12 additions & 0 deletions
12
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
43 changes: 43 additions & 0 deletions
43
QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.