Skip to content

Commit

Permalink
fix: attempt to correct the wrong popup window position on moving
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed May 8, 2024
1 parent a969f5d commit c484d7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
88 changes: 52 additions & 36 deletions app/src/main/java/com/osfans/trime/ime/composition/Composition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.text.style.UnderlineSpan
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.TextView
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
Expand All @@ -42,10 +43,12 @@ import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.TextInputManager
import com.osfans.trime.util.sp
import splitties.dimensions.dp
import kotlin.math.absoluteValue

/** 編碼區,顯示已輸入的按鍵編碼,可使用方向鍵或觸屏移動光標位置 */
@SuppressLint("AppCompatCustomView")
class Composition(context: Context, attrs: AttributeSet?) : TextView(context, attrs) {
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
private val theme = ThemeManager.activeTheme
private val textInputManager = TextInputManager.instanceOrNull()

Expand Down Expand Up @@ -92,10 +95,11 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
private val highlightBackColorSpan by lazy { highlightBackColor?.let { BackgroundColorSpan(it) } }

private var firstMove = true
private var mDx = 0f
private var mDy = 0f
private var mCurrentX = 0
private var mCurrentY = 0
private var deltaX = 0f
private var deltaY = 0f
private var initialX = 0f
private var initialY = 0f
private var onActionMove: ((Float, Float) -> Unit)? = null

private val stickyLines: Int
get() =
Expand Down Expand Up @@ -201,46 +205,58 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
val action = event.action

if (action == MotionEvent.ACTION_UP) {
var n = getOffsetForPosition(event.x, event.y)
if (n in preeditRange[0]..preeditRange[1]) {
val s =
text
.toString()
.substring(n, preeditRange[1])
.replace(" ", "")
.replace("", "")
n = Rime.getRimeRawInput()!!.length - s.length // 從右側定位
Rime.setCaretPos(n)
TrimeInputMethodService.getService().updateComposing()
return true
val touched = getOffsetForPosition(event.x, event.y)
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
if (movable != Movable.NEVER) {
if (touched in movableRange[0]..movableRange[1]) {
if (firstMove || movable == Movable.ONCE) {
initialX = event.rawX
initialY = event.rawY
firstMove = false
}
deltaX = initialX - event.rawX
deltaY = initialY - event.rawY
return true
}
}
}
} else if (movable != Movable.NEVER &&
(action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_DOWN)
) {
val n = getOffsetForPosition(event.x, event.y)
if (n in movableRange[0]..movableRange[1]) {
if (action == MotionEvent.ACTION_DOWN) {
if (firstMove || movable == Movable.ONCE) {
firstMove = false
getLocationOnScreen(intArrayOf(mCurrentX, mCurrentY))
MotionEvent.ACTION_MOVE -> {
if (movable != Movable.NEVER) {
if (touched in movableRange[0]..movableRange[1]) {
initialX = event.rawX + deltaX
initialY = event.rawY + deltaY
val absX = initialX.absoluteValue
val absY = initialY.absoluteValue
if (absX > touchSlop || absY > touchSlop) {
onActionMove?.invoke(initialX, initialY)
}
return true
}
mDx = mCurrentX - event.rawX
mDy = mCurrentY - event.rawY
} else { // MotionEvent.ACTION_MOVE
mCurrentX = (event.rawX + mDx).toInt()
mCurrentY = (event.rawY + mDy).toInt()
TrimeInputMethodService.getService().updatePopupWindow(mCurrentX, mCurrentY)
}
return true
}
MotionEvent.ACTION_UP -> {
if (touched in preeditRange[0]..preeditRange[1]) {
val s =
text
.toString()
.substring(touched, preeditRange[1])
.replace(" ", "")
.replace("", "")
val newPos = Rime.getRimeRawInput()!!.length - s.length // 從右側定位
Rime.setCaretPos(newPos)
TrimeInputMethodService.getService().updateComposing()
return true
}
}
}

return super.onTouchEvent(event)
}

fun setOnActionMoveListener(listener: ((Float, Float) -> Unit)? = null) {
onActionMove = listener
}

private fun SpannableStringBuilder.buildSpannedComposition(
m: CompositionComponent,
composition: RimeComposition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class CompositionPopupWindow(
val composition =
CompositionRootBinding.inflate(LayoutInflater.from(ctx)).apply {
root.visibility = if (isPopupWindowEnabled) View.VISIBLE else View.GONE
compositionView.setOnActionMoveListener { x, y ->
updatePopupWindow(x.toInt(), y.toInt())
}
}

// 悬浮窗口是否可移動
Expand Down

0 comments on commit c484d7f

Please sign in to comment.