Skip to content

Commit e2bd8a4

Browse files
authored
Fix InputMethodSession.getTextLocation (#2118)
1 parent 136d88c commit e2bd8a4

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/awt/Utils.desktop.kt

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.compose.ui.awt
1818

19+
import androidx.compose.ui.geometry.Rect
1920
import androidx.compose.ui.graphics.Color
2021
import androidx.compose.ui.unit.Density
2122
import androidx.compose.ui.unit.IntRect
@@ -45,18 +46,38 @@ internal fun Component.isParentOf(component: Component?): Boolean {
4546
return false
4647
}
4748

48-
internal fun IntRect.toAwtRectangle(density: Density): Rectangle {
49-
val left = floor(left / density.density).toInt()
50-
val top = floor(top / density.density).toInt()
51-
val right = ceil(right / density.density).toInt()
52-
val bottom = ceil(bottom / density.density).toInt()
53-
val width = right - left
54-
val height = bottom - top
55-
return Rectangle(
56-
left, top, width, height
57-
)
49+
internal fun toAwtRectangle(
50+
left: Float,
51+
top: Float,
52+
right: Float,
53+
bottom: Float,
54+
density: Float
55+
): Rectangle {
56+
val rleft = floor(left / density).toInt()
57+
val rtop = floor(top / density).toInt()
58+
val rright = ceil(right / density).toInt()
59+
val rbottom = ceil(bottom / density).toInt()
60+
val rwidth = rright - rleft
61+
val rheight = rbottom - rtop
62+
return Rectangle(rleft, rtop, rwidth, rheight)
5863
}
5964

65+
internal fun IntRect.toAwtRectangle(density: Density = Density(1f)) = toAwtRectangle(
66+
left = left.toFloat(),
67+
top = top.toFloat(),
68+
right = right.toFloat(),
69+
bottom = bottom.toFloat(),
70+
density = density.density
71+
)
72+
73+
internal fun Rect.toAwtRectangle(density: Density) = toAwtRectangle(
74+
left = left,
75+
top = top,
76+
right = right,
77+
bottom = bottom,
78+
density = density.density
79+
)
80+
6081
internal fun Color.toAwtColor() = java.awt.Color(red, green, blue, alpha)
6182

6283
internal fun getTransparentWindowBackground(

compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/platform/DesktopTextInputService2.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.compose.ui.platform
1818

19+
import androidx.compose.ui.awt.toAwtRectangle
1920
import androidx.compose.ui.geometry.Rect
2021
import androidx.compose.ui.scene.ComposeSceneMediator
2122
import androidx.compose.ui.text.TextRange
@@ -178,13 +179,13 @@ private class InputMethodSession(
178179
}
179180

180181
override fun getTextLocation(offset: TextHitInfo?): Rectangle? {
181-
return focusedRect?.let {
182-
val x = (it.right / component.density.density).toInt() +
183-
component.locationOnScreen.x
184-
val y = (it.top / component.density.density).toInt() +
185-
component.locationOnScreen.y
186-
Rectangle(x, y, it.width.toInt(), it.height.toInt())
187-
}
182+
val awtRect = focusedRect?.let {
183+
it.copy(left = it.right).toAwtRectangle(component.density)
184+
} ?: return null
185+
186+
val locationOnScreen = component.locationOnScreen
187+
awtRect.translate(locationOnScreen.x, locationOnScreen.y)
188+
return awtRect
188189
}
189190

190191
override fun getCommittedText(

compose/ui/ui/src/desktopTest/kotlin/androidx/compose/ui/window/window/BaseWindowTextFieldTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ open class BaseWindowTextFieldTest {
171171
BasicTextField(
172172
value = textFieldValue,
173173
onValueChange = {
174-
textFieldValue = it.also { println(it) }
174+
textFieldValue = it
175175
},
176176
modifier = Modifier.focusRequester(focusRequester)
177177
)

0 commit comments

Comments
 (0)