Skip to content

Commit

Permalink
fix: Correctly replace emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Jul 23, 2024
1 parent 712d998 commit 685db19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

@Mixin(SelectionManager.class)
Expand All @@ -19,24 +21,29 @@ public abstract class SelectionManagerMixin {
private int selectionStart;

@Shadow
@Final
private Supplier<String> stringGetter;
private int selectionEnd;

@Shadow
public abstract void delete(int cursorOffset);
@Final
private Supplier<String> stringGetter;

@Shadow
public abstract void insert(String string);
@Final
private Consumer<String> stringSetter;

@Inject(method = "insert(Ljava/lang/String;Ljava/lang/String;)V", at = @At("TAIL"))
private void inject(String _unused, String insertion, CallbackInfo ci) {
String text = stringGetter.get();
for (EmojiCode ec : EmojiType.emojiCodes) {
if (ec.match(text, selectionStart - 1)) {
delete(-ec.getCode().length());
insert(ec.getEmoji());
break;
}
String result = stringGetter.get();
for (EmojiCode emojiCode : EmojiType.emojiCodes) {
result = result.replace(emojiCode.getCode(), emojiCode.getEmoji());
}

if (!Objects.equals(stringGetter.get(), result)) {
int lengthDifference = stringGetter.get().length() - result.length();
int newCursorPosition = Math.max(Math.min(this.selectionEnd - lengthDifference + 1, result.length()), 0);
this.selectionEnd = this.selectionStart = newCursorPosition;
}

stringSetter.accept(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,41 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Objects;

@Mixin(TextFieldWidget.class)
public abstract class TextFieldWidgetMixin {
@Shadow
private int selectionEnd;
private String text;

@Shadow
private int selectionStart;

@Shadow
public abstract void eraseCharacters(int characterOffset);
private int selectionEnd;

@Shadow
public abstract String getText();

@Shadow
protected abstract int getCursorPosWithOffset(int offset);

@Shadow
public abstract void write(String text);
protected abstract void onChanged(String newText);

@Inject(method = "charTyped", at = @At("RETURN"))
private void inject(char chr, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if (cir.getReturnValue()) {
int justTyped = getCursorPosWithOffset(-1);
for (EmojiCode ec : EmojiType.emojiCodes) {
if (ec.match(getText(), justTyped)) {
eraseCharacters(-ec.getCode().length());
//When you hold shift (which you do to type ':') it messes up when eraseCharacters trys to move the cursor back, instead extending the selection
selectionEnd = selectionStart;
write(ec.getEmoji());
break;
}
}
if (!cir.getReturnValue()) return;

String result = getText();
for (EmojiCode emojiCode : EmojiType.emojiCodes) {
result = result.replace(emojiCode.getCode(), emojiCode.getEmoji());
}

if (!Objects.equals(this.text, result)) {
int lengthDifference = this.text.length() - result.length();
int newCursorPosition = Math.max(Math.min(this.selectionEnd - lengthDifference + 1, result.length()), 0);
this.selectionEnd = this.selectionStart = newCursorPosition;
}

this.text = result;
this.onChanged(result);
}
}

0 comments on commit 685db19

Please sign in to comment.