CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息
Swift appkitcodexcodex-clideveloper-toolsmacosmacos-appmenu-bar-appopenai-codexproductivityswiftswiftuiusage-tracker
fix: 优化重置次数面板定位
Bob938abc1e3f0c
diff --git a/AGENTS.md b/AGENTS.mdindex 8134929..0605125 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -184,8 +184,8 @@ Hook 统计的配置、存储、保留策略和统计口径见 [Docs/CodexHook.m- `LiquidGlassStyle.swift` 是自绘玻璃效果, 不是 macOS 原生 `.glassEffect`; 没有明确设计要求时不要切换。- 账号图标双击触发刷新。邮箱文本双击切换模糊。- 计划名是右侧加粗纯文字; `planBadgeTint(for:)` 优先级: enterprise -> team/business -> pro -> plus -> edu -> free -> 默认 cyan。-- `resetCreditsAvailableCount > 0` 时, 只在置顶主 limit 标题右侧显示 `重置次数: N`; 该控件是 plain button, 点击后通过 `ResetCreditsPanelController` 切换重置次数侧边详情面板。面板按升序逐行展示 `yyyy-MM-dd HH:mm:ss` 和 `可用: N`, 相同展示时间合并数量, 单个显示 `可用: 1`; 没有过期时间时显示「未知过期时间」。-- 重置次数侧边详情面板是菜单面板的 borderless nonactivating child panel, 可接收鼠标事件但不能成为 key/main window; 与菜单面板 gap 为 `4`, 屏幕边缘保留 `8` px, 内容宽 `147`, 最小高 `62`, 最大高 `260`, 圆角 `12`; 展开 `0.18` 秒、收起 `0.12` 秒。点击面板内部不关闭菜单面板, 点击外部关闭; 与热力图详情面板互斥, 任一面板显示时会立即隐藏另一面板。+- `resetCreditsAvailableCount > 0` 时, 只在置顶主 limit 标题右侧显示 `重置次数: N`; 该控件是 plain button, 点击后通过 `ResetCreditsPanelController` 切换重置次数侧边详情面板。面板按升序逐行展示 `yyyy-MM-dd HH:mm:ss` 和 `可用: N`, 相同展示时间合并数量, 单个显示 `可用: 1`; 多行之间显示渐变分隔线; 没有过期时间时显示「未知过期时间」。+- 重置次数侧边详情面板是菜单面板的 borderless nonactivating child panel, 可接收鼠标事件但不能成为 key/main window; 与菜单面板 gap 为 `4`, 屏幕边缘保留 `8` px, 内容宽 `147`, 最小高 `62`, 无可用锚点时最大高回退为 `260`, 有可用锚点时按额度区顶部到菜单面板底部的可用空间夹紧, 圆角 `12`; 锚点坐标异常或与当前菜单内容区域不相交时丢弃并改为居中对齐菜单面板; 展开 `0.18` 秒、收起 `0.12` 秒。点击面板内部不关闭菜单面板, 点击外部关闭; 与热力图详情面板互斥, 任一面板显示时会立即隐藏另一面板。- 额度条展示剩余百分比, 固定为 50 个胶囊, 每个胶囊宽 `3.5`, 间距 `2`, 高 `12`; 颜色按 20% 一档: 红、橙、黄、薄荷、绿。- 额度行标签列宽 `34`, 居中显示, 标签允许最小缩放到 `0.75`, 标签到额度条间距 `12`, 额度条到百分比间距 `8`, 百分比列宽 `37`, 百分比到重置时间最小间距 `6`, 重置时间列宽 `75`; 不通过加宽菜单面板解决额度行溢出。- 无 quota 数据时显示 `--` / `暂无数据`, 并使用占位色。diff --git a/CodexBar/Controllers/ResetCreditsPanelController.swift b/CodexBar/Controllers/ResetCreditsPanelController.swiftindex 76aa163..8b26924 100644--- a/CodexBar/Controllers/ResetCreditsPanelController.swift+++ b/CodexBar/Controllers/ResetCreditsPanelController.swift@@ -95,13 +95,25 @@ final class ResetCreditsPanelController {}let panel = ensurePanel()- let panelSize = ResetCreditsPanelView.panelSize(for: context)+ let menuSurfaceFrame = contentScreenFrame(for: contentView, in: menuSurfaceWindow) ?? menuSurfaceWindow.frame+ let alignmentScreenFrame = validAlignmentScreenFrame(+ context.alignmentScreenFrame,+ menuSurfaceFrame: menuSurfaceFrame+ )+ let resolvedContext = context.withPanelGeometry(+ alignmentScreenFrame: alignmentScreenFrame,+ maximumPanelHeight: panelMaximumHeight(+ menuSurfaceFrame: menuSurfaceFrame,+ alignmentScreenFrame: alignmentScreenFrame+ )+ )+ let panelSize = ResetCreditsPanelView.panelSize(for: resolvedContext)let position = panelPosition(for: panelSize,- relativeTo: menuSurfaceWindow,- contentView: contentView,- alignmentScreenFrame: context.alignmentScreenFrame,- preferredSide: context.preferredSide+ menuSurfaceFrame: menuSurfaceFrame,+ visibleFrame: (menuSurfaceWindow.screen ?? NSScreen.main)?.visibleFrame ?? menuSurfaceFrame,+ alignmentScreenFrame: resolvedContext.alignmentScreenFrame,+ preferredSide: resolvedContext.preferredSide)panel.level = menuSurfaceWindow.level@@ -110,7 +122,7 @@ final class ResetCreditsPanelController {}visibilityGeneration += 1- apply(context: context, panelSize: panelSize, position: position, to: panel)+ apply(context: resolvedContext, panelSize: panelSize, position: position, to: panel)showPanelWithDrawerAnimation(panel, relativeTo: menuSurfaceWindow)}@@ -231,13 +243,11 @@ final class ResetCreditsPanelController {private func panelPosition(for panelSize: CGSize,- relativeTo menuSurfaceWindow: NSWindow,- contentView: NSView?,+ menuSurfaceFrame: CGRect,+ visibleFrame: CGRect,alignmentScreenFrame: CGRect?,preferredSide: UsageHeatmapDetailSide) -> PanelPosition {- let menuSurfaceFrame = contentScreenFrame(for: contentView, in: menuSurfaceWindow) ?? menuSurfaceWindow.frame- let visibleFrame = (menuSurfaceWindow.screen ?? NSScreen.main)?.visibleFrame ?? menuSurfaceFramelet leftX = menuSurfaceFrame.minX - Metrics.panelGap - panelSize.widthlet rightX = menuSurfaceFrame.maxX + Metrics.panelGaplet horizontal = horizontalPlacement(@@ -293,6 +303,52 @@ final class ResetCreditsPanelController {return menuSurfaceFrame.midY - panelSize.height / 2}+ private func panelMaximumHeight(+ menuSurfaceFrame: CGRect,+ alignmentScreenFrame: CGRect?+ ) -> CGFloat? {+ guard let alignmentScreenFrame else {+ return nil+ }++ let maximumHeight = alignmentScreenFrame.maxY - menuSurfaceFrame.minY+ guard maximumHeight.isFinite, maximumHeight > 0 else {+ return nil+ }+ return maximumHeight+ }++ private func validAlignmentScreenFrame(+ _ alignmentScreenFrame: CGRect?,+ menuSurfaceFrame: CGRect+ ) -> CGRect? {+ guard let alignmentScreenFrame = alignmentScreenFrame?.standardized,+ isValidScreenFrame(alignmentScreenFrame) else {+ return nil+ }++ let validationFrame = menuSurfaceFrame+ .standardized+ .insetBy(+ dx: -Metrics.anchorValidationTolerance,+ dy: -Metrics.anchorValidationTolerance+ )+ guard validationFrame.intersects(alignmentScreenFrame) else {+ return nil+ }++ return alignmentScreenFrame+ }++ private func isValidScreenFrame(_ frame: CGRect) -> Bool {+ frame.minX.isFinite+ && frame.minY.isFinite+ && frame.width.isFinite+ && frame.height.isFinite+ && frame.width > 0+ && frame.height > 0+ }+private func horizontalPlacement(preferredSide: UsageHeatmapDetailSide,left: HorizontalPlacement,@@ -445,6 +501,7 @@ final class ResetCreditsPanelController {private enum Metrics {static let panelGap: CGFloat = 4static let screenPadding: CGFloat = 8+ static let anchorValidationTolerance: CGFloat = 6static let drawerEnterDuration: TimeInterval = 0.18static let drawerExitDuration: TimeInterval = 0.12static let drawerOverscan: CGFloat = 1diff --git a/CodexBar/Views/Menu/CodexStatusMenuSections.swift b/CodexBar/Views/Menu/CodexStatusMenuSections.swiftindex d8f5d96..605d9ab 100644--- a/CodexBar/Views/Menu/CodexStatusMenuSections.swift+++ b/CodexBar/Views/Menu/CodexStatusMenuSections.swift@@ -140,7 +140,7 @@ struct QuotaLimitsSection: View {let resetCreditExpirationDates: [Date]?let isStale: Boollet onResetCreditsTap: (ResetCreditsPanelContext) -> Void- @State private var sectionScreenFrame: CGRect?+ @State private var sectionFrameProvider = ScreenFrameProvider()var body: some View {VStack(alignment: .leading, spacing: 8) {@@ -156,9 +156,7 @@ struct QuotaLimitsSection: View {.padding(MenuMetrics.panelPadding).liquidGlassSurface(cornerRadius: MenuMetrics.panelCornerRadius).background {- ScreenFrameReader { frame in- sectionScreenFrame = frame- }+ ScreenFrameReader(provider: sectionFrameProvider)}}@@ -195,7 +193,7 @@ struct QuotaLimitsSection: View {onResetCreditsTap(ResetCreditsPanelContext(expirationDates: resetCreditExpirationDates ?? [],- alignmentScreenFrame: sectionScreenFrame,+ alignmentScreenFrame: sectionFrameProvider.currentScreenFrame(),preferredSide: .right))diff --git a/CodexBar/Views/Menu/ResetCreditsPanelView.swift b/CodexBar/Views/Menu/ResetCreditsPanelView.swiftindex 8ae6bad..0d41a74 100644--- a/CodexBar/Views/Menu/ResetCreditsPanelView.swift+++ b/CodexBar/Views/Menu/ResetCreditsPanelView.swift@@ -5,6 +5,31 @@ nonisolated struct ResetCreditsPanelContext: Equatable {let expirationDates: [Date]let alignmentScreenFrame: CGRect?let preferredSide: UsageHeatmapDetailSide+ let maximumPanelHeight: CGFloat?++ init(+ expirationDates: [Date],+ alignmentScreenFrame: CGRect?,+ preferredSide: UsageHeatmapDetailSide,+ maximumPanelHeight: CGFloat? = nil+ ) {+ self.expirationDates = expirationDates+ self.alignmentScreenFrame = alignmentScreenFrame+ self.preferredSide = preferredSide+ self.maximumPanelHeight = maximumPanelHeight+ }++ func withPanelGeometry(+ alignmentScreenFrame: CGRect?,+ maximumPanelHeight: CGFloat?+ ) -> Self {+ Self(+ expirationDates: expirationDates,+ alignmentScreenFrame: alignmentScreenFrame,+ preferredSide: preferredSide,+ maximumPanelHeight: maximumPanelHeight+ )+ }}/// 重置次数侧边详情, 按过期时间从近到远展示@@ -14,16 +39,21 @@ struct ResetCreditsPanelView: View {var body: some View {let groups = expirationGroups- let panelSize = Self.panelSize(forGroupCount: groups.count)+ let panelSize = Self.panelSize(for: context)VStack(alignment: .leading, spacing: Metrics.rowSpacing) {if groups.isEmpty {emptyMessage} else {ScrollView(.vertical) {- VStack(alignment: .leading, spacing: Metrics.rowSpacing) {- ForEach(groups) { group in+ VStack(alignment: .leading, spacing: 0) {+ ForEach(Array(groups.enumerated()), id: \.element.id) { index, group inrow(for: group)++ if index < groups.count - 1 {+ rowDivider+ .padding(.vertical, Metrics.dividerVerticalPadding)+ }}}.frame(maxWidth: .infinity, alignment: .center)@@ -56,16 +86,22 @@ struct ResetCreditsPanelView: View {}static func panelSize(for context: ResetCreditsPanelContext) -> CGSize {- panelSize(forGroupCount: ResetCreditExpirationGroup.grouped(from: context.expirationDates).count)+ panelSize(+ forGroupCount: ResetCreditExpirationGroup.grouped(from: context.expirationDates).count,+ maximumHeight: context.maximumPanelHeight+ )}- private static func panelSize(forGroupCount rowCount: Int) -> CGSize {+ private static func panelSize(forGroupCount rowCount: Int, maximumHeight: CGFloat? = nil) -> CGSize {let rowHeight = CGFloat(max(rowCount, 1)) * Metrics.rowHeightlet rowSpacing = CGFloat(max(rowCount - 1, 0)) * Metrics.rowSpacinglet rawHeight = Metrics.baseHeight + rowHeight + rowSpacing+ let maximumPanelHeight = maximumHeight.map {+ max($0, Metrics.minimumPanelHeight)+ } ?? Metrics.fallbackMaximumPanelHeightreturn CGSize(width: Metrics.panelWidth,- height: min(max(rawHeight, Metrics.minimumPanelHeight), Metrics.maximumPanelHeight)+ height: min(max(rawHeight, Metrics.minimumPanelHeight), maximumPanelHeight))}@@ -91,6 +127,25 @@ struct ResetCreditsPanelView: View {.frame(height: Metrics.rowHeight)}+ private var rowDivider: some View {+ Rectangle()+ .fill(+ LinearGradient(+ colors: [+ .clear,+ .primary.opacity(0.18),+ .green.opacity(0.16),+ .primary.opacity(0.18),+ .clear+ ],+ startPoint: .leading,+ endPoint: .trailing+ )+ )+ .frame(height: Metrics.dividerHeight)+ .padding(.horizontal, 2)+ }+private func rowHeader(for group: ResetCreditExpirationGroup) -> some View {expirationText(group.expirationText).hidden()@@ -144,10 +199,12 @@ struct ResetCreditsPanelView: View {static let horizontalPadding: CGFloat = 12static let panelWidth: CGFloat = expirationTextWidth + horizontalPadding * 2static let minimumPanelHeight: CGFloat = 62- static let maximumPanelHeight: CGFloat = 260+ static let fallbackMaximumPanelHeight: CGFloat = 260static let baseHeight: CGFloat = verticalPadding * 2static let rowHeight: CGFloat = 42static let rowSpacing: CGFloat = 7+ static let dividerHeight: CGFloat = 1+ static let dividerVerticalPadding: CGFloat = (rowSpacing - dividerHeight) / 2static let verticalPadding: CGFloat = 10static let cornerRadius: CGFloat = 12static let boundaryLineWidth: CGFloat = 0.8diff --git a/CodexBar/Views/Menu/ScreenFrameReader.swift b/CodexBar/Views/Menu/ScreenFrameReader.swiftindex c583c2e..e57a262 100644--- a/CodexBar/Views/Menu/ScreenFrameReader.swift+++ b/CodexBar/Views/Menu/ScreenFrameReader.swift@@ -3,29 +3,98 @@ import SwiftUI/// 通过 NSView 桥接读取 SwiftUI 视图在屏幕坐标系中的 framestruct ScreenFrameReader: NSViewRepresentable {- let onChange: (CGRect?) -> Void+ let provider: ScreenFrameProvider?+ let onChange: ((CGRect?) -> Void)?++ init(+ provider: ScreenFrameProvider? = nil,+ onChange: ((CGRect?) -> Void)? = nil+ ) {+ self.provider = provider+ self.onChange = onChange+ }func makeNSView(context _: Context) -> ScreenFrameReportingView {let view = ScreenFrameReportingView()+ view.frameProvider = providerview.onChange = onChangereturn view}func updateNSView(_ nsView: ScreenFrameReportingView, context _: Context) {+ nsView.frameProvider = providernsView.onChange = onChangensView.scheduleReport()}}+/// 在交互发生时同步读取最新 screen frame, 避免只依赖异步 SwiftUI 状态+@MainActor+final class ScreenFrameProvider {+ private weak var view: NSView?+ private var lastValidFrame: CGRect?++ func bind(to view: NSView) {+ self.view = view+ }++ func currentScreenFrame() -> CGRect? {+ if let currentFrame = Self.screenFrame(for: view) {+ lastValidFrame = currentFrame+ return currentFrame+ }++ return lastValidFrame+ }++ func updateLastValidFrame(_ frame: CGRect?) {+ guard let frame else {+ return+ }++ lastValidFrame = frame+ }++ fileprivate static func screenFrame(for view: NSView?) -> CGRect? {+ guard let view, let window = view.window else {+ return nil+ }++ view.layoutSubtreeIfNeeded()+ let frameInWindow = view.convert(view.bounds, to: nil)+ let screenFrame = window.convertToScreen(frameInWindow).standardized+ guard isValidScreenFrame(screenFrame) else {+ return nil+ }+ return screenFrame+ }++ private static func isValidScreenFrame(_ frame: CGRect) -> Bool {+ frame.minX.isFinite+ && frame.minY.isFinite+ && frame.width.isFinite+ && frame.height.isFinite+ && frame.width > 0+ && frame.height > 0+ }+}+/// 只在 SwiftUI 布局稳定后上报 frame, 避免重渲染期间反复触发无效定位@MainActorfinal class ScreenFrameReportingView: NSView {var onChange: ((CGRect?) -> Void)?+ weak var frameProvider: ScreenFrameProvider? {+ didSet {+ frameProvider?.bind(to: self)+ }+ }+private var lastReportedFrame: CGRect?private var reportScheduled = falseoverride func viewDidMoveToWindow() {super.viewDidMoveToWindow()+ frameProvider?.bind(to: self)scheduleReport()}@@ -52,13 +121,9 @@ final class ScreenFrameReportingView: NSView {}private func reportFrame() {- guard let window else {- updateFrame(nil)- return- }-- let frameInWindow = convert(bounds, to: nil)- updateFrame(window.convertToScreen(frameInWindow))+ let screenFrame = ScreenFrameProvider.screenFrame(for: self)+ frameProvider?.updateLastValidFrame(screenFrame)+ updateFrame(screenFrame)}private func updateFrame(_ frame: CGRect?) {diff --git a/Docs/DevelopmentGuide.md b/Docs/DevelopmentGuide.mdindex 3e9d9e9..e4718d4 100644--- a/Docs/DevelopmentGuide.md+++ b/Docs/DevelopmentGuide.md@@ -545,7 +545,7 @@ flowchart TD额度区:- 多个 limit 间用 `LiquidGlassDivider` 分隔-- 如果 `resetCreditsAvailableCount > 0`, 只在置顶主 limit 标题右侧显示 `重置次数: N`; 该控件是 plain button, 点击后通过重置次数侧边详情面板展示过期时间。`resetCreditExpirationDates` 非空时按 `yyyy-MM-dd HH:mm:ss` 升序逐行展示, 相同展示时间合并数量, 单个显示 `可用: 1`; 没有过期时间时显示「未知过期时间」+- 如果 `resetCreditsAvailableCount > 0`, 只在置顶主 limit 标题右侧显示 `重置次数: N`; 该控件是 plain button, 点击后通过重置次数侧边详情面板展示过期时间。`resetCreditExpirationDates` 非空时按 `yyyy-MM-dd HH:mm:ss` 升序逐行展示, 相同展示时间合并数量, 单个显示 `可用: 1`, 多行之间显示渐变分隔线; 没有过期时间时显示「未知过期时间」- 每个 quota window 展示标签, 50 个固定胶囊组成的电量条, 剩余百分比和重置时间- 胶囊宽度为 `3.5`, 间距为 `2`, 高度为 `12`- 额度行标签列宽 `34`, 居中显示, 标签允许最小缩放到 `0.75`, 标签到电量条间距 `12`, 电量条到百分比间距 `8`, 百分比列宽 `37`, 百分比到重置时间最小间距 `6`, 重置时间列宽 `75`@@ -553,15 +553,15 @@ flowchart TD- 无数据时百分比和重置时间显示 `--`, 电量条用占位色- stale 数据通过 `.markStale(true)` 降低透明度到 0.55-`QuotaLimitsSection` 通过 `ScreenFrameReader` 记录额度区在屏幕坐标系中的 frame, 作为重置次数详情面板顶部对齐锚点。`ScreenFrameReader` 是通用 `NSViewRepresentable`, 同时服务额度区和热力图, 会在 SwiftUI 布局稳定后上报 frame 并去重, 避免重渲染期间反复触发无效定位。+`QuotaLimitsSection` 通过 `ScreenFrameProvider` 持有额度区桥接 `NSView`, 点击「重置次数」时同步读取额度区在屏幕坐标系中的最新 frame, 作为重置次数详情面板顶部对齐锚点。`ScreenFrameReader` 是通用 `NSViewRepresentable`, 同时服务额度区和热力图, 会在 SwiftUI 布局稳定后上报 frame 并去重; `ScreenFrameProvider` 会记录最后有效 frame 兜底, 避免只依赖异步 SwiftUI 状态更新导致点击瞬间没有锚点。重置次数详情面板由 `ResetCreditsPanelController` 管理:- 面板是菜单面板的 borderless nonactivating child panel, 可接收鼠标事件但不能成为 key/main window- 默认显示在菜单面板右侧, 空间不足时尝试左侧, 最终夹紧到当前屏幕可见区域内并保留 `8` px 边距-- 与菜单面板之间保留 `4` px gap; 纵向优先对齐额度区顶部, 没有锚点时居中对齐菜单面板-- 内容宽 `147`, 高度按过期时间行数在 `62...260` 之间夹紧, 圆角为 `12`-- 过期时间行高 `42`, 行间距 `7`, 横向 padding `12`, 纵向 padding `10`+- 与菜单面板之间保留 `4` px gap; 纵向优先对齐额度区顶部, 锚点坐标异常或与当前菜单内容区域不相交时丢弃, 没有可用锚点时居中对齐菜单面板+- 内容宽 `147`, 最小高度 `62`, 高度按过期时间行数和额度区顶部到菜单面板底部的可用空间夹紧; 没有可用锚点时最大高度回退为 `260`, 圆角为 `12`+- 过期时间行高 `42`, 行间距 `7`; 有多行时行间使用 `1` px 渐变分隔线并通过上下 padding 保持 `7` px 总间距; 横向 padding `12`, 纵向 padding `10`- 展开使用 `0.18` 秒抽屉动画, 收起使用 `0.12` 秒抽屉动画- 与热力图详情面板互斥: 点击重置次数会隐藏热力图详情面板, hover 热力图会隐藏重置次数详情面板