Non polling SystemDUIHelper

This commit is contained in:
Hariharan Mudaliar 2025-10-07 01:30:21 +05:30
commit 194d6b3d0e

View file

@ -8,6 +8,7 @@
import Foundation import Foundation
import Defaults import Defaults
import Combine import Combine
import AppKit
class SystemHUDManager { class SystemHUDManager {
static let shared = SystemHUDManager() static let shared = SystemHUDManager()
@ -60,7 +61,6 @@ class SystemHUDManager {
} }
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
private var osdMonitorTimer: Timer?
/// Public property to check if system operations are in progress /// Public property to check if system operations are in progress
var isOperationInProgress: Bool { var isOperationInProgress: Bool {
@ -93,7 +93,7 @@ class SystemHUDManager {
// Disable system HUD if possible // Disable system HUD if possible
disableSystemHUD() disableSystemHUD()
// Start periodic monitoring to ensure OSD stays disabled // Start event-driven OSD monitoring
startOSDMonitoring() startOSDMonitoring()
print("System HUD replacement started") print("System HUD replacement started")
@ -151,38 +151,63 @@ class SystemHUDManager {
// MARK: - OSD Monitoring // MARK: - OSD Monitoring
/// Start periodic monitoring to ensure OSD stays disabled /// Start event-driven monitoring for OSDUIHelper process launches
@MainActor @MainActor
private func startOSDMonitoring() { private func startOSDMonitoring() {
stopOSDMonitoring() // Stop any existing timer stopOSDMonitoring() // Stop any existing observer
osdMonitorTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { [weak self] _ in // Listen for process launch notifications
guard let self = self else { return } NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.didLaunchApplicationNotification)
.compactMap { notification -> NSRunningApplication? in
// Check if any HUD is enabled notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication
let anyHUDEnabled = Defaults[.enableVolumeHUD] || Defaults[.enableBrightnessHUD] }
guard anyHUDEnabled else { return } .filter { app in
// Check if OSDUIHelper was launched
// Check if OSD is running when it shouldn't be app.bundleIdentifier == "com.apple.OSDUIHelper" ||
Task.detached(priority: .background) { app.localizedName?.contains("OSDUIHelper") == true
let isRunning = await SystemOSDManager.isOSDUIHelperRunningAsync() }
if isRunning { .sink { [weak self] _ in
await MainActor.run { guard let self = self else { return }
print("⚠️ OSDUIHelper detected running while HUDs are enabled - re-disabling")
// Check if any HUD is enabled
let anyHUDEnabled = Defaults[.enableVolumeHUD] || Defaults[.enableBrightnessHUD]
guard anyHUDEnabled else { return }
Task { @MainActor in
print("⚠️ OSDUIHelper launched - re-disabling")
self.disableSystemHUD()
}
}
.store(in: &cancellables)
// Also listen for screen wake events (OSDUIHelper often restarts after wake)
NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.screensDidWakeNotification)
.sink { [weak self] _ in
guard let self = self else { return }
let anyHUDEnabled = Defaults[.enableVolumeHUD] || Defaults[.enableBrightnessHUD]
guard anyHUDEnabled else { return }
// Check after a short delay to ensure system is ready
Task { @MainActor in
try? await Task.sleep(nanoseconds: 500_000_000) // 0.5 seconds
let isRunning = await SystemOSDManager.isOSDUIHelperRunningAsync()
if isRunning {
print("⚠️ OSDUIHelper detected running after wake - re-disabling")
self.disableSystemHUD() self.disableSystemHUD()
} }
} }
} }
} .store(in: &cancellables)
print("✅ OSD monitoring started (checks every 5 seconds)") print("✅ OSD monitoring started (event-driven)")
} }
/// Stop OSD monitoring /// Stop OSD monitoring
@MainActor @MainActor
private func stopOSDMonitoring() { private func stopOSDMonitoring() {
osdMonitorTimer?.invalidate() // Cancellables are automatically cleaned up
osdMonitorTimer = nil
print("🛑 OSD monitoring stopped") print("🛑 OSD monitoring stopped")
} }