// "Is a full-screen app in front?" — the test behind // `DisableEasyMouseWhenForegroundWindowIsFullscreen`. // // Windows asks the shell: `IsEasyMouseBlockedByFullscreenWindow` // (`Common.cs:1602-1636`) calls `SHQueryUserNotificationState` and treats BUSY, // RUNNING_D3D_FULL_SCREEN and PRESENTATION_MODE as "full screen", after ruling // out the desktop and the shell window themselves; an app whose image name is in // `EasyMouseFullscreenSwitchBlockExcludedApps` is then forgiven // (`IsAppIgnoredByEasyMouseFullscreenCheck`, `:1567-1600`, matching both // `Path.GetFileName` and `Path.GetFileNameWithoutExtension`, // `StringComparer.OrdinalIgnoreCase`). // // macOS has no equivalent single question, so this asks the two halves directly: // `NSWorkspace` for which app is in front, and the window list for whether that // app has an on-screen window filling a whole display. Window *bounds* need no // Screen Recording grant; window *names* would, and are never read. // // The gate around the whole thing lives in the Engine (`IsEasyMouseSwitchAllowed`, // `Common.cs:1638-1650`): the check is skipped entirely when the setting is off // or when this Mac is not the one holding the pointer. import AppKit import CoreGraphics import Foundation import MWBCore import os /// Whether the app in front of this Mac is running full screen. public enum Fullscreen { /// How long one answer is reused. An edge storm arrives at the tap's rate; /// asking the window server per event would be a syscall per mouse move. public static let cacheInterval: TimeInterval = 0.25 /// How far a window's bounds may differ from a display's and still count as /// covering it, in points. static let tolerance: CGFloat = 1 private struct Cache: Sendable { var at: TimeInterval? var excluded: Set = [] var answer = false } private static let cache = OSAllocatedUnfairLock(initialState: Cache()) /// `true` when the frontmost application is showing a window that covers a /// whole display and is not on the excluded list. /// /// `displays` is `ScreenLayout.displays`; the caller passes it so nothing /// here re-reads the display list. The answer is cached for /// `cacheInterval`, keyed on the excluded set so that editing the list takes /// effect at once. public static func frontmostIsFullscreen(excluding excluded: Set, displays: [CGRect]) -> Bool { let now = EventTap.now() if let cached = cachedAnswer(excluded: excluded, now: now) { return cached } let answer = measure(excluding: excluded, displays: displays) cache.withLock { $0 = Cache(at: now, excluded: excluded, answer: answer) } return answer } /// Forgets the cached answer. For a settings change that must be felt now. public static func invalidate() { cache.withLock { $0 = Cache() } } private static func cachedAnswer(excluded: Set, now: TimeInterval) -> Bool? { cache.withLock { cache in guard let at = cache.at, now - at < cacheInterval, cache.excluded == excluded else { return nil } return cache.answer } } private static func measure(excluding excluded: Set, displays: [CGRect]) -> Bool { guard let app = NSWorkspace.shared.frontmostApplication else { return false } // `Common.cs:1607-1612`: the desktop and the shell are never full screen. // Finder owns the desktop on a Mac, and it is in front whenever no app is. guard app.bundleIdentifier != "com.apple.finder" else { return false } guard !isExcluded(app, in: excluded) else { return false } return coversADisplay(pid: app.processIdentifier, displays: displays) } /// The excluded-apps test, case-insensitively, against everything a user /// might reasonably type: the bundle identifier, the app's display name and /// its bundle's file name with and without the `.app` extension — the two /// spellings `Common.cs:1597-1599` accepts, in macOS terms. static func isExcluded(_ app: NSRunningApplication, in excluded: Set) -> Bool { guard !excluded.isEmpty else { return false } let folded = Set(excluded.map { $0.lowercased() }) var names: [String] = [] if let identifier = app.bundleIdentifier { names.append(identifier) } if let name = app.localizedName { names.append(name) } if let url = app.bundleURL ?? app.executableURL { names.append(url.lastPathComponent) names.append(url.deletingPathExtension().lastPathComponent) } return names.contains { folded.contains($0.lowercased()) } } /// Whether this process has an ordinary on-screen window the size of one /// whole display. /// /// A macOS window in native full screen owns its Space and matches the /// display exactly; a merely zoomed window stops below the menu bar, so the /// equality is what separates the two. `kCGWindowLayer == 0` keeps status /// items, panels and wallpaper windows out of it. static func coversADisplay(pid: pid_t, displays: [CGRect]) -> Bool { guard !displays.isEmpty, let windows = CGWindowListCopyWindowInfo( [.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { return false } for window in windows { guard let owner = window[kCGWindowOwnerPID as String] as? pid_t, owner == pid, let layer = window[kCGWindowLayer as String] as? Int, layer == 0, let raw = window[kCGWindowBounds as String] as? NSDictionary, let bounds = CGRect(dictionaryRepresentation: raw as CFDictionary) else { continue } if displays.contains(where: { covers(bounds, $0) }) { return true } } return false } private static func covers(_ window: CGRect, _ display: CGRect) -> Bool { abs(window.origin.x - display.origin.x) <= tolerance && abs(window.origin.y - display.origin.y) <= tolerance && abs(window.size.width - display.size.width) <= tolerance && abs(window.size.height - display.size.height) <= tolerance } }