Skip to content

Commit 1b0857a

Browse files
committed
feat: improve candidate item display
- Make the long text can be automatically scaled, powered by the adapted AutoScaleTextView from fcitx5-android [1] - Once a candidate item contains a non-empty comment, all the text will align to baseline - Clean up the code tree of CandidateItemUi Ref: 1. https://github.com/fcitx5-android/fcitx5-android/blob/3dff0a3a94c68cc3e8030b62ef548570b5a0c434/app/src/main/java/org/fcitx/fcitx5/android/input/AutoScaleTextView.kt
1 parent d0c535a commit 1b0857a

File tree

4 files changed

+278
-82
lines changed

4 files changed

+278
-82
lines changed

app/src/main/java/com/osfans/trime/ime/candidates/CandidateItemUi.kt

+61-69
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,100 @@
55
package com.osfans.trime.ime.candidates
66

77
import android.content.Context
8-
import android.text.TextUtils
9-
import androidx.constraintlayout.widget.ConstraintLayout
8+
import android.graphics.drawable.ColorDrawable
9+
import android.view.View
10+
import com.osfans.trime.core.CandidateItem
1011
import com.osfans.trime.data.theme.ColorManager
1112
import com.osfans.trime.data.theme.FontManager
1213
import com.osfans.trime.data.theme.Theme
14+
import com.osfans.trime.ime.core.AutoScaleTextView
1315
import com.osfans.trime.util.pressHighlightDrawable
14-
import splitties.views.dsl.constraintlayout.after
15-
import splitties.views.dsl.constraintlayout.before
1616
import splitties.views.dsl.constraintlayout.centerHorizontally
17-
import splitties.views.dsl.constraintlayout.centerInParent
1817
import splitties.views.dsl.constraintlayout.centerVertically
1918
import splitties.views.dsl.constraintlayout.constraintLayout
20-
import splitties.views.dsl.constraintlayout.endOfParent
21-
import splitties.views.dsl.constraintlayout.lParams
22-
import splitties.views.dsl.constraintlayout.startOfParent
23-
import splitties.views.dsl.constraintlayout.topOfParent
19+
import splitties.views.dsl.constraintlayout.horizontalChain
20+
import splitties.views.dsl.constraintlayout.packed
21+
import splitties.views.dsl.constraintlayout.verticalChain
2422
import splitties.views.dsl.core.Ui
25-
import splitties.views.dsl.core.add
26-
import splitties.views.dsl.core.textView
23+
import splitties.views.dsl.core.view
2724
import splitties.views.dsl.core.wrapContent
2825
import splitties.views.gravityCenter
2926

