**Symptom:**
On macOS, after selecting text in a Swing `JTextField` with the mouse from 
right to left (including the first character, and releasing just past the 
field's left edge), typing does not work normally. Each typed character 
replaces the previous one, so the field only ever shows one character. Clicking 
in the field clears the state.

**Cause:**
On macOS, `nsToJavaModifiers` reads the current button state. If a 
`MOUSE_DRAGGED` event is converted after the button is released, it has no 
button modifier, so the mouse grab is dropped and `MOUSE_RELEASED` is delivered 
to the wrong component. The text field never receives the `MOUSE_RELEASED` 
event, so its `Autoscroller` continues re-applying the drag selection which 
causes the condition where subsequently typed text appears trapped in character 
one of the field in overtype mode.

This issue has already been addressed by JetBrains in their JDK versions:
- JBR issue: [JBR-5762](https://youtrack.jetbrains.com/issue/JBR-5762)
- JBR fix commit: 
[c3cf8bddea970e9ff413b0e7ad613ed003336b21](https://github.com/JetBrains/JetBrainsRuntime/commit/c3cf8bddea970e9ff413b0e7ad613ed003336b21)
- The current code file (refer Jbr5762Fix): 
[CPlatformResponder.java](https://github.com/JetBrains/JetBrainsRuntime/blob/jbr25/src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java)

This PR/commit applies the same fix/logic as the JBR version (without JBR's 
on/off system property).

Testing:
- manual tests are problematic as it depends on system and EDT load, and race 
conditions. I was able to reproduce with manual tests, but the bug reviewer was 
not.
- automated `java.awt.Robot` tests don't trigger the condition
- however, I also tested the condition in our live java application where it's 
easier to reproduce, the error occurred after 8 attempts
- applied this fix as patch to the same working java application and proved 
that the issue did not occur (100 attempts), and that the fix properly caught 
the trigger.
- requires `noreg-hard` label added to bug report by sponsor
- if required, I can upload the test file that intermittently works for me

Further evidence:
- when running our live java app in debug mode within IntelliJ IDEA using JBR 
JDK (and default parameters) the issue does not appear.
- When running with the JBR JDK but with their 
`-Dawt.mac.enforceMouseModifiersForMouseDragged=false` setting to disable the 
JBR fix, the issue is reproduced.
- I would offer that the JBR fix is already working and proven for many users, 
and this simply applies the same fix to OpenJDK.

---------
- [x] I confirm that I make this contribution in accordance with the [OpenJDK 
Interim AI Policy](https://openjdk.org/legal/ai).

-------------

Commit messages:
 - 8396653: [macOS] MOUSE_DRAGGED can arrive without the button modifier, so 
MOUSE_RELEASED goes to the wrong component and JTextField autoscroll never 
stops  (typing keeps overwriting the first character)

Changes: https://git.openjdk.org/jdk/pull/33778/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=33778&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8396653
  Stats: 6 lines in 1 file changed: 4 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/33778.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/33778/head:pull/33778

PR: https://git.openjdk.org/jdk/pull/33778

Reply via email to