Skip to content

Commit 793b121

Browse files
committed
refactor: correct preedit view behavior and polish its appearance
- Fix the view wouldn't disappear after changing theme or color on site - Allow to custom its background color - Add top left corner radius - Improve its visual effect of showup - Get alpha value as float
1 parent 176608b commit 793b121

File tree

11 files changed

+56
-12
lines changed

11 files changed

+56
-12
lines changed

app/src/main/java/com/osfans/trime/data/theme/mapper/GeneralStyleMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class GeneralStyleMapper(
141141

142142
val textFont = getStringList("text_font")
143143

144-
val textSize = getInt("text_size")
144+
val textSize = getFloat("text_size")
145145

146146
val verticalCorrection = getInt("vertical_correction")
147147

app/src/main/java/com/osfans/trime/data/theme/mapper/LayoutStyleMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class LayoutStyleMapper(
5353

5454
val roundCorner = getFloat("round_corner")
5555

56-
val alpha = getInt("alpha")
56+
val alpha = getFloat("alpha", 0.8f)
5757
val elevation = getInt("elevation")
5858
val movable = getString("movable")
5959

app/src/main/java/com/osfans/trime/data/theme/model/GeneralStyle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ data class GeneralStyle(
6969
val symbolFont: List<String>,
7070
val symbolTextSize: Float,
7171
val textFont: List<String>,
72-
val textSize: Int,
72+
val textSize: Float,
7373
val verticalCorrection: Int,
7474
val verticalGap: Int,
7575
val longTextFont: List<String>,

app/src/main/java/com/osfans/trime/data/theme/model/Layout.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ data class Layout(
2626
val realMargin: Int,
2727
val spacing: Int,
2828
val roundCorner: Float,
29-
val alpha: Int,
29+
val alpha: Float,
3030
val elevation: Int,
3131
val movable: String,
3232
)

app/src/main/java/com/osfans/trime/ime/composition/ComposingPopupWindow.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class ComposingPopupWindow(
5555
theme.generalStyle.layout.border,
5656
"border_color",
5757
theme.generalStyle.layout.roundCorner,
58-
theme.generalStyle.layout.alpha,
58+
theme.generalStyle.layout.alpha
59+
.toInt(),
5960
),
6061
)
6162
width = WindowManager.LayoutParams.WRAP_CONTENT

app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66
package com.osfans.trime.ime.composition
77

88
import android.content.Context
9+
import android.graphics.Outline
10+
import android.graphics.Rect
911
import android.view.Gravity
12+
import android.view.View
13+
import android.view.ViewOutlineProvider
1014
import android.view.WindowManager
1115
import android.widget.PopupWindow
1216
import com.osfans.trime.core.RimeProto
1317
import com.osfans.trime.daemon.RimeSession
1418
import com.osfans.trime.daemon.launchOnReady
19+
import com.osfans.trime.data.theme.ColorManager
1520
import com.osfans.trime.data.theme.Theme
1621
import com.osfans.trime.ime.bar.QuickBar
1722
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
1823
import com.osfans.trime.ime.dependency.InputScope
1924
import me.tatarka.inject.annotations.Inject
25+
import splitties.views.backgroundColor
2026

2127
@InputScope
2228
@Inject
@@ -26,8 +32,29 @@ class PreeditModule(
2632
rime: RimeSession,
2733
private val bar: QuickBar,
2834
) : InputBroadcastReceiver {
35+
private val textBackColor = ColorManager.getColor("text_back_color")
36+
37+
private val topLeftCornerRadiusOutlineProvider =
38+
object : ViewOutlineProvider() {
39+
override fun getOutline(
40+
view: View,
41+
outline: Outline,
42+
) {
43+
val radius = theme.generalStyle.layout.roundCorner
44+
val width = view.width
45+
val height = view.height
46+
val rect = Rect(-radius.toInt(), 0, width, (height + radius).toInt())
47+
outline.setRoundRect(rect, radius)
48+
}
49+
}
50+
2951
val ui =
30-
PreeditUi(context, theme).apply {
52+
PreeditUi(context, theme, setupPreeditView = {
53+
textBackColor?.let { backgroundColor = it }
54+
}).apply {
55+
root.alpha = theme.generalStyle.layout.alpha
56+
root.outlineProvider = topLeftCornerRadiusOutlineProvider
57+
root.clipToOutline = true
3158
preedit.setOnCursorMoveListener { position ->
3259
rime.launchOnReady { it.moveCursorPos(position) }
3360
}
@@ -42,13 +69,17 @@ class PreeditModule(
4269
override fun onInputContextUpdate(ctx: RimeProto.Context) {
4370
ui.update(ctx.composition)
4471
if (ctx.composition.length > 0) {
72+
window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, 0, 0)
4573
val (x, y) = intArrayOf(0, 0).also { bar.view.getLocationInWindow(it) }
46-
window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, x, y)
4774
ui.root.post {
4875
window.update(x, y - ui.root.height, -1, -1)
4976
}
5077
} else {
5178
window.dismiss()
5279
}
5380
}
81+
82+
fun onDetached() {
83+
window.dismiss()
84+
}
5485
}

app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.text.style.BackgroundColorSpan
1111
import android.text.style.ForegroundColorSpan
1212
import android.view.MotionEvent
1313
import android.widget.LinearLayout
14+
import android.widget.TextView
1415
import androidx.core.text.buildSpannedString
1516
import com.osfans.trime.core.RimeProto
1617
import com.osfans.trime.data.theme.ColorManager
@@ -25,6 +26,7 @@ import splitties.views.setPaddingDp
2526
open class PreeditUi(
2627
final override val ctx: Context,
2728
private val theme: Theme,
29+
private val setupPreeditView: (TextView.() -> Unit)? = null,
2830
) : Ui {
2931
private val textColor = ColorManager.getColor("text_color")
3032
private val highlightTextColor = ColorManager.getColor("hilited_text_color")
@@ -34,8 +36,9 @@ open class PreeditUi(
3436
view(::PreeditTextView) {
3537
setPaddingDp(3, 1, 3, 1)
3638
textColor?.let { setTextColor(it) }
37-
textSize = theme.generalStyle.textSize.toFloat()
39+
textSize = theme.generalStyle.textSize
3840
typeface = FontManager.getTypeface("text_font")
41+
setupPreeditView?.invoke(this)
3942
}
4043

4144
override val root =

app/src/main/java/com/osfans/trime/ime/core/InputView.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ class InputView(
375375
// implies that InputView should not be attached again after detached.
376376
baseCallbackHandler.cancelJob()
377377
updateWindowViewHeightJob.cancel()
378+
preedit.onDetached()
378379
preview.root.removeAllViews()
379380
broadcaster.clear()
380381
super.onDetachedFromWindow()

app/src/main/java/com/osfans/trime/ime/keyboard/CommonKeyboardActionListener.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,18 @@ class CommonKeyboardActionListener(
7575
}
7676

7777
private fun showThemePicker() {
78-
showDialog {
79-
ThemePickerDialog.build(service.lifecycleScope, context)
78+
showDialog { api ->
79+
ThemePickerDialog.build(service.lifecycleScope, context) {
80+
api.commitComposition()
81+
}
8082
}
8183
}
8284

8385
private fun showColorPicker() {
84-
showDialog {
85-
ColorPickerDialog.build(service.lifecycleScope, context)
86+
showDialog { api ->
87+
ColorPickerDialog.build(service.lifecycleScope, context) {
88+
api.commitComposition()
89+
}
8690
}
8791
}
8892

app/src/main/java/com/osfans/trime/ui/main/settings/ColorPickerDialog.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ object ColorPickerDialog {
1717
suspend fun build(
1818
scope: LifecycleCoroutineScope,
1919
context: Context,
20+
afterConfirm: (suspend () -> Unit)? = null,
2021
): AlertDialog {
2122
val all = withContext(Dispatchers.Default) { ColorManager.presetColorSchemes }
2223
val allIds = all.keys
@@ -35,6 +36,7 @@ object ColorPickerDialog {
3536
currentIndex,
3637
) { dialog, which ->
3738
scope.launch {
39+
afterConfirm?.invoke()
3840
if (which != currentIndex) {
3941
ColorManager.setColorScheme(allIds.elementAt(which))
4042
}

0 commit comments

Comments
 (0)