2
votes

Can anyone think how to call an action when double clicking a NavigationLink in a List in MacOS? I've tried adding onTapGesture(count:2) but it does not have the desired effect and overrides the ability of the link to be selected reliably.

var body: some View {
    NavigationView {
        List {
            ForEach(items) { item in
                NavigationLink(destination: Item(itemDetail: item)) {
                    ItemRow(itemRow: item) //<-my row view
                }.buttonStyle(PlainButtonStyle())
                 .simultaneousGesture(TapGesture(count:2)
                 .onEnded {
                     print("double tap")
                })
            }
        }
    }
}

EDIT:

I've set up a tag/selection in the NavigationLink and can now double or single click the content of the row. The only trouble is, although the itemDetail view is shown, the "active" state with the accent does not appear on the link. Is there a way to either set the active state (highlighted state) or extend the NavigationLink functionality to accept double tap as well as a single?

@State var selection:String?
var body: some View {
    NavigationView {
        List {
            ForEach(items) { item in
                NavigationLink(destination: Item(itemDetail: item), tag: item.id, selection: self.$selection) {
                    ItemRow(itemRow: item) //<-my row view
                }.onTapGesture(count:2) { //<- Needed to be first!
                    print("doubletap")
                }.onTapGesture(count:1) {
                    self.selection = item.id
                }
            }
        }
    }
}
2

2 Answers

0
votes

Use simultaneous gesture, like below (tested with Xcode 11.4 / macOS 10.15.5)

NavigationLink(destination: Text("View One")) {
    Text("ONE")
}
.buttonStyle(PlainButtonStyle())        // << required !!
.simultaneousGesture(TapGesture(count: 2)
    .onEnded { print(">> double tap")})

or .highPriorityGesture(... if you need double-tap has higher priority

0
votes

Looking for a similar solution I tried @asperi answer, but had the same issue with tappable areas as the original poster. After trying many variations the following is working for me:

@State var selection: String?
...
NavigationLink(destination: HistoryListView(branch: string), tag: string, selection: self.$selection) {
  Text(string)
    .gesture(
      TapGesture(count:1)
        .onEnded({
          print("Tap Single")
          selection = string
        })
    )
    .highPriorityGesture(
      TapGesture(count:2)
        .onEnded({
          print("Tap Double")
        })
    )
}