| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | |
| 5 | /// Ask in the menu bar, presented by Base itself rather than SwiftUI's |
| 6 | /// MenuBarExtra, whose window and button are private: the global shortcut |
| 7 | /// has to be able to open the panel, and MenuBarExtra gives no way to. |
| 8 | /// |
| 9 | /// An NSStatusItem (Base's mark) toggles a borderless, non-activating |
| 10 | /// NSPanel under it holding the menu-bar panel's SwiftUI content. The |
| 11 | /// shortcut (`shortcutPressed`) toggles the same panel: under the item, or |
| 12 | /// with the item switched off in Settings, at the top right of the screen. |
| 13 | /// The panel closes on Escape and when it loses key, as a menu-bar panel |
| 14 | /// does. |
| 15 | @MainActor public final class StatusPanelController: NSObject, NSWindowDelegate { |
| 16 | public static let size = CGSize(width: 410, height: 560) |
| 17 | |
| 18 | public let panel: NSPanel |
| 19 | public private(set) var statusItem: NSStatusItem? |
| 20 | let accessibilityLabel: String |
| 21 | |
| 22 | /// Runs each time the panel opens, before it shows: the app re-reads |
| 23 | /// what can change while it's closed (the shortcut's hint). |
| 24 | public var onShow: (() -> Void)? |
| 25 | |
| 26 | /// Whether Base's item is in the menu bar (Settings, on by default). |
| 27 | public var itemShown: Bool { |
| 28 | didSet { updateItem() } |
| 29 | } |
| 30 | |
| 31 | public init<Content: View>(itemShown: Bool, accessibilityLabel: String, @ViewBuilder content: () -> Content) { |
| 32 | self.itemShown = itemShown |
| 33 | self.accessibilityLabel = accessibilityLabel |
| 34 | panel = KeyPanel(contentRect: NSRect(origin: .zero, size: Self.size), |
| 35 | styleMask: [.borderless, .nonactivatingPanel, .fullSizeContentView], backing: .buffered, defer: true) |
| 36 | super.init() |
| 37 | (panel as? KeyPanel)?.controller = self |
| 38 | panel.isFloatingPanel = true |
| 39 | panel.level = .popUpMenu |
| 40 | panel.hidesOnDeactivate = false |
| 41 | panel.isOpaque = false |
| 42 | panel.backgroundColor = .clear |
| 43 | panel.hasShadow = true |
| 44 | panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .transient] |
| 45 | panel.isReleasedWhenClosed = false |
| 46 | panel.delegate = self |
| 47 | let host = NSHostingView(rootView: content() |
| 48 | .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous)) |
| 49 | .frame(width: Self.size.width, height: Self.size.height)) |
| 50 | host.frame = NSRect(origin: .zero, size: Self.size) |
| 51 | panel.contentView = host |
| 52 | updateItem() |
| 53 | } |
| 54 | |
| 55 | /// The global shortcut: the panel, wherever Base's item is; pressed |
| 56 | /// again, it closes. |
| 57 | public func shortcutPressed() { toggle() } |
| 58 | |
| 59 | public func toggle() { panel.isVisible ? hide() : show() } |
| 60 | |
| 61 | public func show() { |
| 62 | onShow?() |
| 63 | position() |
| 64 | panel.makeKeyAndOrderFront(nil) |
| 65 | NSApp.activate() |
| 66 | statusItem?.button?.highlight(true) |
| 67 | } |
| 68 | |
| 69 | public func hide() { |
| 70 | panel.orderOut(nil) |
| 71 | statusItem?.button?.highlight(false) |
| 72 | } |
| 73 | |
| 74 | public func windowDidResignKey(_ notification: Notification) { hide() } |
| 75 | |
| 76 | @objc func itemClicked() { toggle() } |
| 77 | |
| 78 | private func updateItem() { |
| 79 | if itemShown, statusItem == nil { |
| 80 | let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) |
| 81 | if let button = item.button { |
| 82 | let image = NSImage(systemSymbolName: "square.fill", accessibilityDescription: accessibilityLabel) |
| 83 | image?.isTemplate = true |
| 84 | button.image = image |
| 85 | button.target = self |
| 86 | button.action = #selector(itemClicked) |
| 87 | button.setAccessibilityLabel(accessibilityLabel) |
| 88 | } |
| 89 | statusItem = item |
| 90 | } else if !itemShown, let item = statusItem { |
| 91 | NSStatusBar.system.removeStatusItem(item) |
| 92 | statusItem = nil |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Under the item, centred on it and kept on its screen; with no item, |
| 97 | /// the top right of the main screen's visible area. |
| 98 | private func position() { |
| 99 | let size = Self.size |
| 100 | if let button = statusItem?.button, let window = button.window, let screen = window.screen ?? NSScreen.main { |
| 101 | let item = window.convertToScreen(button.convert(button.bounds, to: nil)) |
| 102 | let visible = screen.visibleFrame |
| 103 | let x = min(max(item.midX - size.width / 2, visible.minX + 8), visible.maxX - size.width - 8) |
| 104 | panel.setFrameOrigin(NSPoint(x: x, y: item.minY - size.height - 6)) |
| 105 | } else if let visible = NSScreen.main?.visibleFrame { |
| 106 | panel.setFrameOrigin(NSPoint(x: visible.maxX - size.width - 12, y: visible.maxY - size.height - 6)) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// A borderless panel that can still take the keyboard (the Ask field), |
| 112 | /// and closes on Escape, as a menu-bar panel does. |
| 113 | final class KeyPanel: NSPanel { |
| 114 | weak var controller: StatusPanelController? |
| 115 | override var canBecomeKey: Bool { true } |
| 116 | override func cancelOperation(_ sender: Any?) { |
| 117 | if let controller { controller.hide() } else { orderOut(sender) } |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | |