Skip to content

Commit text composition on focus changes #2026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: jb-main
Choose a base branch
from

Conversation

m-sasha
Copy link
Member

@m-sasha m-sasha commented Apr 16, 2025

There is a difference in behavior of CInputMethod in JBR and Corretto (and possibly other JREs).
JBR ignores calls to setAWTFocussedComponent with a null component, which allows endComposition to work after a component loses focus. This is exactly what happens in the two linked issues; however in one of them it causes a bug while in the other it works around a different bug.

This PR fixes these two issues by:

  1. Forcing a call to endComposition whenever Compose gains focus. Note that this also happens when focus switches inside Compose, due to DesktopPlatformComponent calling resetFocus.
  2. Ignoring the extra InputMethodEvent that asks us to commit the composition when we receive it as the first event in a text editing session in DesktopTextInputService2.

Fixes https://youtrack.jetbrains.com/issue/CMP-7976
Fixes https://youtrack.jetbrains.com/issue/CMP-7989

Testing

Tested manually on:

    Column(
        modifier = Modifier.fillMaxSize().padding(16.dp),
    ) {
        Text("TextField2 1")
        TextField(
            state = rememberTextFieldState(""),
        )

        Spacer(Modifier.height(64.dp))
        Text("TextField2 2")
        TextField(
            state = rememberTextFieldState(""),
        )

        Spacer(Modifier.height(64.dp))
        Text("JTextField")
        SwingPanel(
            factory = {
                JTextField()
            },
            modifier = Modifier
                .size(200.dp, 60.dp)
        )
    }

The following tests were performed using 2-Set Korean input in 8 configurations.

  1. Type several composite characters in a Compose TextField
  2. With a composition in a Compose TextField: Switch to another Compose TextField - composition should be committed. Switch back and press backspace - last character should be deleted.
  3. With a composition in a Compose TextField: Switch to a JTextField, switch back and press backspace - last character should be deleted.
  4. With a composition in a Compose TextField: Switch to another window - ideally composition should be committed, but not must (doesn't work on Corretto). Switch back - composition should be committed. Press backspace - last character should be deleted.
  5. With a live composition in a JTextField: Switch to a Compose TextField, start a composition

The configurations are the combinations of:

  1. JBR/Corretto 17.0.14 and JBR/Corretto 21.0.6
  2. WindowSkiaLayerComponent (via regular singleWindowApplication) or SwingSkiaLayerComponent with compose.swing.render.on.graphics=true (via ComposePanel in a JFrame).

This should be tested by QA.
It's also a good idea to test whether everything works as before on Windows/Linux.

I discovered an additional bug, only in Corretto:
If, during a composition in a textfield, you switch away from the window and then back, the system completely stops sending mouse-press events to the window. Typing something seems to "shake" the system back into behaving normally. This seems to be caused by the endComposition call on focus-gain in CInputMethodWorkaround, but it's also present in pure Java (but to trigger it, you have to first press e.g. backspace after switching back to the window).

Release Notes

Fixes - Desktop

  • [macOS; JBR] Fixed the current composition in a text field being duplicated into another text field when switching focus to it.
  • [macOS] Fixed strange glyph being displayed in a text field if window becomes unfocused, then focused again while there's an active composition in the text field (after pressing e.g. backspace).

@m-sasha m-sasha requested a review from igordmn April 16, 2025 16:04
@m-sasha m-sasha force-pushed the m-sasha/cinputmethod-endcomposition-workaround branch from f632aa8 to 82ccbd7 Compare April 16, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant