-
Notifications
You must be signed in to change notification settings - Fork 7
/
Plugin.cs
101 lines (82 loc) · 2.8 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.PDFViewer;
namespace QuickLook.Plugin.PostScriptViewer
{
public class Plugin : IViewer
{
private static readonly string[] Extensions = {".ps", ".eps"};
private MemoryStream _buffer;
private ContextObject _context;
private string _path;
private PdfViewerControl _pdfControl;
public int Priority => 0;
public void Init()
{
}
public bool CanHandle(string path)
{
return !Directory.Exists(path) && Extensions.Any(path.ToLower().EndsWith);
}
public void Prepare(string path, ContextObject context)
{
_context = context;
_path = path;
var width = 800d;
var height = 600d;
var pages = GhostScriptWrapper.GetPageSizes(path);
pages?.ForEach(p =>
{
width = Math.Max(width, p.Width);
height = Math.Max(height, p.Height);
});
context.SetPreferredSizeFit(new Size(width, height), 0.8);
}
public void View(string path, ContextObject context)
{
_pdfControl = new PdfViewerControl();
context.ViewerContent = _pdfControl;
_buffer = GhostScriptWrapper.ConvertToPdf(path);
if (_buffer == null || _buffer.Length == 0)
{
context.ViewerContent = new Label {Content = "Conversion to PDF failed."};
context.IsBusy = false;
return;
}
Exception exception = null;
_pdfControl.Dispatcher.BeginInvoke(new Action(() =>
{
try
{
_pdfControl.LoadPdf(_buffer);
context.Title = $"1 / {_pdfControl.TotalPages}: {Path.GetFileName(path)}";
_pdfControl.CurrentPageChanged += UpdateWindowCaption;
context.IsBusy = false;
}
catch (Exception e)
{
exception = e;
}
}), DispatcherPriority.Loaded).Wait();
if (exception != null)
ExceptionDispatchInfo.Capture(exception).Throw();
}
public void Cleanup()
{
_pdfControl?.Dispose();
_pdfControl = null;
_context = null;
_buffer = null;
}
private void UpdateWindowCaption(object sender, EventArgs e2)
{
_context.Title = $"{_pdfControl.CurrentPage + 1} / {_pdfControl.TotalPages}: {Path.GetFileName(_path)}";
}
}
}