I'm getting this odd issue where focusedSceneValue doesn't work on macOS Monterey Beta 6, but adding searchable to one of the views fixes it.
Here's how to reproduce:
First paste this code into ContentView:
struct ContentView: View {
@State var search = ""
var body: some View {
NavigationView {
Text("First")
.focusedSceneValue(\.customAction) {
print("Pressed!")
}
Text("Second")
.searchable(text: $search) // Comment this line and focusedSceneValue breaks
}
}
}
struct ActionKey: FocusedValueKey {
typealias Value = () -> Void
}
extension FocusedValues {
var customAction: (() -> Void)? {
get { self[ActionKey.self] }
set { self[ActionKey.self] = newValue }
}
}
And then paste this into your app file:
struct CustomCommands: Commands {
@FocusedValue(\.customAction) var action
var body: some Commands {
CommandGroup(before: .newItem) {
Button(action: {
action?()
}) {
Text("Press me")
}
.disabled(action == nil)
}
}
}
And add this into the body of your Scene:
.commands {
CustomCommands()
}
This just adds a menu item to File, and the menu item is enabled only if a variable named action is not nil.
action is supposed to be assigned in the focusedSceneValue line in ContentView. This should always happen, as long as the ContentView is visible.
This code only works if I add the searchable modifier to one of the views however. If I don't add it, then the menu item is permanently disabled.
Is anybody else able to reproduce this?