How to set conditional properties on style in WindowGroup?

133 Views Asked by At

I'm working on a visionOS app which relies heavily on window groups. If you use window style plain, you can remove the glass background which I would like to do sometimes. Other times I would like the glass. The property window style is apparently a protocol and I can't use a ternary operator when trying to set these as it requires the enum type to be the same. How can I go about this?

I gave the code down below showing that if I use each one separately it works fine but if I try to tie it to a boolean it errors. I've included the error message next to each attempt.

WindowGroup {
    //Display the tutorial view.
    SetupView()
}
//This Works
.windowStyle(.plain)

//This also works
.windowStyle(.automatic)

//But they can't combine. What I have tried.
.windowStyle(showGlassBackground ? .plain : .automatic) //Member 'automatic' in 'PlainWindowStyle' produces result of type 'DefaultWindowStyle', but context expects 'PlainWindowStyle'
.windowStyle(showGlassBackground ? (.plain as WindowStyle) : (.automatic as WindowStyle) //Type 'any WindowStyle' cannot conform to 'WindowStyle'
.windowStyle(showGlassBackground ? (PlainWindowStyle.plain as any WindowStyle) : (DefaultWindowStyle.automatic as any WindowStyle)) //Value of type 'any WindowStyle' has no member 'windowStyle'
.windowStyle(showGlassBackground ? (PlainWindowStyle.plain as WindowStyle) : (DefaultWindowStyle.automatic as WindowStyle)) //Type 'any WindowStyle' cannot conform to 'WindowStyle'
.windowStyle(showGlassBackground ? WindowStyle(.plain) : WindowStyle(.automatic)) // 'any WindowStyle' cannot be constructed because it has no accessible initializers
1

There are 1 best solutions below

2
On

So I would still love to hear how this could be done, as like @Alexander said this is most likely a skill issue, but I found a way to get this to work the way I wanted. If you set the window style to plain, the glass window disappears but then you can put in glass effect separately. That can be ? into an always and never case that works for what I need.

WindowGroup {
    SetupView().glassBackgroundEffect(displayMode: showGlassBackground ? .always : .never)
}.windowStyle(.plain)