#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`) 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 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) public let panel: NSPanel 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() } } public init(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 as? KeyPanel)?.controller = self 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; pressed /// again, it closes. public func shortcutPressed() { toggle() } public func toggle() { panel.isVisible ? hide() : show() } public func show() { onShow?() 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), /// 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