Skip to content

Commit

Permalink
refactor: make sure CandidatesView will not display overflow the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 27, 2025
1 parent 0a5e944 commit 2b4736e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class CandidatesView(
val y: Float
val (horizontal, top, _, bottom) = anchorPosition
val (parentWidth, parentHeight) = parentSize
if (parentWidth <= 0 || parentHeight <= 0) {
translationX = 0f
translationY = 0f
return
}
val selfWidth = width.toFloat()
val selfHeight = height.toFloat()
val (_, inputViewHeight) =
Expand Down Expand Up @@ -186,7 +191,7 @@ class CandidatesView(

fun updateCursorAnchor(
info: CursorAnchorInfo,
updateLocation: (FloatArray, FloatArray) -> Unit,
updateDecorLocation: (FloatArray, FloatArray) -> Unit,
) {
val bounds = info.getCharacterBounds(0)
// update anchorPosition
Expand All @@ -206,8 +211,15 @@ class CandidatesView(
anchorPosition.bottom = bounds.bottom
anchorPosition.right = horizontal
}
updateDecorLocation(decorLocation, parentSize)
@Suppress("KotlinConstantConditions")
// Any component of anchorPosition can be NaN,
// meaning it will not equal itself!
if (anchorPosition != anchorPosition) {
anchorPosition.set(0f, parentSize[1], 0f, parentSize[1])
return
}
info.matrix.mapRect(anchorPosition)
updateLocation(decorLocation, parentSize)
val (dX, dY) = decorLocation
anchorPosition.offset(-dX, -dY)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,27 +349,24 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private var decorLocationUpdated = false

private fun updateDecorLocation(
@Size(2) outDecorLocation: FloatArray,
@Size(2) outContentSize: FloatArray,
@Size(2) outDecor: FloatArray,
@Size(2) outContent: FloatArray,
) {
outContentSize[0] = contentView.width.toFloat()
outContentSize[1] = contentView.height.toFloat()
if (decorLocationUpdated) return
outContent[0] = contentView.width.toFloat()
outContent[1] = contentView.height.toFloat()
decorView.getLocationOnScreen(decorLocationInt)
outDecorLocation[0] = decorLocationInt[0].toFloat()
outDecorLocation[1] = decorLocationInt[1].toFloat()
outDecor[0] = decorLocationInt[0].toFloat()
outDecor[1] = decorLocationInt[1].toFloat()
// contentSize and decorLocation can be completely wrong,
// when measuring right after the very first onStartInputView() of an IMS' lifecycle
if (outContentSize[0] > 0 && outContentSize[1] > 0) {
if (outContent[0] > 0 && outContent[1] > 0) {
decorLocationUpdated = true
}
}

override fun onUpdateCursorAnchorInfo(cursorAnchorInfo: CursorAnchorInfo) {
candidatesView?.updateCursorAnchor(cursorAnchorInfo) { outDecorLocation, outParentSize ->
if (!decorLocationUpdated) {
updateDecorLocation(outDecorLocation, outParentSize)
}
}
candidatesView?.updateCursorAnchor(cursorAnchorInfo, ::updateDecorLocation)
}

override fun onUpdateSelection(
Expand Down

0 comments on commit 2b4736e

Please sign in to comment.