import AppKit import ApplicationServices import CoreGraphics // AppDelegate wires every module of EPOD Window Bar together. The delegate // starts the tracking, the strip, the activation, and the constraint services // after Accessibility is granted. The delegate implements AppControl for the // menu bar item and the settings window. @MainActor final class AppDelegate: NSObject, NSApplicationDelegate, AppControl { private let permissions = PermissionManager() private let settings = Settings.shared private let store = WindowStore() private let cache = AXElementCache() private let spaceService = SpaceService() private var menuBar: MenuBarController? private var onboarding: OnboardingWindow? private var settingsWindow: SettingsWindow? private var tracking: TrackingController? private var strip: StripController? private var activator: WindowActivator? private var constraint: WindowConstraintService? private var servicesStarted = false private var userContinuedReduced = false private var isFullScreenNow = false private var screenPollTimer: Timer? private var lastVisibleFrame: CGRect = .zero func applicationDidFinishLaunching(_ notification: Notification) { Log.app.info("EPOD Window Bar starts.") menuBar = MenuBarController(permissions: permissions, control: self, settings: settings) permissions.onChange = { [weak self] state in self?.handle(state) } permissions.start() handle(permissions.current) } // handle reacts to a new permission state. The app starts the services if // Accessibility is granted. The app shows the onboarding window if // Accessibility is missing (spec section 6.1). private func handle(_ state: PermissionState) { menuBar?.updatePermissionState(state) onboarding?.update(state: state) if state.accessibility { startServicesIfNeeded() updateStripVisibility() if state.allGranted { onboarding?.close() } else if userContinuedReduced == false { showOnboarding() } } else { userContinuedReduced = false stopServices() showOnboarding() } } // MARK: - Services private func startServicesIfNeeded() { guard servicesStarted == false else { return } servicesStarted = true let tracking = TrackingController(store: store, cache: cache, spaceService: spaceService, settings: settings) let strip = StripController(store: store, settings: settings) let activator = WindowActivator(store: store, cache: cache) let constraint = WindowConstraintService(store: store, cache: cache, settings: settings) // The strip forwards the tab actions to the activator. strip.onActivate = { [weak self] id in self?.activator?.activate(windowID: id) } strip.onMinimize = { [weak self] id in self?.activator?.minimize(windowID: id) } strip.onClose = { [weak self] id in self?.activator?.close(windowID: id) } // The tracking controller reports the focused window for the active tab. tracking.onFocusedWindowChanged = { [weak self] id in self?.strip?.activeWindowID = id } // The tracking controller reports the full-screen state. The strip // hides in full screen (decision D7). tracking.onFrontmostFocusedFullScreenChanged = { [weak self] full in self?.isFullScreenNow = full self?.updateStripVisibility() } // The tracking controller forwards the geometry events to the // constraint service (spec section 7.10). The controller resolves the // CGWindowID on the serial AX queue. tracking.onWindowGeometryEvent = { [weak self] _, name, id in guard let self = self, self.settings.paused == false else { return } if name == kAXWindowCreatedNotification { self.constraint?.constrainOnCreate(windowID: id) } else { self.constraint?.handleMovedOrResized(windowID: id) } } self.tracking = tracking self.strip = strip self.activator = activator self.constraint = constraint strip.start() tracking.start() constraint.stripFrame = strip.stripFrame startScreenObservers() updateStripVisibility() } // stopServices stops all AX work. The app calls this function when // Accessibility is revoked at runtime (spec section 6.1). The app does not // crash. private func stopServices() { guard servicesStarted else { return } servicesStarted = false tracking?.onAccessibilityLost() strip?.setVisible(false) stopScreenObservers() tracking = nil strip = nil activator = nil constraint = nil cache.removeAll() } // updateStripVisibility shows the strip only if the app is active, the app // is not paused, and the frontmost window is not full screen. private func updateStripVisibility() { let visible = settings.paused == false && isFullScreenNow == false && permissions.current.accessibility strip?.setVisible(visible) } // MARK: - Screen and Dock changes private func startScreenObservers() { NotificationCenter.default.addObserver( self, selector: #selector(screenParametersChanged), name: NSApplication.didChangeScreenParametersNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver( self, selector: #selector(didWake), name: NSWorkspace.didWakeNotification, object: nil) lastVisibleFrame = NSScreen.main?.visibleFrame ?? .zero // The Dock preference change has no public notification. The app polls // the visible frame every 2 s (spec section 7.8). screenPollTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { [weak self] _ in MainActor.assumeIsolated { self?.pollVisibleFrame() } } } private func stopScreenObservers() { NotificationCenter.default.removeObserver( self, name: NSApplication.didChangeScreenParametersNotification, object: nil) NSWorkspace.shared.notificationCenter.removeObserver( self, name: NSWorkspace.didWakeNotification, object: nil) screenPollTimer?.invalidate() screenPollTimer = nil } @objc private func screenParametersChanged() { repositionStrip() } @objc private func didWake() { repositionStrip() } private func pollVisibleFrame() { let current = NSScreen.main?.visibleFrame ?? .zero if current != lastVisibleFrame { lastVisibleFrame = current repositionStrip() } } private func repositionStrip() { strip?.reposition() if let strip = strip { constraint?.stripFrame = strip.stripFrame } } // MARK: - Onboarding private func showOnboarding() { if onboarding == nil { let window = OnboardingWindow(permissions: permissions) window.onContinue = { [weak self] in self?.userContinuedReduced = true self?.onboarding?.close() } onboarding = window } onboarding?.show() } // MARK: - AppControl var isPaused: Bool { return settings.paused } func togglePause() { settings.paused.toggle() updateStripVisibility() Log.app.info("Pause is now \(self.settings.paused, privacy: .public).") } var constrainWindows: Bool { return settings.constrainWindows } func setConstrainWindows(_ on: Bool) { settings.constrainWindows = on Log.app.info("Constrain windows is now \(on, privacy: .public).") } func dumpWindowsToLog() { tracking?.dumpWindowsToLog() } func copyWindowDump() { tracking?.dumpWindowsToPasteboard() } func openSettings() { if settingsWindow == nil { settingsWindow = SettingsWindow(settings: settings) } settingsWindow?.show() } }