oficina / base Public

Present the menu-bar panel ourselves, so the shortcut opens it

Astra's Task 16 finding 5: the shortcut looked for MenuBarExtra's
private status button among its window's direct subviews; on macOS 27
it isn't there, so the panel never opened and the main window came up
instead. Base now owns the presentation: StatusPanelController puts
Base's mark in the menu bar as an NSStatusItem and toggles a borderless,
non-activating NSPanel under it holding the same SwiftUI panel. The
shortcut calls its shortcutPressed(), which shows the panel under the
item, or at the top right of the screen when Settings has the item off;
the panel closes when it loses key. Open Base brings the window back
through the scene's openWindow, even after it was closed.

StatusPanelTests creates the real status item in the menu bar from the
test process (as an accessory app), calls the shortcut's handler and
asserts the panel is visible and under the item; then the item's click
closes and reopens it; and with the item off the shortcut still shows it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 9e6c88234818890eb7f928f5bdde975267351b89 parent 2743702
4 files changed, +194 −32
  • native/apple/Sources/BaseKit/Mac/StatusPanel.swift +107 −0
  • native/apple/Sources/BaseMac/BaseMacApp.swift +43 −14
  • native/apple/Sources/BaseMac/MacActions.swift +0 −18
  • native/apple/Tests/BaseKitTests/StatusPanelTests.swift +44 −0
