|
1 | 1 | package tech.tnze.mixin.client; |
2 | 2 |
|
| 3 | +import net.minecraft.client.gui.font.TextFieldHelper; |
3 | 4 | import net.minecraft.client.gui.screens.inventory.AbstractSignEditScreen; |
4 | 5 | import net.minecraft.client.input.KeyEvent; |
5 | 6 | import org.jspecify.annotations.Nullable; |
| 7 | +import org.spongepowered.asm.mixin.Final; |
6 | 8 | import org.spongepowered.asm.mixin.Mixin; |
7 | 9 | import org.spongepowered.asm.mixin.Shadow; |
8 | 10 | import org.spongepowered.asm.mixin.Unique; |
9 | 11 | import org.spongepowered.asm.mixin.injection.At; |
10 | 12 | import org.spongepowered.asm.mixin.injection.Inject; |
| 13 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
11 | 14 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
12 | 15 | import tech.tnze.client.ACPSinkRegister; |
| 16 | +import tech.tnze.client.AbstractTextFieldACP; |
13 | 17 | import tech.tnze.client.Manager; |
| 18 | +import tech.tnze.client.SignEditLineACP; |
14 | 19 | import tech.tnze.msctf.windows.win32.ui.textservices.ITextStoreACPSink; |
| 20 | +import tech.tnze.msctf.windows.win32.ui.textservices.TS_TEXTCHANGE; |
| 21 | + |
| 22 | +import java.lang.foreign.Arena; |
15 | 23 |
|
16 | 24 | import static tech.tnze.client.IMEClient.LOGGER; |
| 25 | +import static tech.tnze.msctf.WindowsException.checkResult; |
| 26 | +import static tech.tnze.msctf.windows.win32.ui.textservices.Constants.TS_SD_READONLY; |
17 | 27 |
|
18 | 28 | @Mixin(AbstractSignEditScreen.class) |
19 | 29 | public class AbstractSignEditScreenMixin implements ACPSinkRegister { |
20 | 30 | @Shadow |
21 | | - private int line; |
| 31 | + public int line; |
22 | 32 |
|
23 | | - @Inject(method = "keyPressed", at = @At(value = "RETURN")) |
| 33 | + @Shadow |
| 34 | + @Nullable |
| 35 | + public TextFieldHelper signField; |
| 36 | + |
| 37 | + @Shadow |
| 38 | + @Final |
| 39 | + public String[] messages; |
| 40 | + |
| 41 | + @Inject(method = "keyPressed", at = @At("RETURN")) |
24 | 42 | public void tnze$keyPressed(KeyEvent keyEvent, CallbackInfoReturnable<Boolean> cir) { |
25 | 43 | if (keyEvent.isUp() || keyEvent.isDown()) { |
26 | 44 | LOGGER.info("SignEditScreen line shifted to {}", this.line); |
27 | 45 | Manager.getInstance().onScreenFocusedChange((AbstractSignEditScreen) (Object) this); |
| 46 | + } else if (sinkEnabled) { |
| 47 | + var sink = textStoreSink[this.line]; |
| 48 | + if (sink != null) { |
| 49 | + checkResult(sink.OnSelectionChange()); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Inject(method = "init", at = @At("TAIL")) |
| 55 | + protected void tnze$init(CallbackInfo ci) { |
| 56 | + if (!sinkEnabled) { |
| 57 | + return; |
| 58 | + } |
| 59 | + for (int i = 0; i < textStoreSink.length; i++) { |
| 60 | + var sink = textStoreSink[i]; |
| 61 | + if (sink != null) { |
| 62 | + checkResult(sink.OnStatusChange(this.line == i && this.signField != null ? 0 : TS_SD_READONLY)); |
| 63 | + try (var arena = Arena.ofConfined()) { |
| 64 | + var change = arena.allocate(TS_TEXTCHANGE.layout()); |
| 65 | + TS_TEXTCHANGE.acpStart(change, 0); |
| 66 | + TS_TEXTCHANGE.acpOldEnd(change, 0); |
| 67 | + TS_TEXTCHANGE.acpNewEnd(change, this.messages[i].length()); |
| 68 | + checkResult(sink.OnTextChange(0, change)); |
| 69 | + } |
| 70 | + checkResult(sink.OnSelectionChange()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Unique |
| 76 | + private int changeStart, changeEnd; |
| 77 | + |
| 78 | + @Inject(method = "setMessage", at = @At("HEAD")) |
| 79 | + protected void tnze$setMessage$head(String string, CallbackInfo ci) { |
| 80 | + changeStart = 0; |
| 81 | + changeEnd = this.messages[this.line].length(); |
| 82 | + } |
| 83 | + |
| 84 | + @Inject(method = "setMessage", at = @At("TAIL")) |
| 85 | + protected void tnze$setMessage(String string, CallbackInfo ci) { |
| 86 | + if (!sinkEnabled) { |
| 87 | + return; |
| 88 | + } |
| 89 | + var sink = textStoreSink[this.line]; |
| 90 | + if (sink != null) { |
| 91 | + try (var arena = Arena.ofConfined()) { |
| 92 | + var change = arena.allocate(TS_TEXTCHANGE.layout()); |
| 93 | + TS_TEXTCHANGE.acpStart(change, changeStart); |
| 94 | + TS_TEXTCHANGE.acpOldEnd(change, changeEnd); |
| 95 | + TS_TEXTCHANGE.acpNewEnd(change, string.length()); |
| 96 | + checkResult(sink.OnTextChange(0, change)); |
| 97 | + } |
28 | 98 | } |
29 | 99 | } |
30 | 100 |
|
31 | 101 | @Unique |
32 | 102 | @Nullable |
33 | | - private ITextStoreACPSink textStoreSink; |
| 103 | + private final ITextStoreACPSink[] textStoreSink = new ITextStoreACPSink[4]; |
34 | 104 |
|
35 | 105 | @Unique |
36 | 106 | private boolean sinkEnabled = true; |
37 | 107 |
|
38 | 108 | @Override |
39 | | - public void tnze$registerACPSink(ITextStoreACPSink sink) { |
40 | | - textStoreSink = sink; |
| 109 | + public void tnze$registerACPSink(AbstractTextFieldACP acpImpl, ITextStoreACPSink sink) { |
| 110 | + int line = ((SignEditLineACP) acpImpl).getLine(); |
| 111 | + textStoreSink[line] = sink; |
41 | 112 | } |
42 | 113 |
|
43 | 114 | @Override |
44 | | - public void tnze$unregisterACPSink(ITextStoreACPSink sink) { |
45 | | - assert sink.equals(textStoreSink); |
46 | | - textStoreSink = null; |
| 115 | + public void tnze$unregisterACPSink(AbstractTextFieldACP acpImpl, ITextStoreACPSink sink) { |
| 116 | + int line = ((SignEditLineACP) acpImpl).getLine(); |
| 117 | + assert sink.equals(textStoreSink[line]); |
| 118 | + textStoreSink[line] = null; |
47 | 119 | } |
48 | 120 |
|
49 | 121 | @Override |
|
0 commit comments