Ariver
2026-06-06 5c054407508d90811f2498522d4511c520b10c4c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import Foundation
import AlignerCore
 
protocol QuickSwitchSnapshotLoading: Sendable {
    func snapshot() throws -> QuickSwitchSnapshotLoad
}
 
struct QuickSwitchSnapshotLoad: Sendable {
    let snapshot: QuickSwitchSnapshot
    let currentSpaceIDs: Set<UInt64>
}
 
enum FixtureQuickSwitchMode: Sendable {
    case layout
    case candidateFiltering
    case activation
}
 
final class LiveQuickSwitchSnapshotLoader: QuickSwitchSnapshotLoading {
    func snapshot() throws -> QuickSwitchSnapshotLoad {
        let spaceEnumerator = try SkyLightSpaceEnumerator()
        let displays = try spaceEnumerator.displays()
        let currentSpaceIDs = try spaceEnumerator.currentSpaceIDs()
        let fullscreenSpaceIDs = Set(
            displays
                .flatMap(\.spaces)
                .filter { $0.type == .fullscreen }
                .map(\.id)
        )
        let windowService = CGWindowAXWindowService(
            spaceIDsByWindowIDProvider: spaceEnumerator.spaceIDsByWindowID(windowIDs:),
            fullscreenSpaceIDs: fullscreenSpaceIDs
        )
        let windows = try windowService.allWindows()
 
        return QuickSwitchSnapshotLoad(
            snapshot: QuickSwitchSnapshotBuilder.snapshot(displays: displays, windows: windows),
            currentSpaceIDs: currentSpaceIDs
        )
    }
}
 
final class DelayedQuickSwitchSnapshotLoader: QuickSwitchSnapshotLoading {
    private let base: any QuickSwitchSnapshotLoading
    private let delay: TimeInterval
 
    init(base: any QuickSwitchSnapshotLoading, delay: TimeInterval) {
        self.base = base
        self.delay = delay
    }
 
    func snapshot() throws -> QuickSwitchSnapshotLoad {
        if delay > 0 {
            Thread.sleep(forTimeInterval: delay)
        }
 
        return try base.snapshot()
    }
}
 
final class FixtureQuickSwitchSnapshotLoader: QuickSwitchSnapshotLoading {
    private let appCount: Int
    private let windowsPerApp: Int
    private let mode: FixtureQuickSwitchMode
 
    init(appCount: Int, windowsPerApp: Int = 1, mode: FixtureQuickSwitchMode = .layout) {
        self.appCount = max(1, appCount)
        self.windowsPerApp = max(1, windowsPerApp)
        self.mode = mode
    }
 
    func snapshot() throws -> QuickSwitchSnapshotLoad {
        if mode == .candidateFiltering {
            return candidateFilteringSnapshot()
        }
        if mode == .activation {
            return activationSnapshot()
        }
 
        return layoutSnapshot()
    }
 
    private func layoutSnapshot() -> QuickSwitchSnapshotLoad {
        let displayUUID = "fixture-display-a"
        let displays = [
            AlignerDisplay(
                uuid: displayUUID,
                physical: true,
                spaces: [
                    AlignerSpace(id: 1, type: .user, displayUUID: displayUUID, index: 1),
                    AlignerSpace(id: 2, type: .fullscreen, displayUUID: displayUUID, index: 2),
                    AlignerSpace(id: 3, type: .user, displayUUID: displayUUID, index: 3)
                ]
            )
        ]
        let windows = (0..<appCount).flatMap { index in
            let app = AlignerApp(
                bundleIdentifier: "com.example.fixture.\(String(format: "%03d", index))",
                name: "Fixture App \(String(format: "%03d", index))",
                category: .generic,
                processIdentifier: Int32(20_000 + index)
            )
 
            return (0..<windowsPerApp).map { windowIndex in
                let spaceSeed = index + windowIndex
                let spaceID = spaceSeed % 5 == 0 ? UInt64(2) : UInt64(spaceSeed % 2 == 0 ? 1 : 3)
                return AlignerWindow(
                    id: UInt32(10_000 + index * 100 + windowIndex),
                    app: app,
                    title: "Fixture Window \(String(format: "%03d", index))-\(String(format: "%02d", windowIndex))",
                    isFullscreen: spaceID == 2,
                    spaceIDs: [spaceID]
                )
            }
        }
 
        return QuickSwitchSnapshotLoad(
            snapshot: QuickSwitchSnapshotBuilder.snapshot(displays: displays, windows: windows),
            currentSpaceIDs: [2]
        )
    }
 
