When creating a default macOS document-based Xcode project and using the code below (and wiring the File menu's Print item to printDocument:
instead of the default print:
, which does nothing), opening the print panel causes PrintView.knowsPageRange(_:)
to be called twice.
Is this a bug? My app populates PrintView
dynamically, and for large documents it can be quite inefficient to populate it once, only for the contents to be immediately discarded and populated again.
A workaround that came to my mind would be to check if the print options have changed, though I'm not sure if it's a reliable indicator that the print preview is effectively the same.
I created FB17018494.
class Document: NSDocument { override func makeWindowControllers() { addWindowController(NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController) } override func printOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) throws -> NSPrintOperation { return NSPrintOperation(view: PrintView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)), printInfo: NSPrintInfo(dictionary: printSettings)) } } class PrintView: NSView { override func knowsPageRange(_ range: NSRangePointer) -> Bool { print("knowsPageRange") range.pointee = NSRange(location: 1, length: 1) return true } }