Skip to content

Commit

Permalink
refactor: try to improve the showing of the preedit view
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Dec 29, 2024
1 parent a6e4633 commit 4d467f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -75,6 +88,6 @@ open class PreeditUi(
append(string, cursorPos, string.length)
}
}
if (hasPreedit) preedit.text = stringWithCursor
updateTextView(stringWithCursor, hasPreedit)
}
}

0 comments on commit 4d467f4

Please sign in to comment.