Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions Sources/CodexBar/StatusItemController+HostedSubmenus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ extension StatusItemController {

let chartView = UsageBreakdownChartMenuView(breakdown: breakdown, width: width)
let hosting = MenuHostingView(rootView: chartView)
let controller = NSHostingController(rootView: chartView)
let size = controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let chartItem = NSMenuItem()
chartItem.view = hosting
Expand All @@ -213,9 +213,9 @@ extension StatusItemController {

let chartView = CreditsHistoryChartMenuView(breakdown: breakdown, width: width)
let hosting = MenuHostingView(rootView: chartView)
let controller = NSHostingController(rootView: chartView)
let size = controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let chartItem = NSMenuItem()
chartItem.view = hosting
Expand Down Expand Up @@ -252,9 +252,9 @@ extension StatusItemController {
windowLabel: tokenSnapshot.historyLabel,
width: width)
let hosting = MenuHostingView(rootView: chartView)
let controller = NSHostingController(rootView: chartView)
let size = controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let chartItem = NSMenuItem()
chartItem.view = hosting
Expand Down Expand Up @@ -288,9 +288,9 @@ extension StatusItemController {
let maxHeight = self.storageBreakdownMenuMaxHeight()
let view = StorageBreakdownMenuView(footprint: footprint, width: width, maxHeight: maxHeight)
let hosting = MenuHostingView(rootView: view)
let controller = NSHostingController(rootView: view)
let size = controller.sizeThatFits(in: CGSize(width: width, height: maxHeight))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let item = NSMenuItem()
item.view = hosting
Expand Down Expand Up @@ -328,9 +328,9 @@ extension StatusItemController {

let chartView = ZaiHourlyUsageChartMenuView(modelUsage: modelUsage, width: width)
let hosting = MenuHostingView(rootView: chartView)
let controller = NSHostingController(rootView: chartView)
let size = controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let chartItem = NSMenuItem()
chartItem.view = hosting
Expand Down
15 changes: 12 additions & 3 deletions Sources/CodexBar/StatusItemController+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1541,13 +1541,22 @@ extension StatusItemController {

for item in menu.items {
guard let view = item.view else { continue }
view.frame = NSRect(origin: .zero, size: NSSize(width: width, height: 1))
view.layoutSubtreeIfNeeded()
let height = view.fittingSize.height
let height = self.hostedSubviewFittingHeight(for: view, width: width)
view.frame = NSRect(origin: .zero, size: NSSize(width: width, height: height))
}
}

/// Measures the natural height of a hosted submenu view at the given width using the live
/// view that will actually be displayed. Hosted chart items used to spin up a second,
/// throwaway `NSHostingController` purely to size the chart even though every build path
/// immediately re-measures the live view via `fittingSize`; that extra SwiftUI hierarchy was
/// pure overhead on a popup-menu hot path, so callers now size the displayed view directly.
func hostedSubviewFittingHeight(for view: NSView, width: CGFloat) -> CGFloat {
view.frame = NSRect(origin: .zero, size: NSSize(width: width, height: 1))
view.layoutSubtreeIfNeeded()
return view.fittingSize.height
}

@objc func menuCardNoOp(_ sender: NSMenuItem) {
_ = sender
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/CodexBar/StatusItemController+UsageHistoryMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ extension StatusItemController {
snapshot: snapshot,
width: width)
let hosting = UsageHistoryMenuHostingView(rootView: chartView)
let controller = NSHostingController(rootView: chartView)
let size = controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude))
hosting.frame = NSRect(origin: .zero, size: NSSize(width: width, height: size.height))
hosting.frame = NSRect(
origin: .zero,
size: NSSize(width: width, height: self.hostedSubviewFittingHeight(for: hosting, width: width)))

let chartItem = NSMenuItem()
chartItem.view = hosting
Expand Down
70 changes: 70 additions & 0 deletions Tests/CodexBarTests/StatusMenuHostedSubmenuRefreshTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,76 @@ struct StatusMenuHostedSubmenuRefreshTests {
seed: Self.seedZaiHourlyUsage)
}

@Test
func `hosted chart items size to the displayed view without a throwaway controller`() throws {
try self.assertHostedChartItemHeightMatchesRefresh(
chartID: StatusItemController.costHistoryChartID,
provider: .claude,
seed: Self.seedClaudeSnapshots)
{ controller, submenu, width in
controller.appendCostHistoryChartItem(to: submenu, provider: .claude, width: width)
}
try self.assertHostedChartItemHeightMatchesRefresh(
chartID: StatusItemController.usageHistoryChartID,
provider: .claude,
seed: Self.seedPlanUtilizationHistory)
{ controller, submenu, width in
controller.appendUsageHistoryChartItem(to: submenu, provider: .claude, width: width)
}
try self.assertHostedChartItemHeightMatchesRefresh(
chartID: StatusItemController.storageBreakdownID,
provider: .claude,
seed: Self.seedStorageFootprint)
{ controller, submenu, width in
controller.appendStorageBreakdownItem(to: submenu, provider: .claude, width: width)
}
}

private func assertHostedChartItemHeightMatchesRefresh(
chartID: String,
provider: UsageProvider,
seed: (UsageStore) -> Void,
append: (StatusItemController, NSMenu, CGFloat) -> Bool) throws
{
let previousMenuCardRendering = StatusItemController.menuCardRenderingEnabled
StatusItemController.menuCardRenderingEnabled = true
defer { StatusItemController.menuCardRenderingEnabled = previousMenuCardRendering }

let settings = Self.makeSettings()
settings.statusChecksEnabled = false
settings.refreshFrequency = .manual
settings.costUsageEnabled = true
settings.providerStorageFootprintsEnabled = true
Self.enableOnly(settings, provider: provider)

let fetcher = UsageFetcher()
let store = UsageStore(fetcher: fetcher, browserDetection: BrowserDetection(cacheTTL: 0), settings: settings)
seed(store)

let controller = StatusItemController(
store: store,
settings: settings,
account: fetcher.loadAccountInfo(),
updater: DisabledUpdaterController(),
preferencesSelection: PreferencesSelection(),
statusBar: .system)
defer { controller.releaseStatusItemsForTesting() }

let width = StatusItemController.menuCardBaseWidth
let submenu = NSMenu()
submenu.minimumWidth = width
#expect(append(controller, submenu, width))

let item = try #require(submenu.items.first)
let view = try #require(item.view)
let heightFromAppend = view.frame.height
// The height the append path assigns must match the authoritative re-measure pass; otherwise
// dropping the throwaway NSHostingController would have changed sizing behavior.
controller.refreshHostedSubviewHeights(in: submenu)
#expect(view.frame.height == heightFromAppend)
#expect(heightFromAppend > 1)
}

private func assertHostedSubmenuPreservesIdentity(
chartID: String,
provider: UsageProvider,
Expand Down