Fix: Brightness and Volume Control

This commit is contained in:
Hariharan Mudaliar 2025-09-06 21:54:19 +05:30
commit 98c826aaae
11 changed files with 340 additions and 97 deletions

View file

@ -14,6 +14,8 @@ class SystemHUDManager {
private var changesObserver: SystemChangesObserver?
private weak var coordinator: DynamicIslandViewCoordinator?
private var isSetupComplete = false
private var isSystemOperationInProgress = false
private init() {
// Set up observer for settings changes
@ -23,11 +25,14 @@ class SystemHUDManager {
private func setupSettingsObserver() {
// Use Defaults publisher instead of @Default property wrapper
Defaults.publisher(.enableSystemHUD, options: []).sink { [weak self] change in
guard let self = self, self.isSetupComplete else {
return
}
Task { @MainActor in
if change.newValue {
await self?.startSystemHUD()
await self.startSystemHUD()
} else {
await self?.stopSystemHUD()
await self.stopSystemHUD()
}
}
}.store(in: &cancellables)
@ -35,34 +40,46 @@ class SystemHUDManager {
private var cancellables = Set<AnyCancellable>()
/// Public property to check if system operations are in progress
var isOperationInProgress: Bool {
return isSystemOperationInProgress
}
func setup(coordinator: DynamicIslandViewCoordinator) {
self.coordinator = coordinator
if Defaults[.enableSystemHUD] {
Task { @MainActor in
await startSystemHUD()
self.isSetupComplete = true
}
} else {
isSetupComplete = true
}
}
@MainActor
private func startSystemHUD() async {
guard let coordinator = coordinator else { return }
guard let coordinator = coordinator, !isSystemOperationInProgress else { return }
isSystemOperationInProgress = true
await stopSystemHUD() // Stop any existing observer
changesObserver = SystemChangesObserver(coordinator: coordinator)
changesObserver?.startObserving()
// Disable system HUD if possible
// Note: This would require additional implementation similar to OSDUIManager
disableSystemHUD()
print("System HUD replacement started")
isSystemOperationInProgress = false
}
@MainActor
private func stopSystemHUD() async {
guard !isSystemOperationInProgress else { return }
isSystemOperationInProgress = true
changesObserver?.stopObserving()
changesObserver = nil
@ -70,6 +87,7 @@ class SystemHUDManager {
enableOriginalSystemHUD()
print("System HUD replacement stopped")
isSystemOperationInProgress = false
}
private func disableSystemHUD() {
@ -83,21 +101,32 @@ class SystemHUDManager {
// Re-enable the system HUD using SystemOSDManager
SystemOSDManager.enableSystemHUD()
// Verify it's working after a short delay
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let isRunning = SystemOSDManager.isOSDUIHelperRunning()
// Check status after a reasonable delay, using async to avoid blocking
Task {
try await Task.sleep(nanoseconds: 2_000_000_000) // 2 seconds instead of 1
let isRunning = await SystemOSDManager.isOSDUIHelperRunningAsync()
print("🔍 OSDUIHelper status after re-enable: \(isRunning ? "✅ Running" : "❌ Not running")")
}
}
// MARK: - Media Key Event Handling
/// Handle volume key press events from MediaKeyApplication
public func handleVolumeKeyEvent() {
changesObserver?.handleVolumeKeyEvent()
}
/// Handle brightness key press events from MediaKeyApplication
public func handleBrightnessKeyEvent() {
changesObserver?.handleBrightnessKeyEvent()
}
deinit {
print("🧹 SystemHUDManager deinitializing - ensuring system HUD is restored")
cancellables.removeAll()
Task { @MainActor in
await stopSystemHUD()
}
// Also call the emergency restore as backup
SystemHUDDebugger.forceRestartOSDUIHelper()
// Emergency restore is available via SystemHUDManager.emergencyRestoreSystemHUD() if needed
}
/// Emergency restore function - call this if system HUD is not working