codeburn/mac/Sources/CodeBurnMenubar/MenubarSecondRow.swift
ozymandiashh a0d08c81f0 fix(menubar): pick the second row's provider by its worst quota window
The "quota remaining" row selected each provider's headline window, which
is a billing horizon: weekly, else monthly, and only then the busiest
window. That is the right number for the Capacity Dock rail and the wrong
one for a row whose whole job is to name the provider nearest its limit.
On a real machine it discarded Cursor's API window at 100% and Claude's
5-hour window at 18% in favour of Monthly 9.5% and Weekly 5%, so the row
read as if nothing were near a limit — while the flame beside it was
tinting off the worst window across every provider. The comment claiming
the row followed the flame was wrong on both counts.

Each provider now contributes its worst window, per-model rows included,
which is exactly the rule aggregateQuotaStatus tints by, so the row and
the flame agree and README, CHANGELOG and the Settings help become true
as written. A provider in transientFailure keeps its last-known window
instead of dropping out, matching the dock, which shows the same data
dimmed during a backoff: the row no longer vanishes and resizes the
status item for the length of a retry.

Three smaller fixes from the same review: the row is capped at 24
characters — about the widest the single row gets at 13pt, since the
second line renders at 9pt — shortening a long provider name rather than
letting "GitHub Copilot 12% left · 6d 3h" more than double the item's
width; the two-line title gets a VoiceOver label that reads as one phrase
instead of a string with a newline in it; and the snapshot feeding the
row is no longer built on refreshes where the feature is off. With the
setting off the title is byte-for-byte the single-row composition it has
always been.

Closes #1310
2026-09-12 20:29:35 +03:00

385 lines
17 KiB
Swift

