NSView.knowsPageRange(_:) called twice when showing print panel

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
}
}

Hi. Thanks for filing a bug report. Looks like it has been dispatched over to the AppKit team for investigation. I'll check again and provide an update (if there's anything to report) next time I shuffle through forums posts with bug reports that I am tracking.

NSView.knowsPageRange(_:) called twice when showing print panel
 
 
Q