Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

Replace token bars with 30-week heatmap

Bobc73c911db20f
diff --git a/AGENTS.md b/AGENTS.md
index b8f5c4c..5e1300e 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -12,8 +12,8 @@ CodexBar 是一个 macOS 菜单栏应用,用来展示当前本机 Codex 账号
- 弹窗第一行展示账号信息和套餐。
- 弹窗展示两行分段电量条,额度行标签根据 `windowDurationMins` 动态生成。
- 每行额度在进度条右侧展示剩余百分比和重置时间。
-- 弹窗展示最近 14 个完整自然日的 token 柱状图、累计 token 和单日峰值 token。
-- 鼠标划过 token 柱状图中的单日柱子时,展示对应日期和用量。
+- 弹窗展示 30 周宽度的完整自然日 token 蓝色贡献墙、累计 token 和单日峰值 token。
+- 鼠标划过 token 贡献墙中的单日方块时,在鼠标旁展示对应日期和用量。
- 应用运行时每分钟自动刷新,即使弹窗未打开也会刷新。
- 双击账号图标可手动刷新。
- 按住 Option/Alt 点击菜单栏图标时,才展示设置区。设置区为单行布局:左侧「开机自动启动」开关,右侧「退出」按钮,不展示刷新按钮。
@@ -103,7 +103,7 @@ Xcode target 已关闭 App Sandbox,因为应用需要启动本机 Codex CLI
- 分段条展示剩余额度,剩余额度计算方式是 `100 - usedPercent`。
- 额度行标签示例:`300` 分钟显示 `5 小时`,`10080` 分钟显示 `7 天`;缺少 `windowDurationMins` 时再回退到默认文案。
- 重置时间格式使用 `MM-dd HH:mm`;更新时间格式使用 `HH:mm:ss`。
-- token 数按 `M`、`B` 紧凑单位展示;token 柱状图使用橙色,单日用量越高颜色越浓。
+- token 数按 `M`、`B` 紧凑单位展示;token 贡献墙使用蓝色方块,7 行 30 列布局,单日用量越高颜色越深
## 错误展示约定
diff --git a/CodexBar.xcodeproj/project.pbxproj b/CodexBar.xcodeproj/project.pbxproj
index 3b2136a..3bb6c41 100644
--- a/CodexBar.xcodeproj/project.pbxproj
+++ b/CodexBar.xcodeproj/project.pbxproj
@@ -268,7 +268,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 1.1.0;
+ MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
@@ -305,7 +305,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 1.1.0;
+ MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
diff --git a/CodexBar/QuotaRow.swift b/CodexBar/QuotaRow.swift
index f8fb8f7..71c9223 100644
--- a/CodexBar/QuotaRow.swift
+++ b/CodexBar/QuotaRow.swift
@@ -75,11 +75,16 @@ private struct SegmentedQuotaBar: View {
let percent: Int
var body: some View {
- HStack(spacing: Metrics.spacing) {
- ForEach(0..<Metrics.segmentCount, id: \.self) { index in
- Capsule(style: .continuous)
- .fill(index < filledSegments ? fillStyle : emptyStyle)
- .frame(maxWidth: .infinity)
+ GeometryReader { proxy in
+ let segmentCount = segmentCount(for: proxy.size.width)
+ let filledSegments = filledSegments(for: segmentCount)
+
+ HStack(spacing: Metrics.spacing) {
+ ForEach(0..<segmentCount, id: \.self) { index in
+ Capsule(style: .continuous)
+ .fill(index < filledSegments ? fillStyle : emptyStyle)
+ .frame(maxWidth: .infinity)
+ }
}
}
.accessibilityLabel("剩余 \(percent)%")
@@ -89,11 +94,15 @@ private struct SegmentedQuotaBar: View {
private extension SegmentedQuotaBar {
enum Metrics {
static let spacing: CGFloat = 2
- static let segmentCount = 20
+ static let idealSegmentWidth: CGFloat = 6
+ }
+
+ func segmentCount(for width: CGFloat) -> Int {
+ max(1, Int((width + Metrics.spacing) / (Metrics.idealSegmentWidth + Metrics.spacing)))
}
- var filledSegments: Int {
- Int((Double(percent) / 100.0 * Double(Metrics.segmentCount)).rounded())
+ func filledSegments(for segmentCount: Int) -> Int {
+ Int((Double(percent) / 100.0 * Double(segmentCount)).rounded())
}
var fillStyle: Color {
diff --git a/CodexBar/RateLimitsMenuView.swift b/CodexBar/RateLimitsMenuView.swift
index c1a1866..80fcd51 100644
--- a/CodexBar/RateLimitsMenuView.swift
+++ b/CodexBar/RateLimitsMenuView.swift
@@ -9,7 +9,7 @@ import AppKit
import SwiftUI
struct RateLimitsMenuView: View {
- static let menuWidth: CGFloat = 360
+ static let menuWidth: CGFloat = Metrics.padding * 2 + UsageHeatmap.Metrics.totalWidth
@ObservedObject var viewModel: RateLimitsViewModel
@StateObject private var loginItemSettings = LoginItemSettings()
@@ -211,12 +211,12 @@ private extension RateLimitsMenuView {
private struct UsageSummaryView: View {
let usage: CodexUsageSnapshot
- private let recentDays: [CodexUsageSnapshot.Day]
+ private let days: [CodexUsageSnapshot.Day]
@State private var hoveredDay: CodexUsageSnapshot.Day?
init(usage: CodexUsageSnapshot) {
self.usage = usage
- self.recentDays = usage.recentDays(count: 14, endingDaysAgo: 1)
+ self.days = usage.recentDays(count: UsageHeatmap.Metrics.dayCount, endingDaysAgo: 1)
}
var body: some View {
@@ -226,45 +226,16 @@ private struct UsageSummaryView: View {
HStack(alignment: .firstTextBaseline) {
metric(label: "单日峰值", value: usage.summary.peakDailyTokens, alignment: .leading)
Spacer()
- hoveredMetric
- Spacer()
metric(label: "全时累计", value: usage.summary.lifetimeTokens, alignment: .trailing)
}
- UsageBarChart(days: recentDays, hoveredDay: $hoveredDay)
+ UsageHeatmap(days: days, hoveredDay: $hoveredDay)
}
.onAppear {
hoveredDay = nil
}
}
- var hoveredMetric: some View {
- VStack(spacing: 2) {
- Text(hoveredDateText)
- .font(.caption2.monospacedDigit())
- .foregroundStyle(.secondary)
-
- Text(hoveredTokensText)
- .font(.caption.monospacedDigit().weight(.semibold))
- .lineLimit(1)
- .minimumScaleFactor(0.8)
- }
- .opacity(hoveredDay == nil ? 0 : 1)
- .frame(width: 112, alignment: .center)
- }
-
- private var hoveredDateText: String {
- hoveredDay?.startDate ?? " "
- }
-
- private var hoveredTokensText: String {
- guard let hoveredDay else {
- return " "
- }
-
- return TokenCountFormatter.string(from: hoveredDay.tokens)
- }
-
private func metric(label: String, value: Int, alignment: Alignment) -> some View {
VStack(alignment: .leading, spacing: 2) {
Text(label)
@@ -282,64 +253,190 @@ private struct UsageSummaryView: View {
}
}
-private struct UsageBarChart: View {
+private struct UsageHeatmap: View {
let days: [CodexUsageSnapshot.Day]
@Binding var hoveredDay: CodexUsageSnapshot.Day?
+ @State private var hoverLocation: CGPoint = .zero
private var peakTokens: Int {
- max(days.map(\.tokens).max() ?? 0, 1)
+ max(days.lazy.map(\.tokens).max() ?? 0, 1)
}
var body: some View {
- HStack(alignment: .bottom, spacing: 5) {
- ForEach(days) { day in
- UsageBar(
- day: day,
- percent: Double(day.tokens) / Double(peakTokens),
- isHovered: hoveredDay?.id == day.id
- )
- .onHover { isHovering in
- if isHovering {
- hoveredDay = day
- } else if hoveredDay?.id == day.id {
- hoveredDay = nil
+ ZStack(alignment: .topLeading) {
+ heatmapGrid
+
+ if let hoveredDay {
+ UsageHeatmapTooltip(day: hoveredDay)
+ .allowsHitTesting(false)
+ .offset(tooltipOffset)
+ .zIndex(1)
+ .transition(.opacity.combined(with: .scale(scale: 0.96, anchor: .topLeading)))
+ .transaction { transaction in
+ transaction.animation = nil
}
+ }
+ }
+ .frame(height: Metrics.height)
+ .coordinateSpace(name: Metrics.coordinateSpaceName)
+ .animation(.snappy(duration: 0.16), value: hoveredDay?.id)
+ }
+
+ private var heatmapGrid: some View {
+ VStack(alignment: .leading, spacing: 6) {
+ HStack(alignment: .firstTextBaseline) {
+ Text("近 \(days.count) 天")
+ .font(.caption2.weight(.medium))
+ .foregroundStyle(.secondary)
+
+ Spacer()
+
+ if let firstDate = days.first?.startDate, let lastDate = days.last?.startDate {
+ Text("\(firstDate) ~ \(lastDate)")
+ .font(.caption2.monospacedDigit())
+ .foregroundStyle(.tertiary)
+ .lineLimit(1)
}
}
+
+ HStack(alignment: .top, spacing: Metrics.squareSpacing) {
+ ForEach(0..<Metrics.columnCount, id: \.self) { column in
+ VStack(spacing: Metrics.squareSpacing) {
+ ForEach(0..<Metrics.rowCount, id: \.self) { row in
+ let index = column * Metrics.rowCount + row
+
+ if days.indices.contains(index) {
+ let day = days[index]
+ UsageHeatmapSquare(
+ day: day,
+ percent: Double(day.tokens) / Double(peakTokens),
+ isHovered: hoveredDay?.id == day.id
+ ) { hoveredDay, location in
+ self.hoveredDay = hoveredDay
+ hoverLocation = location
+ } onEnded: { day in
+ if self.hoveredDay?.id == day.id {
+ self.hoveredDay = nil
+ }
+ }
+ } else {
+ Color.clear
+ .frame(width: Metrics.squareSize, height: Metrics.squareSize)
+ }
+ }
+ }
+ }
+ }
+ }
+ .frame(maxWidth: .infinity, alignment: .leading)
+ }
+
+ private var tooltipOffset: CGSize {
+ CGSize(
+ width: min(max(hoverLocation.x + 10, 0), max(0, Metrics.totalWidth - Metrics.tooltipWidth)),
+ height: min(max(hoverLocation.y + 12, 0), max(0, Metrics.height - Metrics.tooltipHeight))
+ )
+ }
+}
+
+private extension UsageHeatmap {
+ enum Metrics {
+ static let coordinateSpaceName = "usageHeatmap"
+ static let columnCount = 30
+ static let rowCount = 7
+ static let squareSize: CGFloat = 12
+ static let squareSpacing: CGFloat = 3
+ static let height: CGFloat = 120
+ static let tooltipWidth: CGFloat = 78
+ static let tooltipHeight: CGFloat = 38
+
+ static var dayCount: Int { columnCount * rowCount }
+
+ static var totalWidth: CGFloat {
+ CGFloat(columnCount) * squareSize + CGFloat(columnCount - 1) * squareSpacing
}
- .frame(height: 58)
}
}
-private struct UsageBar: View {
+private struct UsageHeatmapSquare: View {
let day: CodexUsageSnapshot.Day
let percent: Double
let isHovered: Bool
+ let onActive: (CodexUsageSnapshot.Day, CGPoint) -> Void
+ let onEnded: (CodexUsageSnapshot.Day) -> Void
var body: some View {
GeometryReader { proxy in
- let barHeight = max(day.tokens > 0 ? 2 : 0, proxy.size.height * clampedPercent)
-
- ZStack(alignment: .bottom) {
- Rectangle()
- .fill(.secondary.opacity(isHovered ? 0.20 : 0.12))
-
- Rectangle()
- .fill(Color.orange.opacity(day.tokens > 0 ? barOpacity : 0))
- .frame(height: barHeight)
- }
+ RoundedRectangle(cornerRadius: 4, style: .continuous)
+ .fill(fillColor)
+ .overlay {
+ RoundedRectangle(cornerRadius: 4, style: .continuous)
+ .stroke(borderColor, lineWidth: isHovered ? 1.2 : 0.7)
+ }
+ .shadow(color: .blue.opacity(0.22), radius: isHovered ? 5 : 0, y: isHovered ? 2 : 0)
+ .scaleEffect(isHovered ? 1.08 : 1)
+ .onContinuousHover { phase in
+ switch phase {
+ case .active(let location):
+ let frame = proxy.frame(in: .named(UsageHeatmap.Metrics.coordinateSpaceName))
+ onActive(day, CGPoint(x: frame.minX + location.x, y: frame.minY + location.y))
+ case .ended:
+ onEnded(day)
+ }
+ }
}
- .frame(maxWidth: .infinity)
- .contentShape(Rectangle())
+ .frame(width: UsageHeatmap.Metrics.squareSize, height: UsageHeatmap.Metrics.squareSize)
+ .contentShape(RoundedRectangle(cornerRadius: 4, style: .continuous))
+ .animation(.snappy(duration: 0.14), value: isHovered)
}
private var clampedPercent: Double {
min(max(percent, 0), 1)
}
- private var barOpacity: Double {
- let opacity = 0.28 + clampedPercent * 0.62
- return isHovered ? min(opacity + 0.10, 1.0) : opacity
+ private var fillColor: Color {
+ guard day.tokens > 0 else {
+ return Color.blue.opacity(isHovered ? 0.16 : 0.08)
+ }
+
+ let intensity = pow(clampedPercent, 0.62)
+ let opacity = 0.18 + intensity * 0.68
+ return Color.blue.opacity(isHovered ? min(opacity + 0.10, 1.0) : opacity)
+ }
+
+ private var borderColor: Color {
+ if isHovered {
+ return Color.blue.opacity(0.78)
+ }
+
+ return Color.blue.opacity(day.tokens > 0 ? 0.18 : 0.10)
+ }
+}
+
+private struct UsageHeatmapTooltip: View {
+ let day: CodexUsageSnapshot.Day
+
+ var body: some View {
+ VStack(alignment: .leading, spacing: 2) {
+ Text(day.startDate)
+ .font(.caption2.monospacedDigit())
+ .foregroundStyle(Color(nsColor: .secondaryLabelColor))
+
+ Text(TokenCountFormatter.string(from: day.tokens))
+ .font(.caption.monospacedDigit().weight(.semibold))
+ .foregroundStyle(Color(nsColor: .labelColor))
+ .lineLimit(1)
+ .minimumScaleFactor(0.8)
+ }
+ .padding(.horizontal, 7)
+ .padding(.vertical, 5)
+ .frame(width: UsageHeatmap.Metrics.tooltipWidth, alignment: .leading)
+ .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 7, style: .continuous))
+ .overlay {
+ RoundedRectangle(cornerRadius: 7, style: .continuous)
+ .stroke(Color.blue.opacity(0.22), lineWidth: 0.8)
+ }
+ .shadow(color: .black.opacity(0.14), radius: 7, y: 3)
}
}
@@ -351,7 +448,6 @@ private enum TokenCountFormatter {
return decimal(Double(tokens) / 1_000_000) + "M"
}
-
private static func decimal(_ value: Double) -> String {
let rounded = value >= 0.1 ? (value * 10).rounded() / 10 : (value * 100).rounded() / 100
return formatter.string(from: NSNumber(value: rounded)) ?? String(format: "%.1f", rounded)
diff --git a/README.md b/README.md
index c49e41e..e7a1a91 100644
--- a/README.md
+++ b/README.md
@@ -11,8 +11,8 @@ CodexBar 是一个轻量级 macOS 菜单栏应用,用来查看当前本机 Cod
- 弹窗第一行展示账号信息和套餐
- 弹窗使用两行分段电量条展示额度窗口,标签根据 `windowDurationMins` 动态生成
- 每行额度在进度条右侧展示剩余百分比和重置时间
-- 弹窗展示最近 14 个完整自然日的 token 柱状图、累计 token 和单日峰值 token
-- 鼠标划过 token 柱状图中的单日柱子时,展示对应日期和用量
+- 弹窗展示 30 周宽度的完整自然日 token 蓝色贡献墙、累计 token 和单日峰值 token
+- 鼠标划过 token 贡献墙中的单日方块时,在鼠标旁展示对应日期和用量
- 更新时间显示在额度行下方,并使用 `HH:mm:ss` 格式
- 应用运行时每分钟自动刷新额度,即使弹窗未打开也会刷新
- 双击账号图标可手动刷新
@@ -86,7 +86,7 @@ token 使用量来自 `account/usage/read`:
如果本机 Codex app-server 不支持 `account/usage/read`,token 统计区域不会展示,额度信息仍会正常显示。
token 数使用 `M`、`B` 紧凑单位展示。
-token 柱状图使用橙色,单日用量越高颜色越浓。
+token 贡献墙使用蓝色方块,7 行 30 列布局,单日用量越高颜色越深。
## 错误展示