import AppKit // OnboardingWindow explains the two permissions. Each row has a button that // opens the correct System Settings pane. The window updates the status of // each permission when the state changes. The Continue button is active only // when Accessibility is granted. Screen Recording is optional (reduced mode). @MainActor final class OnboardingWindow: NSObject, NSWindowDelegate { private let permissions: PermissionManager var onContinue: (() -> Void)? private var window: NSWindow? private var accessibilityStatus: NSTextField? private var screenStatus: NSTextField? private var continueButton: NSButton? init(permissions: PermissionManager) { self.permissions = permissions super.init() } func show() { if window == nil { build() } window?.center() window?.makeKeyAndOrderFront(nil) NSApp.activate(ignoringOtherApps: true) } func close() { window?.orderOut(nil) } // update sets the status text of each permission. // The function enables the Continue button if Accessibility is granted. func update(state: PermissionState) { accessibilityStatus?.stringValue = state.accessibility ? "Granted" : "Not granted" accessibilityStatus?.textColor = state.accessibility ? .systemGreen : .systemRed screenStatus?.stringValue = state.screenRecording ? "Granted" : "Not granted — reduced mode" screenStatus?.textColor = state.screenRecording ? .systemGreen : .systemOrange continueButton?.isEnabled = state.accessibility } private func build() { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 480, height: 320), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.title = "EPOD Window Bar — Permissions" window.isReleasedWhenClosed = false window.delegate = self let title = label(text: "EPOD Window Bar needs two permissions.", font: .boldSystemFont(ofSize: 15)) let accessibilityRow = permissionRow( name: "Accessibility", detail: "EPOD Window Bar reads, raises, and resizes windows.", buttonTitle: "Open Accessibility Settings", action: #selector(openAccessibility), statusOut: &accessibilityStatus) let screenRow = permissionRow( name: "Screen Recording", detail: "EPOD Window Bar reads window titles on other desktops.", buttonTitle: "Open Screen Recording Settings", action: #selector(openScreen), statusOut: &screenStatus) let continueButton = NSButton(title: "Continue", target: self, action: #selector(continueTapped)) continueButton.bezelStyle = .rounded continueButton.keyEquivalent = "\r" continueButton.isEnabled = false self.continueButton = continueButton let stack = NSStackView(views: [title, accessibilityRow, screenRow, continueButton]) stack.orientation = .vertical stack.alignment = .leading stack.spacing = 18 stack.edgeInsets = NSEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) stack.translatesAutoresizingMaskIntoConstraints = false let content = NSView() content.addSubview(stack) NSLayoutConstraint.activate([ stack.topAnchor.constraint(equalTo: content.topAnchor), stack.leadingAnchor.constraint(equalTo: content.leadingAnchor), stack.trailingAnchor.constraint(equalTo: content.trailingAnchor), stack.bottomAnchor.constraint(equalTo: content.bottomAnchor) ]) window.contentView = content self.window = window update(state: permissions.current) } // permissionRow builds one row: a name, a detail line, a status line, and // a button that opens the correct settings pane. private func permissionRow(name: String, detail: String, buttonTitle: String, action: Selector, statusOut: inout NSTextField?) -> NSView { let nameLabel = label(text: name, font: .boldSystemFont(ofSize: 13)) let detailLabel = label(text: detail, font: .systemFont(ofSize: 12)) detailLabel.textColor = .secondaryLabelColor let status = label(text: "—", font: .systemFont(ofSize: 12)) statusOut = status let button = NSButton(title: buttonTitle, target: self, action: action) button.bezelStyle = .rounded let stack = NSStackView(views: [nameLabel, detailLabel, status, button]) stack.orientation = .vertical stack.alignment = .leading stack.spacing = 4 return stack } private func label(text: String, font: NSFont) -> NSTextField { let field = NSTextField(labelWithString: text) field.font = font return field } @objc private func openAccessibility() { permissions.openAccessibilitySettings() } @objc private func openScreen() { permissions.requestScreenRecording() permissions.openScreenRecordingSettings() } @objc private func continueTapped() { onContinue?() } // windowShouldClose hides the window. The window stays in memory. // The app opens the window again if a permission is revoked. func windowShouldClose(_ sender: NSWindow) -> Bool { window?.orderOut(nil) return false } }