Ben Jones
Ben Jones

Reputation: 957

Get SCWindow from SCContentSharingPicker filter

I'd like to keep track of which windows I've captured using ScreenCaptureKit (so users can quickly swap through their history) and I'm using the SCContentSharingPicker.

From what I can tell there seems to be no way to get information about the window (the SCWindow or equivalent) that's accessible via the filter passed to contentSharingPicker(_ picker: SCContentSharingPicker, didUpdateWith filter: SCContentFilter, for stream: SCStream?).

In the debugger I can see the the filter has a (I assume private) member called _individualwindow which contains the SCWindow but I couldn't find a way to access it.

info(for filter: SCContentFilter) -> SCShareableContentInfo doesn't seem to return anything useful (only the size of the recorded content)

Any suggestions for how to access the SCWindow? The only other idea I have is to use one of the getExcludingDesktopWindows methods and try to match what's in the filter somehow?

Upvotes: 0

Views: 150

Answers (1)

Ben Jones
Ben Jones

Reputation: 957

The best answer I could come up with was to list all shareable windows and assume that the any (hopefully only one) window the same size as the filter was the one I wanted

private func getCurrentlySharedWindow(size: CGSize) async -> [SCWindow] {
    do {
        
        //Try to figure out window is about to get swapped out
        let allContent = try await SCShareableContent.excludingDesktopWindows(true, onScreenWindowsOnly: false)
        let allWindows = allContent.windows
        let matchingWindows = allWindows.filter {window in window.isActive && window.frame.size == size}
        return matchingWindows
        
    } catch {
        Logger().debug("failed figuring out what the old window was: \(error)")
        return []
    }
}

// hopefully this returns just one window:
let matchingWindows = await getCurrentlySharedWindow(size: filter.contentRect.size)

Upvotes: 0

Related Questions