Shouldn't the `ContextMenuDataProvider` work on ev...
# compose-desktop
d
Shouldn't the
ContextMenuDataProvider
work on every Composable and show menu (with custom entries) on right click? Doesn't work for me on Text, but works fine (with defaults) on TextField. Tested with alpha 2 and alpha3. Code in 🧵
1
My stripped down example code:
Copy code
@ExperimentalComposeUiApi
@ExperimentalFoundationApi
@Composable
fun ExampleContextMenu() {
    ContextMenuDataProvider(
        items = {
            listOf(
                ContextMenuItem("Item 1") {},
                ContextMenuItem("Item 2") {}
            )
        }
    ) {
        Box {
            Text("No menu on right click")
        }

        Text("No menu on right click")

        TextField("Menu with default entries shown", {})
    }
}
a
By default it works only with TextFields and Selectable text. But you can enable it for any widget, See example below:
Copy code
@ExperimentalComposeUiApi
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun contextMenuDemo() {
    DesktopMaterialTheme {
        Column {
            ContextMenuDataProvider(
                items = {
                    listOf(
                        ContextMenuItem("Item 1") {},
                        ContextMenuItem("Item 2") {}
                    )
                }
            ) {
                TextField("Hello non-editable", {})

                SelectionContainer {
                    Text("Hello World!")
                }
            }

            ContextMenuArea(items = {
                listOf(
                    ContextMenuItem("Item 1") {},
                    ContextMenuItem("Item 2") {}
                )
            }) {
                Box(modifier = Modifier.background(Color.Blue).height(100.dp).width(100.dp)) {
                }
            }
        }
    }
}
🙇 1
👍 1
d
Thanks, didn't found/see the
ContextMenuArea
in the Compose source code. When I change my example and use the
ContextMenuArea
instead of the
ContextMenuDataProvider
it works like charm.
👍 1
a
It is indeed not clear. We will prepare a small tutorial on this
👍 1