|
| 1 | +import AppKit |
| 2 | +import AXExtension |
| 3 | +import AXNotificationStream |
| 4 | +import Foundation |
| 5 | +import SwiftUI |
| 6 | +import XcodeInspector |
| 7 | + |
| 8 | +public protocol IDEWorkspaceWindowOverlayWindowControllerContentProvider { |
| 9 | + associatedtype Content: View |
| 10 | + func createWindow() -> NSWindow? |
| 11 | + func createContent() -> Content |
| 12 | + |
| 13 | + init(windowInspector: WorkspaceXcodeWindowInspector, application: NSRunningApplication) |
| 14 | +} |
| 15 | + |
| 16 | +extension IDEWorkspaceWindowOverlayWindowControllerContentProvider { |
| 17 | + var contentBody: AnyView { |
| 18 | + AnyView(createContent()) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +@MainActor |
| 23 | +final class IDEWorkspaceWindowOverlayWindowController { |
| 24 | + private var lastAccessDate: Date = .init() |
| 25 | + let application: NSRunningApplication |
| 26 | + let inspector: WorkspaceXcodeWindowInspector |
| 27 | + let contentProviders: [any IDEWorkspaceWindowOverlayWindowControllerContentProvider] |
| 28 | + private var isDestroyed: Bool = false |
| 29 | + private let maskPanel: NSPanel |
| 30 | + private var axNotificationTask: Task<Void, Never>? |
| 31 | + |
| 32 | + init( |
| 33 | + inspector: WorkspaceXcodeWindowInspector, |
| 34 | + application: NSRunningApplication, |
| 35 | + contentProviderFactory: ( |
| 36 | + _ windowInspector: WorkspaceXcodeWindowInspector, _ application: NSRunningApplication |
| 37 | + ) -> [any IDEWorkspaceWindowOverlayWindowControllerContentProvider] |
| 38 | + ) { |
| 39 | + self.inspector = inspector |
| 40 | + self.application = application |
| 41 | + contentProviders = contentProviderFactory(inspector, application) |
| 42 | + |
| 43 | + // Create the invisible panel |
| 44 | + let panel = NSPanel( |
| 45 | + contentRect: .zero, |
| 46 | + styleMask: [.borderless], |
| 47 | + backing: .buffered, |
| 48 | + defer: false |
| 49 | + ) |
| 50 | + panel.isReleasedWhenClosed = false |
| 51 | + panel.isOpaque = false |
| 52 | + panel.backgroundColor = .clear |
| 53 | + panel.hasShadow = false |
| 54 | + panel.ignoresMouseEvents = true |
| 55 | + panel.alphaValue = 0 |
| 56 | + panel.collectionBehavior = [.canJoinAllSpaces, .transient] |
| 57 | + panel.level = widgetLevel(0) |
| 58 | + panel.setIsVisible(true) |
| 59 | + maskPanel = panel |
| 60 | + |
| 61 | + panel.contentView = NSHostingView( |
| 62 | + rootView: ZStack { |
| 63 | + ForEach(0..<contentProviders.count, id: \.self) { (index: Int) in |
| 64 | + self.contentProviders[index].contentBody |
| 65 | + } |
| 66 | + } |
| 67 | + .allowsHitTesting(false) |
| 68 | + ) |
| 69 | + |
| 70 | + for contentProvider in contentProviders { |
| 71 | + if let window = contentProvider.createWindow() { |
| 72 | + panel.addChildWindow(window, ordered: .above) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Listen to AX notifications for window move/resize |
| 77 | + let windowElement = inspector.uiElement |
| 78 | + let stream = AXNotificationStream( |
| 79 | + app: application, |
| 80 | + element: windowElement, |
| 81 | + notificationNames: kAXMovedNotification, kAXResizedNotification |
| 82 | + ) |
| 83 | + |
| 84 | + axNotificationTask = Task { [weak self] in |
| 85 | + for await notification in stream { |
| 86 | + guard let self else { return } |
| 87 | + switch notification.name { |
| 88 | + case kAXMovedNotification, kAXResizedNotification: |
| 89 | + if let rect = windowElement.rect { |
| 90 | + self.maskPanel.setFrame(rect, display: false) |
| 91 | + } |
| 92 | + default: continue |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if let rect = windowElement.rect { |
| 98 | + maskPanel.setFrame(rect, display: false) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + deinit { |
| 103 | + axNotificationTask?.cancel() |
| 104 | + _ = withExtendedLifetime(self) { |
| 105 | + Task { @MainActor in |
| 106 | + precondition( |
| 107 | + !self.isDestroyed, |
| 108 | + "IDEWorkspaceWindowOverlayWindowController should be destroyed before deinit" |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// Make the window the top most window and visible. |
| 115 | + func access() { |
| 116 | + lastAccessDate = Date() |
| 117 | + maskPanel.level = widgetLevel(0) |
| 118 | + maskPanel.setIsVisible(true) |
| 119 | + maskPanel.orderFrontRegardless() |
| 120 | + } |
| 121 | + |
| 122 | + /// Stop keeping the window the top most window, do not change visibility. |
| 123 | + func dim() { |
| 124 | + maskPanel.level = .normal |
| 125 | + } |
| 126 | + |
| 127 | + /// Hide the window. |
| 128 | + func hide() { |
| 129 | + maskPanel.setIsVisible(false) |
| 130 | + maskPanel.level = .normal |
| 131 | + } |
| 132 | + |
| 133 | + /// Destroy the controller and clean up resources. |
| 134 | + func destroy() { |
| 135 | + axNotificationTask?.cancel() |
| 136 | + maskPanel.close() |
| 137 | + isDestroyed = true |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func widgetLevel(_ addition: Int) -> NSWindow.Level { |
| 142 | + let minimumWidgetLevel: Int |
| 143 | + #if DEBUG |
| 144 | + minimumWidgetLevel = NSWindow.Level.floating.rawValue + 1 |
| 145 | + #else |
| 146 | + minimumWidgetLevel = NSWindow.Level.floating.rawValue |
| 147 | + #endif |
| 148 | + return .init(minimumWidgetLevel + addition) |
| 149 | +} |
| 150 | + |
0 commit comments