Skip to content

Commit

Permalink
fix: ChatHistoryNavigator would lose results when window is resized
Browse files Browse the repository at this point in the history
Now also calls an entire new Screen when pops up (to clear the search box)
  • Loading branch information
70CentsApple committed Jul 11, 2024
1 parent 884a076 commit 210cd74
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.client.gui.screen.ChatScreen;

public class ChatHistoryNavigator {
public static ChatHistoryNavigatorScreen navigatorScreen;
public static boolean shouldWork() {
if (!(boolean) ChatTools.CONFIG.get("general.ChatHistoryNavigator.Enabled")) {
return false;
Expand All @@ -21,9 +20,6 @@ public static boolean shouldWork() {
}

public static void popupNavigatorScreen() {
if (navigatorScreen == null) {
navigatorScreen = new ChatHistoryNavigatorScreen(TextUtils.trans("texts.ChatHistoryNavigator.title"), navigatorScreen);
}
MinecraftClient.getInstance().setScreen(navigatorScreen);
MinecraftClient.getInstance().setScreen(new ChatHistoryNavigatorScreen(TextUtils.trans("texts.ChatHistoryNavigator.title")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,19 @@
public class ChatHistoryNavigatorScreen extends Screen {
@Nullable
private TextFieldWidget keywordField;
private List<String> hashcodeResultList;
ChatUnitListWidget chatUnitListWidget;

public ChatHistoryNavigatorScreen(Text title, @Nullable ChatHistoryNavigatorScreen copyFrom) {
public ChatHistoryNavigatorScreen(Text title) {
super(title);
this.hashcodeResultList = new ArrayList<>();
if (copyFrom != null) {
this.hashcodeResultList = copyFrom.hashcodeResultList;
}
}

@Override
protected void init() {
super.init();
this.keywordField = new TextFieldWidget(this.textRenderer, 30, 35, this.width - 60, 20, this.keywordField, TextUtils.trans("texts.ChatHistoryNavigator.placeholder"));
this.keywordField.setChangedListener(keyword -> updateResultList(keyword));
updateResultList(keywordField.getText());
this.keywordField.setChangedListener(keyword -> {
this.chatUnitListWidget.setKeyword(keyword);
});

//#if MC>=11700
this.addDrawableChild(this.keywordField);
Expand All @@ -64,7 +60,7 @@ protected void init() {
//#endif
this.setInitialFocus(this.keywordField);

this.chatUnitListWidget = new ChatUnitListWidget(MinecraftClient.getInstance(), this.width - 60, this.height - 120, 65, textRenderer.fontHeight + 3);
this.chatUnitListWidget = new ChatUnitListWidget(MinecraftClient.getInstance(), this.width - 60, this.height - 120, 65, textRenderer.fontHeight + 3, this.keywordField.getText(), this.chatUnitListWidget);
//#if MC>=12000
this.chatUnitListWidget.setX(30);
//#else
Expand Down Expand Up @@ -112,26 +108,11 @@ public void render(
//$$ drawCenteredTextWithShadow(context,this.textRenderer, this.title, this.width / 2, 15, 16777215);
//#endif

if (!hashcodeResultList.isEmpty()) {
if (!chatUnitListWidget.hashcodeResultList.isEmpty()) {
chatUnitListWidget.render(context, mouseX, mouseY, delta);
}
}

protected void updateResultList(String keyword) {
hashcodeResultList.clear();
if (keyword == null || keyword.isBlank()) {
return;
}
hashcodeResultList = TextUtils.messageMap.entrySet().stream().filter(entry -> TextUtils
.wash(entry.getValue().message.getString().toLowerCase()).contains(keyword.toLowerCase()))
.map(Map.Entry::getKey).collect(Collectors.toList());

this.chatUnitListWidget.clearAllEntries();
for (String hashcode : hashcodeResultList) {
this.chatUnitListWidget.addChatUnitEntry(new ChatUnitEntry(hashcode));
}
}

protected class ChatUnitEntry extends ElementListWidget.Entry<ChatUnitEntry> {
TextUtils.MessageUnit messageUnit;

Expand Down Expand Up @@ -202,16 +183,49 @@ public void render(
}

protected class ChatUnitListWidget extends EntryListWidget<ChatUnitEntry> {
public void clearAllEntries() {
clearEntries();
private List<String> hashcodeResultList;

protected void updateResultList(String keyword) {
if (hashcodeResultList == null) {
hashcodeResultList = new ArrayList<>();
}
hashcodeResultList.clear();
if (keyword == null || keyword.isBlank()) {
return;
}
hashcodeResultList = TextUtils.messageMap.entrySet().stream().filter(entry -> TextUtils
.wash(entry.getValue().message.getString().toLowerCase()).contains(keyword.toLowerCase()))
.map(Map.Entry::getKey).collect(Collectors.toList());
}

public ChatUnitListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) {
protected void refreshUnitEntries() {
if (chatUnitListWidget == null) {
return;
}
this.clearEntries();
for (String hashcode : hashcodeResultList) {
this.addEntry(new ChatUnitEntry(hashcode));
}
}

public void setKeyword(String keyword) {
this.updateResultList(keyword);
this.refreshUnitEntries();
}

public ChatUnitListWidget(MinecraftClient client, int width, int height, int y, int itemHeight, String keyword, @Nullable ChatUnitListWidget copyFrom) {
//#if MC>=12000
super(client, width, height, y, itemHeight);
//#else
//$$ super(client, width, height, y, y + height, itemHeight);
//#endif
if (copyFrom != null) {
this.hashcodeResultList = copyFrom.hashcodeResultList;
} else {
this.hashcodeResultList = new ArrayList<>();
this.updateResultList(keyword);
}
this.refreshUnitEntries();
}

@Override
Expand All @@ -235,10 +249,6 @@ protected void drawMenuListBackground(DrawContext context) {
}
//#endif

public void addChatUnitEntry(ChatUnitEntry entry) {
this.addEntry(entry);
}

//#if MC>=11900
@Nullable
public Tooltip getTooltip() {
Expand Down

0 comments on commit 210cd74

Please sign in to comment.