From b90e0c5e3603cbf698beecfc6416d7be091a65fe Mon Sep 17 00:00:00 2001 From: psoares Date: Fri, 30 Oct 2026 14:19:58 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20menu=20bar=20height=20bein?= =?UTF-8?q?g=20capped=20at=2024pt=20on=20Retina=20displays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The measured height was compared against a 40 limit after being scaled by the backing scale factor, so on Retina displays any real menu bar (e.g. 30pt = 60px) was rejected and replaced with 24pt. combineImages then capped the height to 24pt again. Measure and validate the height in points, take the notch safe area into account, fall back to the primary screen's menu bar height for displays without one, and draw the bar at the measured height. Co-Authored-By: Claude Opus 5.5 --- .../Extensions/ImageFunctions.swift | 5 ++-- .../Extensions/NSScreen+Extensions.swift | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Sources/ChangeMenuBarColor/Extensions/ImageFunctions.swift b/Sources/ChangeMenuBarColor/Extensions/ImageFunctions.swift index 1b42509..208ae43 100644 --- a/Sources/ChangeMenuBarColor/Extensions/ImageFunctions.swift +++ b/Sources/ChangeMenuBarColor/Extensions/ImageFunctions.swift @@ -80,9 +80,8 @@ func combineImages(baseImage: NSImage, addedImage: NSImage) -> NSImage? { operation: .copy, fraction: 1.0) - // Determine proper menu bar height - use a reasonable default if needed - let scaleFactor = NSScreen.main?.backingScaleFactor ?? 2.0 - let menuBarHeight = min(addedImage.size.height, 24 * scaleFactor) + // The added image is already sized to the menu bar height of the target screen + let menuBarHeight = addedImage.size.height // Draw the menu bar portion with high quality addedImage.draw(in: NSRect(x: 0, y: height - menuBarHeight, width: width, height: menuBarHeight), diff --git a/Sources/ChangeMenuBarColor/Extensions/NSScreen+Extensions.swift b/Sources/ChangeMenuBarColor/Extensions/NSScreen+Extensions.swift index c13324a..5472aba 100644 --- a/Sources/ChangeMenuBarColor/Extensions/NSScreen+Extensions.swift +++ b/Sources/ChangeMenuBarColor/Extensions/NSScreen+Extensions.swift @@ -13,16 +13,30 @@ extension NSScreen { return CGSize(width: frame.size.width * backingScaleFactor, height: frame.size.height * backingScaleFactor) } + // Menu bar height in points, or nil when it cannot be determined (e.g. auto-hidden menu bar or no menu bar on this display) + private var measuredMenuBarHeight: CGFloat? { + // Height difference between the top of the full frame and the top of the visible frame + var heightInPoints = frame.maxY - visibleFrame.maxY + + // On displays with a notch the menu bar is taller and matches the top safe area inset + if #available(macOS 12.0, *) { + heightInPoints = max(heightInPoints, safeAreaInsets.top) + } + + return heightInPoints > 0 && heightInPoints <= 60 ? heightInPoints : nil + } + var menuBarHeight: CGFloat { - // Get the height difference between full frame and visible frame - let calculatedHeight = (frame.size.height - visibleFrame.height - visibleFrame.origin.y) * backingScaleFactor - - // Apply safety limits - macOS menu bar is typically between 22-25 points - if calculatedHeight <= 0 || calculatedHeight > 40 { - Log.debug("Menu bar height calculation appears incorrect (\(calculatedHeight)), using standard height") - return 24 * backingScaleFactor // Standard menu bar height + let heightInPoints: CGFloat + if let measured = measuredMenuBarHeight { + heightInPoints = measured + } else { + // Fall back to the primary screen's menu bar, or the standard height + heightInPoints = NSScreen.screens.first?.measuredMenuBarHeight ?? 24 + Log.debug("Menu bar height could not be calculated for this screen, using \(heightInPoints) points") } - + + let calculatedHeight = heightInPoints * backingScaleFactor Log.debug("Using calculated menu bar height: \(calculatedHeight)") return calculatedHeight }