Skip to content

Commit

Permalink
fix: labeled candidate item in CandidatesView couldn't break line cor…
Browse files Browse the repository at this point in the history
…rectly

The spans for spanned string can influence the line break strategy in TextView, therefore, CandidateItemSpan is implemented to replace the original CandidateSpan to fix the issue.
  • Loading branch information
WhiredPlanck committed Jan 27, 2025
1 parent 2b4736e commit 0d45c41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2015 - 2025 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.ime.candidates.popup

import android.graphics.Typeface
import android.text.TextPaint
import android.text.style.MetricAffectingSpan
import androidx.annotation.ColorInt

class CandidateItemSpan(
@ColorInt
private val color: Int,
private val textSize: Float,
private val typeface: Typeface,
) : MetricAffectingSpan() {
override fun updateDrawState(textPaint: TextPaint) {
textPaint.color = color
updateState(textPaint)
}

override fun updateMeasureState(textPaint: TextPaint) {
updateState(textPaint)
}

private fun updateState(textPaint: TextPaint) {
textPaint.textSize = textSize
textPaint.typeface = typeface
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ package com.osfans.trime.ime.candidates.popup

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.text.TextPaint
import android.text.style.UnderlineSpan
import androidx.annotation.ColorInt
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import com.osfans.trime.core.RimeProto
Expand All @@ -28,19 +24,6 @@ class LabeledCandidateItemUi(
override val ctx: Context,
val theme: Theme,
) : Ui {
class CandidateSpan(
@ColorInt private val color: Int,
private val textSize: Float,
private val typeface: Typeface,
) : UnderlineSpan() {
override fun updateDrawState(ds: TextPaint) {
ds.isUnderlineText = false
ds.color = color
ds.textSize = textSize
ds.typeface = typeface
}
}

private val labelSize = theme.generalStyle.labelTextSize
private val textSize = theme.generalStyle.candidateTextSize
private val commentSize = theme.generalStyle.commentTextSize
Expand Down Expand Up @@ -69,11 +52,11 @@ class LabeledCandidateItemUi(
val commentFg = if (highlighted) highlightCommentTextColor else commentColor
root.text =
buildSpannedString {
inSpans(CandidateSpan(labelFg, ctx.sp(labelSize), labelFont)) { append(candidate.label) }
inSpans(CandidateSpan(textFg, ctx.sp(textSize), textFont)) { append(candidate.text) }
if (!candidate.comment.isNullOrEmpty()) {
inSpans(CandidateItemSpan(labelFg, ctx.sp(labelSize), labelFont)) { append(candidate.label) }
inSpans(CandidateItemSpan(textFg, ctx.sp(textSize), textFont)) { append(candidate.text) }
if (!candidate.comment.isNullOrBlank()) {
append(" ")
inSpans(CandidateSpan(commentFg, ctx.sp(commentSize), commentFont)) { append(candidate.comment) }
inSpans(CandidateItemSpan(commentFg, ctx.sp(commentSize), commentFont)) { append(candidate.comment) }
}
}
val bg =
Expand Down

0 comments on commit 0d45c41

Please sign in to comment.