Skip to content

Commit 6b7a5eb

Browse files
Merge pull request #70 from SimonSchubert/fix/compose-text-crash-workaround
Fix: Workaround for IndexOutOfBoundsException in Compose text rendering
2 parents aa9bc6e + 1fad1ba commit 6b7a5eb

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

  • android/src/main/java/com/inspiredandroid/linuxcommandbibliotheca/ui/composables
  • common/src/commonMain/kotlin/com/linuxcommandlibrary/shared

android/src/main/java/com/inspiredandroid/linuxcommandbibliotheca/ui/composables/CommandView.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.ui.Modifier
1717
import androidx.compose.ui.platform.LocalContext
1818
import androidx.compose.ui.res.stringResource
1919
import androidx.compose.ui.text.LinkAnnotation
20+
import androidx.compose.ui.text.ParagraphStyle
2021
import androidx.compose.ui.text.SpanStyle
2122
import androidx.compose.ui.text.buildAnnotatedString
2223
import androidx.compose.ui.text.withStyle
@@ -53,7 +54,7 @@ fun CommandView(
5354
verticalPadding: Dp = 6.dp,
5455
) {
5556
val codeColor = MaterialTheme.colors.primary
56-
val annotatedString = remember(elements, codeColor) {
57+
val baseAnnotatedString = remember(elements, codeColor) {
5758
buildAnnotatedString {
5859
elements.forEach { element ->
5960
when (element) {
@@ -97,9 +98,24 @@ fun CommandView(
9798
}
9899
}
99100

101+
val finalAnnotatedString = remember(baseAnnotatedString) {
102+
if (baseAnnotatedString.text.isEmpty()) {
103+
baseAnnotatedString
104+
} else {
105+
buildAnnotatedString {
106+
append(baseAnnotatedString)
107+
addStyle(
108+
style = ParagraphStyle(), // Default ParagraphStyle
109+
start = 0,
110+
end = baseAnnotatedString.text.length
111+
)
112+
}
113+
}
114+
}
115+
100116
Row(modifier = Modifier.padding(start = 12.dp, end = 4.dp).padding(vertical = verticalPadding)) {
101117
Text(
102-
text = annotatedString,
118+
text = finalAnnotatedString,
103119
modifier = Modifier
104120
.weight(1f)
105121
.align(Alignment.CenterVertically),

common/src/commonMain/kotlin/com/linuxcommandlibrary/shared/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fun String.getCommandList(
7171
var isCommand = false
7272
command.trim().forEach {
7373
if (it == 'ü') {
74-
list.add(CommandElement.Text(currentText.replace("\\n", "")))
74+
list.add(CommandElement.Text(currentText.replace("\n", "")))
7575
currentText = ""
7676
isCommand = true
7777
} else if (it == 'ä') {
@@ -102,7 +102,7 @@ fun String.getCommandList(
102102
}
103103
}
104104
}
105-
list.add(CommandElement.Text(currentText.replace("[cmd]", "[command]").replace("\\n", "")))
105+
list.add(CommandElement.Text(currentText.replace("[cmd]", "[command]").replace("\n", "")))
106106
return list.toList()
107107
}
108108

0 commit comments

Comments
 (0)