focusedSceneValue(_:_:)
Modifies this view by injecting a value that you provide for use by other views whose state depends on the focused scene.
Declaration
nonisolated func focusedSceneValue<T>(_ keyPath: WritableKeyPath<FocusedValues, T?>, _ value: T) -> some View
Parameters
- keyPath:
The key path to associate
valuewith when adding it to the existing table of published focus values. - value:
The focus value to publish.
Return Value
A modified representation of this view.
Discussion
Use this method instead of focusedValue(_:_:) for values that must be visible regardless of where focus is located in the active scene. For example, if an app needs a command for moving focus to a particular text field in the sidebar, it could use this modifier to publish a button action that’s visible to command views as long as the scene is active, and regardless of where focus happens to be in it.
struct Sidebar: View {
@FocusState var isFiltering: Bool
var body: some View {
VStack {
TextField(...)
.focused(when: $isFiltering)
.focusedSceneValue(\.filterAction) {
isFiltering = true
}
}
}
}
struct NavigationCommands: Commands {
@FocusedValue(\.filterAction) var filterAction
var body: some Commands {
CommandMenu("Navigate") {
Button("Filter in Sidebar") {
filterAction?()
}
}
.disabled(filterAction == nil)
}
}
extension FocusedValues {
@Entry var filterAction: (() -> Void)?
}