Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
134 lines
6.1 KiB
Swift
Executable File
134 lines
6.1 KiB
Swift
Executable File
// StatusBarController.swift — menu-bar presence for CrossSound.
|
|
//
|
|
// Port of CrossSound-Flutter macos/Runner/StatusBarController.swift. Closing
|
|
// the window HIDES it (MacAppDelegate.windowShouldClose) and the app keeps
|
|
// running with audio playing; this status item is how you get back to it, and
|
|
// it doubles as a mini transport. Quit is explicit — via this menu or ⌘Q.
|
|
//
|
|
// State comes from NowPlayingCenter.onState (title/artist/isPlaying on every
|
|
// pump tick — the de-Fluttered MediaControlPlugin push), and actions leave
|
|
// through onCommand. Both wires are attached by MacAppDelegate once AppRoot
|
|
// registers the scene's live services (see MacControlSurface in AppRoot.swift)
|
|
// — this file knows nothing about the Kit's player types on purpose.
|
|
|
|
import AppKit
|
|
import CrossSoundKit
|
|
|
|
@MainActor
|
|
final class StatusBarController: NSObject {
|
|
static let shared = StatusBarController()
|
|
|
|
/// Transport out of the menu, as the original channel's command strings:
|
|
/// "play" / "pause" / "next" / "prev" / "like". MacAppDelegate maps these
|
|
/// onto NowPlayingCenter.onCommand, so a menu click runs through the SAME
|
|
/// switch in AppRoot.wireControlHooks that the media keys use.
|
|
public var onCommand: ((String) -> Void)?
|
|
/// "Show CrossSound", window half: the original reached
|
|
/// MainFlutterWindow.shared directly; here the window belongs to
|
|
/// MacAppDelegate, which plugs its restore in. The activation half
|
|
/// (policy + activate) stays in onShow(), like the source.
|
|
public var onShowWindow: (() -> Void)?
|
|
|
|
private var item: NSStatusItem?
|
|
private let nowPlaying = NSMenuItem(title: "Nothing playing", action: nil, keyEquivalent: "")
|
|
private let playPause = NSMenuItem(
|
|
title: "Play", action: #selector(onPlayPause), keyEquivalent: "")
|
|
/// Last-known playback state, recorded by apply(). The Play/Pause menu item
|
|
/// resolves its toggle against this — see onPlayPause() for the fix story.
|
|
private var isPlaying = false
|
|
|
|
override private init() {}
|
|
|
|
/// Idempotent, like the original (it was called from two launch paths;
|
|
/// here only MacAppDelegate calls it, but the guard costs nothing).
|
|
func install() {
|
|
guard item == nil else { return }
|
|
let it = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
// The CrossSound mark, as a TEMPLATE image: macOS recolours it for light/
|
|
// dark menu bars and inverts it while the menu is open. The asset isn't in
|
|
// this target's Resources yet — the lookup is kept so dropping the Flutter
|
|
// app's MenuBarIcon in later lights it up with no code change.
|
|
// A status item with neither image nor title collapses to ZERO WIDTH and
|
|
// is invisible, hence the fallbacks.
|
|
if let logo = NSImage(named: "MenuBarIcon") {
|
|
logo.isTemplate = true
|
|
logo.size = NSSize(width: 16 * (logo.size.width / logo.size.height), height: 16)
|
|
it.button?.image = logo
|
|
} else if let img = NSImage(systemSymbolName: "music.note",
|
|
accessibilityDescription: "CrossSound") {
|
|
it.button?.image = img
|
|
} else {
|
|
it.button?.title = "♪"
|
|
}
|
|
it.button?.toolTip = "CrossSound"
|
|
|
|
let menu = NSMenu()
|
|
nowPlaying.isEnabled = false
|
|
menu.addItem(nowPlaying)
|
|
menu.addItem(.separator())
|
|
playPause.target = self
|
|
menu.addItem(playPause)
|
|
menu.addItem(withTargetted: "Next", action: #selector(onNext), target: self)
|
|
menu.addItem(withTargetted: "Previous", action: #selector(onPrev), target: self)
|
|
menu.addItem(withTargetted: "Like", action: #selector(onLike), target: self)
|
|
menu.addItem(.separator())
|
|
menu.addItem(withTargetted: "Show CrossSound", action: #selector(onShow), target: self)
|
|
menu.addItem(
|
|
withTargetted: "Quit CrossSound", action: #selector(onQuit), target: self, key: "q")
|
|
it.menu = menu
|
|
item = it
|
|
CSLog.shared.log(
|
|
"statusbar",
|
|
"installed (button=\(it.button != nil) image=\(it.button?.image != nil) logo=\(NSImage(named: "MenuBarIcon") != nil))"
|
|
)
|
|
}
|
|
|
|
/// Body ported verbatim from the source's apply(). Access widened from
|
|
/// private because the onState wiring lives in MacAppDelegate now, not in
|
|
/// install(); the isPlaying capture is new (it feeds onPlayPause's toggle).
|
|
/// NowPlayingCenter.onState fires on main in this port (the Flutter plugin
|
|
/// made no threading promise, so the old consumer hopped to main itself).
|
|
func apply(title: String, artist: String, isPlaying: Bool) {
|
|
self.isPlaying = isPlaying
|
|
nowPlaying.title = title.isEmpty
|
|
? "Nothing playing"
|
|
: (artist.isEmpty ? title : "\(title) — \(artist)")
|
|
playPause.title = isPlaying ? "Pause" : "Play"
|
|
item?.button?.toolTip = nowPlaying.title
|
|
}
|
|
|
|
// MARK: actions — routed into the same switch the media keys use.
|
|
|
|
/// FIX from the Flutter era: the original sent "playpause" over the media
|
|
/// channel, but the Dart media handler had NO case for that string (only
|
|
/// the separate local-API command path knew it) — the menu item was a
|
|
/// silent no-op. Resolve the toggle HERE from the last-known state (the
|
|
/// same idea as togglePlayPauseCommand resolving via playbackState) and
|
|
/// send an explicit "play"/"pause" the command switch actually handles.
|
|
@objc private func onPlayPause() { onCommand?(isPlaying ? "pause" : "play") }
|
|
@objc private func onNext() { onCommand?("next") }
|
|
@objc private func onPrev() { onCommand?("prev") }
|
|
@objc private func onLike() { onCommand?("like") }
|
|
@objc private func onQuit() { NSApp.terminate(nil) }
|
|
|
|
@objc private func onShow() {
|
|
// Defensive, ported as-is: the app never goes .accessory, but re-assert
|
|
// .regular anyway, activate, then let the delegate order the window in.
|
|
NSApp.setActivationPolicy(.regular)
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
onShowWindow?()
|
|
}
|
|
}
|
|
|
|
extension NSMenu {
|
|
/// addItem + target in one line — NSMenuItem needs an explicit target or the
|
|
/// responder chain swallows the action while the window is hidden.
|
|
fileprivate func addItem(
|
|
withTargetted title: String, action: Selector, target: AnyObject, key: String = ""
|
|
) {
|
|
let mi = NSMenuItem(title: title, action: action, keyEquivalent: key)
|
|
mi.target = target
|
|
addItem(mi)
|
|
}
|
|
}
|