diff --git a/alt-tab-macos/logic/Application.swift b/alt-tab-macos/logic/Application.swift index 9e4fcb61..b2969add 100644 --- a/alt-tab-macos/logic/Application.swift +++ b/alt-tab-macos/logic/Application.swift @@ -22,7 +22,7 @@ class Application: NSObject { private func addAndObserveWindows() { axUiElement = AXUIElementCreateApplication(runningApplication.processIdentifier) - AXObserverCreate(runningApplication.processIdentifier, axObserverApplicationCallback, &axObserver) + AXObserverCreate(runningApplication.processIdentifier, axObserverCallback, &axObserver) observeAllWindows() } @@ -59,47 +59,63 @@ class Application: NSObject { private func observeEvents(_ windows: [AXUIElement]) { guard let axObserver = axObserver else { return } let selfPointer = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()) - AXObserverAddNotification(axObserver, axUiElement!, kAXApplicationActivatedNotification as CFString, selfPointer) - AXObserverAddNotification(axObserver, axUiElement!, kAXFocusedWindowChangedNotification as CFString, selfPointer) - AXObserverAddNotification(axObserver, axUiElement!, kAXWindowCreatedNotification as CFString, selfPointer) - AXObserverAddNotification(axObserver, axUiElement!, kAXApplicationHiddenNotification as CFString, selfPointer) - AXObserverAddNotification(axObserver, axUiElement!, kAXApplicationShownNotification as CFString, selfPointer) + for notification in [ + kAXApplicationActivatedNotification, + kAXFocusedWindowChangedNotification, + kAXWindowCreatedNotification, + kAXApplicationHiddenNotification, + kAXApplicationShownNotification, + ] { + AXObserverAddNotification(axObserver, axUiElement!, notification as CFString, selfPointer) + } CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(axObserver), .defaultMode) } } -func axObserverApplicationCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, applicationPointer: UnsafeMutableRawPointer?) -> Void { +private func axObserverCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, applicationPointer: UnsafeMutableRawPointer?) -> Void { let application = Unmanaged.fromOpaque(applicationPointer!).takeUnretainedValue() let app = App.shared as! App let type = notificationName as String debugPrint("OS event: " + type, element.title()) switch type { - case kAXApplicationActivatedNotification: - guard !app.appIsBeingUsed, - let appFocusedWindow = element.focusedWindow(), - let existingIndex = Windows.list.firstIndexThatMatches(appFocusedWindow) else { return } - Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0) - case kAXApplicationHiddenNotification, kAXApplicationShownNotification: - for window in Windows.list { - guard window.application.axUiElement!.pid() == element.pid() else { continue } - window.isHidden = type == kAXApplicationHiddenNotification - } - app.refreshOpenUi() - case kAXWindowCreatedNotification: - guard element.isActualWindow() else { return } - // a window being un-minimized can trigger kAXWindowCreatedNotification - guard Windows.list.firstIndexThatMatches(element) == nil else { return } - let window = Window(element, application) - Windows.list.insert(window, at: 0) - Windows.moveFocusedWindowIndexAfterWindowCreatedInBackground() - // TODO: find a better way to get thumbnail of the new window - window.refreshThumbnail() - app.refreshOpenUi() - case kAXFocusedWindowChangedNotification: - guard !app.appIsBeingUsed, - element.isActualWindow(), - let existingIndex = Windows.list.firstIndexThatMatches(element) else { return } - Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0) + case kAXApplicationActivatedNotification: eventApplicationActivated(app, element) + case kAXApplicationHiddenNotification, kAXApplicationShownNotification: eventApplicationHiddenOrShown(app, element, type) + case kAXWindowCreatedNotification: eventWindowCreated(app, element, application) + case kAXFocusedWindowChangedNotification: eventFocusedWindowChanged(app, element) default: return } } + +private func eventApplicationActivated(_ app: App, _ element: AXUIElement) { + guard !app.appIsBeingUsed, + let appFocusedWindow = element.focusedWindow(), + let existingIndex = Windows.list.firstIndexThatMatches(appFocusedWindow) else { return } + Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0) +} + +private func eventApplicationHiddenOrShown(_ app: App, _ element: AXUIElement, _ type: String) { + for window in Windows.list { + guard window.application.axUiElement!.pid() == element.pid() else { continue } + window.isHidden = type == kAXApplicationHiddenNotification + } + app.refreshOpenUi() +} + +private func eventWindowCreated(_ app: App, _ element: AXUIElement, _ application: Application) { + guard element.isActualWindow() else { return } + // a window being un-minimized can trigger kAXWindowCreatedNotification + guard Windows.list.firstIndexThatMatches(element) == nil else { return } + let window = Window(element, application) + Windows.list.insert(window, at: 0) + Windows.moveFocusedWindowIndexAfterWindowCreatedInBackground() + // TODO: find a better way to get thumbnail of the new window + window.refreshThumbnail() + app.refreshOpenUi() +} + +private func eventFocusedWindowChanged(_ app: App, _ element: AXUIElement) { + guard !app.appIsBeingUsed, + element.isActualWindow(), + let existingIndex = Windows.list.firstIndexThatMatches(element) else { return } + Windows.list.insert(Windows.list.remove(at: existingIndex), at: 0) +} \ No newline at end of file diff --git a/alt-tab-macos/logic/Applications.swift b/alt-tab-macos/logic/Applications.swift index 07aa1363..7b570979 100644 --- a/alt-tab-macos/logic/Applications.swift +++ b/alt-tab-macos/logic/Applications.swift @@ -29,12 +29,7 @@ class Applications { static func removeRunningApplications(_ runningApps: [NSRunningApplication]) { for runningApp in runningApps { guard let app = Applications.list.first(where: { $0.runningApplication.isEqual(runningApp) }) else { continue } - var windowsToKeep = [Window]() - for window in Windows.list { - guard !window.application.runningApplication.isEqual(runningApp) else { continue } - windowsToKeep.append(window) - } - Windows.list = windowsToKeep + Windows.list.removeAll(where: { $0.application.runningApplication.isEqual(runningApp) }) // some apps never finish launching; the observer leaks for them without this app.removeObserver() Applications.list.removeAll(where: { $0.runningApplication.isEqual(runningApp) }) diff --git a/alt-tab-macos/logic/Screen.swift b/alt-tab-macos/logic/Screen.swift index 210f1f29..3a7504ff 100644 --- a/alt-tab-macos/logic/Screen.swift +++ b/alt-tab-macos/logic/Screen.swift @@ -4,10 +4,8 @@ import Cocoa class Screen { static func preferred() -> NSScreen { switch Preferences.showOnScreen! { - case .MOUSE: - return withMouse() ?? NSScreen.main!; // .main as fall-back - case .MAIN: - return NSScreen.main!; + case .MOUSE: return withMouse() ?? NSScreen.main!; // .main as fall-back + case .MAIN: return NSScreen.main!; } } diff --git a/alt-tab-macos/logic/Window.swift b/alt-tab-macos/logic/Window.swift index 90bfe6a3..07603ea6 100644 --- a/alt-tab-macos/logic/Window.swift +++ b/alt-tab-macos/logic/Window.swift @@ -30,12 +30,16 @@ class Window { } private func observeEvents() { - AXObserverCreate(application.runningApplication.processIdentifier, axObserverWindowCallback, &axObserver) + AXObserverCreate(application.runningApplication.processIdentifier, axObserverCallback, &axObserver) guard let axObserver = axObserver else { return } - AXObserverAddNotification(axObserver, axUiElement, kAXUIElementDestroyedNotification as CFString, nil) - AXObserverAddNotification(axObserver, axUiElement, kAXTitleChangedNotification as CFString, nil) - AXObserverAddNotification(axObserver, axUiElement, kAXWindowMiniaturizedNotification as CFString, nil) - AXObserverAddNotification(axObserver, axUiElement, kAXWindowDeminiaturizedNotification as CFString, nil) + for notification in [ + kAXUIElementDestroyedNotification, + kAXTitleChangedNotification, + kAXWindowMiniaturizedNotification, + kAXWindowDeminiaturizedNotification, + ] { + AXObserverAddNotification(axObserver, axUiElement, notification as CFString, nil) + } CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(axObserver), .defaultMode) } @@ -66,22 +70,18 @@ class Window { // The following function was ported from https://github.com/Hammerspoon/hammerspoon/issues/370#issuecomment-545545468 func makeKeyWindow(_ psn: ProcessSerialNumber) -> Void { var psn_ = psn - var bytes1 = [UInt8](repeating: 0, count: 0xf8) bytes1[0x04] = 0xF8 bytes1[0x08] = 0x01 bytes1[0x3a] = 0x10 - var bytes2 = [UInt8](repeating: 0, count: 0xf8) bytes2[0x04] = 0xF8 bytes2[0x08] = 0x02 bytes2[0x3a] = 0x10 - memcpy(&bytes1[0x3c], &cgWindowId, MemoryLayout.size) memset(&bytes1[0x20], 0xFF, 0x10) memcpy(&bytes2[0x3c], &cgWindowId, MemoryLayout.size) memset(&bytes2[0x20], 0xFF, 0x10) - SLPSPostEventRecordTo(&psn_, &(UnsafeMutablePointer(mutating: UnsafePointer(bytes1)).pointee)) SLPSPostEventRecordTo(&psn_, &(UnsafeMutablePointer(mutating: UnsafePointer(bytes2)).pointee)) } @@ -98,30 +98,41 @@ class Window { } } -func axObserverWindowCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, _: UnsafeMutableRawPointer?) -> Void { +private func axObserverCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, _: UnsafeMutableRawPointer?) -> Void { let type = notificationName as String let app = App.shared as! App debugPrint("OS event: " + type, element.title()) switch type { - case kAXUIElementDestroyedNotification: - guard let existingIndex = Windows.list.firstIndexThatMatches(element) else { return } - Windows.list.remove(at: existingIndex) - guard Windows.list.count > 0 else { app.hideUi(); return } - Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(existingIndex) - app.refreshOpenUi() - case kAXWindowMiniaturizedNotification, kAXWindowDeminiaturizedNotification: - guard let window = Windows.list.firstWindowThatMatches(element) else { return } - window.isMinimized = type == kAXWindowMiniaturizedNotification - // TODO: find a better way to get thumbnail of the new window (when AltTab is triggered min/demin animation) - window.refreshThumbnail() - app.refreshOpenUi() - case kAXTitleChangedNotification: - guard element.isActualWindow(), - let window = Windows.list.firstWindowThatMatches(element), - let newTitle = window.axUiElement.title(), - newTitle != window.title else { return } - window.title = newTitle - app.refreshOpenUi() + case kAXUIElementDestroyedNotification: eventWindowDestroyed(app, element) + case kAXWindowMiniaturizedNotification, kAXWindowDeminiaturizedNotification: eventWindowMiniaturizedOrDeminiaturized(app, element, type) + case kAXTitleChangedNotification: eventWindowTitleChanged(app, element) default: return } } + +private func eventWindowDestroyed(_ app: App, _ element: AXUIElement) { + guard let existingIndex = Windows.list.firstIndexThatMatches(element) else { return } + Windows.list.remove(at: existingIndex) + guard Windows.list.count > 0 else { app.hideUi(); return } + Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(existingIndex) + app.refreshOpenUi() +} + +private func eventWindowMiniaturizedOrDeminiaturized(_ app: App, _ element: AXUIElement, _ type: String) { + guard let window = Windows.list.firstWindowThatMatches(element) else { return } + window.isMinimized = type == kAXWindowMiniaturizedNotification + // TODO: find a better way to get thumbnail of the new window (when AltTab is triggered min/demin animation) + window.refreshThumbnail() + app.refreshOpenUi() +} + +private func eventWindowTitleChanged(_ app: App, _ element: AXUIElement) { + guard element.isActualWindow(), + let window = Windows.list.firstWindowThatMatches(element), + let newTitle = window.axUiElement.title(), + newTitle != window.title else { return } + window.title = newTitle + app.refreshOpenUi() +} + + diff --git a/alt-tab-macos/ui/Cell.swift b/alt-tab-macos/ui/Cell.swift index 746e409f..30f826cd 100644 --- a/alt-tab-macos/ui/Cell.swift +++ b/alt-tab-macos/ui/Cell.swift @@ -11,7 +11,7 @@ class Cell: NSCollectionViewItem { var minimizedIcon = FontIcon(FontIcon.sfSymbolCircledMinusSign, Preferences.fontIconSize, .white) var hiddenIcon = FontIcon(FontIcon.sfSymbolCircledDotSign, Preferences.fontIconSize, .white) var spaceIcon = FontIcon(FontIcon.sfSymbolCircledNumber0, Preferences.fontIconSize, .white) - var openWindow: Window? + var window: Window? var mouseDownCallback: MouseDownCallback? var mouseMovedCallback: MouseMovedCallback? @@ -29,7 +29,7 @@ class Cell: NSCollectionViewItem { } override func mouseDown(with theEvent: NSEvent) { - mouseDownCallback!(openWindow!) + mouseDownCallback!(window!) } override var isSelected: Bool { @@ -40,7 +40,7 @@ class Cell: NSCollectionViewItem { } func updateWithNewContent(_ element: Window, _ mouseDownCallback: @escaping MouseDownCallback, _ mouseMovedCallback: @escaping MouseMovedCallback, _ screen: NSScreen) { - openWindow = element + window = element thumbnail.image = element.thumbnail let (width, height) = Cell.computeDownscaledSize(element.thumbnail, screen) thumbnail.image?.size = NSSize(width: width, height: height) @@ -51,8 +51,8 @@ class Cell: NSCollectionViewItem { label.string = element.title // workaround: setting string on NSTextView change the font (most likely a Cocoa bug) label.font = Preferences.font! - hiddenIcon.isHidden = !openWindow!.isHidden - minimizedIcon.isHidden = !openWindow!.isMinimized + hiddenIcon.isHidden = !window!.isHidden + minimizedIcon.isHidden = !window!.isMinimized spaceIcon.isHidden = element.spaceIndex == nil || Spaces.isSingleSpace || Preferences.hideSpaceNumberLabels if !spaceIcon.isHidden { spaceIcon.setNumber(UInt32(element.spaceIndex!)) @@ -92,11 +92,7 @@ class Cell: NSCollectionViewItem { private func makeHStackView() -> NSStackView { let hStackView = NSStackView() hStackView.spacing = Preferences.interItemPadding - hStackView.addView(appIcon, in: .leading) - hStackView.addView(label, in: .leading) - hStackView.addView(hiddenIcon, in: .leading) - hStackView.addView(minimizedIcon, in: .leading) - hStackView.addView(spaceIcon, in: .leading) + hStackView.setViews([appIcon, label, hiddenIcon, minimizedIcon, spaceIcon], in: .leading) return hStackView } @@ -110,8 +106,7 @@ class Cell: NSCollectionViewItem { vStackView.edgeInsets = NSEdgeInsets(top: Preferences.cellPadding, left: Preferences.cellPadding, bottom: Preferences.cellPadding, right: Preferences.cellPadding) vStackView.orientation = .vertical vStackView.spacing = Preferences.interItemPadding - vStackView.addView(hStackView, in: .leading) - vStackView.addView(thumbnail, in: .leading) + vStackView.setViews([hStackView, thumbnail], in: .leading) return vStackView } }