import Foundation @MainActor final class AccessibilityPanelSession { enum State: Equatable { case closed case hidden case visible(CGRect) } private(set) var state = State.closed private let isTrusted: () -> Bool private let windowFrame: () throws -> CGRect? private let present: (CGRect) -> Void private let hide: () -> Void private let polling: (Bool) -> Void private let reportError: (any Error) -> Void init( isTrusted: @escaping () -> Bool, windowFrame: @escaping () throws -> CGRect?, present: @escaping (CGRect) -> Void, hide: @escaping () -> Void, polling: @escaping (Bool) -> Void, reportError: @escaping (any Error) -> Void ) { self.isTrusted = isTrusted self.windowFrame = windowFrame self.present = present self.hide = hide self.polling = polling self.reportError = reportError } func show() { if state == .closed { state = .hidden } refresh() } func refresh() { guard state != .closed else { return } guard !isTrusted() else { close(); return } do { guard let frame = try windowFrame() else { conceal(); return } let wasHidden = state == .hidden guard state != .visible(frame) else { return } state = .visible(frame) present(frame) if wasHidden { polling(true) } } catch { close() reportError(error) } } func close() { conceal() state = .closed } private func conceal() { guard case .visible = state else { return } polling(false) hide() state = .hidden } }