mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 23:24:32 +00:00
102 lines
3.7 KiB
Swift
102 lines
3.7 KiB
Swift
/*
|
|
* Atoll (DynamicIsland)
|
|
* Copyright (C) 2024-2026 Atoll Contributors
|
|
*
|
|
* Originally from boring.notch project
|
|
* Modified and adapted for Atoll (DynamicIsland)
|
|
* See NOTICE for details.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
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)
|
|
}
|
|
}
|