128 lines
5.1 KiB
Swift
128 lines
5.1 KiB
Swift
import AVKit
|
|
import UIKit
|
|
import SwiftUI
|
|
|
|
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|
var window: UIWindow?
|
|
private let pipManager = PipScreenManager()
|
|
private var didSetupPip = false
|
|
|
|
func scene(
|
|
_ scene: UIScene,
|
|
willConnectTo session: UISceneSession,
|
|
options connectionOptions: UIScene.ConnectionOptions
|
|
) {
|
|
guard let windowScene = scene as? UIWindowScene else { return }
|
|
let window = UIWindow(windowScene: windowScene)
|
|
window.rootViewController = UIHostingController(rootView: ContentView())
|
|
self.window = window
|
|
registerSceneObservers()
|
|
window.makeKeyAndVisible()
|
|
setupDesktopLyricPip()
|
|
|
|
if let url = connectionOptions.urlContexts.first?.url {
|
|
if !QqShareHost.shared.handleOpen(url) {
|
|
HostBridge.shared.handleDeepLink(url.absoluteString)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sceneWillResignActive(_ scene: UIScene) {
|
|
// 键盘还在时,系统会把键盘场景挂到画中画上;连接一旦被拆掉就会报 scene 不存在。
|
|
window?.endEditing(true)
|
|
let defaults = UserDefaults(suiteName: "group.com.zhushenwudi.lovelivemusicplayer")
|
|
let playing = defaults?.bool(forKey: "pipIsPlaying") ?? false
|
|
let enabled = defaults?.bool(forKey: "desktopLyricEnabled") ?? false
|
|
if playing && enabled {
|
|
pipManager.manualChangePicInPic(needStart: true)
|
|
}
|
|
}
|
|
|
|
func sceneDidBecomeActive(_ scene: UIScene) {
|
|
// 前台不显示桌面歌词。开关仍开着时,下次进后台再弹出。
|
|
pipManager.stopPictureInPictureOnly()
|
|
let defaults = UserDefaults(suiteName: "group.com.zhushenwudi.lovelivemusicplayer")
|
|
let playing = defaults?.bool(forKey: "pipIsPlaying") ?? false
|
|
let enabled = defaults?.bool(forKey: "desktopLyricEnabled") ?? false
|
|
if playing && enabled {
|
|
pipManager.picInPicAutoOpen(nil)
|
|
}
|
|
}
|
|
|
|
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
|
|
guard let url = URLContexts.first?.url else { return }
|
|
if QqShareHost.shared.handleOpen(url) {
|
|
return
|
|
}
|
|
HostBridge.shared.handleDeepLink(url.absoluteString)
|
|
}
|
|
|
|
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
|
|
guard let url = userActivity.webpageURL else { return }
|
|
if QqShareHost.shared.handleUniversalLink(url) {
|
|
return
|
|
}
|
|
HostBridge.shared.handleDeepLink(url.absoluteString)
|
|
}
|
|
|
|
private func setupDesktopLyricPip() {
|
|
guard !didSetupPip else { return }
|
|
guard AVPictureInPictureController.isPictureInPictureSupported() else { return }
|
|
guard let window else { return }
|
|
pipManager.addScreenView(on: window)
|
|
let enabled = UserDefaults(suiteName: "group.com.zhushenwudi.lovelivemusicplayer")?
|
|
.bool(forKey: "desktopLyricEnabled") ?? false
|
|
pipManager.picInPicAutoOpen(enabled)
|
|
didSetupPip = true
|
|
}
|
|
|
|
private func registerSceneObservers() {
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpPiPRequest"),
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] notification in
|
|
guard let self else { return }
|
|
self.setupDesktopLyricPip()
|
|
let action = notification.userInfo?["action"] as? String ?? ""
|
|
switch action {
|
|
case "pipAutoOpen":
|
|
let enabled = (notification.userInfo?["enabled"] as? String) == "true"
|
|
self.pipManager.picInPicAutoOpen(enabled)
|
|
case "playing":
|
|
let playing = (notification.userInfo?["playing"] as? String) == "true"
|
|
if playing {
|
|
self.pipManager.picInPicAutoOpen(nil)
|
|
} else {
|
|
self.pipManager.manualChangePicInPic(needStart: false)
|
|
}
|
|
case "show":
|
|
self.pipManager.manualChangePicInPic(needStart: true)
|
|
case "hide":
|
|
self.pipManager.stopPictureInPictureOnly()
|
|
case "foreground":
|
|
self.pipManager.stopPictureInPictureOnly()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
NotificationCenter.default.addObserver(
|
|
forName: Notification.Name("llmpDesktopLyricUpdate"),
|
|
object: nil,
|
|
queue: .main
|
|
) { [weak self] notification in
|
|
let line1Null = (notification.userInfo?["line1Null"] as? String) == "true"
|
|
let line2Null = (notification.userInfo?["line2Null"] as? String) == "true"
|
|
let line1: String? = line1Null ? nil : notification.userInfo?["line1"] as? String
|
|
let line2: String? = line2Null ? nil : notification.userInfo?["line2"] as? String
|
|
let currentLine = Int(notification.userInfo?["currentLine"] as? String ?? "-1") ?? -1
|
|
self?.pipManager.updatePipScreenView(
|
|
lyricLine1: line1,
|
|
lyricLine2: line2,
|
|
currentLine: currentLine
|
|
)
|
|
}
|
|
}
|
|
}
|