Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 661
Vadim Avatar asked Oct 03 '19 10:10

Vadim


1 Answers

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!

like image 77
Vadim Avatar answered Sep 28 '22 23:09

Vadim