Michael Paus
05/08/2023, 1:04 PMClickableText
with a URL string inside the annotated text with a corresponding string annotation. When I click the URL text a browser window is opened with that URL. So my setup is working correctly. My question now is, how can I somehow temporarily highlight the URL text when the user hovers over it with the mouse so that he/she knows that there is something to click on?orangy
onTextLayout
and save TextLayoutResult
in a mutable state
2. Handle onPointerEvent(PointerEventType.Move)
and use mouse position to find offset in text textLayout?.multiParagraph?.getOffsetForPosition(position)
3. Find URL annotation in the text matching the position and save its range in a mutable state
4. (re-)Style annotated text depending on which range is hovered, if anyMichael Paus
05/08/2023, 4:57 PMval annotatedText = remember(styledMessage) { styledMessage.parseTaggedText() }
var textLayoutResult by remember { mutableStateOf<TextLayoutResult?>(null) }
var path by remember { mutableStateOf<Path?>(null) }
fun urlRange(offset: Int?): AnnotatedString.Range<String>? {
return offset?.let {
annotatedText.getStringAnnotations(
tag = TaggedTextAnnotationTag.Hyperlink.name,
start = it,
end = it
).firstOrNull() }
}
ClickableText(
text = annotatedText,
modifier = Modifier
.fillMaxWidth()
.drawBehind { path?.let { drawPath(it, primaryUltraLightColor) } },
style = LocalTextStyle.current.copy(textAlign = textAlign, fontSize = 14.sp, color = primaryDarkColor),
onTextLayout = { textLayoutResult = it },
onHover = { offset -> path = urlRange(offset)?.let { textLayoutResult?.getPathForRange(it.start, it.end) } },
onClick = { offset -> urlRange(offset)?.let { mapService.showUrl(it.item) } }
)
Marcin Wisniowski
02/14/2025, 8:15 PMonHover
was removed, and ClickableText
is deprecated anyway. So what's the proper solution now?Michael Paus
02/15/2025, 8:57 AMMarcin Wisniowski
02/15/2025, 1:42 PMLinkAnnotation
, and styling the text itself on hover is no problem, but I can no longer change the cursor pointer icon on hover.
https://youtrack.jetbrains.com/issue/CMP-7615Michael Paus
02/15/2025, 3:23 PMMarcin Wisniowski
02/15/2025, 3:27 PM