It works on Monterey, but not work on Ventura.
struct ContentView: View {
@FocusState private var focus: FocusableElement?
@FocusedValue(\.focusedElement) var focusElement
var body: some View {
VStack(spacing: 30) {
HStack {
GroupBox {
Circle()
.focusable()
.focused($focus, equals: .circle)
.focusedValue(\.focusedElement, .circle)
.frame(width: 100, height: 100)
.padding()
Text(focus == .circle ? "Selected" : "Not selected")
}
.onTapGesture {
focus = .circle
}
GroupBox {
Rectangle()
.focusable()
.focused($focus, equals: .rectangle)
.focusedValue(\.focusedElement, .rectangle)
.frame(width: 100, height: 100)
.padding()
Text(focus == .rectangle ? "Selected" : "Not selected")
}
}
.onTapGesture {
focus = .rectangle
}
Text("Focused Element: \(focusElement?.rawValue ?? "None")")
}
.padding()
.frame(width: 500, height: 300)
}
}
enum FocusableElement: Equatable, Hashable {
case rectangle
case circle
}
enum Selection: String, Hashable {
case none
case rectangle
case circle
}
extension FocusedValues {
struct FocusedElement: FocusedValueKey {
typealias Value = Selection
}
var focusedElement: FocusedElement.Value? {
get { self[FocusedElement.self] }
set {
self[FocusedElement.self] = newValue
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}