I would like to make something like the following work:
import SwiftUI
@main
struct TestWindowApp: App {
init() {
_showWindow = .constant(false)
}
var body: some Scene {
MenuBarExtra {
Button("Show window") { showWindow = true }
} label: {
Text(">_<")
}
if showWindow {
Window("window", id: "main") {
Text("Hello, SwiftUI!")
.padding()
}
}
}
@Binding
var showWindow: Bool
}
This doesn’t compile.
I tried using WindowGroup
instead of Window
which compiles if the condition is in the WindowGroup
. I first found WindowGroup
to work unreliably when MenuBarExtra
is present, and when it works it shows an empty window when the showWindow
condition is not satisfied:
WindowGroup {
if showWindow {
Text("Hello, SwiftUI!")
.padding()
}
}
I’m quite confident I could get the desired behavior with creating an NSWindow
when showWindow
is true, but I would prefer not mixing AppKit
with SwiftUI
.