diff --git a/QuickLook.Installer/QuickLook.Installer.wixproj b/QuickLook.Installer/QuickLook.Installer.wixproj index 82625db..42482d0 100644 --- a/QuickLook.Installer/QuickLook.Installer.wixproj +++ b/QuickLook.Installer/QuickLook.Installer.wixproj @@ -64,14 +64,6 @@ Binaries;Content;Satellites INSTALLFOLDER - - QuickLook.Plugin.OfficeViewer - {e37675ea-d957-4495-8655-2609bf86756c} - True - True - Binaries;Content;Satellites - INSTALLFOLDER - QuickLook.Plugin.PdfViewer {a82ac69c-edf5-4f0d-8cbd-8e5e3c06e64d} diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs index e479e60..ce66728 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs @@ -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) { diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs index ca3f94b..448367d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs @@ -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) diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IInitializeWithFile.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IInitializeWithFile.cs new file mode 100644 index 0000000..7c4caed --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IInitializeWithFile.cs @@ -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); + } +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IPreviewHandler.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IPreviewHandler.cs new file mode 100644 index 0000000..ffdf3a1 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/IPreviewHandler.cs @@ -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); + } +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PluginInterface.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PluginInterface.cs new file mode 100644 index 0000000..1fb61d8 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PluginInterface.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewHandlerHost.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewHandlerHost.cs new file mode 100644 index 0000000..2c5c22f --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewHandlerHost.cs @@ -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 +{ + /// + /// A Windows Forms host for Preview Handlers. + /// + public class PreviewHandlerHost : Control + { + /// + /// The GUID for the IShellItem interface. + /// + internal const string GuidIshellitem = "43826d1e-e718-42ee-bc55-a1e261c37bfe"; + + private IPreviewHandler _mCurrentPreviewHandler; + + /// + /// Initialialises a new instance of the PreviewHandlerHost class. + /// + public PreviewHandlerHost() + { + Size = new Size(320, 240); + } + + /// + /// Gets the GUID of the current preview handler. + /// + [Browsable(false)] + [ReadOnly(true)] + public Guid CurrentPreviewHandler { get; private set; } = Guid.Empty; + + /// + /// Releases the unmanaged resources used by the PreviewHandlerHost and optionally releases the managed resources. + /// + /// + protected override void Dispose(bool disposing) + { + UnloadPreviewHandler(); + + if (_mCurrentPreviewHandler != null) + { + Marshal.FinalReleaseComObject(_mCurrentPreviewHandler); + _mCurrentPreviewHandler = null; + GC.Collect(); + } + + base.Dispose(disposing); + } + + /// + /// Returns the GUID of the preview handler associated with the specified file. + /// + /// + /// + 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; + } + + /// + /// Resizes the hosted preview handler when this PreviewHandlerHost is resized. + /// + /// + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + + var r = ClientRectangle; + _mCurrentPreviewHandler?.SetRect(ref r); + } + + /// + /// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost. + /// + /// + /// + 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; + } + + /// + /// Unloads the preview handler hosted in this PreviewHandlerHost and closes the file stream. + /// + public void UnloadPreviewHandler() + { + try + { + _mCurrentPreviewHandler?.Unload(); + } + catch (Exception) + { + // ignored + } + } + } + + #region COM Interop + + #endregion +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml new file mode 100644 index 0000000..004b288 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs new file mode 100644 index 0000000..0c165cb --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs @@ -0,0 +1,43 @@ +using System; +using System.Runtime.InteropServices; +using System.Windows.Controls; + +namespace QuickLook.Plugin.IPreviewHandlers +{ + /// + /// Interaction logic for PreviewPanel.xaml + /// + 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); + } +} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/AssemblyInfo.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/AssemblyInfo.cs similarity index 93% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/AssemblyInfo.cs rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/AssemblyInfo.cs index c19697e..e862382 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/AssemblyInfo.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/AssemblyInfo.cs @@ -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("")] diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Resources.Designer.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Resources.Designer.cs similarity index 92% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Resources.Designer.cs rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Resources.Designer.cs index 784867b..9a0aa79 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Resources.Designer.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Resources.Designer.cs @@ -8,7 +8,8 @@ // //------------------------------------------------------------------------------ -namespace QuickLook.Plugin.OfficeViewer.Properties { +namespace QuickLook.Plugin.IPreviewHandlers.Properties { + using System; /// @@ -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; diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Resources.resx b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Resources.resx similarity index 100% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Resources.resx rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Resources.resx diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Settings.Designer.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Settings.Designer.cs similarity index 94% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Settings.Designer.cs rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Settings.Designer.cs index ac678b1..96331b4 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Settings.Designer.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Settings.Designer.cs @@ -8,7 +8,7 @@ // //------------------------------------------------------------------------------ -namespace QuickLook.Plugin.OfficeViewer.Properties { +namespace QuickLook.Plugin.IPreviewHandlers.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Settings.settings b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Settings.settings similarity index 100% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/Properties/Settings.settings rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Properties/Settings.settings diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/QuickLook.Plugin.OfficeViewer.csproj b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/QuickLook.Plugin.IPreviewHandlers.csproj similarity index 64% rename from QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/QuickLook.Plugin.OfficeViewer.csproj rename to QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/QuickLook.Plugin.IPreviewHandlers.csproj index 11a0174..134cc2d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/QuickLook.Plugin.OfficeViewer.csproj +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/QuickLook.Plugin.IPreviewHandlers.csproj @@ -6,8 +6,8 @@ AnyCPU {E37675EA-D957-4495-8655-2609BF86756C} library - QuickLook.Plugin.OfficeViewer - QuickLook.Plugin.OfficeViewer + QuickLook.Plugin.IPreviewHandlers + QuickLook.Plugin.IPreviewHandlers v4.5.2 512 {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} @@ -15,7 +15,7 @@ true - ..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\ + ..\..\Build\Debug\QuickLook.Plugin\QuickLook.Plugin.IPreviewHandlers\ DEBUG;TRACE full x86 @@ -23,7 +23,7 @@ MinimumRecommendedRules.ruleset - ..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.OfficeViewer\ + ..\..\Build\Release\QuickLook.Plugin\QuickLook.Plugin.IPreviewHandlers\ TRACE true pdbonly @@ -32,44 +32,35 @@ MinimumRecommendedRules.ruleset - - True - ..\..\packages\Microsoft.Office.Interop.Excel.15.0.4795.1000\lib\net20\Microsoft.Office.Interop.Excel.dll - True - - - True - ..\..\packages\Microsoft.Office.Interop.PowerPoint.15.0.4420.1017\lib\net20\Microsoft.Office.Interop.PowerPoint.dll - True - - - True - ..\..\packages\Microsoft.Office.Interop.Word.15.0.4797.1003\lib\net20\Microsoft.Office.Interop.Word.dll - True - - - True - - + + 4.0 + Properties\GitVersion.cs - + + Code + + Component + + + PreviewPanel.xaml + Code @@ -87,7 +78,6 @@ ResXFileCodeGenerator Resources.Designer.cs - SettingsSingleFileGenerator Settings.Designer.cs @@ -99,11 +89,12 @@ QuickLook False - - {a82ac69c-edf5-4f0d-8cbd-8e5e3c06e64d} - QuickLook.Plugin.PdfViewer - False - + + + + Designer + MSBuild:Compile + \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index e222287..d65a709 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -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) { diff --git a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs index f3c98b4..c40f1e4 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs @@ -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) diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/OfficeInteropWrapper.cs b/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/OfficeInteropWrapper.cs deleted file mode 100644 index bfd4571..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/OfficeInteropWrapper.cs +++ /dev/null @@ -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(); - } - } -} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs b/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs deleted file mode 100644 index cec37f6..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs +++ /dev/null @@ -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(); - } - } -} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/packages.config b/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/packages.config deleted file mode 100644 index a1e4fad..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/packages.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs index ee3f990..0eee92f 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs @@ -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) { diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index 72a75bb..7c3fd6c 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -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) diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs index d0f6356..da09636 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs @@ -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) { diff --git a/QuickLook.sln b/QuickLook.sln index efe5f87..6700d6e 100644 --- a/QuickLook.sln +++ b/QuickLook.sln @@ -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 diff --git a/QuickLook/Converters/BooleanToResizeBorderThicknessConverter.cs b/QuickLook/Converters/BooleanToResizeBorderThicknessConverter.cs new file mode 100644 index 0000000..c77d1a0 --- /dev/null +++ b/QuickLook/Converters/BooleanToResizeBorderThicknessConverter.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/QuickLook/Converters/BooleanToVisibilityCollapsedConverter.cs b/QuickLook/Converters/BooleanToVisibilityCollapsedConverter.cs new file mode 100644 index 0000000..bc26cab --- /dev/null +++ b/QuickLook/Converters/BooleanToVisibilityCollapsedConverter.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/QuickLook/Helpers/BlurLibrary/BlurWindow.cs b/QuickLook/Helpers/BlurLibrary/BlurWindow.cs index c1a6183..3e996de 100644 --- a/QuickLook/Helpers/BlurLibrary/BlurWindow.cs +++ b/QuickLook/Helpers/BlurLibrary/BlurWindow.cs @@ -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 /// Window object 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 /// Window object public static void DisableWindowBlur(Window window) { - DisableWindowBlur(Helpers.GetWindowHandle(window)); + DisableWindowBlur(new WindowInteropHelper(window).Handle); } } } \ No newline at end of file diff --git a/QuickLook/MainWindowNoTransparent.cs b/QuickLook/MainWindowNoTransparent.cs new file mode 100644 index 0000000..a213792 --- /dev/null +++ b/QuickLook/MainWindowNoTransparent.cs @@ -0,0 +1,13 @@ +using System.Windows.Media; + +namespace QuickLook +{ + internal class MainWindowNoTransparent : MainWindowTransparent + { + public MainWindowNoTransparent() + { + Background = new SolidColorBrush(Colors.White); + AllowsTransparency = false; + } + } +} \ No newline at end of file diff --git a/QuickLook/MainWindow.xaml b/QuickLook/MainWindowTransparent.xaml similarity index 86% rename from QuickLook/MainWindow.xaml rename to QuickLook/MainWindowTransparent.xaml index 380c3b7..5f650f3 100644 --- a/QuickLook/MainWindow.xaml +++ b/QuickLook/MainWindowTransparent.xaml @@ -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"> - - - + Focusable="False" WindowStyle="None" + Background="#E5FAFAFA" AllowsTransparency="True" + ShowActivated="False" ShowInTaskbar="False" + FontFamily="Segoe UI,Microsoft Yahei UI"> + + + + + + @@ -48,10 +54,11 @@ - + diff --git a/QuickLook/MainWindow.xaml.cs b/QuickLook/MainWindowTransparent.xaml.cs similarity index 76% rename from QuickLook/MainWindow.xaml.cs rename to QuickLook/MainWindowTransparent.xaml.cs index 5a1bbf5..f5b4118 100644 --- a/QuickLook/MainWindow.xaml.cs +++ b/QuickLook/MainWindowTransparent.xaml.cs @@ -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 { /// - /// Interaction logic for MainWindow.xaml + /// Interaction logic for MainWindowTransparent.xaml /// - 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); diff --git a/QuickLook/PidHelper.cs b/QuickLook/PidHelper.cs index 9dbe811..28b7721 100644 --- a/QuickLook/PidHelper.cs +++ b/QuickLook/PidHelper.cs @@ -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 { diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index 9c23159..7e39a77 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -13,12 +13,19 @@ namespace QuickLook.Plugin /// 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; + /// + /// Get the viewer window. + /// + public MainWindowTransparent ViewerWindow { get; internal set; } + /// /// Get or set the title of Viewer window. /// @@ -62,12 +69,28 @@ namespace QuickLook.Plugin /// /// Set whether user are allowed to resize the viewer window. /// - public bool CanResize { get; set; } = true; + public bool CanResize + { + get => _canResize; + set + { + _canResize = value; + OnPropertyChanged(); + } + } /// /// Set whether user are allowed to set focus at the viewer window. /// - 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] diff --git a/QuickLook/Plugin/IViewer.cs b/QuickLook/Plugin/IViewer.cs index cf2eb5f..94277c8 100644 --- a/QuickLook/Plugin/IViewer.cs +++ b/QuickLook/Plugin/IViewer.cs @@ -11,6 +11,11 @@ /// int Priority { get; } + /// + /// Set whether the viewer window has blur effect. + /// + bool AllowsTransparency { get; } + /// /// Determine whether this plugin can open this file. Please also check the file header, if applicable. /// diff --git a/QuickLook/Plugin/InfoPanel/PluginInterface.cs b/QuickLook/Plugin/InfoPanel/PluginInterface.cs index c39c13f..1a3fb46 100644 --- a/QuickLook/Plugin/InfoPanel/PluginInterface.cs +++ b/QuickLook/Plugin/InfoPanel/PluginInterface.cs @@ -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); diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index ff59b66..7bb8d27 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -80,6 +80,8 @@ Properties\GitVersion.cs + + @@ -103,6 +105,7 @@ + @@ -128,7 +131,7 @@ Designer MSBuild:Compile - + MSBuild:Compile Designer @@ -142,8 +145,8 @@ - - MainWindow.xaml + + MainWindowTransparent.xaml Code diff --git a/QuickLook/ViewWindowManager.cs b/QuickLook/ViewWindowManager.cs index 9e205e7..0c52de5 100644 --- a/QuickLook/ViewWindowManager.cs +++ b/QuickLook/ViewWindowManager.cs @@ -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);