Skip to content

Commit

Permalink
Fix Input methods on JBR, disable input methods when we lose focus
Browse files Browse the repository at this point in the history
1. Fixes JetBrains/compose-multiplatform#2628
2. Doesn't show input methods popup if there is no focused TextField (only on Windows for now, on macOs, Swing seems has a bug: JetBrains/compose-multiplatform#3839)

## Testing
Tested manually on Windows/macOs/Linux, OpenJDK/JBR, Accessibility enabled/disabled
  • Loading branch information
igordmn committed Oct 19, 2023
1 parent 54577e1 commit a435648
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,32 @@ internal abstract class ComposeBridge(

private var window: Window? = null

private inner class InputComponent : Component() {
fun requestFocusTemporary(): Boolean {
return super.requestFocus(true)
}
}

private val _inputComponent = InputComponent()
val inputComponent: Component get() = _inputComponent

private val platformComponent: PlatformComponent = object : PlatformComponent {
override fun enableInput(inputMethodRequests: InputMethodRequests) {
currentInputMethodRequests = inputMethodRequests
component.enableInputMethods(true)
val focusGainedEvent = FocusEvent(focusComponentDelegate, FocusEvent.FOCUS_GAINED)
component.inputContext.dispatchEvent(focusGainedEvent)
if (component.isFocusOwner) {
_inputComponent.requestFocusTemporary()
component.requestFocus()
}
}

override fun disableInput() {
currentInputMethodRequests = null
component.enableInputMethods(false)
if (component.isFocusOwner) {
_inputComponent.requestFocusTemporary()
component.requestFocus()
}
}

override val locationOnScreen: Point
Expand Down Expand Up @@ -181,6 +197,7 @@ internal abstract class ComposeBridge(

@OptIn(ExperimentalComposeUiApi::class)
protected fun attachComposeToComponent() {
component.enableInputMethods(false)
component.addInputMethodListener(object : InputMethodListener {
override fun caretPositionChanged(event: InputMethodEvent?) {
if (isDisposed) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ComposePanel @ExperimentalComposeUiApi constructor(
if (bridge != null) {
bridge!!.dispose()
super.remove(bridge!!.component)
super.remove(bridge!!.inputComponent)
bridge = null
}
}
Expand Down Expand Up @@ -187,6 +188,7 @@ class ComposePanel @ExperimentalComposeUiApi constructor(
if (bridge == null) {
bridge = createComposeBridge()
initContent()
super.add(bridge!!.inputComponent, Integer.valueOf(1))
super.add(bridge!!.component, Integer.valueOf(1))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ internal class ComposeWindowDelegate(

init {
layout = null
super.add(bridge.inputComponent, 1)
super.add(bridge.component, 1)
}

fun dispose() {
super.remove(bridge.component)
super.remove(bridge.inputComponent)
}
}

Expand Down

0 comments on commit a435648

Please sign in to comment.