Given a View
in SwiftUI for macOS, how can I tell if that view is hidden either because it, or any of its ancestor's opacity
is 0.0 or the .hidden
modifier has been applied?
Presumably I can manually do this with an Environment
value on the ancestor view, but I'm curious if this can be done more idiomatically.
An example use case:
I have views that run long-running Tasks
via the .task(id:)
modifier. These tasks only need to be running if the View
itself is visible to the user.
When the View is hidden, the task should stop. When the View reappears, the Task should restart. This happens automatically when Views are created and destroyed, but does not happen when a view is only hidden.