import Foundation
import CoreGraphics
/// What the optional second menu-bar row shows. Every case is a figure the app
/// already computes for the popover — the second row only re-renders it, so no
/// case can introduce a new fetch or a new refresh cadence.
enum MenubarSecondRowMetric: String, CaseIterable, Identifiable, Sendable {
/// Quota remaining plus reset countdown for the primary connected provider.
case quotaRemaining
/// Today's all-provider cost.
case todayCost
/// Today's all-provider input + output tokens.
case todayTokens
/// Sessions the CLI reported as live in its liveness window.
case activeSessions
var id: String { rawValue }
/// Settings picker label.
var settingsLabel: String {
switch self {
case .quotaRemaining: "Quota remaining"
case .todayCost: "Today's cost"
case .todayTokens: "Today's tokens"
case .activeSessions: "Active sessions"
}
}
}
/// The persisted row configuration. The first row is never configurable here:
/// it stays the existing Metric/Period/Scope badge, so an off setting leaves
/// the historical single-row title untouched.
struct MenubarRowSettings: Equatable, Sendable {
var isSecondRowEnabled: Bool
var secondRowMetric: MenubarSecondRowMetric
static let `default` = MenubarRowSettings(
isSecondRowEnabled: false,
secondRowMetric: .quotaRemaining
)
init(
isSecondRowEnabled: Bool = false,
secondRowMetric: MenubarSecondRowMetric = .quotaRemaining
) {
self.isSecondRowEnabled = isSecondRowEnabled
self.secondRowMetric = secondRowMetric
}
}
/// UserDefaults storage for the row settings, in the same shape as the other
/// menubar preferences (`CodeBurnMenubarPeriod`, `CodeBurnMenubarScope`,
/// `CodeBurnDisplayMetric`): a string-keyed value in the app's own domain, read
/// through a small loader so an unknown stored value degrades to the default
/// rather than failing.
enum MenubarRowPreferences {
static let secondRowEnabledKey = "CodeBurnMenubarSecondRowEnabled"
static let secondRowMetricKey = "CodeBurnMenubarSecondRowMetric"
static func load(defaults: UserDefaults = .standard) -> MenubarRowSettings {
MenubarRowSettings(
isSecondRowEnabled: defaults.bool(forKey: secondRowEnabledKey),
secondRowMetric: defaults.string(forKey: secondRowMetricKey)
.flatMap(MenubarSecondRowMetric.init(rawValue:))
?? MenubarRowSettings.default.secondRowMetric
)
}
static func setSecondRowEnabled(_ enabled: Bool, defaults: UserDefaults = .standard) {
defaults.set(enabled, forKey: secondRowEnabledKey)
}
static func setSecondRowMetric(
_ metric: MenubarSecondRowMetric,
defaults: UserDefaults = .standard
) {
defaults.set(metric.rawValue, forKey: secondRowMetricKey)
}
}
/// One connected provider's headline quota window, reduced to plain values so
/// the row selection and formatting stay testable without an AppStore.
struct MenubarQuotaCandidate: Equatable, Sendable {
let label: String
/// Fraction of the window consumed, 0...1.
let percentUsed: Double
let resetsAt: Date?
}
enum MenubarQuotaRowSelection {
/// The "primary" connected provider for the second row: the one nearest its
/// limit. Ties break on label so the row does not flip between equal
/// providers from one refresh to the next.
static func primary(from candidates: [MenubarQuotaCandidate]) -> MenubarQuotaCandidate? {
candidates
.sorted { lhs, rhs in
if lhs.percentUsed != rhs.percentUsed { return lhs.percentUsed > rhs.percentUsed }
return lhs.label < rhs.label
}
.first
}
/// One provider reduced to its row candidate, or nil when it has nothing the
/// row may report.
///
/// The window is the provider's *worst* one, not `headlineWindow`. The
/// headline window is a billing horizon — weekly, else monthly, and only
/// then the busiest window — which is the right number for the Capacity Dock
/// rail but the wrong one here: it discards a Cursor API window sitting at
/// 100% in favour of a monthly window at 9.5%, so the row names a provider
/// that is nowhere near its limit. The worst window is also exactly what the
/// menu-bar flame tints by (`AppStore.aggregateQuotaStatus` takes the max
/// across every window a provider reports, per-model rows included), so the
/// two surfaces now agree about which provider is in trouble.
static func candidate(label: String, summary: QuotaSummary) -> MenubarQuotaCandidate? {
guard feedsRow(summary.connection), let window = worstWindow(summary) else { return nil }
return MenubarQuotaCandidate(
label: label,
percentUsed: window.percent,
resetsAt: window.resetsAt
)
}
/// Connection states whose last-known data still feeds the row. A provider
/// backing off (`transientFailure`) keeps its data in the Capacity Dock,
/// dimmed, so dropping it here made the row — and with it the status item's
/// width — flicker away for the length of a backoff. Only states with
/// nothing to show are excluded.
static func feedsRow(_ connection: QuotaSummary.Connection) -> Bool {
switch connection {
case .connected, .stale, .transientFailure: true
case .disconnected, .loading, .terminalFailure: false
}
}
/// The window nearest exhaustion across everything the provider reports,
/// `primary` included. Ties break on label so the countdown does not swap
/// between two equally used windows from one refresh to the next.
static func worstWindow(_ summary: QuotaSummary) -> QuotaSummary.Window? {
var windows = summary.details
if let primary = summary.primary, !windows.contains(primary) {
windows.append(primary)
}
return windows
.filter { $0.percent.isFinite }
.sorted { lhs, rhs in
if lhs.percent != rhs.percent { return lhs.percent > rhs.percent }
return lhs.label < rhs.label
}
.first
}
}
/// Everything the second row can read, captured as plain values. A nil field
/// means "no data" for that metric and makes the row degrade to one line; it
/// never renders as a zero.
struct MenubarRowSnapshot: Equatable, Sendable {
var quota: MenubarQuotaCandidate?
var todayCost: Double?
var todayTotalTokens: Int?
/// Nil when the payload carries no live-session block at all (a CLI that
/// predates it), which is unknown rather than "nothing running".
var activeSessionCount: Int?
var currencySymbol: String
var currencyRate: Double
init(
quota: MenubarQuotaCandidate? = nil,
todayCost: Double? = nil,
todayTotalTokens: Int? = nil,
activeSessionCount: Int? = nil,
currencySymbol: String = "$",
currencyRate: Double = 1
) {
self.quota = quota
self.todayCost = todayCost
self.todayTotalTokens = todayTotalTokens
self.activeSessionCount = activeSessionCount
self.currencySymbol = currencySymbol
self.currencyRate = currencyRate
}
}
/// Pure composition of the menu-bar rows. Returns one string when the second
/// row is off or its metric has no data, two when it has something to say.
enum MenubarRowFormatter {
static func rows(
firstRow: String,
settings: MenubarRowSettings,
snapshot: MenubarRowSnapshot,
now: Date = Date()
) -> [String] {
guard let second = secondRow(settings: settings, snapshot: snapshot, now: now) else {
return [firstRow]
}
return [firstRow, second]
}
/// The second row's text, or nil when the setting is off or the chosen
/// metric has no data yet.
static func secondRow(
settings: MenubarRowSettings,
snapshot: MenubarRowSnapshot,
now: Date = Date()
) -> String? {
guard settings.isSecondRowEnabled else { return nil }
let row: String?
switch settings.secondRowMetric {
case .quotaRemaining:
row = quotaRow(snapshot.quota, now: now)
case .todayCost:
guard let cost = snapshot.todayCost, cost.isFinite else { return nil }
let converted = cost * snapshot.currencyRate
row = String(format: "\(snapshot.currencySymbol)%.2f today", converted)
case .todayTokens:
guard let tokens = snapshot.todayTotalTokens else { return nil }
row = "\(compactTokens(Double(tokens))) tok today"
case .activeSessions:
guard let count = snapshot.activeSessionCount else { return nil }
// Live sessions are identity-derived, so the exact phrasing applies.
row = SessionCountLabel.compact(sessions: count, basis: "identity")
}
// Last line of defence: whatever a metric produces, the row never gets
// to set the status item's width on its own.
return row.map { clampToRowBudget($0) }
}
/// "Claude 42% left · 3h 12m". The countdown is dropped when the provider
/// reports no reset instant; the whole row is dropped when there is no
/// usable percentage.
///
/// The percentage and the countdown are bounded by their own shapes; the
/// provider label is not ("GitHub Copilot"), so it is the part that gives
/// way when the row would exceed `secondRowCharacterBudget`. Shortening the
/// name keeps both figures — the two things the row exists to say — rather
/// than clipping the countdown off the end.
private static func quotaRow(_ quota: MenubarQuotaCandidate?, now: Date) -> String? {
guard let quota, quota.percentUsed.isFinite else { return nil }
let remaining = min(max(1 - quota.percentUsed, 0), 1)
let percent = Int((remaining * 100).rounded())
var figures = "\(percent)% left"
if let countdown = resetCountdown(quota.resetsAt, now: now) {
figures += " · \(countdown)"
}
guard !quota.label.isEmpty else { return figures }
let label = abbreviate(quota.label, to: secondRowCharacterBudget - figures.count - 1)
return label.isEmpty ? figures : "\(label) \(figures)"
}
/// How wide the second row may get, in characters.
///
/// The status item is variable width, so the widest line wins; an unbounded
/// second row ("GitHub Copilot 12% left · 6d 3h") made the item more than
/// twice as wide as the figure above it. The single row bounds itself by
/// abbreviating (`Period.menubarSuffix(compact:)`, `asCompactCurrencyWhole`),
/// and at its widest — tokens, a period suffix and a device-shortfall marker
/// — it runs to roughly 24 characters at 13pt. The second row renders at 9pt
/// (`MenubarRowTypography.twoRowFontSize`), so the same character count there
/// is about two thirds of that width: turning the row on cannot push the item
/// past what the first row alone could already occupy.
static let secondRowCharacterBudget = 24
/// Shortens `text` to `limit` characters, marking the cut with an ellipsis.
/// Returns "" when there is no room for even one character plus the mark, so
/// the caller can drop the part entirely rather than render a bare "…".
static func abbreviate(_ text: String, to limit: Int) -> String {
guard text.count > limit else { return text }
guard limit >= 2 else { return "" }
var kept = String(text.prefix(limit - 1))
while kept.last == " " { kept.removeLast() }
return kept.isEmpty ? "" : kept + "…"
}
/// Applies `secondRowCharacterBudget` to an already-composed row.
static func clampToRowBudget(_ row: String) -> String {
abbreviate(row, to: secondRowCharacterBudget)
}
/// What VoiceOver reads for the status item once the second row is on.
///
/// The rendered title is one attributed string carrying a literal newline
/// between the rows, and the row separator is a middle dot; read verbatim
/// that is neither a sentence nor a pause. This spells the same information
/// as one comma-separated phrase, from the title as composed, so the label
/// cannot drift from what is on screen.
static func accessibilityLabel(title: String) -> String {
let parts = title
// U+FFFC is the inline flame attachment's placeholder character.
.replacingOccurrences(of: "\u{FFFC}", with: " ")
.split(whereSeparator: \.isNewline)
.flatMap { $0.split(separator: "·") }
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
return (["CodeBurn"] + parts).joined(separator: ", ")
}
/// Same shape as the popover's quota rows (`QuotaSummary.Window.resetsInLabel`),
/// with an injectable clock so the row is testable.
static func resetCountdown(_ resetsAt: Date?, now: Date) -> String? {
guard let resetsAt else { return nil }
let seconds = max(0, resetsAt.timeIntervalSince(now))
if seconds < 60 { return "now" }
let minutes = Int(seconds / 60)
let hours = minutes / 60
let days = hours / 24
if days > 0 { return "\(days)d \(hours % 24)h" }
if hours > 0 { return "\(hours)h \(minutes % 60)m" }
return "\(minutes)m"
}
/// Menu-bar token shorthand, matching `Double.asCompactTokens()`. Kept here
/// so the formatter stays free of the main actor.
static func compactTokens(_ n: Double) -> String {
if n >= 1_000_000_000 { return String(format: "%.1fB", n / 1_000_000_000) }
if n >= 1_000_000 { return String(format: "%.1fM", n / 1_000_000) }
if n >= 1_000 { return String(format: "%.0fK", n / 1_000) }
return String(format: "%.0f", n)
}
}
/// Geometry for the two-line title. The menu bar is 22pt on every standard
/// display (`NSStatusBar.system.thickness`), so the two lines get 10pt each and
/// the remaining 2pt is the slack AppKit's button cell centres the block in.
///
/// 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. `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
static let twoRowFontSize: CGFloat = 9
static let twoRowLineHeight: CGFloat = 10
static let standardMenuBarThickness: CGFloat = 22
static let twoRowBaselineOffset: CGFloat = 0
/// 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 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
/// 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
}
}