Skip to content

Commit 6c5dc9c

Browse files
OnePatchGuymkartashev
authored andcommitted
JBR-9817 Wayland: text-input-unstable-v3: Japanese input text partially highlighted in search
Working around https://gitlab.gnome.org/GNOME/mutter/-/issues/3547.
1 parent c977d17 commit 6c5dc9c

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2026 JetBrains s.r.o.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package sun.awt.wl.im.text_input_unstable_v3;
27+
28+
import sun.awt.UNIXToolkit;
29+
30+
import java.awt.Toolkit;
31+
import java.util.Objects;
32+
import java.util.concurrent.atomic.AtomicReference;
33+
34+
final class CurrentDesktopInfo {
35+
36+
private CurrentDesktopInfo() {}
37+
38+
39+
public static boolean isGnome() {
40+
Boolean result = isGnome.get();
41+
42+
if (result == null) {
43+
synchronized (CurrentDesktopInfo.class) {
44+
if (Toolkit.getDefaultToolkit() instanceof UNIXToolkit unixToolkit) {
45+
result = "gnome".equals(unixToolkit.getDesktop());
46+
} else {
47+
result = false;
48+
}
49+
50+
isGnome.set(result);
51+
}
52+
53+
return result;
54+
}
55+
56+
return result;
57+
}
58+
// {@code null} if not initialized yet
59+
private static final AtomicReference<Boolean> isGnome = new AtomicReference<>(null);
60+
61+
/** negative if couldn't obtain or the desktop is not GNOME */
62+
public static int getGnomeShellMajorVersion() {
63+
Integer result = gnomeVersion.get();
64+
65+
if (result == null) {
66+
synchronized (CurrentDesktopInfo.class) {
67+
if (!isGnome()) {
68+
result = -1;
69+
} else if (Toolkit.getDefaultToolkit() instanceof UNIXToolkit unixToolkit) {
70+
try {
71+
result = Objects.requireNonNullElse(unixToolkit.getGnomeShellMajorVersion(), -1);
72+
} catch (Exception ignored) {
73+
result = -1;
74+
}
75+
} else {
76+
result = -1;
77+
}
78+
79+
gnomeVersion.set(result);
80+
}
81+
82+
return result;
83+
}
84+
85+
return result;
86+
}
87+
// {@code null} if not initialized yet, negative if couldn't obtain or the desktop is not GNOME
88+
private static final AtomicReference<Integer> gnomeVersion = new AtomicReference<>(null);
89+
}

src/java.desktop/unix/classes/sun/awt/wl/im/text_input_unstable_v3/WLInputMethodZwpTextInputV3.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,29 @@ private IncomingChanges wlGetIncomingChanges() {
11961196
return wlIncomingChanges;
11971197
}
11981198

1199+
1200+
private JavaPreeditString wlFixPreeditStringIfBroken(final JavaPreeditString preeditString) {
1201+
if (preeditString == null) {
1202+
return null;
1203+
}
1204+
1205+
final boolean isGNOME46OrBelow;
1206+
if (CurrentDesktopInfo.isGnome()) {
1207+
final int gnomeVersion = CurrentDesktopInfo.getGnomeShellMajorVersion();
1208+
isGNOME46OrBelow = gnomeVersion >= 0 && gnomeVersion <= 46;
1209+
} else {
1210+
isGNOME46OrBelow = false;
1211+
}
1212+
1213+
// https://gitlab.gnome.org/GNOME/mutter/-/issues/3547.
1214+
// Working around it here by resetting cursor_end to cursor_begin.
1215+
if (isGNOME46OrBelow) {
1216+
return new JavaPreeditString(preeditString.text(), preeditString.cursorBeginCodeUnit(), preeditString.cursorBeginCodeUnit());
1217+
}
1218+
return preeditString;
1219+
}
1220+
1221+
11991222
/** Called by {@link ClientComponentCaretPositionTracker} */
12001223
boolean wlUpdateCursorRectangle(final boolean forceUpdate) {
12011224
assert EventQueue.isDispatchThread() : "Method must only be invoked on EDT";
@@ -1427,6 +1450,7 @@ private void zwp_text_input_v3_onDone(long doneSerial) {
14271450
);
14281451
}
14291452
}
1453+
preeditStringToApplyInitializer = wlFixPreeditStringIfBroken(preeditStringToApplyInitializer);
14301454
preeditStringToApply = Objects.requireNonNullElse(preeditStringToApplyInitializer, PropertiesInitials.PREEDIT_STRING);
14311455

14321456
JavaCommitString commitStringToApplyInitializer;

0 commit comments

Comments
 (0)