3027
class CandidateItemUi(
3128
override val ctx: Context,
3229
theme: Theme,
3330
) : Ui {
34-
private val maybeCandidateTextColor = ColorManager.getColor("candidate_text_color")
35-
private val maybeCommentTextColor = ColorManager.getColor("comment_text_color")
36-
private val maybeHighlightedCandidateTextColor = ColorManager.getColor("hilited_candidate_text_color")
37-
private val maybeHighlightedCommentTextColor = ColorManager.getColor("hilited_comment_text_color")
38-
private val maybeHighlightedCandidateBackColor = ColorManager.getColor("hilited_candidate_back_color")
31+
private val firstTextSize = theme.generalStyle.candidateTextSize
32+
private val lastTextSize = theme.generalStyle.commentTextSize
33+
private val firstTextFont = FontManager.getTypeface("candidate_font")
34+
private val lastTextFont = FontManager.getTypeface("comment_font")
35+
private val firstTextColor = ColorManager.getColor("candidate_text_color")!!
36+
private val lastTextColor = ColorManager.getColor("comment_text_color")!!
37+
private val lastTextColorH = ColorManager.getColor("hilited_comment_text_color")!!
38+
private val firstTextColorH = ColorManager.getColor("hilited_candidate_text_color")!!
39+
private val firstBackColorH = ColorManager.getColor("hilited_candidate_back_color")!!
3940

40-
val label =
41-
textView {
42-
textSize = theme.generalStyle.candidateTextSize.toFloat()
43-
typeface = FontManager.getTypeface("candidate_font")
41+
private val firstText =
42+
view(::AutoScaleTextView) {
43+
textSize = firstTextSize
44+
typeface = firstTextFont
4445
isSingleLine = true
4546
gravity = gravityCenter
46-
ellipsize = TextUtils.TruncateAt.END
47-
maybeCandidateTextColor?.let { setTextColor(it) }
47+
scaleMode = AutoScaleTextView.Mode.Proportional
4848
}
4949

50-
val altLabel =
51-
textView {
52-
textSize = theme.generalStyle.commentTextSize.toFloat()
53-
typeface = FontManager.getTypeface("comment_font")
50+
private val lastText =
51+
view(::AutoScaleTextView) {
52+
textSize = lastTextSize
53+
typeface = lastTextFont
5454
isSingleLine = true
5555
gravity = gravityCenter
56-
ellipsize = TextUtils.TruncateAt.END
57-
maybeCommentTextColor?.let { setTextColor(it) }
56+
scaleMode = AutoScaleTextView.Mode.Proportional
5857
}
5958

6059
override val root =
6160
constraintLayout {
6261
if (theme.generalStyle.commentOnTop) {
63-
add(
64-
altLabel,
65-
lParams(wrapContent, wrapContent) {
66-
topOfParent()
67-
centerHorizontally()
68-
},
69-
)
70-
add(
71-
label,
72-
lParams(wrapContent, wrapContent) {
73-
centerInParent()
74-
},
62+
verticalChain(
63+
listOf(lastText, firstText),
64+
style = packed,
65+
defaultWidth = wrapContent,
66+
defaultHeight = wrapContent,
67+
initParams = { centerHorizontally() },
7568
)
7669
} else {
77-
add(
78-
label,
79-
lParams(wrapContent, wrapContent) {
80-
startOfParent()
81-
centerVertically()
82-
before(altLabel)
83-
84-
horizontalChainStyle = ConstraintLayout.LayoutParams.CHAIN_PACKED
85-
horizontalBias = 0.5f
86-
},
87-
)
88-
add(
89-
altLabel,
90-
lParams(wrapContent, wrapContent) {
91-
after(label)
92-
centerVertically()
93-
endOfParent()
94-
95-
horizontalBias = 0.5f
96-
},
70+
horizontalChain(
71+
listOf(firstText, lastText),
72+
style = packed,
73+
defaultWidth = wrapContent,
74+
initParams = { centerVertically() },
9775
)
9876
}
9977
}
10078

101-
fun highlight(yes: Boolean) {
102-
if (yes) {
103-
maybeHighlightedCandidateTextColor?.let { label.setTextColor(it) }
104-
maybeHighlightedCommentTextColor?.let { altLabel.setTextColor(it) }
105-
maybeHighlightedCandidateBackColor?.let { root.setBackgroundColor(it) }
106-
} else {
107-
maybeCandidateTextColor?.let { label.setTextColor(it) }
108-
maybeCommentTextColor?.let { altLabel.setTextColor(it) }
109-
maybeHighlightedCandidateBackColor?.let { root.background = pressHighlightDrawable(it) }
79+
fun update(
80+
item: CandidateItem,
81+
isHighlighted: Boolean,
82+
obtainLast: Boolean,
83+
) {
84+
val firstColor = if (isHighlighted) firstTextColorH else firstTextColor
85+
val lastColor = if (isHighlighted) lastTextColorH else lastTextColor
86+
firstText.text = item.text
87+
firstText.setTextColor(firstColor)
88+
lastText.run {
89+
if (obtainLast) {
90+
lastText.text = item.comment
91+
lastText.setTextColor(lastColor)
92+
if (visibility == View.GONE) visibility = View.VISIBLE
93+
} else if (visibility != View.GONE) {
94+
visibility = View.GONE
95+
}
11096
}
97+
root.background =
98+
if (isHighlighted) {
99+
ColorDrawable(firstBackColorH)
100+
} else {
101+
pressHighlightDrawable(firstBackColorH)
102+
}
111103
}
112104
}

app/src/main/java/com/osfans/trime/ime/candidates/compact/CompactCandidateViewAdapter.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ open class CompactCandidateViewAdapter(
6262
position: Int,
6363
item: CandidateItem?,
6464
) {
65-
val (comment, text) = item!!
66-
holder.ui.run {
67-
label.text = text
68-
altLabel.text = comment
69-
highlight(theme.generalStyle.candidateUseCursor && position == highlightedIdx)
70-
}
71-
holder.text = text
72-
holder.comment = comment
65+
item ?: return
66+
val isHighlighted = theme.generalStyle.candidateUseCursor && position == highlightedIdx
67+
val obtainComment = items.any { it.comment.isNotEmpty() }
68+
holder.ui.update(item, isHighlighted, obtainComment)
69+
holder.text = item.text
70+
holder.comment = item.comment
7371
holder.idx = previous + position // unused
7472
holder.ui.root.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
7573
minWidth = 0

app/src/main/java/com/osfans/trime/ime/candidates/unrolled/PagingCandidateViewAdapter.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ open class PagingCandidateViewAdapter(
4848
holder: CandidateViewHolder,
4949
position: Int,
5050
) {
51-
val (comment, text) = getItem(position)!!
52-
holder.ui.label.text = text
53-
holder.ui.altLabel.text = comment
54-
holder.text = text
55-
holder.comment = comment
51+
val item = getItem(position) ?: return
52+
val obtainComment = snapshot().items.any { it.comment.isNotEmpty() }
53+
holder.ui.update(item, false, obtainComment)
54+
holder.text = item.text
55+
holder.comment = item.comment
5656
holder.idx = position + offset
5757
}
5858
}

0 commit comments

Comments
 (0)