Skip to content

Commit

Permalink
Fix text input popup position (#339)
Browse files Browse the repository at this point in the history
* PlatformTextInputService. Undeprecate notifyFocusedRect

Fixes JetBrains/compose-multiplatform#2040
Fixes JetBrains/compose-multiplatform#2493

notifyFocusedRect was deprecated in https://android-review.googlesource.com/c/platform/frameworks/support/+/1959647

On Android, before deprecation this method was used to scroll to the focused text input.
This functionality was extracted to the text field itself, but we had another functionality on the other platforms.
On Desktop we show text input popup near text input (for some languages):
JetBrains/compose-multiplatform#2040 (comment)

I think that this method is the right choice to implement this functionality, but I am not completely sure. Here my thoughts about alternatives:

1. Use BringIntoViewRequester. Not sure that it is possible, because its purpose - to show the view to the user, not use the passed information to determine the text popup position
2. Get information about focused area from another source that is not related to text input. For example, we can inject FocusManager, and retrieve the focused rect from it. Probably not a good idea, because the rect of arbitrary focused node isn't the same thing as the rect of focused input area.

* Keep @deprecated until we don't merget this into AOSP
  • Loading branch information
igordmn committed Dec 5, 2022
1 parent 4654cb8 commit 01ea158
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ internal fun CoreTextField(
textInputService,
state,
value,
imeOptions
imeOptions,
offsetMapping
)

// The focusable modifier itself will request the entire focusable be brought into view
Expand Down Expand Up @@ -392,6 +393,19 @@ internal fun CoreTextField(
state.showCursorHandle =
manager.isSelectionHandleInVisibleBound(isStartHandle = true)
}
state.layoutResult?.let { layoutResult ->
state.inputSession?.let { inputSession ->
TextFieldDelegate.notifyFocusedRect(
value,
state.textDelegate,
layoutResult.value,
it,
inputSession,
state.hasFocus,
offsetMapping
)
}
}
}
state.layoutResult?.innerTextFieldCoordinates = it
}
Expand Down Expand Up @@ -882,11 +896,13 @@ private fun tapToFocus(
}
}

@OptIn(InternalFoundationTextApi::class)
private fun notifyTextInputServiceOnFocusChange(
textInputService: TextInputService,
state: TextFieldState,
value: TextFieldValue,
imeOptions: ImeOptions
imeOptions: ImeOptions,
offsetMapping: OffsetMapping
) {
if (state.hasFocus) {
state.inputSession = TextFieldDelegate.onFocus(
Expand All @@ -896,7 +912,21 @@ private fun notifyTextInputServiceOnFocusChange(
imeOptions,
state.onValueChange,
state.onImeActionPerformed
)
).also { newSession ->
state.layoutCoordinates?.let { coords ->
state.layoutResult?.let { layoutResult ->
TextFieldDelegate.notifyFocusedRect(
value,
state.textDelegate,
layoutResult.value,
coords,
newSession,
state.hasFocus,
offsetMapping
)
}
}
}
} else {
onBlur(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package androidx.compose.foundation.text

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.Paragraph
import androidx.compose.ui.text.SpanStyle
Expand Down Expand Up @@ -126,6 +129,57 @@ internal class TextFieldDelegate {
TextPainter.paint(canvas, textLayoutResult)
}

/**
* Notify system that focused input area.
*
* System is typically scrolled up not to be covered by keyboard.
*
* @param value The editor model
* @param textDelegate The text delegate
* @param layoutCoordinates The layout coordinates
* @param textInputSession The current input session.
* @param hasFocus True if focus is gained.
* @param offsetMapping The mapper from/to editing buffer to/from visible text.
*/
@JvmStatic
internal fun notifyFocusedRect(
value: TextFieldValue,
textDelegate: TextDelegate,
textLayoutResult: TextLayoutResult,
layoutCoordinates: LayoutCoordinates,
textInputSession: TextInputSession,
hasFocus: Boolean,
offsetMapping: OffsetMapping
) {
if (!hasFocus) {
return
}
val focusOffsetInTransformed = offsetMapping.originalToTransformed(value.selection.max)
val bbox = when {
focusOffsetInTransformed < textLayoutResult.layoutInput.text.length -> {
textLayoutResult.getBoundingBox(focusOffsetInTransformed)
}
focusOffsetInTransformed != 0 -> {
textLayoutResult.getBoundingBox(focusOffsetInTransformed - 1)
}
else -> { // empty text.
val defaultSize = computeSizeForDefaultText(
textDelegate.style,
textDelegate.density,
textDelegate.fontFamilyResolver
)
Rect(0f, 0f, 1.0f, defaultSize.height.toFloat())
}
}
val globalLT = layoutCoordinates.localToRoot(Offset(bbox.left, bbox.top))

// TODO remove `Deprecated`, if it is removed in AOSP repository
@Suppress("DEPRECATION")
textInputSession.notifyFocusedRect(
Rect(Offset(globalLT.x, globalLT.y), Size(bbox.width, bbox.height))
)
}

/**
* Called when edit operations are passed from TextInputService
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,20 @@ class TextInputSession(
}
}

@Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION")
/**
* Notify the focused rectangle to the system.
*
* The system can ignore this information or use it to show additional functionality near this rectangle.
*
* For example, desktop systems show a popup near the focused input area (for some languages).
*
* If the session is not open, no action will be performed.
*
* @param rect the rectangle that describes the boundaries on the screen that requires focus
* @return false if this session expired and no action was performed
*/
// TODO remove `Deprecated`, if it is removed in AOSP repository
@Suppress("DEPRECATION")
@Deprecated("This method should not be called, used BringIntoViewRequester instead.")
fun notifyFocusedRect(rect: Rect): Boolean = ensureOpenSession {
platformTextInputService.notifyFocusedRect(rect)
Expand Down Expand Up @@ -265,6 +278,14 @@ interface PlatformTextInputService {
*/
fun updateState(oldValue: TextFieldValue?, newValue: TextFieldValue)

/**
* Notify the focused rectangle to the system.
*
* The system can ignore this information or use it to show additional functionality near this rectangle.
*
* For example, desktop systems show a popup near the focused input area (for some languages).
*/
// TODO remove `Deprecated`, if it is removed in AOSP repository
@Deprecated("This method should not be called, used BringIntoViewRequester instead.")
fun notifyFocusedRect(rect: Rect) {
}
Expand Down

0 comments on commit 01ea158

Please sign in to comment.