I'm building a sidebar that behaves the same as in the macOS Notes app. I'd like that a list item gets selected when right-clicking it.
I've been looking into events and gestures and couldn't find anything for a right-click. It seems like they're all built for iOS, not macOS, though something surely exists if it's in the Notes app. Does anybody know how it's done?
Below is my sidebar view. The view just has a basic structure to display a list in a sidebar. Currently, when right-clicking an item, a context menu is being displayed, though the menu item isn't getting selected.
I've been looking into events and gestures and couldn't find anything for a right-click. It seems like they're all built for iOS, not macOS, though something surely exists if it's in the Notes app. Does anybody know how it's done?
Below is my sidebar view. The view just has a basic structure to display a list in a sidebar. Currently, when right-clicking an item, a context menu is being displayed, though the menu item isn't getting selected.
Code Block struct Sidebar: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest(entity: Element.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Element.title, ascending: false)]) private var elements: FetchedResults<Element> @AppStorage("SelectedElementId") var selectedElementId: String? var body: some View { VStack (alignment: .leading) { List (selection: self.$selectedElementId){ ForEach(self.elements) { element in NavigationLink(destination: ContentView(), tag: element.id!, selection: self.$selectedElementId) { Text(element.title!) } .contextMenu { Button(action: {}) { Text("Delete") } } } }.listStyle(SidebarListStyle()) Button(action: addElement) { HStack { Text("Add element") } } } } private func addElement() { let element = Element(context: viewContext) element.id = UUID().uuidString element.title = "My title" self.selectedElementId = element.id do { try viewContext.save() } catch { let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } } }