88 lines · 3.5 KB · SwiftRaw
import AppKitimport SwiftUI/// The menu-bar item and its popover (the shell of CottonHost's/// `StatusItemController`, with a popover where that one had a menu).@MainActorfinal class StatusItemController: NSObject, NSPopoverDelegate { private let statusItem: NSStatusItem private let popover = NSPopover() private let model: AppModel init(model: AppModel) { self.model = model statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) super.init() popover.behavior = .transient popover.animates = true popover.contentSize = NSSize(width: 360, height: 560) popover.contentViewController = NSHostingController(rootView: PopoverView(model: model)) popover.delegate = self if let button = statusItem.button { button.image = MenuBarMark.image() button.toolTip = "Cotton" button.setAccessibilityLabel("Cotton") button.target = self button.action = #selector(toggle(_:)) } watchDot() if AppModel.flavour != nil { DistributedNotificationCenter.default().addObserver( self, selector: #selector(walkShot(_:)), name: Self.walkShotName, object: nil) } } /// A flavour only (never the app a person installs): on the distributed notification /// `com.decemberfm.cotton.walk.shot`, whose object is an absolute .png path, the live popover draws itself /// into that file. A walk by hand drives the panel through Accessibility and sees each state this way, on a /// Mac whose computer-use index cannot name a flavour (23 Nov 2026). static let walkShotName = Notification.Name("com.decemberfm.cotton.walk.shot") @objc private func walkShot(_ note: Notification) { guard AppModel.flavour != nil, let path = note.object as? String, path.hasPrefix("/"), path.hasSuffix(".png"), let view = popover.contentViewController?.view, popover.isShown else { return } let bounds = view.bounds guard let rep = view.bitmapImageRepForCachingDisplay(in: bounds) else { return } view.cacheDisplay(in: bounds, to: rep) let image = NSImage(size: bounds.size) image.lockFocus() NSColor.windowBackgroundColor.setFill() bounds.fill() rep.draw(in: bounds) image.unlockFocus() guard let tiff = image.tiffRepresentation, let bitmap = NSBitmapImageRep(data: tiff), let png = bitmap.representation(using: .png, properties: [:]) else { return } try? png.write(to: URL(fileURLWithPath: path)) } /// The mark carries a dot while the person must act (AppModel.needsYou). private func watchDot() { let dot = withObservationTracking { model.needsYou } onChange: { [weak self] in Task { @MainActor in self?.watchDot() } } statusItem.button?.image = MenuBarMark.image(dot: dot) } @objc private func toggle(_ sender: Any?) { if popover.isShown { popover.performClose(sender) } else { show() } } func show() { guard let button = statusItem.button else { return } NSApp.activate() popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY) popover.contentViewController?.view.window?.makeKey() } func popoverDidShow(_ notification: Notification) { model.popoverOpen = true } func popoverDidClose(_ notification: Notification) { model.popoverOpen = false }}