Does anyone have an idea on how to add context men...
# compose-desktop
s
Does anyone have an idea on how to add context menus to part of an
AnnotatedString
? When a link is clicked, I want to open the associated context menu. Currently, I'm using a
DropdownMenu
that opens in an arbitrary location, which isn't great.
a
Take a look at
LocalContextMenuData
s
Thanks. Is there any documentation on how this works?
a
Doesn’t seem to be. Maybe you can fimd something useful here: https://github.com/JetBrains/compose-multiplatform/issues/3493
I’m afk right now, and unfortunately don’t remember how to use it by heart.
Hmm, I may be barking up the wrong tree, having re-read your question.
You can probably get the position of your link from the text layout.
1
s
When I couldn't solve this quickly, it became a low priority. I finally got back to it today. There's no obvious way to directly get the position of the click, but I can save every click position.
Copy code
var clickOffset by remember { mutableStateOf<Offset?>(null) }
BasicText(
    modifier = Modifier
        .onPointerEvent(PointerEventType.Press) { event ->
            clickOffset = event.changes.firstOrNull()?.position
        },
   text = text,
)
DropdownMenu(
    offset = clickOffset?.let { with(LocalDensity.current) { DpOffset(it.x.toDp(), it.y.toDp()) } } ?: DpOffset.Zero,
    expanded = true,
    onDismissRequest = onDismiss,
) { ... }
The position seems a bit off vertically, not sure why. Maybe there's something weird with it being in a
LazyColumn
This basically works though.