From 210cd7459ebe42c28c5b58b37cef60c75002158e Mon Sep 17 00:00:00 2001 From: 70CentsApple Date: Thu, 11 Jul 2024 13:38:25 +0800 Subject: [PATCH] fix: ChatHistoryNavigator would lose results when window is resized Now also calls an entire new Screen when pops up (to clear the search box) --- .../general/ChatHistoryNavigator.java | 6 +- .../utils/ChatHistoryNavigatorScreen.java | 74 +++++++++++-------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/main/java/net/apple70cents/chattools/features/general/ChatHistoryNavigator.java b/src/main/java/net/apple70cents/chattools/features/general/ChatHistoryNavigator.java index f36d779..a4c6f85 100644 --- a/src/main/java/net/apple70cents/chattools/features/general/ChatHistoryNavigator.java +++ b/src/main/java/net/apple70cents/chattools/features/general/ChatHistoryNavigator.java @@ -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; @@ -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"))); } } diff --git a/src/main/java/net/apple70cents/chattools/utils/ChatHistoryNavigatorScreen.java b/src/main/java/net/apple70cents/chattools/utils/ChatHistoryNavigatorScreen.java index a67da25..774967f 100644 --- a/src/main/java/net/apple70cents/chattools/utils/ChatHistoryNavigatorScreen.java +++ b/src/main/java/net/apple70cents/chattools/utils/ChatHistoryNavigatorScreen.java @@ -39,23 +39,19 @@ public class ChatHistoryNavigatorScreen extends Screen { @Nullable private TextFieldWidget keywordField; - private List 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); @@ -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 @@ -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 { TextUtils.MessageUnit messageUnit; @@ -202,16 +183,49 @@ public void render( } protected class ChatUnitListWidget extends EntryListWidget { - public void clearAllEntries() { - clearEntries(); + private List 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 @@ -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() {