I used CGRectNull because that is what Apple suggested to use in the WWDC'19 talk.
Yes, I can replace it using ScreenCaptureKit. I came up with the following:
@available(macOS 27.0, *)
private func refreshDesktopImage( cgWin: CGWindowID, win: MyWindow, onScreen: Bool) {
let scale = win.screen?.backingScaleFactor ?? 1
Task {
do {
// false is important: include Finder/Dock/Desktop windows.
let content = try await SCShareableContent.excludingDesktopWindows( false, onScreenWindowsOnly: false)
guard let desktopWindow = content.windows.first(where: { $0.windowID == cgWin }) else { return }
let config = SCStreamConfiguration()
config.width = Int((desktopWindow.frame.width * scale).rounded())
config.height = Int((desktopWindow.frame.height * scale).rounded())
config.showsCursor = false
let cgImage = try await SCScreenshotManager.captureImage( contentFilter: SCContentFilter( desktopIndependentWindow: desktopWindow), configuration: config)
await MainActor.run {
// Avoid applying an old async result to a reused window.
guard self.myDesktops[cgWin] === win else { return }
win.lastGoodImage = cgImage
let image = NSImage(cgImage: cgImage, size: .zero)
win.setWin( imageView: NSImageView(image: image), onScreen: onScreen, hidden: self.hidden)
}
} catch {
NSLog("Desktop capture failed: \(error)")
}
}
}
And while this works, it also throws up two Alert dialogs and numerous dialogs after it redirects the User to System Settings to toggle. Furthermore, those Alerts are completely misleading to what the app is doing. The app is NOT recording audio and is simply taking a snapshot of the Desktop background.
I also have an annoying bug in on first run, it doesn't even capture the actual background picture.