SwiftUI : tvOS, .contextMenu and .ButtonStyle not working together

Using tvOS

I am trying to have a contextmenu appear when the user long presses on a button.

If I don't use .buttonStyle() or use one of the built-in buttonStyles, the contextMenu appears.

However, I want to use a custom button style. When I do, the .contextMenu is ignored.

Here is my basic code :

import SwiftUI

struct TestButtonStyle: ButtonStyle {
    @Environment(\.isFocused) var focused: Bool
    @State private var isFocused: Bool = false

    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding()
            .frame(height: 50)
            .background(RoundedRectangle(cornerRadius: 20).fill(isFocused ? .red.opacity(0.75) : .clear))
            .onChange(of: focused) { hasFocus in
                if hasFocus {
                    isFocused = true
                } else {
                    isFocused = false
                }
            }
    }
}

struct ContentView: View {
    var body: some View {
        HStack {
            Button {
                print("Button 1 Pressed")
            } label: {
                Text("Button 1")
            }
            .buttonStyle(TestButtonStyle())
            .contextMenu {
                Button {
                    //
                } label: {
                    Text("Option 1")
                }
                Button {
                    //
                } label: {
                    Text("Option 2")
                }
            }

            Button {
                print("Button 2 Pressed")
            } label: {
                Text("Button 2")
            }
            .contextMenu {
                Button {
                    //
                } label: {
                    Text("Option 3")
                }
                Button {
                    //
                } label: {
                    Text("Option 4")
                }
            }
            .buttonStyle(TestButtonStyle())
        }
    }
}

Has anyone come across this and solved it? thanks.

Fur future reference, this has been fixed in tvOS 16 Beta 4.

Broken again in the released tvOS 16. I submitted feedback to apple.

I have found a solution to this issue

struct MoviesBlankButtonStyle: PrimitiveButtonStyle {
  @Environment(\.isFocused) var focused: Bool
  @State private var isFocused: Bool = false
   
  func makeBody(configuration: Configuration) -> some View {
    configuration.label
      .compositingGroup()
      .padding(.all, 9)
      .focusable(true, onFocusChange: { focused in
        if focused {
          isFocused = true
        } else {
          isFocused = false
        }
      })
      .background(RoundedRectangle(cornerRadius: 20).fill(isFocused ? .red : .clear).opacity(0.7 ))
      .foregroundColor(isFocused ? .white : .white)
      .onTapGesture(perform: configuration.trigger)
  }
}
SwiftUI : tvOS, .contextMenu and .ButtonStyle not working together
 
 
Q