Skip to content

Instantly share code, notes, and snippets.

@phillipcaudell
Created September 22, 2023 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipcaudell/269e1d9abe1dea1709327fc95560760f to your computer and use it in GitHub Desktop.
Save phillipcaudell/269e1d9abe1dea1709327fc95560760f to your computer and use it in GitHub Desktop.
Setting a focused scene value whilst the List has a selection prevents the Menu in the toolbar from opening.
import SwiftUI
@main
struct NavigationSplitViewMenuApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
NavigationSplitView {
Sidebar()
} content: {
ContentList()
} detail: {
Text("Detail")
}
}
}
struct Sidebar: View {
@State private var selection = Set<Int>()
var body: some View {
// 1. Select an item in the list
List(selection: $selection) {
// 2. You can open me just fine
TestMenu()
ForEach(1...10, id: \.self) {
Text("Item \($0)")
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
// 3. However you can't open me
TestMenu()
}
}
// Setting a focused scene value whilst the List has a selection
// prevents the Menu in the toolbar from opening.
// This is a regression from iOS 16.4.
.focusedSceneValue(\.filterAction) {
// do something
}
}
}
struct ContentList: View {
var body: some View {
List {
ForEach(1...100, id: \.self) {
Text("Row \($0)")
}
}
}
}
struct TestMenu: View {
var body: some View {
Menu {
Text("Catch")
Text("Me")
Text("While")
Text("You")
Text("Can")
} label: {
Label("New", systemImage: "plus")
}
}
}
struct FilterActionKey: FocusedValueKey {
typealias Value = () -> Void
}
extension FocusedValues {
var filterAction: (() -> Void)? {
get { self[FilterActionKey.self] }
set { self[FilterActionKey.self] = newValue }
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment