Close the menu-bar panel with Escape, and toggle it with the shortcut
Fable's re-review finding 3: nothing closed the panel from the keyboard; the shortcut only opened it. The panel now closes on Escape (KeyPanel's cancelOperation, which reaches it with the Ask field focused), and the shortcut toggles it. StatusPanelTests sends a real Escape key event to the panel with a focused field, and presses the shortcut three times.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+41
−5
native/apple/Sources/BaseKit/Mac/StatusPanel.swift+13 −5native/apple/Tests/BaseKitTests/StatusPanelTests.swift+28 −0
diff --git a/native/apple/Sources/BaseKit/Mac/StatusPanel.swift b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift| index e8f37ec..f42feb9 100644 |
| --- a/native/apple/Sources/BaseKit/Mac/StatusPanel.swift |
| +++ b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift |
| @@ -8,9 +8,10 @@ import SwiftUI |
| /// |
| /// An NSStatusItem (Base's mark) toggles a borderless, non-activating |
| /// NSPanel under it holding the menu-bar panel's SwiftUI content. The |
| -/// shortcut (`shortcutPressed`) shows the same panel: under the item, or |
| +/// shortcut (`shortcutPressed`) toggles the same panel: under the item, or |
| /// with the item switched off in Settings, at the top right of the screen. |
| -/// The panel closes when it loses key, as a menu-bar panel does. |
| +/// The panel closes on Escape and when it loses key, as a menu-bar panel |
| +/// does. |
| @MainActor public final class StatusPanelController: NSObject, NSWindowDelegate { |
| public static let size = CGSize(width: 410, height: 560) |
| @@ -29,6 +30,7 @@ import SwiftUI |
| panel = KeyPanel(contentRect: NSRect(origin: .zero, size: Self.size), |
| styleMask: [.borderless, .nonactivatingPanel, .fullSizeContentView], backing: .buffered, defer: true) |
| super.init() |
| + (panel as? KeyPanel)?.controller = self |
| panel.isFloatingPanel = true |
| panel.level = .popUpMenu |
| panel.hidesOnDeactivate = false |
| @@ -46,8 +48,9 @@ import SwiftUI |
| updateItem() |
| } |
| - /// The global shortcut: the panel, wherever Base's item is. |
| - public func shortcutPressed() { show() } |
| + /// The global shortcut: the panel, wherever Base's item is; pressed |
| + /// again, it closes. |
| + public func shortcutPressed() { toggle() } |
| public func toggle() { panel.isVisible ? hide() : show() } |
| @@ -100,8 +103,13 @@ import SwiftUI |
| } |
| } |
| -/// A borderless panel that can still take the keyboard (the Ask field). |
| +/// A borderless panel that can still take the keyboard (the Ask field), |
| +/// and closes on Escape, as a menu-bar panel does. |
| final class KeyPanel: NSPanel { |
| + weak var controller: StatusPanelController? |
| override var canBecomeKey: Bool { true } |
| + override func cancelOperation(_ sender: Any?) { |
| + if let controller { controller.hide() } else { orderOut(sender) } |
| + } |
| } |
| #endif |
diff --git a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift| index e891886..f72d239 100644 |
| --- a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift |
| @@ -32,6 +32,25 @@ import XCTest |
| XCTAssertTrue(controller.panel.isVisible) |
| } |
| + /// Fable's re-review finding 3: the shortcut toggles; Escape closes it, |
| + /// with the Ask field focused. |
| + func testTheShortcutTogglesAndEscapeCloses() throws { |
| + let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { EscapeProbe() } |
| + defer { controller.hide() } |
| + controller.shortcutPressed() |
| + XCTAssertTrue(controller.panel.isVisible) |
| + controller.shortcutPressed() |
| + XCTAssertFalse(controller.panel.isVisible, "the shortcut again closes it") |
| + controller.shortcutPressed() |
| + XCTAssertTrue(controller.panel.isVisible) |
| + RunLoop.main.run(until: Date().addingTimeInterval(0.3)) |
| + let escape = try XCTUnwrap(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, |
| + windowNumber: controller.panel.windowNumber, context: nil, |
| + characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53)) |
| + controller.panel.sendEvent(escape) |
| + XCTAssertFalse(controller.panel.isVisible, "Escape closes it") |
| + } |
| + |
| func testTheShortcutOpensThePanelWithTheItemOff() { |
| let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } |
| defer { controller.hide() } |
| @@ -44,6 +63,15 @@ import XCTest |
| #endif |
| #if os(macOS) |
| +/// A field with the keyboard, as the panel's composer has it on opening. |
| +private struct EscapeProbe: View { |
| + @State private var text = "" |
| + @FocusState private var focused: Bool |
| + var body: some View { |
| + TextField(text: $text) { Text(verbatim: "Ask") }.focused($focused).onAppear { focused = true } |
| + } |
| +} |
| + |
| /// Astra's Task 16 finding 6: however many jobs wait, the panel's top |
| /// takes at most its bound, and the composer still has room under it. |
| @MainActor final class MenuPanelTests: XCTestCase { |