From e91e2d409bd3664e24afab4416571db546481ff0 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 5 Feb 2024 14:08:12 -0500 Subject: [PATCH 1/3] fix(macOS): Prevent incorrect scaling of the Metal layer after DPI change Ref: https://developer.apple.com/library/archive/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/CapturingScreenContents/CapturingScreenContents.html#//apple_ref/doc/uid/TP40012302-CH10-SW27 --- macos/Sources/Ghostty/SurfaceView_AppKit.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 40d4795cc..93c8e467f 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -403,6 +403,22 @@ extension Ghostty { } override func viewDidChangeBackingProperties() { + super.viewDidChangeBackingProperties() + + // The Core Animation compositing engine uses the layer's contentsScale property + // to determine whether to scale its contents during compositing. When the window + // moves between a high DPI display and a low DPI display, or the user modifies + // the DPI scaling for a display in the system settings, this can result in the + // layer being scaled inappropriately. Since we handle the adjustment of scale + // and resolution ourselves below, we update the layer's contentsScale property + // to match the window's backingScaleFactor, so as to ensure it is not scaled by + // the compositor. + // + // Ref: High Resolution Guidelines for OS X + if let window = window { + layer?.contentsScale = window.backingScaleFactor + } + guard let surface = self.surface else { return } // Detect our X/Y scale factor so we can update our surface From 7a4c97329e614ffd9ee0b747e1188e6cb3406d7b Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 5 Feb 2024 15:41:33 -0500 Subject: [PATCH 2/3] macOS: Add URL to documentation in comment --- macos/Sources/Ghostty/SurfaceView_AppKit.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 93c8e467f..37de41093 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -415,6 +415,7 @@ extension Ghostty { // the compositor. // // Ref: High Resolution Guidelines for OS X + // https://developer.apple.com/library/archive/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/CapturingScreenContents/CapturingScreenContents.html#//apple_ref/doc/uid/TP40012302-CH10-SW27 if let window = window { layer?.contentsScale = window.backingScaleFactor } From cf2968c186b25eea3f1b16cd95fc924e3d47976d Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 5 Feb 2024 15:48:02 -0500 Subject: [PATCH 3/3] fix(macOS): Prevent janky transition animation on DPI change --- macos/Sources/Ghostty/SurfaceView_AppKit.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 37de41093..f1f4f0f7e 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -417,7 +417,13 @@ extension Ghostty { // Ref: High Resolution Guidelines for OS X // https://developer.apple.com/library/archive/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/CapturingScreenContents/CapturingScreenContents.html#//apple_ref/doc/uid/TP40012302-CH10-SW27 if let window = window { + CATransaction.begin() + // Disable the implicit transition animation that Core Animation applies to + // property changes. Otherwise it will apply a scale animation to the layer + // contents which looks pretty janky. + CATransaction.setDisableActions(true) layer?.contentsScale = window.backingScaleFactor + CATransaction.commit() } guard let surface = self.surface else { return }