Skip to content

Commit

Permalink
fix: window preview sizing issues on multiple displays
Browse files Browse the repository at this point in the history
Fixed issue where window previews would be incorrectly sized

closes #392
  • Loading branch information
ejbills committed Dec 22, 2024
1 parent cf71ff2 commit 9fae2c0
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions DockDoor/Utilities/WindowUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,16 @@ enum WindowUtil {
let filter = SCContentFilter(desktopIndependentWindow: window)
let config = SCStreamConfiguration()

let scaleFactor = getScaleFactorForWindow(window: window)
let nativeScaleFactor = getScaleFactorForWindow(window: window)
let previewScale = Int(Defaults[.windowPreviewImageScale])

// Calculate the window's true dimensions in its native screen space
let nativeWidth = window.frame.width * nativeScaleFactor
let nativeHeight = window.frame.height * nativeScaleFactor

let width = Int(window.frame.width * scaleFactor) / Int(Defaults[.windowPreviewImageScale])
let height = Int(window.frame.height * scaleFactor) / Int(Defaults[.windowPreviewImageScale])
// Apply the user's preview scale while preserving the window's native proportions
let width = Int(nativeWidth) / previewScale
let height = Int(nativeHeight) / previewScale

config.width = width
config.height = height
Expand Down Expand Up @@ -162,11 +168,8 @@ enum WindowUtil {
}

// Use ScreenCaptureKit's API if available, otherwise fall back to a legacy (deprecated) API
let image: CGImage = if #available(macOS 14.0, *) {
try await captureImageModern(of: window)
} else {
try await captureImageLegacy(of: window)
}

let image = try await captureImageLegacy(of: window)

let cachedImage = CachedImage(image: image, timestamp: Date(), windowname: window.title)
imageCache[window.windowID] = cachedImage
Expand All @@ -177,11 +180,16 @@ enum WindowUtil {
// Helper function to get the scale factor for a given window
private static func getScaleFactorForWindow(window: SCWindow) -> CGFloat {
let windowFrame = window.frame
let containingScreen = NSScreen.screens.first { screen in

// Find the specific screen containing this window
guard let containingScreen = NSScreen.screens.first(where: { screen in
screen.frame.intersects(windowFrame)
} ?? NSScreen.main
}) else {
return NSScreen.main?.backingScaleFactor ?? 2.0
}

return containingScreen?.backingScaleFactor ?? 2.0
// Get this screen's native backing scale factor
return containingScreen.backingScaleFactor
}

private static func getCachedImage(window: SCWindow) -> CGImage? {
Expand Down

0 comments on commit 9fae2c0

Please sign in to comment.