-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from niscy-eudiw/refactor/replace_deprecated_…
…clickable_text Refactor: Replace deprecated ClickableText.
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2023 European Commission | ||
* | ||
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European | ||
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work | ||
* except in compliance with the Licence. | ||
* | ||
* You may obtain a copy of the Licence at: | ||
* https://joinup.ec.europa.eu/software/page/eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF | ||
* ANY KIND, either express or implied. See the Licence for the specific language | ||
* governing permissions and limitations under the Licence. | ||
*/ | ||
|
||
package eu.europa.ec.uilogic.component | ||
|
||
import androidx.compose.foundation.gestures.detectTapGestures | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.TextLayoutResult | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.style.TextOverflow | ||
|
||
/** | ||
* Composable function that displays text and allows the user to click on it. | ||
* The click event provides the character offset within the text that was clicked. | ||
* | ||
* @param text The text to display. | ||
* @param modifier Modifier to be applied to the Text composable. | ||
* @param style Style configuration for the text. | ||
* @param softWrap Whether the text should break at soft line breaks. | ||
* @param overflow How visual overflow should be handled. | ||
* @param maxLines An optional maximum number of lines for the text to span, wrapping if necessary. | ||
* @param onTextLayout Callback that is executed when a new text layout is calculated. | ||
* @param onClick Callback that is executed when the user clicks on the text. The callback provides | ||
* the character offset within the text that was clicked. | ||
*/ | ||
@Composable | ||
fun ClickableText( | ||
text: AnnotatedString, | ||
modifier: Modifier = Modifier, | ||
style: TextStyle = TextStyle.Default, | ||
softWrap: Boolean = true, | ||
overflow: TextOverflow = TextOverflow.Clip, | ||
maxLines: Int = Int.MAX_VALUE, | ||
onTextLayout: (TextLayoutResult) -> Unit = {}, | ||
onClick: (Int) -> Unit, | ||
) { | ||
val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) } | ||
val pressIndicator = Modifier.pointerInput(onClick) { | ||
detectTapGestures { pos -> | ||
layoutResult.value?.let { layoutResult -> | ||
onClick(layoutResult.getOffsetForPosition(pos)) | ||
} | ||
} | ||
} | ||
|
||
Text( | ||
text = text, | ||
modifier = modifier.then(pressIndicator), | ||
style = style, | ||
softWrap = softWrap, | ||
overflow = overflow, | ||
maxLines = maxLines, | ||
onTextLayout = { | ||
layoutResult.value = it | ||
onTextLayout(it) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters