ContextMenu of List stops working when applying custom DisclosureGroupStyle in macOS

Hi there

Issue

I've implemented a custom DisclosureGroupStyle for a List, which shows hierarchical data. However, it seems that when applying this (or any other) custom DisclosureGroupStyle in macOS the context menu stops working, except on the lowest level. In iOS, on the other hand, the context menu keeps working on all levels.

Is this excepted behaviour?

Example


    var body: some View {

        List(categories, id: \.value, children: \.children) { tree in

            Text(tree.value).font(.subheadline)

                .contextMenu{

                    Button("Test") {}

                }

        }

        .disclosureGroupStyle(SideBarDisclosureGroupStyle())

        .padding(50)

    }

}



struct SideBarDisclosureGroupStyle: DisclosureGroupStyle {

    func makeBody(configuration: Configuration) -> some View {

        HStack(alignment: .firstTextBaseline) {

            configuration.label

            Spacer()

            Image(systemName:"chevron.\(configuration.isExpanded ? "down" : "right").circle")

                .imageScale(.large)

                .foregroundStyle(.secondary)

                .onTapGesture() {

                    configuration.isExpanded.toggle()

                }

        }

        .contentShape(Rectangle())

        

        if configuration.isExpanded {

            configuration.content

                .disclosureGroupStyle(self)

        }

    }

}





// Example Data (Source: https://swiftwithmajid.com/2020/09/02/displaying-recursive-data-using-outlinegroup-in-swiftui/)

struct Tree<Value: Hashable>: Hashable {

    let value: Value

    var children: [Tree]? = nil

}



let categories: [Tree<String>] = [

    .init(

        value: "Clothing",

        children: [

            .init(value: "Hoodies"),

            .init(value: "Jackets"),

            .init(value: "Joggers"),

            .init(value: "Jumpers"),

            .init(

                value: "Jeans",

                children: [

                    .init(value: "Regular"),

                    .init(value: "Slim")

                ]

            ),

        ]

    ),

    .init(

        value: "Shoes",

        children: [

            .init(value: "Boots"),

            .init(value: "Sliders"),

            .init(value: "Sandals"),

            .init(value: "Trainers"),

        ]

    )

]

System info

macOS 13.0 Beta(22A5358e) Xcode 14.0 beta 6 (14A5294g)

Many thanks Sebastian

Accepted Reply

I figured it out (after having tried for several days before posting this question...): When .contentShape(Rectangle()) is removed, the contextMenu works again in macOS as well. However, with this, the user cannot expand/collapse items by clicking on the item itself anymore (which, luckily, this isn't an issue in my case clicking an item should select/unselect it).

Given the above-mentioned .contentShape(Rectangle()) is also included in Apple's official DisclosureGroupStyle example and given that contextMenu is only invoked by right clicks, thus, left klicks should still be possible, I'd say this behaviour can be considered a bug.

Replies

I figured it out (after having tried for several days before posting this question...): When .contentShape(Rectangle()) is removed, the contextMenu works again in macOS as well. However, with this, the user cannot expand/collapse items by clicking on the item itself anymore (which, luckily, this isn't an issue in my case clicking an item should select/unselect it).

Given the above-mentioned .contentShape(Rectangle()) is also included in Apple's official DisclosureGroupStyle example and given that contextMenu is only invoked by right clicks, thus, left klicks should still be possible, I'd say this behaviour can be considered a bug.