@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44import android.content.ClipData
55import android.content.ClipboardManager
66import android.content.Context
7+ import android.content.Intent
78import android.net.Uri
89import android.provider.OpenableColumns
910import android.webkit.CookieManager
@@ -63,6 +64,7 @@ import androidx.compose.foundation.rememberScrollState
6364import androidx.compose.foundation.shape.CircleShape
6465import androidx.compose.foundation.shape.RoundedCornerShape
6566import androidx.compose.foundation.text.BasicTextField
67+ import androidx.compose.foundation.text.ClickableText
6668import androidx.compose.foundation.text.KeyboardOptions
6769import androidx.compose.material.icons.Icons
6870import androidx.compose.material.icons.filled.Add
@@ -1181,24 +1183,46 @@ private fun ChatBubble(
11811183 splitMessage(message.text).forEach { part ->
11821184 when (part) {
11831185 is MessagePart .Text -> if (part.value.isNotBlank()) {
1184- Text (
1185- part.value.trim(),
1186+ val linkedText = buildAnnotatedString {
1187+ val value = part.value.trim()
1188+ append(value)
1189+ // Keep the original message intact while annotating each URL.
1190+ Regex (""" https?://[^\\s<>]+""" ).findAll(value).forEach { match ->
1191+ val cleanUrl = match.value.trimEnd(' .' , ' ,' , ' ;' , ' :' , ' !' , ' ?' , ' )' , ' ]' , ' }' )
1192+ if (cleanUrl.isNotBlank()) {
1193+ addStringAnnotation(
1194+ tag = " URL" ,
1195+ annotation = cleanUrl,
1196+ start = match.range.first,
1197+ end = match.range.first + cleanUrl.length
1198+ )
1199+ }
1200+ }
1201+ }
1202+ ClickableText (
1203+ text = linkedText,
11861204 style = TextStyle (
11871205 textDirection = TextDirection .ContentOrRtl ,
1188- shadow = SoftTextShadow
1206+ shadow = SoftTextShadow ,
1207+ color = MaterialTheme .colorScheme.onSurface
11891208 ),
11901209 lineHeight = 23 .sp,
1191- modifier = Modifier .fillMaxWidth().pointerInput(Unit ) {
1192- detectTapGestures(onTap = { offset ->
1193- // Simple URL detection and opening
1194- val urlRegex = Regex (""" (https?://[^\s]+)""" )
1195- val matches = urlRegex.findAll(part.value)
1196- matches.forEach { match ->
1197- // This is a naive implementation; for real URL handling,
1198- // use TextLinkScope in M3 or similar.
1199- // For now, let's just show an toast or try to open browser if we can detect the click.
1210+ modifier = Modifier .fillMaxWidth(),
1211+ onClick = { offset ->
1212+ linkedText.getStringAnnotations(" URL" , offset, offset)
1213+ .firstOrNull()?.let { annotation ->
1214+ runCatching {
1215+ context.startActivity(
1216+ Intent (Intent .ACTION_VIEW , Uri .parse(annotation.item))
1217+ )
1218+ }.onFailure {
1219+ android.widget.Toast .makeText(
1220+ context,
1221+ " لینک قابل باز کردن نیست" ,
1222+ android.widget.Toast .LENGTH_SHORT
1223+ ).show()
1224+ }
12001225 }
1201- })
12021226 }
12031227 )
12041228 }
0 commit comments