Skip to content

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

fix: 修复弹窗收回并发布 2.4.2

Bob8f9cb5237329
diff --git a/CodexBar.xcodeproj/project.pbxproj b/CodexBar.xcodeproj/project.pbxproj
index 4e74257..f8b0516 100644
--- a/CodexBar.xcodeproj/project.pbxproj
+++ b/CodexBar.xcodeproj/project.pbxproj
@@ -275,7 +275,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
- CURRENT_PROJECT_VERSION = 17;
+ CURRENT_PROJECT_VERSION = 18;
DEVELOPMENT_TEAM = 4KJ362VJW5;
ENABLE_APP_INTENTS_METADATA_GENERATION = NO;
ENABLE_APP_SANDBOX = NO;
@@ -290,7 +290,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 2.4.1;
+ MARKETING_VERSION = 2.4.2;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
@@ -312,7 +312,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
- CURRENT_PROJECT_VERSION = 17;
+ CURRENT_PROJECT_VERSION = 18;
DEVELOPMENT_TEAM = 4KJ362VJW5;
ENABLE_APP_INTENTS_METADATA_GENERATION = NO;
ENABLE_APP_SANDBOX = NO;
@@ -327,7 +327,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
- MARKETING_VERSION = 2.4.1;
+ MARKETING_VERSION = 2.4.2;
PRODUCT_BUNDLE_IDENTIFIER = app.zabrian.codexbar;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
diff --git a/CodexBar/Controllers/PopoverDismissMonitor.swift b/CodexBar/Controllers/PopoverDismissMonitor.swift
index a0a816b..3e473c0 100644
--- a/CodexBar/Controllers/PopoverDismissMonitor.swift
+++ b/CodexBar/Controllers/PopoverDismissMonitor.swift
@@ -7,7 +7,10 @@ final class PopoverDismissMonitor {
private var localEventMonitor: Any?
private var globalMouseEventMonitor: Any?
private var appResignActiveObserver: NSObjectProtocol?
+ private var workspaceActivateObserver: NSObjectProtocol?
private var popoverWindowResignKeyObserver: NSObjectProtocol?
+ private var deferredWindowFocusTask: Task<Void, Never>?
+ private var onDismiss: (() -> Void)?
init(
popover: NSPopover,
@@ -19,51 +22,120 @@ final class PopoverDismissMonitor {
func install(onDismiss: @escaping () -> Void) {
remove()
+ self.onDismiss = onDismiss
localEventMonitor = NSEvent.addLocalMonitorForEvents(matching: Metrics.dismissEventMask) { [weak self] event in
guard let self else {
return event
}
- if self.dismissesKeyEvent(event, onDismiss: onDismiss) {
+ if self.handleKeyEvent(event) {
return nil
}
- self.dismissIfNeeded(for: event, onDismiss: onDismiss)
+ self.dismissIfNeeded(for: event)
self.restabilizePopoverChromeAfterEvent()
return event
}
- globalMouseEventMonitor = NSEvent.addGlobalMonitorForEvents(matching: Metrics.mouseDismissEventMask) { _ in
- Task { @MainActor in
- onDismiss()
- }
+ globalMouseEventMonitor = NSEvent.addGlobalMonitorForEvents(matching: Metrics.mouseDismissEventMask) { [weak self] _ in
+ self?.scheduleDismiss()
}
appResignActiveObserver = NotificationCenter.default.addObserver(
- forName: NSApplication.didResignActiveNotification,
+ forName: NSApplication.willResignActiveNotification,
object: NSApplication.shared,
queue: .main
- ) { _ in
- onDismiss()
+ ) { [weak self] _ in
+ self?.scheduleDismiss()
}
- if let popoverWindow = popover.contentViewController?.view.window {
- popoverWindowResignKeyObserver = NotificationCenter.default.addObserver(
- forName: NSWindow.didResignKeyNotification,
- object: popoverWindow,
- queue: .main
- ) { _ in
- onDismiss()
+ workspaceActivateObserver = NSWorkspace.shared.notificationCenter.addObserver(
+ forName: NSWorkspace.didActivateApplicationNotification,
+ object: nil,
+ queue: .main
+ ) { [weak self] notification in
+ let activatedProcessIdentifier = (
+ notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication
+ )?.processIdentifier
+
+ Task { @MainActor [weak self, activatedProcessIdentifier] in
+ self?.dismissIfDifferentApplication(processIdentifier: activatedProcessIdentifier)
+ }
+ }
+
+ installPopoverWindowObserverAndFocus()
+ deferredWindowFocusTask = Task { @MainActor [weak self] in
+ await Task.yield()
+ guard let self, !Task.isCancelled, self.popover.isShown else {
+ return
}
+
+ self.installPopoverWindowObserverAndFocus()
}
}
func remove() {
+ onDismiss = nil
+ deferredWindowFocusTask?.cancel()
+ deferredWindowFocusTask = nil
removeEventMonitor(&localEventMonitor)
removeEventMonitor(&globalMouseEventMonitor)
removeObserver(&appResignActiveObserver)
+ removeObserver(&workspaceActivateObserver, center: NSWorkspace.shared.notificationCenter)
+ removeObserver(&popoverWindowResignKeyObserver)
+ }
+
+ private func installPopoverWindowObserverAndFocus() {
+ installPopoverWindowObserver()
+ focusPopoverWindow()
+ }
+
+ private func installPopoverWindowObserver() {
removeObserver(&popoverWindowResignKeyObserver)
+ guard let popoverWindow = popover.contentViewController?.view.window else {
+ return
+ }
+
+ popoverWindowResignKeyObserver = NotificationCenter.default.addObserver(
+ forName: NSWindow.didResignKeyNotification,
+ object: popoverWindow,
+ queue: .main
+ ) { [weak self] _ in
+ self?.scheduleDismiss()
+ }
+ }
+
+ private nonisolated func scheduleDismiss() {
+ Task { @MainActor [weak self] in
+ self?.dismiss()
+ }
+ }
+
+ private func dismiss() {
+ guard popover.isShown else {
+ return
+ }
+
+ onDismiss?()
+ }
+
+ private func dismissIfDifferentApplication(processIdentifier: pid_t?) {
+ if let processIdentifier, processIdentifier == NSRunningApplication.current.processIdentifier {
+ return
+ }
+
+ dismiss()
+ }
+
+ private func focusPopoverWindow() {
+ guard popover.isShown,
+ let popoverWindow = popover.contentViewController?.view.window else {
+ return
+ }
+
+ NSApplication.shared.activate()
+ popoverWindow.makeKey()
}
private func removeEventMonitor(_ monitor: inout Any?) {
@@ -73,24 +145,44 @@ final class PopoverDismissMonitor {
}
}
- private func removeObserver(_ observer: inout NSObjectProtocol?) {
+ private func removeObserver(
+ _ observer: inout NSObjectProtocol?,
+ center: NotificationCenter = .default
+ ) {
if let currentObserver = observer {
- NotificationCenter.default.removeObserver(currentObserver)
+ center.removeObserver(currentObserver)
observer = nil
}
}
- private func dismissesKeyEvent(_ event: NSEvent, onDismiss: () -> Void) -> Bool {
- guard popover.isShown, event.type == .keyDown, event.keyCode == Metrics.escapeKeyCode else {
+ private func handleKeyEvent(_ event: NSEvent) -> Bool {
+ guard popover.isShown, event.type == .keyDown else {
return false
}
- onDismiss()
- return true
+ if event.keyCode == Metrics.escapeKeyCode {
+ dismiss()
+ return true
+ }
+
+ if isSystemDismissShortcut(event) {
+ dismiss()
+ }
+
+ return false
}
- private func dismissIfNeeded(for event: NSEvent, onDismiss: () -> Void) {
- guard popover.isShown else {
+ private func isSystemDismissShortcut(_ event: NSEvent) -> Bool {
+ let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
+ guard modifierFlags.contains(.command) else {
+ return false
+ }
+
+ return event.keyCode == Metrics.tabKeyCode || event.keyCode == Metrics.spaceKeyCode
+ }
+
+ private func dismissIfNeeded(for event: NSEvent) {
+ guard popover.isShown, isMouseDismissEvent(event) else {
return
}
@@ -98,7 +190,16 @@ final class PopoverDismissMonitor {
return
}
- onDismiss()
+ dismiss()
+ }
+
+ private func isMouseDismissEvent(_ event: NSEvent) -> Bool {
+ switch event.type {
+ case .leftMouseDown, .rightMouseDown, .otherMouseDown:
+ return true
+ default:
+ return false
+ }
}
private func restabilizePopoverChromeAfterEvent() {
@@ -162,5 +263,7 @@ final class PopoverDismissMonitor {
]
static let dismissEventMask: NSEvent.EventTypeMask = mouseDismissEventMask.union(.keyDown)
static let escapeKeyCode: UInt16 = 53
+ static let tabKeyCode: UInt16 = 48
+ static let spaceKeyCode: UInt16 = 49
}
}
diff --git a/Docs/CodexHook.md b/Docs/CodexHook.md
index a617d28..3e21a1a 100644
--- a/Docs/CodexHook.md
+++ b/Docs/CodexHook.md
@@ -139,7 +139,7 @@ CodexBar 最多保留最近 210 天数据。
```text
date, eventCount, sessionStartCount, stopCount, preToolUseCount, postToolUseCount,
permissionRequestCount, preCompactCount, postCompactCount, subagentStartCount,
-subagentStopCount, sessionIds, turnIds, sessionCount, turnCount, projectCounts
+subagentStopCount, sessionCount, turnCount, projectCounts, sessionIds, turnIds
```
其中 `sessionIds` / `turnIds` / `sessionCount` / `turnCount` 没有值时写为 `null`,`projectCounts` 内部项目名按稳定顺序写出。
diff --git a/appcast.xml b/appcast.xml
index d4da1a2..c6688e7 100644
--- a/appcast.xml
+++ b/appcast.xml
@@ -7,6 +7,15 @@
<link>https://codexbar.zabrian.app/appcast.xml</link>
<description>CodexBar appcast feed</description>
<language>zh-CN</language>
+ <item>
+ <title>CodexBar.app 2.4.2</title>
+ <sparkle:version>18</sparkle:version>
+ <sparkle:shortVersionString>2.4.2</sparkle:shortVersionString>
+ <sparkle:minimumSystemVersion>15.0</sparkle:minimumSystemVersion>
+ <sparkle:releaseNotesLink>https://codexbar.zabrian.app/notes#2.4.2</sparkle:releaseNotesLink>
+ <pubDate>Mon, 22 Jun 2026 10:19:47 +0800</pubDate>
+ <enclosure url="https://codexbar.zabrian.app/download/CodexBar-v2.4.2.dmg" sparkle:edSignature="KyzjZ4lj6KWA/idFXs/2IZp5MTdfVfyCVxSEwbJ92KRLeNQzY0+ZMjfvR919KezFzp+qRo/Svt0gC+aY+/brCw==" length="2894098" type="application/octet-stream"/>
+ </item>
<item>
<title>CodexBar.app 2.4.1</title>
<sparkle:version>17</sparkle:version>