-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathOverlayWindowController.swift
More file actions
206 lines (184 loc) · 7.17 KB
/
Copy pathOverlayWindowController.swift
File metadata and controls
206 lines (184 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import AppKit
import DebounceFunction
import Foundation
import Perception
import XcodeInspector
@MainActor
public final class OverlayWindowController {
public typealias IDEWorkspaceWindowOverlayWindowControllerContentProviderFactory =
@MainActor @Sendable (
_ windowInspector: WorkspaceXcodeWindowInspector,
_ application: NSRunningApplication
) -> any IDEWorkspaceWindowOverlayWindowControllerContentProvider
static var ideWindowOverlayWindowControllerContentProviderFactories:
[IDEWorkspaceWindowOverlayWindowControllerContentProviderFactory] = []
var ideWindowOverlayWindowControllers =
[ObjectIdentifier: IDEWorkspaceWindowOverlayWindowController]()
var updateWindowStateTask: Task<Void, Error>?
let windowUpdateThrottler = ThrottleRunner(duration: 0.2)
lazy var fullscreenDetector = {
let it = NSWindow(
contentRect: .zero,
styleMask: .borderless,
backing: .buffered,
defer: false
)
it.isReleasedWhenClosed = false
it.isOpaque = false
it.backgroundColor = .clear
it.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .transient]
it.hasShadow = false
it.setIsVisible(false)
return it
}()
public init() {}
public func start() {
observeEvents()
_ = fullscreenDetector
}
public nonisolated static func registerIDEWorkspaceWindowOverlayWindowControllerContentProviderFactory(
_ factory: @escaping IDEWorkspaceWindowOverlayWindowControllerContentProviderFactory
) {
Task { @MainActor in
ideWindowOverlayWindowControllerContentProviderFactories.append(factory)
}
}
}
extension OverlayWindowController {
func observeEvents() {
observeWindowChange()
updateWindowStateTask = Task { [weak self] in
if let self { await handleSpaceChange() }
await withThrowingTaskGroup(of: Void.self) { [weak self] group in
// active space did change
_ = group.addTaskUnlessCancelled { [weak self] in
let sequence = NSWorkspace.shared.notificationCenter
.notifications(named: NSWorkspace.activeSpaceDidChangeNotification)
for await _ in sequence {
guard let self else { return }
try Task.checkCancellation()
await handleSpaceChange()
}
}
}
}
}
}
private extension OverlayWindowController {
func observeWindowChange() {
if ideWindowOverlayWindowControllers.isEmpty {
if let app = XcodeInspector.shared.activeXcode,
let windowInspector = XcodeInspector.shared
.focusedWindow as? WorkspaceXcodeWindowInspector
{
createNewIDEOverlayWindowController(
inspector: windowInspector,
application: app.runningApplication
)
}
}
withPerceptionTracking {
_ = XcodeInspector.shared.focusedWindow
_ = XcodeInspector.shared.activeXcode
_ = XcodeInspector.shared.activeApplication
} onChange: { [weak self] in
guard let self else { return }
Task { @MainActor in
defer { self.observeWindowChange() }
await self.windowUpdateThrottler.throttle { [weak self] in
await self?.handleOverlayStatusChange()
}
}
}
}
func createNewIDEOverlayWindowController(
inspector: WorkspaceXcodeWindowInspector,
application: NSRunningApplication
) {
let id = ObjectIdentifier(inspector)
let newController = IDEWorkspaceWindowOverlayWindowController(
inspector: inspector,
application: application,
contentProviderFactory: {
windowInspector, application in
OverlayWindowController.ideWindowOverlayWindowControllerContentProviderFactories
.map { $0(windowInspector, application) }
}
)
newController.access()
ideWindowOverlayWindowControllers[id] = newController
}
func removeIDEOverlayWindowController(for id: ObjectIdentifier) {
if let controller = ideWindowOverlayWindowControllers[id] {
controller.destroy()
}
ideWindowOverlayWindowControllers[id] = nil
}
func handleSpaceChange() async {
let windowInspector = XcodeInspector.shared.focusedWindow
guard let activeWindowController = {
if let windowInspector = windowInspector as? WorkspaceXcodeWindowInspector {
let id = ObjectIdentifier(windowInspector)
return ideWindowOverlayWindowControllers[id]
} else {
return nil
}
}() else { return }
let activeXcode = XcodeInspector.shared.activeXcode
let xcode = activeXcode?.appElement
let isXcodeActive = xcode?.isFrontmost ?? false
if isXcodeActive {
activeWindowController.maskPanel.moveToActiveSpace()
}
if fullscreenDetector.isOnActiveSpace, xcode?.focusedWindow != nil {
activeWindowController.maskPanel.orderFrontRegardless()
}
}
func handleOverlayStatusChange() {
guard XcodeInspector.shared.activeApplication?.isXcode ?? false else {
var closedControllers: [ObjectIdentifier] = []
for (id, controller) in ideWindowOverlayWindowControllers {
if controller.isWindowClosed {
controller.dim()
closedControllers.append(id)
} else {
controller.dim()
}
}
for id in closedControllers {
removeIDEOverlayWindowController(for: id)
}
return
}
guard let app = XcodeInspector.shared.activeXcode else {
for (_, controller) in ideWindowOverlayWindowControllers {
controller.hide()
}
return
}
let windowInspector = XcodeInspector.shared.focusedWindow
if let ideWindowInspector = windowInspector as? WorkspaceXcodeWindowInspector {
let objectID = ObjectIdentifier(ideWindowInspector)
// Workspace window is active
// Hide all controllers first
for (id, controller) in ideWindowOverlayWindowControllers {
if id != objectID {
controller.hide()
}
}
if let controller = ideWindowOverlayWindowControllers[objectID] {
controller.access()
} else {
createNewIDEOverlayWindowController(
inspector: ideWindowInspector,
application: app.runningApplication
)
}
} else {
// Not a workspace window, dim all controllers
for (_, controller) in ideWindowOverlayWindowControllers {
controller.dim()
}
}
}
}