Skip to content

Commit 2aa0fe3

Browse files
feat: add Iconics font support for popup UI components
1 parent 94daf71 commit 2aa0fe3

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

app/src/main/java/com/osfans/trime/ime/popup/PopupEntryUi.kt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,47 @@
55
package com.osfans.trime.ime.popup
66

77
import android.content.Context
8+
import android.graphics.PorterDuff
9+
import android.graphics.PorterDuffColorFilter
810
import android.graphics.drawable.GradientDrawable
911
import android.view.ViewOutlineProvider
12+
import androidx.appcompat.widget.AppCompatImageView
13+
import androidx.core.view.isVisible
14+
import com.mikepenz.iconics.IconicsDrawable
15+
import com.mikepenz.iconics.utils.sizeDp
1016
import com.osfans.trime.data.theme.ColorManager
1117
import com.osfans.trime.data.theme.FontManager
1218
import com.osfans.trime.data.theme.Theme
1319
import com.osfans.trime.ime.core.AutoScaleTextView
20+
import com.osfans.trime.ime.keyboard.isIconFont
21+
import com.osfans.trime.ime.keyboard.toIconName
1422
import splitties.dimensions.dp
1523
import splitties.views.dsl.constraintlayout.centerHorizontally
1624
import splitties.views.dsl.constraintlayout.constraintLayout
1725
import splitties.views.dsl.constraintlayout.lParams
1826
import splitties.views.dsl.constraintlayout.topOfParent
1927
import splitties.views.dsl.core.Ui
2028
import splitties.views.dsl.core.add
21-
import splitties.views.dsl.core.matchParent
2229
import splitties.views.dsl.core.view
30+
import splitties.views.dsl.core.wrapContent
2331
import splitties.views.gravityCenter
2432

