904b9b3d-c0eb-42f3-acef-958.../Sources/MusicPlugin.swift
徐翔宇 c67ddd0024 v2.0.1: compact UI + AppleScript timeouts
UI polish (ExpandedView rewrite):
- Horizontal hero row: 128×128 album art on the left, title/artist/
  album + source badge on the right. Half the vertical footprint of
  v2.0.0 at the same info density.
- Dropped the "NOW PLAYING" eyebrow (redundant with the source badge).
- Tightened outer padding 28 → 20, inter-section spacing 22-28 → 16.
- Play button 56 → 48, prev/next 44 → 36; still 44pt tap targets via
  the invisible hover frame.

AppleScript timeout fix (the real bug, unrelated to UI):
- Every fetch() script now wraps the `tell application` block in
  `with timeout of N seconds` (2s for Spotify/Music, 3s for Chrome).
- Music.app hanging was stalling the entire source router for 120s
  (default AppleEvent timeout), freezing the UI on stale Spotify data.
- runAppleScript() suppresses error -1712 (errAETimeout) alongside
  existing -600 / -1728 — expected, not noisy.

Info.plist: CFBundleShortVersionString 2.0.0 → 2.0.1,
CFBundleVersion 2 → 3.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:09:29 +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.1" }
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
}
}
}