From 7a7111e6c09e16a84279b913b76d3c506aae6a32 Mon Sep 17 00:00:00 2001 From: isXander Date: Thu, 7 Dec 2023 19:01:08 +0000 Subject: Fix tooltips flickering --- .../java/dev/isxander/yacl3/gui/YACLScreen.java | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'common/src/main/java/dev/isxander/yacl3') diff --git a/common/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java b/common/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java index 0bf9c65..d4de9c4 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java @@ -47,10 +47,16 @@ public class YACLScreen extends Screen { public Tooltip saveButtonTooltipMessage; private int saveButtonMessageTime; + private boolean pendingChanges; + public YACLScreen(YetAnotherConfigLib config, Screen parent) { super(config.title()); this.config = config; this.parent = parent; + + OptionUtils.forEachOptions(config, option -> { + option.addListener((opt, val) -> onOptionChanged(opt)); + }); } @Override @@ -151,16 +157,20 @@ public class YACLScreen extends Screen { } private boolean pendingChanges() { - AtomicBoolean pendingChanges = new AtomicBoolean(false); - OptionUtils.consumeOptions(config, (option) -> { - if (option.changed()) { - pendingChanges.set(true); - return true; - } - return false; + return pendingChanges; + } + + private void onOptionChanged(Option option) { + pendingChanges = false; + + OptionUtils.consumeOptions(config, opt -> { + pendingChanges |= opt.changed(); + return pendingChanges; }); - return pendingChanges.get(); + if (tabManager.getCurrentTab() instanceof CategoryTab categoryTab) { + categoryTab.updateButtons(); + } } @Override @@ -314,7 +324,6 @@ public class YACLScreen extends Screen { @Override public void tick() { - updateButtons(); descriptionWidget.tick(); } @@ -324,14 +333,14 @@ public class YACLScreen extends Screen { return tooltip; } - private void updateButtons() { + public void updateButtons() { boolean pendingChanges = pendingChanges(); undoButton.active = pendingChanges; saveFinishedButton.setMessage(pendingChanges ? Component.translatable("yacl.gui.save") : GuiUtils.translatableFallback("yacl.gui.done", CommonComponents.GUI_DONE)); - saveFinishedButton.setTooltip(Tooltip.create(pendingChanges ? Component.translatable("yacl.gui.save.tooltip") : Component.translatable("yacl.gui.finished.tooltip"))); + saveFinishedButton.setTooltip(new YACLTooltip(pendingChanges ? Component.translatable("yacl.gui.save.tooltip") : Component.translatable("yacl.gui.finished.tooltip"), saveFinishedButton)); cancelResetButton.setMessage(pendingChanges ? GuiUtils.translatableFallback("yacl.gui.cancel", CommonComponents.GUI_CANCEL) : Component.translatable("controls.reset")); - cancelResetButton.setTooltip(Tooltip.create(pendingChanges ? Component.translatable("yacl.gui.cancel.tooltip") : Component.translatable("yacl.gui.reset.tooltip"))); + cancelResetButton.setTooltip(new YACLTooltip(pendingChanges ? Component.translatable("yacl.gui.cancel.tooltip") : Component.translatable("yacl.gui.reset.tooltip"), cancelResetButton)); } } -- cgit