How to detect active appearance for NSView or for its parent NSWindow?

All native controls have different appearance when their parent window is active or inactive. How should we check this state in custom components e.g. while rendering a button cell?

We could inspect controlView.window’s properties like isMainWindow and isKeyWindow, but they don’t cover all cases. For instance, if you open one window of the app on the Desktop and another in a full-screen Space, only one of them can be key or main according to public APIs. However, standard controls seem to render them as active in both Spaces:

Please note how toolbar buttons in both Safari windows are rendered as active. How do we achieve the same behavior?


  • Fortunately, SwiftUI allows to inherit a new magic property from the Environment:

    /// Window state.
    var windowState: ControlActiveState

    This is an official solution. Cheers!