Hi,
I've been working quite a lot on a pretty big SwiftUI App, which will actively support multiple windows. I am looking to use the commands feature new in macOS 11, like this for example:
Now the action obviously needs to be propagated. I would work with environment objects on every window here which basically hold a CommandState, specifying if a command is currently available and which action to take on it.
Every window would have one of those and when you switch windows the global state would update to be the one of the current window.
Now the question is: How do I do that? As far as I can see, there is no way in SwiftUI to know which window is in the foreground, there is a scenePhase environment variable, but it is active for both windows when I create two. There is also an isFocused environment variable, but in my testing it never changes and I also do not really get what it would do since, in my opinion, the documentation is not very clear on that.
Any options I haven't considered/other ideas?
Thanks in advance,
Jann
I've been working quite a lot on a pretty big SwiftUI App, which will actively support multiple windows. I am looking to use the commands feature new in macOS 11, like this for example:
Code Block Swift WindowGroup { WindowView() } .commands { CommandMenu("Menu") { Button(action: { print("test") }) { Text("Action") } } }
Now the action obviously needs to be propagated. I would work with environment objects on every window here which basically hold a CommandState, specifying if a command is currently available and which action to take on it.
Every window would have one of those and when you switch windows the global state would update to be the one of the current window.
Now the question is: How do I do that? As far as I can see, there is no way in SwiftUI to know which window is in the foreground, there is a scenePhase environment variable, but it is active for both windows when I create two. There is also an isFocused environment variable, but in my testing it never changes and I also do not really get what it would do since, in my opinion, the documentation is not very clear on that.
Any options I haven't considered/other ideas?
Thanks in advance,
Jann