904b9b3d-c0eb-42f3-acef-958.../Sources/MusicPlugin.swift
徐翔宇 63885fe121 v2.0.3: compact panel via host size hint + Apple Music artwork + faster poll
Host-facing:
- Info.plist requests a 440x340 expanded panel via the new
  MioPluginPreferredWidth/MioPluginPreferredHeight keys (MioIsland
  v2.1.8+). Old hosts ignore the keys and use their default.

UI:
- Fix vertical stretching of the playing card. Outer ZStack now centers
  children instead of wrapping in a maxHeight:.infinity frame which was
  letting an inner Spacer propagate fill-height up to the top-level VStack.
- Hero HStack clipped to album art height (128pt) so the meta column
  can't bleed a fill-height hint upward either.

Data:
- Apple Music artwork is now fetched via a temp file (write artwork data
  of current track to /tmp, load NSImage from disk). First-class cover art
  instead of the generic music.note placeholder.
- apply(appleScript:) clears albumArt when the track identity changes so
  the next refresh reloads cover art for the new track.

Latency:
- Poll interval 3s → 1.5s. Track changes typically reflect within 2s.
- Also subscribe to the legacy com.apple.iTunes.playerInfo distributed
  notification in addition to com.apple.Music.playerInfo — some builds of
  Music.app still emit the iTunes name.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 20:42:14 +08:00

72 lines
2.3 KiB
Swift

//
// MusicPlugin.swift
// MioIsland Music Plugin
//
// Principal class for the music-player.bundle plugin (v2.0.0).
//
// Wires together the data layer (NowPlayingState + sources/*) and the UI
// layer (ui/*). Loaded at runtime by the host's NativePluginManager via
// Info.plist -> NSPrincipalClass = "MusicPlugin.MusicPlugin".
//
// v2.0.0 is a complete rewrite of the v1.0.0 shell. The old files
// (NowPlayingBridge / MusicPlayerView / MusicHeaderButton) have been
// replaced by a layered design:
//
// NowPlayingState -> orchestrator + sticky source routing
// +-> sources/MediaRemoteSource (dlopen private framework)
// +-> sources/SpotifyAppleScript
// +-> sources/AppleMusicAppleScript
// +-> sources/ChromeWebSource (JS injection into video/audio)
// +-> support/ChineseAppDetector (QQ / NetEase / Kugou)
// +-> support/HostVersionCheck (host >= 2.1.7 gate)
//
// ui/ExpandedView -> main panel (makeView)
// ui/HeaderSlotView -> 20x20 header icon + pseudo-spectrum
// ui/AlbumArtColorExtractor + ui/SeekBar
// support/Localization (zh/en)
//
import AppKit
import SwiftUI
/// Principal class. Module is `MusicPlugin`, class is `MusicPlugin`, so
/// Info.plist NSPrincipalClass = "MusicPlugin.MusicPlugin".
final class MusicPlugin: NSObject, MioPlugin {
var id: String { "music-player" }
var name: String { "Music Player" }
var icon: String { "music.note" }
var version: String { "2.0.3" }
func activate() {
NSLog("[mio-plugin-music] activate")
Task { @MainActor in
NowPlayingState.shared.start()
}
}
func deactivate() {
NSLog("[mio-plugin-music] deactivate")
Task { @MainActor in
NowPlayingState.shared.stop()
}
}
func makeView() -> NSView {
let view = NSHostingView(rootView: ExpandedView())
view.autoresizingMask = [.width, .height]
return view
}
@objc func viewForSlot(_ slot: String, context: [String: Any]) -> NSView? {
switch slot {
case "header":
let view = NSHostingView(rootView: HeaderSlotView())
view.frame = NSRect(x: 0, y: 0, width: 20, height: 20)
view.setFrameSize(NSSize(width: 20, height: 20))
return view
default:
return nil
}
}
}