Auto resize PDF viewer window

This commit is contained in:
ema
2024-12-13 15:26:11 +08:00
parent cb59a3d082
commit a82a93d2d9

View File

@@ -16,9 +16,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using PdfiumViewer; using PdfiumViewer;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin; using QuickLook.Common.Plugin;
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
@@ -98,9 +100,23 @@ public class Plugin : IViewer
{ {
var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path, password); var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path, password);
context.SetPreferredSizeFit(desiredSize, 0.9); // Actually it is no longer effective here context.SetPreferredSizeFit(desiredSize, 0.9); // Actually it is no longer effective here
context.ViewerContent = _pdfControl; 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; context.IsBusy = true;
_pdfControl.LoadPdf(path, password); _pdfControl.LoadPdf(path, password);