From a82a93d2d9323d511769eb758c08b0df9d18dfc6 Mon Sep 17 00:00:00 2001 From: ema Date: Fri, 13 Dec 2024 15:26:11 +0800 Subject: [PATCH] Auto resize PDF viewer window --- .../QuickLook.Plugin.PDFViewer/Plugin.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs index 30cbf3b..d1d6f20 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs @@ -16,9 +16,11 @@ // along with this program. If not, see . using PdfiumViewer; +using QuickLook.Common.Helpers; using QuickLook.Common.Plugin; using System; using System.IO; +using System.Reflection; using System.Runtime.ExceptionServices; using System.Text; using System.Windows; @@ -98,9 +100,23 @@ public class Plugin : IViewer { var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path, password); context.SetPreferredSizeFit(desiredSize, 0.9); // Actually it is no longer effective here - context.ViewerContent = _pdfControl; + // Call the viewer window private method using reflection + // QuickLook.ViewerWindow.ResizeAndCentreExistingWindow + if (Window.GetWindow(_pdfControl) is Window window) + { + var ResizeAndCentreExistingWindow = window.GetType().GetMethod("ResizeAndCentreExistingWindow", + BindingFlags.NonPublic | BindingFlags.Instance); + + if (ResizeAndCentreExistingWindow != null) + { + var newRect = (Rect)ResizeAndCentreExistingWindow.Invoke(window, [context.PreferredSize]); + + window.MoveWindow(newRect.Left, newRect.Top, newRect.Width, newRect.Height); + } + } + context.IsBusy = true; _pdfControl.LoadPdf(path, password);