    private func candidateFilteringSnapshot() -> QuickSwitchSnapshotLoad {
        let displayUUID = "fixture-display-a"
        let displays = [
            AlignerDisplay(
                uuid: displayUUID,
                physical: true,
                spaces: [
                    AlignerSpace(id: 1, type: .user, displayUUID: displayUUID, index: 1),
                    AlignerSpace(id: 2, type: .fullscreen, displayUUID: displayUUID, index: 2)
                ]
            )
        ]
 
        let app = AlignerApp(
            bundleIdentifier: "com.example.candidate.standard",
            name: "Candidate Standard",
            category: .documentNotes,
            processIdentifier: 30_001
        )
        let hiddenApp = AlignerApp(
            bundleIdentifier: "com.example.candidate.hidden",
            name: "Candidate Hidden",
            category: .generic,
            isHidden: true,
            processIdentifier: 30_002
        )
        let systemApp = AlignerApp(
            bundleIdentifier: "com.apple.SecurityAgent",
            name: "SecurityAgent",
            category: .generic,
            processIdentifier: 30_003
        )
        let finderApp = AlignerApp(
            bundleIdentifier: "com.apple.finder",
            name: "Finder",
            category: .finder,
            processIdentifier: 30_004
        )
 
        let records = [
            WindowEnumerationRecord(
                id: 20_001,
                app: app,
                title: "Standard Candidate",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                hasAXBacking: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_002,
                app: app,
                title: "Minimized Candidate",
                identifierSource: .syntheticAX,
                size: .zero,
                subrole: .standard,
                isMinimized: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_003,
                app: app,
                title: "Fullscreen Candidate",
                size: .zero,
                subrole: .standard,
                isFullscreen: true,
                spaceIDs: [2]
            ),
            WindowEnumerationRecord(
                id: 20_004,
                app: app,
                title: "Sheet Candidate",
                size: CGSize(width: 520, height: 320),
                subrole: .sheet,
                hasAXBacking: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_005,
                app: app,
                title: "Dialog Candidate",
                size: CGSize(width: 560, height: 360),
                subrole: .dialog,
                hasAXBacking: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_006,
                app: app,
                title: "",
                size: CGSize(width: 840, height: 540),
                subrole: .standard,
                hasAXBacking: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_007,
                app: app,
                activationPolicy: .accessory,
                title: "Accessory Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_008,
                app: hiddenApp,
                title: "Hidden Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_009,
                app: app,
                title: "Tiny Rejected",
                size: CGSize(width: 80, height: 40),
                subrole: .standard,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_010,
                app: app,
                title: "Ghost Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                isGhost: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_011,
                app: app,
                title: "Popover Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .unknown("AXPopover"),
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_012,
                app: finderApp,
                title: "Desktop",
                size: CGSize(width: 1710, height: 1112),
                subrole: .standard,
                isDesktopElement: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_013,
                app: systemApp,
                title: "Security Confirmation",
                size: CGSize(width: 620, height: 420),
                subrole: .dialog,
                isSystemCritical: true,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_014,
                app: app,
                title: "Noninteractive Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                isInteractable: false,
                spaceIDs: [1]
            ),
            WindowEnumerationRecord(
                id: 20_015,
                app: app,
                title: "CG-only Offscreen Rejected",
                size: CGSize(width: 900, height: 600),
                subrole: .standard,
                hasAXBacking: false,
                isOnscreen: false,
                spaceIDs: [1]
            )
        ]
        let windows = records.compactMap(WindowEnumerationPolicy.window)
 
        return QuickSwitchSnapshotLoad(
            snapshot: QuickSwitchSnapshotBuilder.snapshot(displays: displays, windows: windows),
            currentSpaceIDs: [1]
        )
    }
 
    private func activationSnapshot() -> QuickSwitchSnapshotLoad {
        let displayUUID = "fixture-display-a"
        let displays = [
            AlignerDisplay(
                uuid: displayUUID,
                physical: true,
                spaces: [
                    AlignerSpace(id: 1, type: .user, displayUUID: displayUUID, index: 1),
                    AlignerSpace(id: 2, type: .fullscreen, displayUUID: displayUUID, index: 2)
                ]
            )
        ]
 
        let normalApp = AlignerApp(
            bundleIdentifier: "com.example.activation.000",
            name: "Activation App 000",
            category: .generic,
            processIdentifier: 40_000
        )
        let minimizedApp = AlignerApp(
            bundleIdentifier: "com.example.activation.001",
            name: "Activation App 001",
            category: .generic,
            processIdentifier: 40_001
        )
        let windows = [
            AlignerWindow(
                id: 40_100,
                app: normalApp,
                title: "Normal Activation Window",
                isMinimized: false,
                spaceIDs: [1]
            ),
            AlignerWindow(
                id: 40_110,
                app: normalApp,
                title: "App Activated Only Window",
                isMinimized: false,
                spaceIDs: [1]
            ),
            AlignerWindow(
                id: 40_200,
                app: minimizedApp,
                title: "Minimized Activation Window",
                identifierSource: .syntheticAX,
                isMinimized: true,
                spaceIDs: [1]
            )
        ]
 
        return QuickSwitchSnapshotLoad(
            snapshot: QuickSwitchSnapshotBuilder.snapshot(displays: displays, windows: windows),
            currentSpaceIDs: [1]
        )
    }
}