25-
class PopupEntryUi(override val ctx: Context, theme: Theme, keyHeight: Int, radius: Float) : Ui {
33+
class PopupEntryUi(override val ctx: Context, private val theme: Theme, keyHeight: Int, radius: Float) : Ui {
2634

2735
var lastShowTime = -1L
2836

2937
val textView = view(::AutoScaleTextView) {
38+
scaleMode = AutoScaleTextView.Mode.Proportional
3039
textSize = theme.generalStyle.popupTextSize
3140
gravity = gravityCenter
3241
setTextColor(ColorManager.getColor("popup_text_color"))
3342
typeface = FontManager.getTypeface("POPUP_FONT")
3443
}
3544

45+
val imageView = view(::AppCompatImageView) {
46+
visibility = android.view.View.GONE
47+
}
48+
3649
override val root = constraintLayout {
3750
background = GradientDrawable().apply {
3851
cornerRadius = radius
@@ -42,14 +55,34 @@ class PopupEntryUi(override val ctx: Context, theme: Theme, keyHeight: Int, radi
4255
elevation = dp(2f)
4356
add(
4457
textView,
45-
lParams(matchParent, keyHeight) {
58+
lParams(wrapContent, keyHeight) {
59+
topOfParent()
60+
centerHorizontally()
61+
},
62+
)
63+
add(
64+
imageView,
65+
lParams(wrapContent, keyHeight) {
4666
topOfParent()
4767
centerHorizontally()
4868
},
4969
)
5070
}
5171

5272
fun setText(text: String) {
53-
textView.text = text
73+
if (text.isIconFont) {
74+
imageView.setImageDrawable(
75+
IconicsDrawable(ctx, text.toIconName()).apply {
76+
sizeDp = theme.generalStyle.popupTextSize.toInt()
77+
colorFilter = PorterDuffColorFilter(ColorManager.getColor("popup_text_color"), PorterDuff.Mode.SRC_IN)
78+
},
79+
)
80+
imageView.isVisible = true
81+
textView.isVisible = false
82+
} else {
83+
textView.text = text
84+
textView.isVisible = true
85+
imageView.isVisible = false
86+
}
5487
}
5588
}

app/src/main/java/com/osfans/trime/ime/popup/PopupKeyboardUi.kt

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
package com.osfans.trime.ime.popup
66

77
import android.content.Context
8+
import android.graphics.PorterDuff
9+
import android.graphics.PorterDuffColorFilter
810
import android.graphics.Rect
911
import android.graphics.drawable.GradientDrawable
1012
import android.view.ViewOutlineProvider
13+
import androidx.appcompat.widget.AppCompatImageView
14+
import androidx.core.view.isVisible
15+
import com.mikepenz.iconics.IconicsDrawable
16+
import com.mikepenz.iconics.utils.sizeDp
1117
import com.osfans.trime.data.theme.ColorManager
1218
import com.osfans.trime.data.theme.FontManager
1319
import com.osfans.trime.data.theme.KeyActionManager
1420
import com.osfans.trime.data.theme.Theme
1521
import com.osfans.trime.ime.core.AutoScaleTextView
1622
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
23+
import com.osfans.trime.ime.keyboard.isIconFont
24+
import com.osfans.trime.ime.keyboard.toIconName
1725
import splitties.dimensions.dp
1826
import splitties.views.dsl.core.Ui
1927
import splitties.views.dsl.core.add
@@ -62,20 +70,44 @@ class PopupKeyboardUi(
6270
class PopupKeyUi(override val ctx: Context, val theme: Theme, val text: String) : Ui {
6371

6472
val textView = view(::AutoScaleTextView) {
65-
text = this@PopupKeyUi.text
6673
scaleMode = AutoScaleTextView.Mode.Proportional
6774
textSize = theme.generalStyle.popupTextSize
6875
setTextColor(ColorManager.getColor("popup_text_color"))
6976
typeface = FontManager.getTypeface("POPUP_FONT")
7077
}
7178

79+
val imageView = view(::AppCompatImageView) {}
80+
7281
override val root = frameLayout {
7382
add(
7483
textView,
7584
lParams {
7685
gravity = gravityCenter
7786
},
7887
)
88+
add(
89+
imageView,
90+
lParams {
91+
gravity = gravityCenter
92+
},
93+
)
94+
}
95+
96+
init {
97+
if (text.isIconFont) {
98+
imageView.setImageDrawable(
99+
IconicsDrawable(ctx, text.toIconName()).apply {
100+
sizeDp = theme.generalStyle.popupTextSize.toInt()
101+
colorFilter = PorterDuffColorFilter(ColorManager.getColor("popup_text_color"), PorterDuff.Mode.SRC_IN)
102+
},
103+
)
104+
imageView.isVisible = true
105+
textView.isVisible = false
106+
} else {
107+
textView.text = text
108+
textView.isVisible = true
109+
imageView.isVisible = false
110+
}
79111
}
80112
}
81113

@@ -163,10 +195,13 @@ class PopupKeyboardUi(
163195
if (label.length == 1 && label[0].code < 128) {
164196
label
165197
} else {
166-
KeyActionManager
167-
.getAction(label)
168-
.getLabel(KeyboardSwitcher.currentKeyboard)
169-
.let { if (it.isNotEmpty()) String(Character.toChars(it.codePointAt(0))) else "" }
198+
KeyActionManager.getAction(label).getLabel(KeyboardSwitcher.currentKeyboard).let {
199+
when {
200+
it.isIconFont -> it
201+
it.isNotEmpty() -> String(Character.toChars(it.codePointAt(0)))
202+
else -> ""
203+
}
204+
}
170205
}
171206

172207
PopupKeyUi(ctx, theme, displayLabel)
@@ -205,14 +240,18 @@ class PopupKeyboardUi(
205240
private fun markFocus(index: Int) {
206241
keyUis.getOrNull(index)?.apply {
207242
root.background = focusBackground
208-
textView.setTextColor(ColorManager.getColor("hilited_popup_text_color"))
243+
val color = ColorManager.getColor("hilited_popup_text_color")
244+
textView.setTextColor(color)
245+
imageView.drawable?.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
209246
}
210247
}
211248

212249
private fun markInactive(index: Int) {
213250
keyUis.getOrNull(index)?.apply {
214251
root.background = null
215-
textView.setTextColor(ColorManager.getColor("popup_text_color"))
252+
val color = ColorManager.getColor("popup_text_color")
253+
textView.setTextColor(color)
254+
imageView.drawable?.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
216255
}
217256
}
218257

0 commit comments

Comments
 (0)