diff --git a/native/apple/Sources/BaseKit/Mac/StatusPanel.swift b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift
new file mode 100644
index 0000000..e8f37ec
--- /dev/null
+++ b/native/apple/Sources/BaseKit/Mac/StatusPanel.swift
@@ -0,0 +1,107 @@
+#if os(macOS)
+import AppKit
+import SwiftUI
+
+/// Ask in the menu bar, presented by Base itself rather than SwiftUI's
+/// MenuBarExtra, whose window and button are private: the global shortcut
+/// has to be able to open the panel, and MenuBarExtra gives no way to.
+///
+/// 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
+/// 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.
+@MainActor public final class StatusPanelController: NSObject, NSWindowDelegate {
+ public static let size = CGSize(width: 410, height: 560)
+
+ public let panel: NSPanel
+ public private(set) var statusItem: NSStatusItem?
+ let accessibilityLabel: String
+
+ /// Whether Base's item is in the menu bar (Settings, on by default).
+ public var itemShown: Bool {
+ didSet { updateItem() }
+ }
+
+ public init<Content: View>(itemShown: Bool, accessibilityLabel: String, @ViewBuilder content: () -> Content) {
+ self.itemShown = itemShown
+ self.accessibilityLabel = accessibilityLabel
+ panel = KeyPanel(contentRect: NSRect(origin: .zero, size: Self.size),
+ styleMask: [.borderless, .nonactivatingPanel, .fullSizeContentView], backing: .buffered, defer: true)
+ super.init()
+ panel.isFloatingPanel = true
+ panel.level = .popUpMenu
+ panel.hidesOnDeactivate = false
+ panel.isOpaque = false
+ panel.backgroundColor = .clear
+ panel.hasShadow = true
+ panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .transient]
+ panel.isReleasedWhenClosed = false
+ panel.delegate = self
+ let host = NSHostingView(rootView: content()
+ .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous))
+ .frame(width: Self.size.width, height: Self.size.height))
+ host.frame = NSRect(origin: .zero, size: Self.size)
+ panel.contentView = host
+ updateItem()
+ }
+
+ /// The global shortcut: the panel, wherever Base's item is.
+ public func shortcutPressed() { show() }
+
+ public func toggle() { panel.isVisible ? hide() : show() }
+
+ public func show() {
+ position()
+ panel.makeKeyAndOrderFront(nil)
+ NSApp.activate()
+ statusItem?.button?.highlight(true)
+ }
+
+ public func hide() {
+ panel.orderOut(nil)
+ statusItem?.button?.highlight(false)
+ }
+
+ public func windowDidResignKey(_ notification: Notification) { hide() }
+
+ @objc func itemClicked() { toggle() }
+
+ private func updateItem() {
+ if itemShown, statusItem == nil {
+ let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
+ if let button = item.button {
+ let image = NSImage(systemSymbolName: "square.fill", accessibilityDescription: accessibilityLabel)
+ image?.isTemplate = true
+ button.image = image
+ button.target = self
+ button.action = #selector(itemClicked)
+ button.setAccessibilityLabel(accessibilityLabel)
+ }
+ statusItem = item
+ } else if !itemShown, let item = statusItem {
+ NSStatusBar.system.removeStatusItem(item)
+ statusItem = nil
+ }
+ }
+
+ /// Under the item, centred on it and kept on its screen; with no item,
+ /// the top right of the main screen's visible area.
+ private func position() {
+ let size = Self.size
+ if let button = statusItem?.button, let window = button.window, let screen = window.screen ?? NSScreen.main {
+ let item = window.convertToScreen(button.convert(button.bounds, to: nil))
+ let visible = screen.visibleFrame
+ let x = min(max(item.midX - size.width / 2, visible.minX + 8), visible.maxX - size.width - 8)
+ panel.setFrameOrigin(NSPoint(x: x, y: item.minY - size.height - 6))
+ } else if let visible = NSScreen.main?.visibleFrame {
+ panel.setFrameOrigin(NSPoint(x: visible.maxX - size.width - 12, y: visible.maxY - size.height - 6))
+ }
+ }
+}
+
+/// A borderless panel that can still take the keyboard (the Ask field).
+final class KeyPanel: NSPanel {
+ override var canBecomeKey: Bool { true }
+}
+#endif
diff --git a/native/apple/Sources/BaseMac/BaseMacApp.swift b/native/apple/Sources/BaseMac/BaseMacApp.swift
index 9cc98c9..9fb4be1 100644
--- a/native/apple/Sources/BaseMac/BaseMacApp.swift
+++ b/native/apple/Sources/BaseMac/BaseMacApp.swift
@@ -14,8 +14,9 @@ extension KeyboardShortcuts.Name {
}
/// Base for the Mac. One window (B): the sky, a floating sidebar and Ask.
-/// Ask is also a menu-bar panel (on by default) and, once the person
-/// records one, a global shortcut. Settings (⌘,) has General, Account and
+/// Ask is also a menu-bar panel (on by default), presented by Base's own
+/// StatusPanelController so the global shortcut, once the person records
+/// one, can open it. Settings (⌘,) has General, Account and
/// Ask Base; the Colophon is under Help.
@main struct BaseMacApp: App {
@NSApplicationDelegateAdaptor(BaseMacDelegate.self) private var delegate
@@ -38,6 +39,8 @@ extension KeyboardShortcuts.Name {
Window(BaseText.text("native.sidebar.ask"), id: "main") {
MacRoot(session: session, actions: delegate.actions)
.onOpenURL { delegate.openURL.handle($0) }
+ .onChange(of: showMenuBar, initial: true) { _, shown in delegate.panel?.itemShown = shown }
+ .modifier(KeepOpenWindow(delegate: delegate))
#if canImport(RastrilloMacUpdates)
.safeAreaInset(edge: .bottom) {
if updater.canInstallPendingUpdate { NativeUpdateNotice(updater: updater).padding(.bottom, 12) }
@@ -58,16 +61,10 @@ extension KeyboardShortcuts.Name {
CommandGroup(replacing: .help) { ColophonCommand() }
}
- MenuBarExtra(isInserted: $showMenuBar) {
- MenuPanelRoot(session: session, actions: delegate.actions)
- } label: {
- Image(systemName: "square.fill").accessibilityLabel(Text(BaseText.text("native.sidebar.ask")))
- }
- .menuBarExtraStyle(.window)
-
Settings {
MacSettingsRoot(session: session, actions: delegate.actions, showMenuBar: $showMenuBar,
openIn: Binding(get: { OpenPreference(rawValue: openIn) ?? .app }, set: { openIn = $0.rawValue }))
+ .onChange(of: showMenuBar) { _, shown in delegate.panel?.itemShown = shown }
}
Window(ColophonView.title, id: "colophon") { ColophonView() }
@@ -87,13 +84,36 @@ struct ColophonCommand: View {
@MainActor final class BaseMacDelegate: NSObject, NSApplicationDelegate {
let session = BaseSession.live()
lazy var actions = MacActions.make(session: session)
- lazy var openURL = BaseOpenURL(showAsk: MacActions.showMainWindow, openWeb: { [actions] in actions.openWeb($0) })
+ private(set) var panel: StatusPanelController?
+ /// 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)?
+ lazy var openURL = BaseOpenURL(showAsk: { [weak self] in self?.showMain() }, openWeb: { [actions] in actions.openWeb($0) })
func applicationDidFinishLaunching(_ notification: Notification) {
- KeyboardShortcuts.onKeyUp(for: .askBase) { MacActions.openPanel() }
+ UserDefaults.standard.register(defaults: ["showMenuBar": true])
+ let session = self.session, actions = self.actions
+ panel = StatusPanelController(itemShown: UserDefaults.standard.bool(forKey: "showMenuBar"),
+ accessibilityLabel: BaseText.text("native.sidebar.ask")) {
+ MenuPanelRoot(session: session, actions: actions) { [weak self] in
+ self?.panel?.hide()
+ self?.showMain()
+ }
+ }
+ KeyboardShortcuts.onKeyUp(for: .askBase) { [weak self] in self?.panel?.shortcutPressed() }
Task { await session.oficina.start() }
}
+ /// Open Base: the window, forward (opened again if it was closed).
+ func showMain() {
+ NSApp.activate()
+ if let window = NSApp.windows.first(where: { $0.identifier?.rawValue.hasPrefix("main") == true && $0.isVisible }) {
+ window.makeKeyAndOrderFront(nil)
+ } else {
+ openMain?()
+ }
+ }
+
func applicationDidBecomeActive(_ notification: Notification) {
Task { await session.foreground() }
}
@@ -101,7 +121,7 @@ struct ColophonCommand: View {
func applicationDidResignActive(_ notification: Notification) {}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
- if !flag { MacActions.showMainWindow() }
+ if !flag { showMain() }
return true
}
}
@@ -136,15 +156,24 @@ struct MacRoot: View {
struct MenuPanelRoot: View {
let session: BaseSession
let actions: BaseActions
- @Environment(\.openWindow) private var openWindow
+ let openBase: () -> Void
var body: some View {
MenuPanel(me: session.me, ask: session.signedIn == nil ? nil : session.ask,
shortcut: KeyboardShortcuts.getShortcut(for: .askBase)?.description,
- openBase: { openWindow(id: "main"); NSApp.activate() }, actions: actions)
+ openBase: openBase, actions: actions)
.baseAppearance(session.me?.appearance ?? .system)
}
}
+/// Keeps the window scene's openWindow for the app delegate.
+struct KeepOpenWindow: ViewModifier {
+ let delegate: BaseMacDelegate
+ @Environment(\.openWindow) private var openWindow
+ func body(content: Content) -> some View {
+ content.onAppear { delegate.openMain = { openWindow(id: "main") } }
+ }
+}
+
/// Settings, from the session and this Mac.
struct MacSettingsRoot: View {
let session: BaseSession
diff --git a/native/apple/Sources/BaseMac/MacActions.swift b/native/apple/Sources/BaseMac/MacActions.swift
index 45338b7..f54ffca 100644
--- a/native/apple/Sources/BaseMac/MacActions.swift
+++ b/native/apple/Sources/BaseMac/MacActions.swift
@@ -32,22 +32,4 @@ import SwiftUI
break
}
}
-
- /// Open Base: the window, forward.
- static func showMainWindow() {
- NSApp.activate()
- NSApp.windows.first { $0.identifier?.rawValue.hasPrefix("main") == true }?.makeKeyAndOrderFront(nil)
- }
-
- /// The shortcut: the menu-bar panel, as a click on Base's item would
- /// open it; with the item switched off, the window.
- static func openPanel() {
- let item = NSApp.windows.first { $0.className.contains("NSStatusBarWindow") }
- if let button = item?.contentView?.subviews.first(where: { $0 is NSStatusBarButton }) as? NSStatusBarButton ?? item?.contentView as? NSStatusBarButton {
- NSApp.activate()
- button.performClick(nil)
- } else {
- showMainWindow()
- }
- }
}
diff --git a/native/apple/Tests/BaseKitTests/StatusPanelTests.swift b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
new file mode 100644
index 0000000..4f07fd0
--- /dev/null
+++ b/native/apple/Tests/BaseKitTests/StatusPanelTests.swift
@@ -0,0 +1,44 @@
+#if os(macOS)
+@testable import BaseKit
+import AppKit
+import SwiftUI
+import XCTest
+
+/// Astra's Task 16 finding 5: the global shortcut really opens the panel.
+/// A real status item in the menu bar and a real panel, in this process as
+/// an accessory app; the shortcut's handler, then the item's click.
+@MainActor final class StatusPanelTests: XCTestCase {
+ override func setUp() {
+ _ = NSApplication.shared
+ NSApp.setActivationPolicy(.accessory)
+ }
+
+ func testTheShortcutOpensThePanelUnderBasesItem() throws {
+ let controller = StatusPanelController(itemShown: true, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") }
+ defer { controller.hide(); controller.itemShown = false }
+ let button = try XCTUnwrap(controller.statusItem?.button, "no status item in the menu bar")
+ XCTAssertFalse(controller.panel.isVisible)
+ controller.shortcutPressed()
+ XCTAssertTrue(controller.panel.isVisible, "the shortcut didn't open the panel")
+ // Under the item.
+ if let window = button.window {
+ let item = window.convertToScreen(button.convert(button.bounds, to: nil))
+ XCTAssertLessThanOrEqual(controller.panel.frame.maxY, item.minY)
+ }
+ // The item's click closes it, and opens it again.
+ controller.itemClicked()
+ XCTAssertFalse(controller.panel.isVisible)
+ controller.itemClicked()
+ XCTAssertTrue(controller.panel.isVisible)
+ }
+
+ func testTheShortcutOpensThePanelWithTheItemOff() {
+ let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") }
+ defer { controller.hide() }
+ XCTAssertNil(controller.statusItem)
+ controller.shortcutPressed()
+ XCTAssertTrue(controller.panel.isVisible)
+ XCTAssertTrue(controller.panel.canBecomeKey, "the Ask field must be able to take the keyboard")
+ }
+}
+#endif