|
| 1 | +/* |
| 2 | + * NeoRegex. |
| 3 | + * |
| 4 | + * Copyright (C) 2024 Irineu A. Silva. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.neo.regex.core.sharedui.component |
| 20 | + |
| 21 | +import androidx.compose.foundation.layout.Arrangement |
| 22 | +import androidx.compose.foundation.layout.Column |
| 23 | +import androidx.compose.foundation.layout.Row |
| 24 | +import androidx.compose.foundation.layout.padding |
| 25 | +import androidx.compose.material3.HorizontalDivider |
| 26 | +import androidx.compose.material3.MaterialTheme.typography |
| 27 | +import androidx.compose.material3.Surface |
| 28 | +import androidx.compose.material3.Text |
| 29 | +import androidx.compose.runtime.Composable |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.graphics.RectangleShape |
| 32 | +import androidx.compose.ui.text.SpanStyle |
| 33 | +import androidx.compose.ui.text.TextStyle |
| 34 | +import androidx.compose.ui.text.buildAnnotatedString |
| 35 | +import androidx.compose.ui.text.font.FontWeight |
| 36 | +import androidx.compose.ui.text.style.TextOverflow |
| 37 | +import androidx.compose.ui.text.withStyle |
| 38 | +import com.neo.regex.core.designsystem.theme.NeoTheme.dimensions |
| 39 | +import com.neo.regex.core.sharedui.model.Match |
| 40 | + |
| 41 | +@Composable |
| 42 | +fun MatchDetails( |
| 43 | + match: Match, |
| 44 | + modifier: Modifier = Modifier, |
| 45 | + textStyle: TextStyle = TextStyle() |
| 46 | +) = Surface( |
| 47 | + modifier = modifier, |
| 48 | + shape = RectangleShape, |
| 49 | + shadowElevation = dimensions.small |
| 50 | +) { |
| 51 | + |
| 52 | + val mergedTextStyle = typography.bodyLarge.merge(textStyle) |
| 53 | + |
| 54 | + Row( |
| 55 | + horizontalArrangement = Arrangement.SpaceAround, |
| 56 | + ) { |
| 57 | + |
| 58 | + Column( |
| 59 | + modifier = Modifier |
| 60 | + .padding(dimensions.default) |
| 61 | + .weight(weight = 1f) |
| 62 | + ) { |
| 63 | + Text( |
| 64 | + text = "match ${match.number}", |
| 65 | + style = mergedTextStyle.copy( |
| 66 | + fontWeight = FontWeight.Bold |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + HorizontalDivider( |
| 71 | + modifier = Modifier.padding( |
| 72 | + vertical = dimensions.small, |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + Text( |
| 77 | + text = buildAnnotatedString { |
| 78 | + withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { |
| 79 | + append("range: ") |
| 80 | + } |
| 81 | + withStyle(SpanStyle(fontWeight = FontWeight.Normal)) { |
| 82 | + append(match.range.toString()) |
| 83 | + } |
| 84 | + }, |
| 85 | + style = mergedTextStyle |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + if (match.groups.isNotEmpty()) { |
| 90 | + Column( |
| 91 | + modifier = Modifier |
| 92 | + .padding(dimensions.default) |
| 93 | + .weight(weight = 1f) |
| 94 | + ) { |
| 95 | + Text( |
| 96 | + text = "groups", |
| 97 | + style = mergedTextStyle.copy( |
| 98 | + fontWeight = FontWeight.Bold |
| 99 | + ) |
| 100 | + ) |
| 101 | + |
| 102 | + HorizontalDivider( |
| 103 | + modifier = Modifier.padding( |
| 104 | + vertical = dimensions.small, |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + match.groups.forEachIndexed { index, group -> |
| 109 | + Text( |
| 110 | + text = buildAnnotatedString { |
| 111 | + withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { |
| 112 | + append("$index: ") |
| 113 | + } |
| 114 | + withStyle(SpanStyle(fontWeight = FontWeight.Normal)) { |
| 115 | + append(group) |
| 116 | + } |
| 117 | + }, |
| 118 | + style = mergedTextStyle, |
| 119 | + overflow = TextOverflow.Ellipsis, |
| 120 | + maxLines = 1 |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments