16QList<QCapturableWindow> QCGCapturableWindows::windows()
const
18 QList<QCapturableWindow> result;
19 QCFType<CFArrayRef> windowList(
20 CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID));
23 CFIndex count = CFArrayGetCount(windowList);
24 for (CFIndex i = 0; i < count; ++i) {
25 auto windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windowList, i);
26 auto windowNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowNumber);
27 auto windowName = (CFStringRef)CFDictionaryGetValue(windowInfo, kCGWindowName);
29 CGWindowID windowId = 0;
30 static_assert(
sizeof(windowId) == 4,
31 "CGWindowID size is not compatible with kCFNumberSInt32Type");
32 CFNumberGetValue(windowNumber, kCFNumberSInt32Type, &windowId);
34 QString windowDescription;
36 windowDescription = QString::fromCFString(windowName);
38 result.push_back(QCapturableWindowPrivate::create(
39 static_cast<QCapturableWindowPrivate::Id>(windowId),
40 std::move(windowDescription)));
46bool QCGCapturableWindows::isWindowValid(
const QCapturableWindowPrivate &window)
const
48 QCFType<CFArrayRef> windowList(
49 CGWindowListCreate(kCGWindowListOptionIncludingWindow, window.id));
50 return CFArrayGetCount(windowList) > 0;
53q23::expected<QCapturableWindow, QString> QCGCapturableWindows::fromQWindow(QWindow *window)
const
55 auto* nsView =
reinterpret_cast<NSView*>(window->winId());
57 NSWindow* nsWindow = [nsView window];
58 if (nsWindow ==
nullptr)
59 return q23::unexpected{ QStringLiteral(
"NSView had no associated NSWindow") };
61 const auto cgWindowId = (CGWindowID)[nsWindow windowNumber];
62 if (cgWindowId == kCGNullWindowID)
63 return q23::unexpected{ QStringLiteral(
"NSWindow has no CGWindowID") };
65 return QCapturableWindowPrivate::create(
66 static_cast<QCapturableWindowPrivate::Id>(cgWindowId),