Showing
3 changed files
with
26 additions
and
11 deletions
@@ -31,6 +31,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -31,6 +31,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
31 | private var page: CGPDFPage? | 31 | private var page: CGPDFPage? |
32 | private let semaphore = DispatchSemaphore(value: 0) | 32 | private let semaphore = DispatchSemaphore(value: 0) |
33 | private var dynamic = false | 33 | private var dynamic = false |
34 | + private var _window: NSWindow? | ||
34 | 35 | ||
35 | public init(printing: PrintingPlugin, index: Int) { | 36 | public init(printing: PrintingPlugin, index: Int) { |
36 | self.printing = printing | 37 | self.printing = printing |
@@ -100,8 +101,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -100,8 +101,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
100 | } | 101 | } |
101 | 102 | ||
102 | DispatchQueue.main.async { | 103 | DispatchQueue.main.async { |
103 | - let window = NSApplication.shared.mainWindow! | ||
104 | - self.printOperation!.runModal(for: window, delegate: self, didRun: #selector(self.printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil) | 104 | + self.printOperation!.runModal(for: self._window!, delegate: self, didRun: #selector(self.printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil) |
105 | } | 105 | } |
106 | } | 106 | } |
107 | 107 | ||
@@ -130,8 +130,9 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -130,8 +130,9 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
130 | return printers | 130 | return printers |
131 | } | 131 | } |
132 | 132 | ||
133 | - public func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect, withPrinter printer: String?, dynamically dyn: Bool) { | 133 | + public func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect, withPrinter printer: String?, dynamically dyn: Bool, andWindow window: NSWindow) { |
134 | dynamic = dyn | 134 | dynamic = dyn |
135 | + _window = window | ||
135 | let sharedInfo = NSPrintInfo.shared | 136 | let sharedInfo = NSPrintInfo.shared |
136 | let sharedDict = sharedInfo.dictionary() | 137 | let sharedDict = sharedInfo.dictionary() |
137 | let printInfoDict = NSMutableDictionary(dictionary: sharedDict) | 138 | let printInfoDict = NSMutableDictionary(dictionary: sharedDict) |
@@ -153,9 +154,8 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -153,9 +154,8 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
153 | } | 154 | } |
154 | 155 | ||
155 | if dynamic { | 156 | if dynamic { |
156 | - let window = NSApplication.shared.mainWindow! | ||
157 | printOperation!.printPanel.options = [.showsPreview, .showsPaperSize, .showsOrientation] | 157 | printOperation!.printPanel.options = [.showsPreview, .showsPaperSize, .showsOrientation] |
158 | - printOperation!.runModal(for: window, delegate: self, didRun: #selector(printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil) | 158 | + printOperation!.runModal(for: _window!, delegate: self, didRun: #selector(printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil) |
159 | return | 159 | return |
160 | } | 160 | } |
161 | 161 | ||
@@ -179,7 +179,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -179,7 +179,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
179 | } | 179 | } |
180 | } | 180 | } |
181 | 181 | ||
182 | - public static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String) { | 182 | + public static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String, andWindow view: NSView) { |
183 | let tempFile = NSTemporaryDirectory() + name | 183 | let tempFile = NSTemporaryDirectory() + name |
184 | let file = NSURL(fileURLWithPath: tempFile) | 184 | let file = NSURL(fileURLWithPath: tempFile) |
185 | 185 | ||
@@ -190,7 +190,6 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -190,7 +190,6 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
190 | return | 190 | return |
191 | } | 191 | } |
192 | 192 | ||
193 | - let view = NSApplication.shared.mainWindow!.contentView! | ||
194 | let sharingServicePicker = NSSharingServicePicker(items: [file]) | 193 | let sharingServicePicker = NSSharingServicePicker(items: [file]) |
195 | sharingServicePicker.show(relativeTo: rect, of: view, preferredEdge: NSRectEdge.maxY) | 194 | sharingServicePicker.show(relativeTo: rect, of: view, preferredEdge: NSRectEdge.maxY) |
196 | 195 |
@@ -22,9 +22,11 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -22,9 +22,11 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
22 | private static var instance: PrintingPlugin? | 22 | private static var instance: PrintingPlugin? |
23 | private var channel: FlutterMethodChannel | 23 | private var channel: FlutterMethodChannel |
24 | public var jobs = [UInt32: PrintJob]() | 24 | public var jobs = [UInt32: PrintJob]() |
25 | + private var registrar: FlutterPluginRegistrar | ||
25 | 26 | ||
26 | - init(_ channel: FlutterMethodChannel) { | 27 | + init(_ channel: FlutterMethodChannel, _ registrar: FlutterPluginRegistrar) { |
27 | self.channel = channel | 28 | self.channel = channel |
29 | + self.registrar = registrar | ||
28 | super.init() | 30 | super.init() |
29 | PrintingPlugin.instance = self | 31 | PrintingPlugin.instance = self |
30 | } | 32 | } |
@@ -42,7 +44,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -42,7 +44,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
42 | /// Entry point | 44 | /// Entry point |
43 | public static func register(with registrar: FlutterPluginRegistrar) { | 45 | public static func register(with registrar: FlutterPluginRegistrar) { |
44 | let channel = FlutterMethodChannel(name: "net.nfet.printing", binaryMessenger: registrar.messenger) | 46 | let channel = FlutterMethodChannel(name: "net.nfet.printing", binaryMessenger: registrar.messenger) |
45 | - let instance = PrintingPlugin(channel) | 47 | + let instance = PrintingPlugin(channel, registrar) |
46 | registrar.addMethodCallDelegate(instance, channel: channel) | 48 | registrar.addMethodCallDelegate(instance, channel: channel) |
47 | } | 49 | } |
48 | 50 | ||
@@ -61,6 +63,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -61,6 +63,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
61 | let printJob = PrintJob(printing: self, index: args["job"] as! Int) | 63 | let printJob = PrintJob(printing: self, index: args["job"] as! Int) |
62 | let dynamic = args["dynamic"] as! Bool | 64 | let dynamic = args["dynamic"] as! Bool |
63 | jobs[args["job"] as! UInt32] = printJob | 65 | jobs[args["job"] as! UInt32] = printJob |
66 | + | ||
67 | + guard let window = registrar.view?.window else { | ||
68 | + result(NSNumber(value: 0)) | ||
69 | + onCompleted(printJob: printJob, completed: false, error: "Unable to find the main window") | ||
70 | + return | ||
71 | + } | ||
72 | + | ||
64 | printJob.printPdf(name: name, | 73 | printJob.printPdf(name: name, |
65 | withPageSize: CGSize( | 74 | withPageSize: CGSize( |
66 | width: width, | 75 | width: width, |
@@ -72,7 +81,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -72,7 +81,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
72 | width: width - marginRight - marginLeft, | 81 | width: width - marginRight - marginLeft, |
73 | height: height - marginBottom - marginTop | 82 | height: height - marginBottom - marginTop |
74 | ), withPrinter: printer, | 83 | ), withPrinter: printer, |
75 | - dynamically: dynamic) | 84 | + dynamically: dynamic, andWindow: window) |
76 | result(NSNumber(value: 1)) | 85 | result(NSNumber(value: 1)) |
77 | } else if call.method == "listPrinters" { | 86 | } else if call.method == "listPrinters" { |
78 | let printJob = PrintJob(printing: self, index: -1) | 87 | let printJob = PrintJob(printing: self, index: -1) |
@@ -80,6 +89,12 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -80,6 +89,12 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
80 | result(r) | 89 | result(r) |
81 | } else if call.method == "sharePdf" { | 90 | } else if call.method == "sharePdf" { |
82 | let object = args["doc"] as! FlutterStandardTypedData | 91 | let object = args["doc"] as! FlutterStandardTypedData |
92 | + | ||
93 | + guard let view = registrar.view else { | ||
94 | + result(NSNumber(value: 0)) | ||
95 | + return | ||
96 | + } | ||
97 | + | ||
83 | PrintJob.sharePdf( | 98 | PrintJob.sharePdf( |
84 | data: object.data, | 99 | data: object.data, |
85 | withSourceRect: CGRect( | 100 | withSourceRect: CGRect( |
@@ -88,7 +103,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -88,7 +103,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
88 | width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0), | 103 | width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0), |
89 | height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0) | 104 | height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0) |
90 | ), | 105 | ), |
91 | - andName: args["name"] as! String | 106 | + andName: args["name"] as! String, andWindow: view |
92 | ) | 107 | ) |
93 | result(NSNumber(value: 1)) | 108 | result(NSNumber(value: 1)) |
94 | } else if call.method == "convertHtml" { | 109 | } else if call.method == "convertHtml" { |
-
Please register or login to post a comment