PreeditEvent .java

net.minecraft.client.input.PreeditEvent (record, 73 lines): members: fullText, caretPosition, blocks, focusedBlock, fromSdlTextEditing, toFormattedText.

net.minecraft.client.inputrecord73 linesclient2.4 KB

API Surface (2)

record PreeditEvent(String fullText, int caretPosition, List<String> blocks, int focusedBlock)
MutableComponent toFormattedText(final Style focusedStyle)

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PreeditEvent.java73 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.input;
2
3
import com.google.common.base.Preconditions;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.ImmutableList.Builder;
6
import java.util.List;
7
import net.fabricmc.api.EnvType;
8
import net.fabricmc.api.Environment;
9
import net.minecraft.network.chat.Component;
10
import net.minecraft.network.chat.MutableComponent;
11
import net.minecraft.network.chat.Style;
12
import org.jspecify.annotations.Nullable;
13
14
@Environment(EnvType.CLIENT)
15
public record PreeditEvent(String fullText, int caretPosition, List<String> blocks, int focusedBlock) {
16
	public PreeditEvent {
17
		Preconditions.checkElementIndex(focusedBlock, blocks.size());
18
	}
19
20
	public static @Nullable PreeditEvent fromSdlTextEditing(final @Nullable String text, final int selectionStart, final int selectionLength) {
21
		if (text != null && !text.isEmpty()) {
22
			int totalCodepoints = text.codePointCount(0, text.length());
23
			int caretCodepoint = selectionStart < 0 ? totalCodepoints : Math.min(selectionStart, totalCodepoints);
24
			int caretChar = text.offsetByCodePoints(0, caretCodepoint);
25
			if (selectionLength > 0) {
26
				int selectionEndCodepoint = Math.min(caretCodepoint + selectionLength, totalCodepoints);
27
				int selectionEndChar = text.offsetByCodePoints(0, selectionEndCodepoint);
28
				if (selectionEndChar <= caretChar) {
29
					return new PreeditEvent(text, caretChar, List.of(text), 0);
30
				}
31
32
				Builder<String> blocks = ImmutableList.builder();
33
				int focusedBlock = 0;
34
				if (caretChar > 0) {
35
					blocks.add(text.substring(0, caretChar));
36
					focusedBlock = 1;
37
				}
38
39
				blocks.add(text.substring(caretChar, selectionEndChar));
40
				if (selectionEndChar < text.length()) {
41
					blocks.add(text.substring(selectionEndChar));
42
				}
43
44
				return new PreeditEvent(text, caretChar, blocks.build(), focusedBlock);
45
			} else {
46
				return new PreeditEvent(text, caretChar, ImmutableList.of(text), 0);
47
			}
48
		} else {
49
			return null;
50
		}
51
	}
52
53
	public MutableComponent toFormattedText(final Style focusedStyle) {
54
		int blockCount = this.blocks.size();
55
		if (blockCount == 1) {
56
			return Component.literal(this.blocks.getFirst()).withStyle(focusedStyle);
57
		}
58
59
		MutableComponent result = Component.empty();
60
61
		for (int i = 0; i < blockCount; i++) {
62
			MutableComponent part = Component.literal(this.blocks.get(i));
63
			if (i == this.focusedBlock) {
64
				part.withStyle(focusedStyle);
65
			}
66
67
			result.append(part);
68
		}
69
70
		return result;
71
	}
72
}
73

Decompiled Minecraft 26.3 source of PreeditEvent (net/minecraft/client/input/PreeditEvent.java) with Mojang official mappings, stored verbatim on the site.