Forráskód Böngészése

Fix Save as PDF hanging in the print dialog.

Grant read-write access for user-selected files and present print operations modally on the host window so the save panel can open reliably.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 1 hónapja
szülő
commit
0413967911

+ 24 - 19
smart_printer/PrintService.swift

@@ -4,38 +4,38 @@ import UniformTypeIdentifiers
 
 enum PrintService {
     static func print(urls: [URL], from window: NSWindow? = nil) {
-        _ = window
         guard !urls.isEmpty else { return }
 
+        let hostWindow = window ?? NSApp.keyWindow
         for url in urls {
             let accessed = url.startAccessingSecurityScopedResource()
             defer {
                 if accessed { url.stopAccessingSecurityScopedResource() }
             }
-            printFile(at: url)
+            printFile(at: url, from: hostWindow)
         }
     }
 
-    private static func printFile(at url: URL) {
+    private static func printFile(at url: URL, from window: NSWindow?) {
         let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
 
         if type.conforms(to: .pdf), let document = PDFDocument(url: url) {
-            printPDF(document, title: url.lastPathComponent)
+            printPDF(document, title: url.lastPathComponent, from: window)
         } else if type.conforms(to: .image), let image = NSImage(contentsOf: url) {
-            printImage(image, title: url.lastPathComponent)
+            printImage(image, title: url.lastPathComponent, from: window)
         } else if let image = NSImage(contentsOf: url) {
-            printImage(image, title: url.lastPathComponent)
+            printImage(image, title: url.lastPathComponent, from: window)
         } else if type.conforms(to: .plainText) || type.conforms(to: .text),
                   let text = try? String(contentsOf: url, encoding: .utf8) {
-            printText(text, title: url.lastPathComponent)
+            printText(text, title: url.lastPathComponent, from: window)
         } else if type.conforms(to: .rtf),
                   let data = try? Data(contentsOf: url),
                   let attributed = NSAttributedString(rtf: data, documentAttributes: nil) {
-            printAttributedText(attributed, title: url.lastPathComponent)
+            printAttributedText(attributed, title: url.lastPathComponent, from: window)
         } else if type.conforms(to: .html),
                   let data = try? Data(contentsOf: url),
                   let attributed = NSAttributedString(html: data, documentAttributes: nil) {
-            printAttributedText(attributed, title: url.lastPathComponent)
+            printAttributedText(attributed, title: url.lastPathComponent, from: window)
         } else {
             showUnsupportedAlert(for: url)
         }
@@ -51,26 +51,26 @@ enum PrintService {
         return UTType(filenameExtension: ext)
     }
 
-    private static func printPDF(_ document: PDFDocument, title: String) {
+    private static func printPDF(_ document: PDFDocument, title: String, from window: NSWindow?) {
         let printInfo = configuredPrintInfo()
         guard let operation = document.printOperation(
             for: printInfo,
             scalingMode: .pageScaleToFit,
             autoRotate: true
         ) else { return }
-        runPrintOperation(operation, title: title)
+        runPrintOperation(operation, title: title, from: window)
     }
 
-    private static func printImage(_ image: NSImage, title: String) {
+    private static func printImage(_ image: NSImage, title: String, from window: NSWindow?) {
         guard let document = pdfDocument(from: image) else { return }
-        printPDF(document, title: title)
+        printPDF(document, title: title, from: window)
     }
 
-    private static func printText(_ text: String, title: String) {
-        printAttributedText(NSAttributedString(string: text), title: title)
+    private static func printText(_ text: String, title: String, from window: NSWindow?) {
+        printAttributedText(NSAttributedString(string: text), title: title, from: window)
     }
 
-    private static func printAttributedText(_ text: NSAttributedString, title: String) {
+    private static func printAttributedText(_ text: NSAttributedString, title: String, from window: NSWindow?) {
         let printInfo = configuredPrintInfo()
         let pageSize = printInfo.paperSize
         let textView = NSTextView(frame: NSRect(origin: .zero, size: pageSize))
@@ -78,10 +78,10 @@ enum PrintService {
         textView.isEditable = false
 
         let operation = NSPrintOperation(view: textView, printInfo: printInfo)
-        runPrintOperation(operation, title: title)
+        runPrintOperation(operation, title: title, from: window)
     }
 
-    private static func runPrintOperation(_ operation: NSPrintOperation, title: String) {
+    private static func runPrintOperation(_ operation: NSPrintOperation, title: String, from window: NSWindow?) {
         operation.showsPrintPanel = true
         operation.showsProgressPanel = true
         operation.jobTitle = title
@@ -92,7 +92,12 @@ enum PrintService {
             .showsOrientation,
             .showsScaling,
         ])
-        operation.run()
+
+        if let window {
+            operation.runModal(for: window, delegate: nil, didRun: nil, contextInfo: nil)
+        } else {
+            operation.run()
+        }
     }
 
     private static func configuredPrintInfo() -> NSPrintInfo {

+ 1 - 1
smart_printer/smart_printer.entitlements

@@ -4,7 +4,7 @@
 <dict>
 	<key>com.apple.security.app-sandbox</key>
 	<true/>
-	<key>com.apple.security.files.user-selected.read-only</key>
+	<key>com.apple.security.files.user-selected.read-write</key>
 	<true/>
 	<key>com.apple.security.network.client</key>
 	<true/>