I have a macOS SwiftUI app with a secondary window that should be opened/displayed if a certain condition occurs. The logic that detects the condition is inside a Swift class.
I see lots of examples where opening a window is done inside another SwiftUI View using something like
struct ContentView: View {
@Environment(\.openWindow) var openWindow
var body: some View {
...
Button("Open window") {
openWindow(id: "secondWindowId")
}
}
In my case, the logic that detects the condition that would prompt the opening of secondWindowId is in a Swift class (not a SwiftUI View).
Is there a standard way to open the window identified with "secondWindowId" from inside a Swift class?
The basic way to do this to have the Swift class an ObservableObject, and have a @Published property there. When that property changes, you can observe that change event to do something in the SwiftUI view.
I tried this, but I could't figure out how to get the code to open an auxiliary window. I feel this would be a cleaner approach than my solution, but I couldn't figure it out.