Files
epod-window-bar/Sources/Permissions/PermissionManager.swift
Erhan Keseli 70d7ca5efe EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock
with one tab per window of the frontmost application. One click raises the
window.

Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig
(ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs
Accessibility and Screen Recording permissions, and runs in a reduced mode
without Screen Recording.

See README.md and docs/ for details.
2026-11-21 21:41:06 +01:00

82 lines
2.7 KiB
Swift

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)
}
}