better and faster window resize; Add "AllowsTransparency" to plugin interface; Use IPreviewHandler for Office files

This commit is contained in:
Paddy Xu
2017-05-21 22:02:42 +03:00
parent e0a7f38d58
commit e450971217
36 changed files with 533 additions and 409 deletions

View File

@@ -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>

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Windows;
using SharpCompress.Archives;
namespace QuickLook.Plugin.ArchiveViewer
{
@@ -10,6 +9,7 @@ namespace QuickLook.Plugin.ArchiveViewer
private ArchiveInfoPanel _panel;
public int Priority => 0;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{

View File

@@ -9,6 +9,7 @@ namespace QuickLook.Plugin.HtmlViewer
private WebkitPanel _panel;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{
@@ -30,7 +31,7 @@ namespace QuickLook.Plugin.HtmlViewer
{
context.PreferredSize = new Size(800, 800);
context.Focusable = true;
context.CanFocus = true;
}
public void View(string path, ContextObject context)

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -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
}

View File

@@ -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>

View File

@@ -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);
}
}

View File

@@ -5,11 +5,11 @@ using System.Windows;
// 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("")]

View File

@@ -8,7 +8,8 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace QuickLook.Plugin.OfficeViewer.Properties {
namespace QuickLook.Plugin.IPreviewHandlers.Properties {
using System;
/// <summary>
@@ -37,8 +38,8 @@ namespace QuickLook.Plugin.OfficeViewer.Properties {
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if ((resourceMan == null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("QuickLook.Plugin.OfficeViewer.Properties.Resources", typeof(Resources).Assembly);
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("QuickLook.Plugin.IPreviewHandlers.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;

View File

@@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace QuickLook.Plugin.OfficeViewer.Properties {
namespace QuickLook.Plugin.IPreviewHandlers.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]

View File

@@ -6,8 +6,8 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E37675EA-D957-4495-8655-2609BF86756C}</ProjectGuid>
<OutputType>library</OutputType>
<RootNamespace>QuickLook.Plugin.OfficeViewer</RootNamespace>
<AssemblyName>QuickLook.Plugin.OfficeViewer</AssemblyName>
<RootNamespace>QuickLook.Plugin.IPreviewHandlers</RootNamespace>
<AssemblyName>QuickLook.Plugin.IPreviewHandlers</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
@@ -15,7 +15,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<OutputPath>..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.IPreviewHandlers\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
@@ -23,7 +23,7 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\</OutputPath>
<OutputPath>..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.IPreviewHandlers\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
@@ -32,44 +32,35 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.PowerPoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\packages\Microsoft.Office.Interop.PowerPoint.15.0.4420.1017\lib\net20\Microsoft.Office.Interop.PowerPoint.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\packages\Microsoft.Office.Interop.Word.15.0.4797.1003\lib\net20\Microsoft.Office.Interop.Word.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\GitVersion.cs">
<Link>Properties\GitVersion.cs</Link>
</Compile>
<Compile Include="OfficeInteropWrapper.cs" />
<Compile Include="IInitializeWithFile.cs" />
<Compile Include="IPreviewHandler.cs" />
<Compile Include="PluginInterface.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="PreviewHandlerHost.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="PreviewPanel.xaml.cs">
<DependentUpon>PreviewPanel.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@@ -87,7 +78,6 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -99,11 +89,12 @@
<Name>QuickLook</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\QuickLook.Plugin.PDFViewer\QuickLook.Plugin.PdfViewer.csproj">
<Project>{a82ac69c-edf5-4f0d-8cbd-8e5e3c06e64d}</Project>
<Name>QuickLook.Plugin.PdfViewer</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="PreviewPanel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -8,7 +8,8 @@ namespace QuickLook.Plugin.ImageViewer
private Size _imageSize;
private ImagePanel _ip;
public int Priority => 9999;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{

View File

@@ -10,6 +10,7 @@ namespace QuickLook.Plugin.MarkdownViewer
private WebkitPanel _panel;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{
@@ -30,7 +31,7 @@ namespace QuickLook.Plugin.MarkdownViewer
{
context.PreferredSize = new Size(800, 800);
context.Focusable = true;
context.CanFocus = true;
}
public void View(string path, ContextObject context)

View File

@@ -1,189 +0,0 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Excel.Application;
using Task = System.Threading.Tasks.Task;
namespace QuickLook.Plugin.OfficeViewer
{
internal class OfficeInteropWrapper : IDisposable
{
public enum FileTypeEnum
{
Word,
Excel,
PowerPoint
}
private readonly string _path;
private readonly string _tempPdf = Path.GetTempFileName();
private Application _excelApp;
private Microsoft.Office.Interop.PowerPoint.Application _powerpointApp;
private Microsoft.Office.Interop.Word.Application _wordApp;
public OfficeInteropWrapper(string path)
{
_path = path;
switch (Path.GetExtension(path).ToLower())
{
case ".doc":
case ".docx":
FileType = FileTypeEnum.Word;
break;
case ".xls":
case ".xlsx":
case ".xlsm":
FileType = FileTypeEnum.Excel;
break;
case ".ppt":
case ".pptx":
FileType = FileTypeEnum.PowerPoint;
break;
default:
throw new NotSupportedException($"{path} is not supported.");
}
LoadApplication();
}
public FileTypeEnum FileType { get; }
public void Dispose()
{
GC.SuppressFinalize(this);
// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
//_wordApp?.Documents.Close(false);
_wordApp?.Quit(false);
_wordApp = null;
_excelApp?.Workbooks.Close();
_excelApp?.Quit();
_excelApp = null;
_powerpointApp?.Quit();
_powerpointApp = null;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();
}
public string SaveAsPdf()
{
if (_wordApp == null && _excelApp == null && _powerpointApp == null)
throw new Exception("Office application launch failed.");
var succeeded = false;
// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
switch (FileType)
{
case FileTypeEnum.Word:
_wordApp.ActiveDocument.ExportAsFixedFormat(_tempPdf,
WdExportFormat.wdExportFormatPDF);
succeeded = true;
break;
case FileTypeEnum.Excel:
_excelApp.ActiveWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
_tempPdf);
succeeded = true;
break;
case FileTypeEnum.PowerPoint:
_powerpointApp.ActivePresentation.ExportAsFixedFormat(_tempPdf,
PpFixedFormatType.ppFixedFormatTypePDF);
succeeded = true;
break;
default:
throw new NotSupportedException($"{_path} is not supported.");
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();
if (succeeded)
return FileType == FileTypeEnum.Excel
? _tempPdf + ".pdf"
: _tempPdf; // Excel will add ".pdf" to our filename
Dispose();
return string.Empty;
}
private void LoadApplication()
{
var succeeded = false;
// communicate with COM in a separate thread
Task.Run(() =>
{
try
{
switch (FileType)
{
case FileTypeEnum.Word:
_wordApp = new Microsoft.Office.Interop.Word.Application();
_wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
_wordApp.Documents.Open(_path);
succeeded = true;
break;
case FileTypeEnum.Excel:
_excelApp = new Application();
_excelApp.DisplayAlerts = false;
_excelApp.Workbooks.Open(_path);
var worksheets = _excelApp.ActiveWorkbook.Sheets;
if (worksheets != null)
foreach (Worksheet sheet in worksheets)
sheet.PageSetup.PrintGridlines = true;
succeeded = true;
break;
case FileTypeEnum.PowerPoint:
_powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application();
_powerpointApp.DisplayAlerts = PpAlertLevel.ppAlertsNone;
_powerpointApp.Presentations.Open(_path);
succeeded = true;
break;
default:
throw new NotSupportedException($"{_path} is not supported.");
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
})
.Wait();
if (!succeeded)
Dispose();
}
~OfficeInteropWrapper()
{
Dispose();
}
}
}

View File

@@ -1,96 +0,0 @@
using System;
using System.IO;
using System.Windows;
using QuickLook.Plugin.PDFViewer;
namespace QuickLook.Plugin.OfficeViewer
{
public class PluginInterface : IViewer
{
private string _pdfPath = "";
private PdfViewerControl _pdfViewer;
public int Priority => int.MaxValue;
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":
case ".ppt":
case ".pptx":
return true;
}
return false;
}
public void Prepare(string path, ContextObject context)
{
context.SetPreferredSizeFit(new Size {Width = 800, Height = 600}, 0.8);
}
public void View(string path, ContextObject context)
{
using (var officeApp = new OfficeInteropWrapper(path))
{
_pdfPath = officeApp.SaveAsPdf();
}
if (string.IsNullOrEmpty(_pdfPath))
throw new Exception("COM failed.");
_pdfViewer = new PdfViewerControl();
_pdfViewer.Loaded += (sender, e) =>
{
try
{
_pdfViewer.LoadPdf(_pdfPath);
}
catch (Exception ex)
{
throw ex;
}
context.Title = $"{Path.GetFileName(path)} (1 / {_pdfViewer.TotalPages})";
};
_pdfViewer.CurrentPageChanged += (sender, e) => context.Title =
$"{Path.GetFileName(path)} ({_pdfViewer.CurrentPage + 1} / {_pdfViewer.TotalPages})";
context.ViewerContent = _pdfViewer;
context.IsBusy = false;
}
public void Cleanup()
{
GC.SuppressFinalize(this);
// release the Pdf file first
_pdfViewer?.Dispose();
_pdfViewer = null;
try
{
File.Delete(_pdfPath);
}
catch (Exception)
{
// ignored
}
}
~PluginInterface()
{
Cleanup();
}
}
}

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Office.Interop.Excel" version="15.0.4795.1000" targetFramework="net452" />
<package id="Microsoft.Office.Interop.PowerPoint" version="15.0.4420.1017" targetFramework="net452" />
<package id="Microsoft.Office.Interop.Word" version="15.0.4797.1003" targetFramework="net452" />
</packages>

View File

@@ -9,6 +9,7 @@ namespace QuickLook.Plugin.PDFViewer
private PdfViewerControl _pdfControl;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{

View File

@@ -9,6 +9,7 @@ namespace QuickLook.Plugin.TextViewer
private TextViewerPanel _tvp;
public int Priority => 0;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{
@@ -43,7 +44,7 @@ namespace QuickLook.Plugin.TextViewer
public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size {Width = 800, Height = 600};
context.Focusable = true;
context.CanFocus = true;
}
public void View(string path, ContextObject context)

View File

@@ -11,6 +11,7 @@ namespace QuickLook.Plugin.VideoViewer
private ViewerPanel _vp;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public bool CanHandle(string path)
{

View File

@@ -20,8 +20,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.PdfViewer"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.TextViewer", "QuickLook.Plugin\QuickLook.Plugin.TextViewer\QuickLook.Plugin.TextViewer.csproj", "{AE041682-E3A1-44F6-8BB4-916A98D89FBE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.OfficeViewer", "QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\QuickLook.Plugin.OfficeViewer.csproj", "{E37675EA-D957-4495-8655-2609BF86756C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.VideoViewer", "QuickLook.Plugin\QuickLook.Plugin.VideoViewer\QuickLook.Plugin.VideoViewer.csproj", "{1B746D92-49A5-4A37-9D75-DCC490393290}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BAE81497-98FA-4A7A-A0FB-2B86C9694B9C}"
@@ -35,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.MarkdownVi
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "QuickLook.Installer", "QuickLook.Installer\QuickLook.Installer.wixproj", "{F0214FC2-EFBE-426C-842D-B42BC37D9525}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLook.Plugin.IPreviewHandlers", "QuickLook.Plugin\QuickLook.Plugin.IPreviewHandlers\QuickLook.Plugin.IPreviewHandlers.csproj", "{E37675EA-D957-4495-8655-2609BF86756C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -79,12 +79,6 @@ Global
{AE041682-E3A1-44F6-8BB4-916A98D89FBE}.Release|Any CPU.ActiveCfg = Release|x86
{AE041682-E3A1-44F6-8BB4-916A98D89FBE}.Release|x86.ActiveCfg = Release|x86
{AE041682-E3A1-44F6-8BB4-916A98D89FBE}.Release|x86.Build.0 = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|Any CPU.ActiveCfg = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|x86.ActiveCfg = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|x86.Build.0 = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|Any CPU.ActiveCfg = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|x86.ActiveCfg = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|x86.Build.0 = Release|x86
{1B746D92-49A5-4A37-9D75-DCC490393290}.Debug|Any CPU.ActiveCfg = Debug|x86
{1B746D92-49A5-4A37-9D75-DCC490393290}.Debug|x86.ActiveCfg = Debug|x86
{1B746D92-49A5-4A37-9D75-DCC490393290}.Debug|x86.Build.0 = Debug|x86
@@ -108,6 +102,12 @@ Global
{F0214FC2-EFBE-426C-842D-B42BC37D9525}.Release|Any CPU.ActiveCfg = Release|x86
{F0214FC2-EFBE-426C-842D-B42BC37D9525}.Release|x86.ActiveCfg = Release|x86
{F0214FC2-EFBE-426C-842D-B42BC37D9525}.Release|x86.Build.0 = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|Any CPU.ActiveCfg = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|x86.ActiveCfg = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Debug|x86.Build.0 = Debug|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|Any CPU.ActiveCfg = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|x86.ActiveCfg = Release|x86
{E37675EA-D957-4495-8655-2609BF86756C}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -117,9 +117,9 @@ Global
{FE5A5111-9607-4721-A7BE-422754002ED8} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{A82AC69C-EDF5-4F0D-8CBD-8E5E3C06E64D} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{AE041682-E3A1-44F6-8BB4-916A98D89FBE} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{E37675EA-D957-4495-8655-2609BF86756C} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{1B746D92-49A5-4A37-9D75-DCC490393290} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{CE22A1F3-7F2C-4EC8-BFDE-B58D0EB625FC} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{AB1270AF-7EB4-4B4F-9E09-6404F1A28EA0} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
{E37675EA-D957-4495-8655-2609BF86756C} = {06EFDBE0-6408-4B37-BCF2-0CF9EBEA2E93}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace QuickLook.Converters
{
public sealed class BooleanToResizeBorderThicknessConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Visibility.Visible;
var v = (bool) value;
return v ? 6 : 0;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace QuickLook.Converters
{
public sealed class BooleanToVisibilityCollapsedConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Visibility.Visible;
var v = (bool) value;
return v ? Visibility.Visible : Visibility.Collapsed;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Windows;
using System.Windows.Interop;
namespace QuickLook.Helpers.BlurLibrary
{
@@ -36,7 +37,7 @@ namespace QuickLook.Helpers.BlurLibrary
/// <param name="window">Window object</param>
public static void EnableWindowBlur(Window window)
{
EnableWindowBlur(Helpers.GetWindowHandle(window));
EnableWindowBlur(new WindowInteropHelper(window).Handle);
}
private static void DisableWindowBlur(IntPtr hwnd)
@@ -53,7 +54,7 @@ namespace QuickLook.Helpers.BlurLibrary
/// <param name="window">Window object</param>
public static void DisableWindowBlur(Window window)
{
DisableWindowBlur(Helpers.GetWindowHandle(window));
DisableWindowBlur(new WindowInteropHelper(window).Handle);
}
}
}

View File

@@ -0,0 +1,13 @@
using System.Windows.Media;
namespace QuickLook
{
internal class MainWindowNoTransparent : MainWindowTransparent
{
public MainWindowNoTransparent()
{
Background = new SolidColorBrush(Colors.White);
AllowsTransparency = false;
}
}
}

View File

@@ -6,18 +6,24 @@
xmlns:control="clr-namespace:QuickLook.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" x:Class="QuickLook.MainWindow" x:Name="mainWindow"
xmlns:converters="clr-namespace:QuickLook.Converters"
mc:Ignorable="d" x:Class="QuickLook.MainWindowTransparent" x:Name="mainWindow"
UseLayoutRounding="True"
d:DesignWidth="624" d:DesignHeight="700"
MinWidth="275" MinHeight="150"
WindowStartupLocation="CenterScreen"
x:ClassModifier="internal" Focusable="False"
ShowActivated="False" ShowInTaskbar="False" WindowStyle="None"
FontFamily="Segoe UI,Microsoft Yahei UI"
ResizeMode="CanResizeWithGrip" AllowsTransparency="True">
<Window.Background>
<SolidColorBrush Color="#E5FAFAFA" />
</Window.Background>
Focusable="False" WindowStyle="None"
Background="#E5FAFAFA" AllowsTransparency="True"
ShowActivated="False" ShowInTaskbar="False"
FontFamily="Segoe UI,Microsoft Yahei UI">
<Window.Resources>
<converters:BooleanToResizeBorderThicknessConverter x:Key="BooleanToResizeBorderThicknessConverter" />
</Window.Resources>
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="{Binding Height, ElementName=titlebar}"
ResizeBorderThickness="{Binding ContextObject.CanResize, Converter={StaticResource BooleanToResizeBorderThicknessConverter}, ElementName=mainWindow}"
UseAeroCaptionButtons="False" />
</WindowChrome.WindowChrome>
<Border x:Name="windowBorder" BorderThickness="1" BorderBrush="#FF7B7B7B">
<Grid>
<DockPanel x:Name="windowPanel">
@@ -48,10 +54,11 @@
</DockPanel.Style>
<DockPanel x:Name="titlebar" Height="28" Dock="Top">
<fa:ImageAwesome DockPanel.Dock="Right" x:Name="buttonCloseWindow" Icon="TimesCircle"
WindowChrome.IsHitTestVisibleInChrome="True"
Height="15" Margin="10,0" Foreground="Gray"
Cursor="Hand" />
<!-- set grid.background colour makes it clickable -->
<Grid x:Name="titleBarArea" Background="Transparent">
<Grid x:Name="titleArea" Background="Transparent">
<TextBlock Text="{Binding ContextObject.Title, ElementName=mainWindow}" FontSize="14"
HorizontalAlignment="Center"
VerticalAlignment="Center" />

View File

@@ -1,9 +1,7 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using QuickLook.ExtensionMethods;
using QuickLook.Helpers;
using QuickLook.Helpers.BlurLibrary;
using QuickLook.Plugin;
@@ -11,11 +9,11 @@ using QuickLook.Plugin;
namespace QuickLook
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// Interaction logic for MainWindowTransparent.xaml
/// </summary>
internal partial class MainWindow : Window
public partial class MainWindowTransparent : Window
{
internal MainWindow()
internal MainWindowTransparent()
{
// this object should be initialized before loading UI components, because many of which are binding to it.
ContextObject = new ContextObject();
@@ -26,33 +24,17 @@ namespace QuickLook
if (!Debugger.IsAttached)
Topmost = true;
Loaded += (sender, e) => BlurWindow.EnableWindowBlur(this);
SourceInitialized += (sender, e) =>
{
if (AllowsTransparency)
BlurWindow.EnableWindowBlur(this);
};
buttonCloseWindow.MouseLeftButtonUp += (sender, e) => { Hide(); };
titleBarArea.PreviewMouseLeftButtonDown += DragMoveCurrentWindow;
buttonCloseWindow.MouseLeftButtonUp += (sender, e) => Hide();
}
public ContextObject ContextObject { get; private set; }
private void DragMoveCurrentWindow(object sender, MouseButtonEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
var dpi = DpiHelper.GetCurrentDpi();
// MouseDevice.GetPosition() returns device-dependent coordinate, however WPF is not like that
var point = PointToScreen(e.MouseDevice.GetPosition(this));
Left = point.X / (dpi.HorizontalDpi / DpiHelper.DEFAULT_DPI) - RestoreBounds.Width * 0.5;
Top = point.Y / (dpi.VerticalDpi / DpiHelper.DEFAULT_DPI);
WindowState = WindowState.Normal;
}
DragMove();
}
private new void Show()
{
// revert UI changes
@@ -65,11 +47,9 @@ namespace QuickLook
ResizeAndCenter(new Size(newWidth, newHeight));
ResizeMode = ContextObject.CanResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize;
base.Show();
//if (!ContextObject.Focusable)
//if (!ContextObject.CanFocus)
// WindowHelper.SetNoactivate(new WindowInteropHelper(this));
}
@@ -125,6 +105,7 @@ namespace QuickLook
{
ContextObject.CurrentContentContainer = container;
ContextObject.ViewerPlugin = matchedPlugin;
ContextObject.ViewerWindow = this;
// get window size before showing it
ContextObject.ViewerPlugin.Prepare(path, ContextObject);

View File

@@ -21,9 +21,11 @@ namespace QuickLook
return -1;
var ppid = -1;
using (var file = File.Open(pid, FileMode.Open, FileAccess.Read,FileShare.ReadWrite))
using (var file = File.Open(pid, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(file))
{
int.TryParse(sr.ReadToEnd(), out ppid);
}
try
{

View File

@@ -13,12 +13,19 @@ namespace QuickLook.Plugin
/// </summary>
public class ContextObject : INotifyPropertyChanged
{
private bool _isBusy = true;
private bool _canFocus;
private bool _canResize = true;
private bool _isBusy = true;
private string _title = "";
internal ContentControl CurrentContentContainer;
internal IViewer ViewerPlugin;
/// <summary>
/// Get the viewer window.
/// </summary>
public MainWindowTransparent ViewerWindow { get; internal set; }
/// <summary>
/// Get or set the title of Viewer window.
/// </summary>
@@ -62,12 +69,28 @@ namespace QuickLook.Plugin
/// <summary>
/// Set whether user are allowed to resize the viewer window.
/// </summary>
public bool CanResize { get; set; } = true;
public bool CanResize
{
get => _canResize;
set
{
_canResize = value;
OnPropertyChanged();
}
}
/// <summary>
/// Set whether user are allowed to set focus at the viewer window.
/// </summary>
public bool Focusable { get; set; }
public bool CanFocus
{
get => _canFocus;
set
{
_canFocus = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
@@ -122,12 +145,13 @@ namespace QuickLook.Plugin
internal void Reset()
{
ViewerWindow = null;
Title = "";
ViewerContent = null;
IsBusy = true;
PreferredSize = new Size();
CanResize = true;
Focusable = false;
CanFocus = false;
}
[NotifyPropertyChangedInvocator]

View File

@@ -11,6 +11,11 @@
/// </summary>
int Priority { get; }
/// <summary>
/// Set whether the viewer window has blur effect.
/// </summary>
bool AllowsTransparency { get; }
/// <summary>
/// Determine whether this plugin can open this file. Please also check the file header, if applicable.
/// </summary>

View File

@@ -7,6 +7,7 @@ namespace QuickLook.Plugin.InfoPanel
private InfoPanel _ip;
public int Priority => int.MinValue;
public bool AllowsTransparency => true;
public bool CanHandle(string sample)
{
@@ -16,15 +17,15 @@ namespace QuickLook.Plugin.InfoPanel
public void Prepare(string path, ContextObject context)
{
context.PreferredSize = new Size {Width = 453, Height = 172};
context.Title = "";
context.CanResize = false;
}
public void View(string path, ContextObject context)
{
_ip = new InfoPanel();
context.Title = "";
context.ViewerContent = _ip;
context.CanResize = false;
_ip.DisplayInfo(path);

View File

@@ -80,6 +80,8 @@
<Compile Include="..\GitVersion.cs">
<Link>Properties\GitVersion.cs</Link>
</Compile>
<Compile Include="Converters\BooleanToVisibilityCollapsedConverter.cs" />
<Compile Include="Converters\BooleanToResizeBorderThicknessConverter.cs" />
<Compile Include="Helpers\AutoStartupHelper.cs" />
<Compile Include="Controls\BackgroundVisualHost.cs" />
<Compile Include="Controls\BusyDecorator.cs" />
@@ -103,6 +105,7 @@
<Compile Include="Helpers\BlurLibrary\PlatformsImpl\Windows81WindowBlurController.cs" />
<Compile Include="Helpers\BlurLibrary\PlatformsImpl\Windows8WindowBlurController.cs" />
<Compile Include="Helpers\BlurLibrary\PlatformsImpl\WindowsVistaWindowBlurController.cs" />
<Compile Include="MainWindowNoTransparent.cs" />
<Compile Include="NativeMethods\ShellLink.cs" />
<Compile Include="PidHelper.cs" />
<Compile Include="PluginManager.cs" />
@@ -128,7 +131,7 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Page Include="MainWindowTransparent.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
@@ -142,8 +145,8 @@
<Compile Include="NativeMethods\User32.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="GlobalKeyboardHook.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<Compile Include="MainWindowTransparent.xaml.cs">
<DependentUpon>MainWindowTransparent.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Styles\BusyDecorator.xaml">

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using QuickLook.Helpers;
using QuickLook.Plugin;
@@ -12,24 +13,29 @@ namespace QuickLook
internal class ViewWindowManager
{
private static ViewWindowManager _instance;
private readonly MainWindowNoTransparent _viewWindowNoTransparent;
private readonly MainWindowTransparent _viewWindowTransparentTransparent;
private readonly MainWindow _viewWindow;
private MainWindowTransparent _currentMainWindow;
internal ViewWindowManager()
{
_viewWindow = new MainWindow();
_viewWindowTransparentTransparent = new MainWindowTransparent();
_viewWindowNoTransparent = new MainWindowNoTransparent();
_currentMainWindow = _viewWindowTransparentTransparent;
}
internal void InvokeRoutine(bool replaceView = false)
{
if (replaceView && _viewWindow.IsLoaded && _viewWindow.Visibility != System.Windows.Visibility.Visible)
if (replaceView && _currentMainWindow.IsLoaded && _currentMainWindow.Visibility != Visibility.Visible)
return;
if (!WindowHelper.IsFocusedControlExplorerItem())
if (!WindowHelper.IsFocusedWindowSelf())
return;
if (!replaceView && _viewWindow.BeginHide())
if (!replaceView && _currentMainWindow.BeginHide())
return;
var path = GetCurrentSelection();
@@ -53,12 +59,21 @@ namespace QuickLook
{
try
{
_viewWindow.UnloadPlugin();
_viewWindow.BeginShow(matchedPlugin, path);
_currentMainWindow.UnloadPlugin();
// switch window
var oldWindow = _currentMainWindow;
_currentMainWindow = matchedPlugin.AllowsTransparency
? _viewWindowTransparentTransparent
: _viewWindowNoTransparent;
if (!ReferenceEquals(oldWindow, _currentMainWindow))
oldWindow.BeginHide();
_currentMainWindow.BeginShow(matchedPlugin, path);
}
catch (Exception e) // if current plugin failed, switch to default one.
{
_viewWindow.BeginHide();
_currentMainWindow.BeginHide();
TrayIconManager.GetInstance().ShowNotification("", $"Failed to preview {Path.GetFileName(path)}", true);