Mission Control Turbo: macOS multitasking turbocharged
1import AppKit
2import ScreenCaptureKit
3
4/// Captures window thumbnails using ScreenCaptureKit.
5enum ThumbnailCapture {
6 /// Max thumbnail dimension — large enough for sharp display at overlay sizes,
7 /// small enough for fast capture. 800px wide covers most overlay thumbnail widths at 2x.
8 private static let maxDimension: Int = 800
9
10 /// Pre-warm ScreenCaptureKit so the first real capture is fast.
11 static func warmUp() async {
12 _ = try? await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)
13 }
14
15 /// Capture thumbnails for all windows in parallel.
16 static func captureThumbnails(for windows: inout [WindowInfo]) async {
17 let t0 = CFAbsoluteTimeGetCurrent()
18 guard let content = try? await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true) else {
19 return
20 }
21 let t1 = CFAbsoluteTimeGetCurrent()
22
23 // Build a lookup from windowID to SCWindow
24 var scWindowsByID: [CGWindowID: SCWindow] = [:]
25 for scWindow in content.windows {
26 scWindowsByID[scWindow.windowID] = scWindow
27 }
28
29 // Capture all windows in parallel
30 let captures: [(Int, CGImage)] = await withTaskGroup(of: (Int, CGImage?).self) { group in
31 for i in windows.indices {
32 guard let scWindow = scWindowsByID[windows[i].windowID] else { continue }
33 let windowFrame = windows[i].frame
34 let idx = i
35
36 group.addTask {
37 let filter = SCContentFilter(desktopIndependentWindow: scWindow)
38 let config = SCStreamConfiguration()
39
40 // Scale down to maxDimension while preserving aspect ratio
41 let aspect = windowFrame.width / max(windowFrame.height, 1)
42 if aspect >= 1 {
43 config.width = maxDimension
44 config.height = max(1, Int(Double(maxDimension) / aspect))
45 } else {
46 config.height = maxDimension
47 config.width = max(1, Int(Double(maxDimension) * aspect))
48 }
49 config.scalesToFit = true
50 config.showsCursor = false
51
52 let image = try? await SCScreenshotManager.captureImage(
53 contentFilter: filter,
54 configuration: config
55 )
56 return (idx, image)
57 }
58 }
59
60 var results: [(Int, CGImage)] = []
61 for await (idx, image) in group {
62 if let image = image {
63 results.append((idx, image))
64 }
65 }
66 return results
67 }
68
69 for (idx, image) in captures {
70 windows[idx].thumbnail = image
71 }
72
73 let t2 = CFAbsoluteTimeGetCurrent()
74 mctLog("[MCT] capture: shareable=%.0fms parallel=%.0fms total=%.0fms (%d windows)",
75 (t1 - t0) * 1000, (t2 - t1) * 1000, (t2 - t0) * 1000, captures.count)
76 }
77}