How to find out if there is a full-screen application running on a specific NSScreen

In Cocoa / AppKit, from the screen [NSScreen screens], how can I find out if there is a full-screen application running on that particular screen? Mostly I'm interested in applications that use Cocoa APIs for full-screen mode, but if there is a solution that also includes other types of full-screen applications, it's even better. The solution should be able to pass Mac App Store approval.

My specific use case involves applying a menu bar ( NSStatusItem) and figuring out whether the [NSScreen mainScreen]menu is generally displayed on [NSScreen mainScreen], so that the global key combination can display either the position of the pop-up window on the status element (if it is visible) or a floating window if there is no visible state element.

They themselves NSScreensdo not provide any information about windows / applications, and NSRunningApplicationalso does not disclose this information.

Perhaps there is a Carbon API to find out? For example, if I have a list of windows, I could go through them and see if any of the window frames exactly fit the screen frame. On the other hand, there may be applications that have a similar structure, but work under other applications (for example, the Backdrop application, https://itunes.apple.com/us/app/backdrop/id411461952?mt=12 ), so the approach is, like this, would have to consider window levels.

+5
source share
2 answers

You can try the CGWindowList API, for example CGWindowListCopyWindowInfo().

, , -[NSApplication currentSystemPresentationOptions] NSApplicationPresentationAutoHideMenuBar NSApplicationPresentationHideMenuBar. , Cocoa (NSApplicationPresentationFullScreen).

0

, CGWindowListCopyWindowInfo, :

- (BOOL)fullScreenAppPresentOn:(NSScreen *)screen
{
    // Get all of the visible windows (across all running applications)
    NSArray<NSDictionary*> *windowInfoList = (__bridge_transfer id)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

    // For each window, see if the bounds are the same size as the screen frame
    for (int windowInfoListIndex = 0; windowInfoListIndex < (int)windowsInfoList.count; windowInfoListIndex++)
    {    
        NSDictionary *windowInfo = windowInfoList[windowInfoListIndex];

        CFDictionaryRef windowInfoRef = (__bridge CFDictionaryRef) windowInfo[(__bridge NSString *)kCGWindowBounds];
        CGRect windowBounds;
        CGRectMakeWithDictionaryRepresentation(windowInfoRef, &windowBounds);

        if (CGRectEqualToRect([screen frame], windowBounds))
        {
            return YES;
        }
    }

    return NO;
}
0

Source: https://habr.com/ru/post/1541869/


All Articles