Skip to content

Commit

Permalink
fix: use safer 'backingScaleFactor' instead of blindly doubling windo…
Browse files Browse the repository at this point in the history
…w preview image
  • Loading branch information
ejbills committed Jul 16, 2024
1 parent be53d31 commit 65d4428
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions DockDoor/Utilities/WindowUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ final class WindowUtil {
if let cachedImage = getCachedImage(window: window) {
return cachedImage
}

let filter = SCContentFilter(desktopIndependentWindow: window)
let config = SCStreamConfiguration()

Expand All @@ -81,9 +82,12 @@ final class WindowUtil {
config.shouldBeOpaque = false
if #available(macOS 14.2, *) { config.includeChildWindows = false }

// Double the width and height for higher resolution capture
config.width = Int(window.frame.width) * 2
config.height = Int(window.frame.height) * 2
// Get the scale factor of the display containing the window
let scaleFactor = await getScaleFactorForWindow(windowID: window.windowID)

// Convert points to pixels
config.width = Int(window.frame.width * scaleFactor)
config.height = Int(window.frame.height * scaleFactor)

config.showsCursor = false
config.captureResolution = .best
Expand All @@ -95,6 +99,23 @@ final class WindowUtil {

return image
}

// Helper function to get the scale factor for a given window
private static func getScaleFactorForWindow(windowID: CGWindowID) async -> CGFloat {
return await MainActor.run {
guard let window = NSApplication.shared.window(withWindowNumber: Int(windowID)) else {
return NSScreen.main?.backingScaleFactor ?? 2.0
}

if NSScreen.screens.count > 1 {
if let currentScreen = window.screen {
return currentScreen.backingScaleFactor
}
}

return NSScreen.main?.backingScaleFactor ?? 2.0
}
}

private static func getCachedImage(window: SCWindow) -> CGImage? {
if let cachedImage = imageCache[window.windowID], Date().timeIntervalSince(cachedImage.timestamp) <= cacheExpirySeconds {
Expand Down

0 comments on commit 65d4428

Please sign in to comment.