Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

package org.polyfrost.polyplus.client.gui.wardrobe

import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextMeasurer
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.TextUnit
import org.polyfrost.oneconfig.api.ui.v1.ExperimentalOneConfigUiApi
import org.polyfrost.oneconfig.api.ui.v1.components.TooltipBox
import org.polyfrost.oneconfig.api.ui.v1.components.TooltipPlacement
import org.polyfrost.oneconfig.api.ui.v1.components.Text
import org.polyfrost.oneconfig.api.ui.v1.theme.OneConfigTheme

private const val ELLIPSIS = "…"

@Composable
internal fun CardLabel(
text: String,
Expand All @@ -26,16 +30,39 @@ internal fun CardLabel(
modifier: Modifier = Modifier,
fontWeight: FontWeight = FontWeight.Normal,
) {
var truncated by remember(text) { mutableStateOf(false) }
TooltipBox(if (truncated) text else null, modifier, TooltipPlacement.AboveStart) {
Text(
text,
color = color,
style = OneConfigTheme.current.typography.body.copy(fontSize = fontSize, fontWeight = fontWeight),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
softWrap = true,
onTextLayout = { truncated = it.hasVisualOverflow },
)
val style = OneConfigTheme.current.typography.body.copy(fontSize = fontSize, fontWeight = fontWeight)
val measurer = rememberTextMeasurer()
BoxWithConstraints(modifier) {
val maxWidth = constraints.maxWidth
val shown = remember(text, style, maxWidth, measurer) { ellipsize(text, measurer, style, maxWidth) }
TooltipBox(if (shown != text) text else null, Modifier, TooltipPlacement.AboveStart) {
Text(
shown,
color = color,
style = style,
maxLines = 1,
overflow = TextOverflow.Clip,
softWrap = false,
legacyFormatting = false,
)
}
}
}

private fun ellipsize(text: String, measurer: TextMeasurer, style: TextStyle, maxWidth: Int): String {
if (maxWidth == Constraints.Infinity) return text
fun width(value: String) = measurer.measure(value, style, softWrap = false, maxLines = 1).size.width
if (width(text) <= maxWidth) return text
var low = 0
var high = text.length
while (low < high) {
val mid = (low + high + 1) / 2
if (width(candidate(text, mid)) <= maxWidth) low = mid else high = mid - 1
}
return candidate(text, low)
}

private fun candidate(text: String, length: Int): String {
val end = if (length > 0 && Character.isHighSurrogate(text[length - 1])) length - 1 else length
return text.substring(0, end).trimEnd() + ELLIPSIS
}