Home > Software design >  How to customize macOS print modal for an app?
How to customize macOS print modal for an app?

Time:04-30

Looking at an existing macOS app I see the print modal has an app specific option:

Don't print bracketed chords (hide [C], [Dm], etc.)

App screenshot

Toggling this option updates the preview instantly. Could someone point me in the direction for the documentation for this feature of customising the print modal?

CodePudding user response:

Check your NSGraphicsContext when you override your drawing

class MyView: NSView {
    override func draw(_ dirtyRect: NSRect) {
        if NSGraphicsContext.currentContextDrawingToScreen() {
            // draw for screen
        } else {
            // draw for print
        }
    }
}

Now create a NSPrintOperation:

let sheetHostingWindow: NSWindow = ...
let viewToPrint = NSStackView()
// ... prepare the stack view ...
let printOperation = NSPrintOperation(view: viewToPrint)
printOperation.runModal(
    for: sheetHostingWindow,
    delegate: nil,
    didRun: nil,
    contextInfo: nil)

Thats it.

For an example take a look here: https://github.com/CleanCocoa/NSTableView-Printing and for more information about the code above here: https://christiantietze.de/posts/2021/06/printing-nstableview-with-label/

CodePudding user response:

I found the answer in apple docs https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Printing/osxp_printpanel/osxp_printpanel.html#//apple_ref/doc/uid/20000862-BAJBFGED

  • Related