oficina / base Public

Show the shortcut as it is each time the panel opens

Fable's nit 7: the panel's foot read the shortcut once, in a view that
now lives as long as the app, so recording or clearing a shortcut left
the old words there. The status panel runs onShow before each opening;
the Mac app re-reads the shortcut there into the observed state the
foot draws from.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 37059d168c69db0e6b68ae600f168ccf11f5dcd2 parent aca798a
3 files changed, +31 −3
  • native/apple/Sources/BaseKit/Mac/StatusPanel.swift +5 −0
  • native/apple/Sources/BaseMac/BaseMacApp.swift +13 −3
  • native/apple/Tests/BaseKitTests/StatusPanelTests.swift +13 −0
diff --git a/native/apple/Sources/BaseKit/Mac/StatusPanel.swift b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift
index f42feb9..4820332 100644
--- a/native/apple/Sources/BaseKit/Mac/StatusPanel.swift
+++ b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift
@@ -19,6 +19,10 @@ import SwiftUI
public private(set) var statusItem: NSStatusItem?
let accessibilityLabel: String
+ /// Runs each time the panel opens, before it shows: the app re-reads
+ /// what can change while it's closed (the shortcut's hint).
+ public var onShow: (() -> Void)?
+
/// Whether Base's item is in the menu bar (Settings, on by default).
public var itemShown: Bool {
didSet { updateItem() }
@@ -55,6 +59,7 @@ import SwiftUI
public func toggle() { panel.isVisible ? hide() : show() }
public func show() {
+ onShow?()
position()
panel.makeKeyAndOrderFront(nil)
NSApp.activate()
diff --git a/native/apple/Sources/BaseMac/BaseMacApp.swift b/native/apple/Sources/BaseMac/BaseMacApp.swift
index 9fb4be1..f0991a8 100644
--- a/native/apple/Sources/BaseMac/BaseMacApp.swift
+++ b/native/apple/Sources/BaseMac/BaseMacApp.swift
@@ -85,6 +85,8 @@ struct ColophonCommand: View {
let session = BaseSession.live()
lazy var actions = MacActions.make(session: session)
private(set) var panel: StatusPanelController?
+ /// The panel's foot shows the shortcut as it is when the panel opens.
+ let panelState = PanelState()
/// The window's scene's openWindow, kept from the window's first
/// appearance, so Open Base can bring it back after it was closed.
var openMain: (() -> Void)?
@@ -92,14 +94,15 @@ struct ColophonCommand: View {
func applicationDidFinishLaunching(_ notification: Notification) {
UserDefaults.standard.register(defaults: ["showMenuBar": true])
- let session = self.session, actions = self.actions
+ let session = self.session, actions = self.actions, state = panelState
panel = StatusPanelController(itemShown: UserDefaults.standard.bool(forKey: "showMenuBar"),
accessibilityLabel: BaseText.text("native.sidebar.ask")) {
- MenuPanelRoot(session: session, actions: actions) { [weak self] in
+ MenuPanelRoot(session: session, actions: actions, state: state) { [weak self] in
self?.panel?.hide()
self?.showMain()
}
}
+ panel?.onShow = { state.shortcut = KeyboardShortcuts.getShortcut(for: .askBase)?.description }
KeyboardShortcuts.onKeyUp(for: .askBase) { [weak self] in self?.panel?.shortcutPressed() }
Task { await session.oficina.start() }
}
@@ -156,15 +159,22 @@ struct MacRoot: View {
struct MenuPanelRoot: View {
let session: BaseSession
let actions: BaseActions
+ let state: PanelState
let openBase: () -> Void
var body: some View {
MenuPanel(me: session.me, ask: session.signedIn == nil ? nil : session.ask,
- shortcut: KeyboardShortcuts.getShortcut(for: .askBase)?.description,
+ shortcut: state.shortcut,
openBase: openBase, actions: actions)
.baseAppearance(session.me?.appearance ?? .system)
}
}
+/// What the panel shows that lives outside the session: the shortcut,
+/// read again each time the panel opens.
+@MainActor @Observable final class PanelState {
+ var shortcut: String? = KeyboardShortcuts.getShortcut(for: .askBase)?.description
+}
+
/// Keeps the window scene's openWindow for the app delegate.
struct KeepOpenWindow: ViewModifier {
let delegate: BaseMacDelegate
diff --git a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
index f72d239..663b3a6 100644
--- a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
+++ b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
@@ -51,6 +51,19 @@ import XCTest
XCTAssertFalse(controller.panel.isVisible, "Escape closes it")
}
+ /// Fable's nit 7: each opening re-reads what changed while it was
+ /// closed (the app hands the shortcut's hint over here).
+ func testEachOpeningRunsOnShow() {
+ let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") }
+ defer { controller.hide() }
+ var shown = 0
+ controller.onShow = { shown += 1 }
+ controller.shortcutPressed()
+ controller.shortcutPressed()
+ controller.shortcutPressed()
+ XCTAssertEqual(shown, 2, "twice opened, once closed")
+ }
+
func testTheShortcutOpensThePanelWithTheItemOff() {
let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") }
defer { controller.hide() }