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.
@Environment(\.controlActiveState)
var windowState: ControlActiveState
This is an official solution. Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With