import AppKit import ApplicationServices import CoreGraphics // PermissionState holds the two permissions that EPOD Window Bar needs. struct PermissionState: Equatable { var accessibility: Bool var screenRecording: Bool // allGranted is true if both permissions are present. var allGranted: Bool { accessibility && screenRecording } // reducedMode is true if Accessibility is present but Screen Recording // is not. In reduced mode the app shows only windows on the current Space. var reducedMode: Bool { accessibility && !screenRecording } } // PermissionManager reads the permission state and polls for changes. // The manager polls every 2 s. The poll catches a grant while the onboarding // window is open. The poll also catches a revoke at runtime. @MainActor final class PermissionManager { private(set) var current: PermissionState var onChange: ((PermissionState) -> Void)? private var timer: Timer? init() { current = PermissionManager.read(prompt: false) } func start() { timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in MainActor.assumeIsolated { self?.refresh() } } refresh() } // refresh reads the state again. The manager calls onChange only if the // state changed. func refresh() { let new = PermissionManager.read(prompt: false) if new != current { current = new Log.app.info("Permission state changed. ax=\(new.accessibility) sr=\(new.screenRecording)") onChange?(new) } } // requestScreenRecording asks macOS for Screen Recording. // macOS shows the system prompt one time. func requestScreenRecording() { CGRequestScreenCaptureAccess() } func openAccessibilitySettings() { open("x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility") } func openScreenRecordingSettings() { open("x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture") } // read returns the current permission state. // prompt shows the system Accessibility prompt if the value is true. static func read(prompt: Bool) -> PermissionState { let key = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String let options = [key: prompt] as CFDictionary let ax = AXIsProcessTrustedWithOptions(options) let sr = CGPreflightScreenCaptureAccess() return PermissionState(accessibility: ax, screenRecording: sr) } private func open(_ value: String) { guard let url = URL(string: value) else { return } NSWorkspace.shared.open(url) } }