From 4d467f45af2c9cbbd875024f02618edb4312f0db Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Thu, 26 Dec 2024 20:55:48 +0800 Subject: [PATCH] refactor: try to improve the showing of the preedit view --- .../osfans/trime/ime/composition/PreeditModule.kt | 3 ++- .../com/osfans/trime/ime/composition/PreeditUi.kt | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt b/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt index 3155f5f4cf..8139e1bb21 100644 --- a/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt +++ b/app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt @@ -64,13 +64,14 @@ class PreeditModule( PopupWindow(ui.root).apply { width = WindowManager.LayoutParams.WRAP_CONTENT height = WindowManager.LayoutParams.WRAP_CONTENT + animationStyle = 0 } override fun onInputContextUpdate(ctx: RimeProto.Context) { ui.update(ctx.composition) if (ctx.composition.length > 0) { - window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, 0, 0) val (x, y) = intArrayOf(0, 0).also { bar.view.getLocationInWindow(it) } + window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, x, y) ui.root.post { window.update(x, y - ui.root.height, -1, -1) } diff --git a/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt b/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt index ff4812a2bb..7cf2983c28 100644 --- a/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt +++ b/app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt @@ -10,6 +10,7 @@ import android.text.Spanned import android.text.style.BackgroundColorSpan import android.text.style.ForegroundColorSpan import android.view.MotionEvent +import android.view.View import android.widget.LinearLayout import android.widget.TextView import androidx.core.text.buildSpannedString @@ -62,6 +63,18 @@ open class PreeditUi( } } + private fun updateTextView( + str: CharSequence, + visible: Boolean, + ) = preedit.run { + if (visible) { + text = str + if (visibility == View.GONE) visibility = View.VISIBLE + } else if (visibility != View.GONE) { + visibility = View.GONE + } + } + fun update(inputComposition: RimeProto.Context.Composition) { val string = inputComposition.toSpannedString() val cursorPos = inputComposition.cursorPos @@ -75,6 +88,6 @@ open class PreeditUi( append(string, cursorPos, string.length) } } - if (hasPreedit) preedit.text = stringWithCursor + updateTextView(stringWithCursor, hasPreedit) } }