diff --git a/mac/Sources/CodeBurnMenubar/CodeBurnApp.swift b/mac/Sources/CodeBurnMenubar/CodeBurnApp.swift index 451be90c..b21c1857 100644 --- a/mac/Sources/CodeBurnMenubar/CodeBurnApp.swift +++ b/mac/Sources/CodeBurnMenubar/CodeBurnApp.swift @@ -1280,19 +1280,36 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM let attachmentPointSize: CGFloat let baselineOffset: CGFloat let attachmentVerticalOffset: CGFloat + /// Two rows only. The single row has the whole menu bar to itself, so its + /// flame keeps the image size the system hands back, exactly as before. + let clampsAttachmentToLineHeight: Bool static let singleRow = MenubarTitleStyle( fontSize: menubarTitleFontSize, attachmentPointSize: menubarTitleFontSize, baselineOffset: -1.0, - attachmentVerticalOffset: -3 + attachmentVerticalOffset: -3, + clampsAttachmentToLineHeight: false ) static let twoRow = MenubarTitleStyle( fontSize: MenubarRowTypography.twoRowFontSize, attachmentPointSize: MenubarRowTypography.twoRowAttachmentPointSize, baselineOffset: MenubarRowTypography.twoRowBaselineOffset, - attachmentVerticalOffset: MenubarRowTypography.twoRowAttachmentVerticalOffset + attachmentVerticalOffset: MenubarRowTypography.twoRowAttachmentVerticalOffset, + clampsAttachmentToLineHeight: true ) + + func attachmentBounds(imageSize: CGSize) -> CGRect { + guard clampsAttachmentToLineHeight else { + return CGRect( + x: 0, + y: attachmentVerticalOffset, + width: imageSize.width, + height: imageSize.height + ) + } + return MenubarRowTypography.twoRowAttachmentBounds(imageSize: imageSize) + } } /// The button cell's single-line state as AppKit handed it to us, captured @@ -1378,12 +1395,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate, NSM let attachment = NSTextAttachment() attachment.image = flame if let size = flame?.size { - attachment.bounds = CGRect( - x: 0, - y: style.attachmentVerticalOffset, - width: size.width, - height: size.height - ) + attachment.bounds = style.attachmentBounds(imageSize: size) } let menubarPeriod = store.menubarPeriod diff --git a/mac/Sources/CodeBurnMenubar/MenubarSecondRow.swift b/mac/Sources/CodeBurnMenubar/MenubarSecondRow.swift index 9294c0e5..37930396 100644 --- a/mac/Sources/CodeBurnMenubar/MenubarSecondRow.swift +++ b/mac/Sources/CodeBurnMenubar/MenubarSecondRow.swift @@ -217,11 +217,17 @@ enum MenubarRowFormatter { /// The inline flame has to shrink too, and by more than the text does: a /// paragraph style clamps *text* line height but not an attachment, so a flame /// whose image is taller than the clamp drags line one — and the whole title — -/// past the menu bar. These numbers were measured with -/// `NSAttributedString.boundingRect` over every first-row and second-row -/// combination the app renders; all of them come out at exactly -/// `twoRowMeasuredHeight`. Raising the font size, the line height or the flame's -/// point size breaks that, so change them together and re-measure. +/// past the menu bar. `twoRowAttachmentBounds` is what actually holds that line, +/// by scaling the image down to the clamp rather than trusting a point size to +/// produce an image of a particular height. +/// +/// The height AppKit then reports is *not* identical across macOS releases: the +/// same composition measures 20pt on macOS 26 and 21pt on the CI runner, +/// because SF Symbol metrics and line rounding move between releases. So the +/// contract here is a fit, not an exact number — the pair has to fit the bar, +/// and has to still look like two clamped lines rather than a collapsed one. +/// AppKit's button cell centres whatever it measures inside the status item, so +/// nothing needs to know the exact figure to place the rows. enum MenubarRowTypography { /// The historical single-row text size, for reference in tests. static let singleRowFontSize: CGFloat = 13 @@ -229,18 +235,43 @@ enum MenubarRowTypography { static let twoRowLineHeight: CGFloat = 10 static let standardMenuBarThickness: CGFloat = 22 static let twoRowBaselineOffset: CGFloat = 0 - /// 8pt renders a 10pt-tall flame, which the -3pt offset seats inside the - /// clamped first line instead of pushing it taller. + /// Asks for a flame around the clamped line height; `twoRowAttachmentBounds` + /// is what guarantees it, since the rendered image size for a point size is + /// the system's business and has changed between releases. static let twoRowAttachmentPointSize: CGFloat = 8 static let twoRowAttachmentVerticalOffset: CGFloat = -3 static var twoRowTextHeight: CGFloat { twoRowLineHeight * 2 } - /// The height AppKit actually lays the two rows out at. - static let twoRowMeasuredHeight: CGFloat = 20 + /// The tallest the pair may lay out: any more and the menu bar clips a row. + static let twoRowMaximumHeight: CGFloat = standardMenuBarThickness + /// A floor, so a lost paragraph clamp or a dropped second line is caught as + /// a regression instead of passing as "fits". + static let twoRowMinimumHeight: CGFloat = 18 - /// True when both clamped lines fit inside a menu bar of this thickness. - static func fitsMenuBar(thickness: CGFloat = standardMenuBarThickness) -> Bool { - thickness > 0 && twoRowMeasuredHeight <= thickness + /// The box the inline flame occupies on the first line. The image is scaled + /// down to the clamped line height — never up — and seated by + /// `twoRowAttachmentVerticalOffset`, so a release whose SF Symbols render + /// taller than the one these numbers were measured on cannot push the title + /// out of the menu bar. + static func twoRowAttachmentBounds(imageSize: CGSize) -> CGRect { + guard imageSize.width > 0, imageSize.height > 0 else { return .zero } + let height = min(imageSize.height, twoRowLineHeight) + let width = imageSize.width * (height / imageSize.height) + return CGRect(x: 0, y: twoRowAttachmentVerticalOffset, width: width, height: height) + } + + /// True when a measured two-row layout fits a menu bar of this thickness. + static func fitsMenuBar( + measuredHeight: CGFloat, + thickness: CGFloat = standardMenuBarThickness + ) -> Bool { + thickness > 0 && measuredHeight > 0 && measuredHeight <= thickness + } + + /// True when a measured height is a plausible two-row layout: tall enough to + /// be two clamped lines, short enough for the standard bar. + static func isExpectedTwoRowHeight(_ measuredHeight: CGFloat) -> Bool { + measuredHeight >= twoRowMinimumHeight && measuredHeight <= twoRowMaximumHeight } } diff --git a/mac/Tests/CodeBurnMenubarTests/MenubarSecondRowTests.swift b/mac/Tests/CodeBurnMenubarTests/MenubarSecondRowTests.swift index ae685c4a..006043ee 100644 --- a/mac/Tests/CodeBurnMenubarTests/MenubarSecondRowTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/MenubarSecondRowTests.swift @@ -288,25 +288,62 @@ struct MenubarSecondRowTests { func twoRowsFitMenuBar() { #expect(MenubarRowTypography.standardMenuBarThickness == 22) #expect(MenubarRowTypography.twoRowTextHeight == 20) - // The measured layout height must still equal the clamped text height: - // anything larger means the inline flame is dragging line one taller. - #expect(MenubarRowTypography.twoRowMeasuredHeight == MenubarRowTypography.twoRowTextHeight) - #expect(MenubarRowTypography.fitsMenuBar()) - #expect(MenubarRowTypography.fitsMenuBar(thickness: 20)) - #expect(!MenubarRowTypography.fitsMenuBar(thickness: 19)) - #expect(!MenubarRowTypography.fitsMenuBar(thickness: 0)) + #expect(MenubarRowTypography.twoRowMaximumHeight == MenubarRowTypography.standardMenuBarThickness) + #expect(MenubarRowTypography.twoRowMinimumHeight < MenubarRowTypography.twoRowTextHeight) + #expect(MenubarRowTypography.fitsMenuBar(measuredHeight: 20)) + #expect(MenubarRowTypography.fitsMenuBar(measuredHeight: 21)) + #expect(MenubarRowTypography.fitsMenuBar(measuredHeight: 20, thickness: 20)) + #expect(!MenubarRowTypography.fitsMenuBar(measuredHeight: 20, thickness: 19)) + #expect(!MenubarRowTypography.fitsMenuBar(measuredHeight: 23)) + #expect(!MenubarRowTypography.fitsMenuBar(measuredHeight: 20, thickness: 0)) + #expect(!MenubarRowTypography.fitsMenuBar(measuredHeight: 0)) + // The exact figure moves between macOS releases (20pt on macOS 26, 21pt + // on the CI runner), so only the band is a contract. + #expect(MenubarRowTypography.isExpectedTwoRowHeight(20)) + #expect(MenubarRowTypography.isExpectedTwoRowHeight(21)) + #expect(!MenubarRowTypography.isExpectedTwoRowHeight(23)) + #expect(!MenubarRowTypography.isExpectedTwoRowHeight(11)) // The two-row text must be smaller than the single-row figure, or the // pair cannot be centred inside the menu bar at all. #expect(MenubarRowTypography.twoRowFontSize < MenubarRowTypography.singleRowFontSize) #expect(MenubarRowTypography.twoRowFontSize <= MenubarRowTypography.twoRowLineHeight) - // The flame is clamped by nothing, so it must be requested smaller than - // the text and seated back inside the line by a negative offset. + // The flame is clamped by nothing the paragraph style does, so it must be + // requested smaller than the text and seated by a negative offset. #expect(MenubarRowTypography.twoRowAttachmentPointSize < MenubarRowTypography.twoRowFontSize) #expect(MenubarRowTypography.twoRowAttachmentVerticalOffset < 0) } - @Test("the two rows AppKit lays out measure 20pt for every row combination") - func twoRowsMeasureTwentyPoints() { + @Test("the flame is scaled into the clamped line, never past it") + func attachmentBoundsClampToLineHeight() { + let clamp = MenubarRowTypography.twoRowLineHeight + let offset = MenubarRowTypography.twoRowAttachmentVerticalOffset + + // An image at or under the clamp keeps its size. + let small = MenubarRowTypography.twoRowAttachmentBounds(imageSize: CGSize(width: 8, height: 10)) + #expect(small == CGRect(x: 0, y: offset, width: 8, height: 10)) + + // A release whose SF Symbols render taller is scaled down, aspect kept, + // so line one cannot grow past its clamp. + let tall = MenubarRowTypography.twoRowAttachmentBounds(imageSize: CGSize(width: 12, height: 15)) + #expect(tall.height == clamp) + #expect(tall.width == 8) + #expect(tall.origin.y == offset) + + // Degenerate sizes produce an empty box rather than a division by zero. + #expect(MenubarRowTypography.twoRowAttachmentBounds(imageSize: .zero) == .zero) + #expect(MenubarRowTypography.twoRowAttachmentBounds(imageSize: CGSize(width: 8, height: 0)) == .zero) + + // Whatever the image, the box never exceeds the clamp. + for height in stride(from: CGFloat(1), through: 40, by: 1) { + let bounds = MenubarRowTypography.twoRowAttachmentBounds( + imageSize: CGSize(width: height * 0.8, height: height) + ) + #expect(bounds.height <= clamp) + } + } + + @Test("the two rows AppKit lays out fit the menu bar for every row combination") + func twoRowsFitTheMenuBarForEveryRowCombination() { // Same composition the status item renders: an inline flame attachment at // the two-row point size, the badge text, then the second row under a // paragraph style that clamps both line heights. @@ -324,12 +361,7 @@ struct MenubarSecondRowTests { let attachment = NSTextAttachment() attachment.image = flame if let size = flame?.size { - attachment.bounds = CGRect( - x: 0, - y: MenubarRowTypography.twoRowAttachmentVerticalOffset, - width: size.width, - height: size.height - ) + attachment.bounds = MenubarRowTypography.twoRowAttachmentBounds(imageSize: size) } let composed = NSMutableAttributedString() composed.append(NSAttributedString(attachment: attachment)) @@ -353,13 +385,17 @@ struct MenubarSecondRowTests { ).height } + // The figure AppKit reports is release-dependent (20pt on macOS 26, 21pt + // on the CI runner), and nothing positions the rows off it — the button + // cell centres whatever it measures. So the contract is the band: it has + // to fit the bar, and it has to still be two clamped lines. let firstRows = [" $12.34", " ↑1.2M ↓340K / wk", " 2.4M / mo", ""] let secondRows = ["Claude 42% left · 3h 12m", "$0.00 today", "12 sess", "1.5M tok today"] for first in firstRows { for second in secondRows { let height = measuredHeight(first: first, second: second) - #expect(height == MenubarRowTypography.twoRowMeasuredHeight) - #expect(height <= MenubarRowTypography.standardMenuBarThickness) + #expect(MenubarRowTypography.fitsMenuBar(measuredHeight: height)) + #expect(MenubarRowTypography.isExpectedTwoRowHeight(height)) } } }