-
Notifications
You must be signed in to change notification settings - Fork 7
refactor: use NSStatusItem popover shell #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import AppKit | ||
| import Combine | ||
| import SwiftUI | ||
|
|
||
| @MainActor | ||
| final class BrainBarStatusPopoverController: NSObject { | ||
| static let contentSize = NSSize(width: 900, height: 640) | ||
|
|
||
| let statusItemForTesting: NSStatusItem | ||
| let popoverForTesting: NSPopover | ||
|
|
||
| private let runtime: BrainBarRuntime | ||
| private var runtimeCancellables: Set<AnyCancellable> = [] | ||
| private var collectorCancellables: Set<AnyCancellable> = [] | ||
|
|
||
| init(runtime: BrainBarRuntime) { | ||
| self.runtime = runtime | ||
| statusItemForTesting = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) | ||
| popoverForTesting = NSPopover() | ||
| super.init() | ||
|
|
||
| configureStatusItem() | ||
| prewarmPopover() | ||
| bindRuntime() | ||
| } | ||
|
|
||
| func toggle(_ sender: Any?) { | ||
| if popoverForTesting.isShown { | ||
| popoverForTesting.performClose(sender) | ||
| } else { | ||
| show(sender) | ||
| } | ||
| } | ||
|
|
||
| func show(_ sender: Any?) { | ||
| guard let button = statusItemForTesting.button else { return } | ||
| popoverForTesting.show(relativeTo: button.bounds, of: button, preferredEdge: .minY) | ||
| popoverForTesting.contentViewController?.view.window?.makeKey() | ||
| } | ||
|
|
||
| func close(_ sender: Any?) { | ||
| popoverForTesting.performClose(sender) | ||
| } | ||
|
|
||
| func stop() { | ||
| close(nil) | ||
| NSStatusBar.system.removeStatusItem(statusItemForTesting) | ||
| } | ||
|
|
||
| private func configureStatusItem() { | ||
| guard let button = statusItemForTesting.button else { return } | ||
| button.image = NSImage(systemSymbolName: "brain", accessibilityDescription: "BrainBar") | ||
| button.target = self | ||
| button.action = #selector(toggleFromStatusItem(_:)) | ||
| button.toolTip = "BrainBar" | ||
| } | ||
|
|
||
| private func prewarmPopover() { | ||
| popoverForTesting.behavior = .transient | ||
| popoverForTesting.contentSize = Self.contentSize | ||
|
|
||
| let hosting = NSHostingController( | ||
| rootView: BrainBarWindowRootView(runtime: runtime, managesWindowFrame: false) | ||
| .frame(width: Self.contentSize.width, height: Self.contentSize.height) | ||
| ) | ||
| _ = hosting.view | ||
| popoverForTesting.contentViewController = hosting | ||
| } | ||
|
|
||
| private func bindRuntime() { | ||
| runtime.$collector | ||
| .receive(on: RunLoop.main) | ||
| .sink { [weak self] collector in | ||
| self?.bindCollector(collector) | ||
| } | ||
| .store(in: &runtimeCancellables) | ||
| } | ||
|
|
||
| private func bindCollector(_ collector: StatsCollector?) { | ||
| collectorCancellables.removeAll() | ||
| guard let collector else { return } | ||
|
|
||
| Publishers.CombineLatest(collector.$stats, collector.$state) | ||
| .receive(on: RunLoop.main) | ||
| .sink { [weak self] stats, state in | ||
| self?.renderStatusIcon(stats: stats, state: state) | ||
| } | ||
| .store(in: &collectorCancellables) | ||
| } | ||
|
|
||
| private func renderStatusIcon(stats: BrainDatabase.DashboardStats, state: PipelineState) { | ||
| let livePresentation = BrainBarLivePresentation.derive(stats: stats) | ||
| statusItemForTesting.button?.image = SparklineRenderer.render( | ||
| state: state, | ||
| values: stats.recentEnrichmentBuckets, | ||
| size: NSSize(width: 22, height: 12), | ||
| accentColor: livePresentation.accentColor | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Status item brain icon replaced entirely by sparklineMedium Severity The Additional Locations (1)Reviewed by Cursor Bugbot for commit 740e90c. Configure here. |
||
| } | ||
|
|
||
| @objc private func toggleFromStatusItem(_ sender: Any?) { | ||
| toggle(sender) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import AppKit | ||
| import XCTest | ||
| @testable import BrainBar | ||
|
|
||
| @MainActor | ||
| final class BrainBarStatusPopoverControllerTests: XCTestCase { | ||
| func testControllerPrewarmsVariableLengthStatusItemPopover() { | ||
| let runtime = BrainBarRuntime(launchMode: .menuBarWindow) | ||
| let controller = BrainBarStatusPopoverController(runtime: runtime) | ||
| defer { controller.stop() } | ||
|
|
||
| XCTAssertEqual(controller.statusItemForTesting.length, NSStatusItem.variableLength) | ||
| XCTAssertEqual(controller.popoverForTesting.behavior, NSPopover.Behavior.transient) | ||
| XCTAssertNotNil(controller.popoverForTesting.contentViewController) | ||
| XCTAssertTrue(controller.popoverForTesting.contentViewController?.isViewLoaded == true) | ||
| } | ||
|
|
||
| func testAppSupportCollectorFactoryWiresBrainBusEvents() { | ||
| let tempDBPath = NSTemporaryDirectory() + "brainbar-status-popover-\(UUID().uuidString).db" | ||
| let eventSource = RecordingBrainBusEventSource() | ||
| let collector = BrainBarAppSupport.makeStatsCollector( | ||
| dbPath: tempDBPath, | ||
| targetPID: ProcessInfo.processInfo.processIdentifier, | ||
| brainBusEvents: eventSource | ||
| ) | ||
| defer { | ||
| collector.stop() | ||
| try? FileManager.default.removeItem(atPath: tempDBPath) | ||
| try? FileManager.default.removeItem(atPath: tempDBPath + "-wal") | ||
| try? FileManager.default.removeItem(atPath: tempDBPath + "-shm") | ||
| } | ||
|
|
||
| collector.start() | ||
|
|
||
| XCTAssertEqual(eventSource.streamRequestCount, 1) | ||
| } | ||
| } | ||
|
|
||
| private final class RecordingBrainBusEventSource: BrainBusEventSource, @unchecked Sendable { | ||
| private let lock = NSLock() | ||
| private var requests = 0 | ||
|
|
||
| var streamRequestCount: Int { | ||
| lock.withLock { requests } | ||
| } | ||
|
|
||
| func events() -> AsyncStream<BrainBusEvent> { | ||
| lock.withLock { | ||
| requests += 1 | ||
| } | ||
| return AsyncStream { _ in } | ||
| } | ||
| } |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Cmd-K/search is invoked while the prewarmed popover is closed and the DB is ready,
runtime.presentQuickAction(.search)is published before thisshowcall. Because the hosting view was already loaded,BrainBarWindowRootViewcan consume and clear the request while its text field still has no window; the async focus attempt then seesnsView.window == nil, and the popover opens without the command bar focused. The capture path has the same ordering, so route the request after the popover is visible or defer focus until the popover window exists.Useful? React with 👍 / 👎.