Fix IME candidate window position for text fields (#399)

This commit is contained in:
Firewooddoge authored and GitHub committed 2026-11-24 19:36:49 +01:00
1 parent b2545808e6
commit abc5a05cff
4 files changed
+115 -1

No files matched your search

@@ -18,6 +18,7 @@ import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.ProgressManager;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import me.eigenraven.lwjgl3ify.CommonProxy;
import me.eigenraven.lwjgl3ify.api.InputEvents;
import me.eigenraven.lwjgl3ify.core.Config;
@@ -117,6 +118,14 @@ public class ClientProxy extends CommonProxy {
}
}
@SubscribeEvent
@SuppressWarnings("unused")
public void onRenderTick(final TickEvent.RenderTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
TextFieldHandler.updateTextInputArea();
}
}
static float lastProgress = -1.0f;
@SuppressWarnings("deprecation")
@@ -0,0 +1,7 @@
package me.eigenraven.lwjgl3ify.client;
/** @see me.eigenraven.lwjgl3ify.mixins.early.game.MixinGuiTextField */
public interface GuiTextFieldExt {
int lwjgl3ify$getCaretOffset();
}
@@ -7,11 +7,15 @@ import static org.lwjgl.system.MemoryUtil.*;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.ChatAllowedCharacters;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.sdl.SDLClipboard;
import org.lwjgl.sdl.SDL_Rect;
import org.lwjgl.system.MemoryStack;
import org.lwjglx.opengl.Display;
import me.eigenraven.lwjgl3ify.Lwjgl3ify;
@@ -25,6 +29,9 @@ public final class TextFieldHandler {
private static final WeakReference<GuiTextField> NULL_TEXT_FIELD = new WeakReference<>(null);
private static WeakReference<GuiTextField> focusedTextInput = NULL_TEXT_FIELD;
public static final StringBuilder textBuffer = new StringBuilder(8);
private static final int[] lastTextInputArea = new int[5];
private static boolean textInputAreaSent = false;
private static boolean textInputAreaSet = false;
public static boolean isTextInputActive() {
return textInputDepth > 0;
@@ -39,6 +46,7 @@ public final class TextFieldHandler {
Lwjgl3ify.LOG.info("[DEBUG] Beginning text input");
}
MainThreadExec.runOnMainThread(() -> { SDL_StartTextInput(Display.getWindow()); });
textInputAreaSent = false;
}
}
@@ -73,6 +81,66 @@ public final class TextFieldHandler {
public static void setFocusedTextField(GuiTextField textField) {
if (focusedTextInput.get() != textField) {
focusedTextInput = new WeakReference<>(textField);
textInputAreaSent = false;
}
}
public static void updateTextInputArea() {
if (textInputDepth <= 0) {
return;
}
final GuiTextField textField = focusedTextInput.get();
if (textField == null || !textField.isFocused() || !textField.getVisible()) {
if (textInputAreaSet) {
if (!MainThreadExec.runOnMainThread(() -> SDL_SetTextInputArea(Display.getWindow(), null, 0))) {
return;
}
textInputAreaSet = false;
textInputAreaSent = false;
if (Config.DEBUG_PRINT_KEY_EVENTS) {
Lwjgl3ify.LOG.info("[DEBUG] Text input area cleared");
}
}
return;
}
final Minecraft mc = Minecraft.getMinecraft();
final int scale = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight).getScaleFactor();
final boolean background = textField.getEnableBackgroundDrawing();
final int textX = background ? textField.xPosition + 4 : textField.xPosition;
final int textY = background ? textField.yPosition + (textField.height - 8) / 2 : textField.yPosition;
final float windowScale = scale / Display.getPixelScaleFactor();
final int x = Math.round(textX * windowScale);
final int y = Math.round((textY - 1) * windowScale);
final int w = Math.round(textField.getWidth() * windowScale);
final int h = Math.round((mc.fontRenderer.FONT_HEIGHT + 2) * windowScale);
final int cursor = Math.round(((GuiTextFieldExt) textField).lwjgl3ify$getCaretOffset() * windowScale);
final int[] area = lastTextInputArea;
if (textInputAreaSent && area[0] == x && area[1] == y && area[2] == w && area[3] == h && area[4] == cursor) {
return;
}
area[0] = x;
area[1] = y;
area[2] = w;
area[3] = h;
area[4] = cursor;
textInputAreaSet = true;
textInputAreaSent = MainThreadExec.runOnMainThread(() -> {
try (MemoryStack stack = MemoryStack.stackPush()) {
final SDL_Rect.Buffer rect = SDL_Rect.malloc(1, stack);
rect.get(0)
.set(x, y, w, h);
return SDL_SetTextInputArea(Display.getWindow(), rect, cursor);
}
});
if (Config.DEBUG_PRINT_KEY_EVENTS) {
Lwjgl3ify.LOG.info(
"[DEBUG] Text input area x={} y={} w={} h={} cursor={} sent={}",
x,
y,
w,
h,
cursor,
textInputAreaSent);
}
}
@@ -1,7 +1,10 @@
package me.eigenraven.lwjgl3ify.mixins.early.game;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiTextField;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -10,14 +13,41 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import me.eigenraven.lwjgl3ify.client.GuiTextFieldExt;
import me.eigenraven.lwjgl3ify.client.TextFieldHandler;
@Mixin(GuiTextField.class)
public class MixinGuiTextField {
public class MixinGuiTextField implements GuiTextFieldExt {
@Shadow
private boolean isFocused;
@Shadow
@Final
private FontRenderer field_146211_a;
@Shadow
private String text;
@Shadow
private int lineScrollOffset;
@Shadow
private int cursorPosition;
@Override
public int lwjgl3ify$getCaretOffset() {
final GuiTextField self = (GuiTextField) (Object) this;
final FontRenderer font = field_146211_a != null ? field_146211_a : Minecraft.getMinecraft().fontRenderer;
if (font == null || text == null) {
return 0;
}
final int scroll = Math.max(0, Math.min(lineScrollOffset, text.length()));
final String visible = font.trimStringToWidth(text.substring(scroll), self.getWidth());
final int caret = Math.max(0, Math.min(cursorPosition - scroll, visible.length()));
return font.getStringWidth(visible.substring(0, caret));
}
@Inject(method = "setFocused", at = @At("HEAD"))
private void lwjgl3ify$onFocusChange(boolean nowFocused, CallbackInfo ci) {
final GuiTextField self = (GuiTextField) (Object) this;