mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
87 lines
3 KiB
Swift
87 lines
3 KiB
Swift
//
|
|
// FullscreenMediaDetection.swift
|
|
// DynamicIsland
|
|
//
|
|
// Created by Richard Kunkli on 06/09/2024.
|
|
//
|
|
|
|
import Defaults
|
|
import MacroVisionKit
|
|
import SwiftUI
|
|
|
|
class FullscreenMediaDetector: ObservableObject {
|
|
static let shared = FullscreenMediaDetector()
|
|
private let detector: MacroVisionKit
|
|
@ObservedObject private var musicManager = MusicManager.shared
|
|
@MainActor @Published private(set) var fullscreenStatus: [String: Bool] = [:]
|
|
private var notificationTask: Task<Void, Never>?
|
|
|
|
private init() {
|
|
self.detector = MacroVisionKit.shared
|
|
detector.configuration.includeSystemApps = true
|
|
setupNotificationObservers()
|
|
updateFullScreenStatus()
|
|
}
|
|
|
|
private func setupNotificationObservers() {
|
|
notificationTask = Task { @Sendable [weak self] in
|
|
await withTaskGroup(of: Void.self) { group in
|
|
group.addTask {
|
|
let activeSpaceNotifications = NSWorkspace.shared.notificationCenter.notifications(
|
|
named: NSWorkspace.activeSpaceDidChangeNotification
|
|
)
|
|
|
|
for await _ in activeSpaceNotifications {
|
|
await self?.handleChange()
|
|
}
|
|
}
|
|
|
|
group.addTask {
|
|
let screenParameterNotifications = NSWorkspace.shared.notificationCenter.notifications(
|
|
named: NSApplication.didChangeScreenParametersNotification
|
|
)
|
|
|
|
for await _ in screenParameterNotifications {
|
|
await self?.handleChange()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func handleChange() async {
|
|
try? await Task.sleep(for: .milliseconds(500))
|
|
self.updateFullScreenStatus()
|
|
}
|
|
|
|
private func updateFullScreenStatus() {
|
|
guard Defaults[.enableFullscreenMediaDetection] else {
|
|
let reset = Dictionary(uniqueKeysWithValues: NSScreen.screens.map { ($0.localizedName, false) })
|
|
if reset != fullscreenStatus {
|
|
fullscreenStatus = reset
|
|
}
|
|
return
|
|
}
|
|
|
|
|
|
let apps = detector.detectFullscreenApps(debug: false)
|
|
let names = NSScreen.screens.map { $0.localizedName }
|
|
var newStatus: [String: Bool] = [:]
|
|
for name in names {
|
|
newStatus[name] = apps.contains { $0.screen.localizedName == name && $0.bundleIdentifier != "com.apple.finder" && ($0.bundleIdentifier == musicManager.bundleIdentifier || Defaults[.hideNotchOption] == .always) }
|
|
}
|
|
|
|
if newStatus != fullscreenStatus {
|
|
fullscreenStatus = newStatus
|
|
NSLog("✅ Fullscreen status: \(newStatus)")
|
|
}
|
|
}
|
|
|
|
private func cleanupNotificationObservers() {
|
|
NSWorkspace.shared.notificationCenter.removeObserver(self)
|
|
}
|
|
|
|
deinit {
|
|
NSWorkspace.shared.notificationCenter.removeObserver(self)
|
|
}
|
|
}
|