SwiftUI: Double click on white space in a list row in macOS?

Viewed 249

I'm trying to get the entirety of a row to be double clickable.

Here is my code:

import SwiftUI

let colourArray = ["red","blue","green"]
struct ContentView: View
{
    var body: some View
    {
        List
        {
            ForEach (colourArray, id: \.self)
            { colour in
                ArrayRow(colour: colour)
                    .gesture(TapGesture(count: 2).onEnded
                     {
                        print("double clicked")
                     })
                .padding(.bottom, 15)
            }
        }
    }
}

struct ArrayRow: View
{
    let colour: String
    
    var body: some View
    {
        HStack(alignment: .top)
        {
            Text(colour)
                .frame(width: 150, alignment: .leading)
            Text(colour)
                .frame(width: 150, alignment: .leading)
            Text(colour)
                .frame(width: 150, alignment: .leading)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The double click works great, but only if you double click on the text. If you double click on the white space then nothing happens. How do I get the whole row to respond to the double click?

1 Answers

You can extend the tappable/clickable area with .contentShape

ArrayRow(colour: colour)
    .contentShape(Rectangle())
    .gesture(TapGesture(count: 2).onEnded
    {
       print("double clicked")
    })
    .padding(.bottom, 15)
           
Related