aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl3/gui/utils
diff options
context:
space:
mode:
authorisXander <xandersmith2008@gmail.com>2023-08-14 23:27:45 +0100
committerisXander <xandersmith2008@gmail.com>2023-08-14 23:27:45 +0100
commitb3355266572deef1a5c3e494ad162c592383e455 (patch)
tree876510a21e27d0052cb7a1501c0295a427c9dc3c /common/src/main/java/dev/isxander/yacl3/gui/utils
parentd37e147dbb4db44a921533b572aed3e54b5c6a42 (diff)
downloadYetAnotherConfigLib-b3355266572deef1a5c3e494ad162c592383e455.tar.gz
YetAnotherConfigLib-b3355266572deef1a5c3e494ad162c592383e455.tar.bz2
YetAnotherConfigLib-b3355266572deef1a5c3e494ad162c592383e455.zip
More-or-less complete API for YACL auto-gen
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl3/gui/utils')
-rw-r--r--common/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java b/common/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java
new file mode 100644
index 0000000..3328c16
--- /dev/null
+++ b/common/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java
@@ -0,0 +1,42 @@
+package dev.isxander.yacl3.gui.utils;
+
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UndoRedoHelper {
+ private final List<FieldState> history = new ArrayList<>();
+ private int index = 0;
+
+ public UndoRedoHelper(String text, int cursorPos, int selectionLength) {
+ history.add(new FieldState(text, cursorPos, selectionLength));
+ }
+
+ public void save(String text, int cursorPos, int selectionLength) {
+ int max = history.size();
+ history.subList(index, max).clear();
+ history.add(new FieldState(text, cursorPos, selectionLength));
+ index++;
+ }
+
+ public @Nullable FieldState undo() {
+ index--;
+ index = Math.max(index, 0);
+
+ if (history.isEmpty())
+ return null;
+ return history.get(index);
+ }
+
+ public @Nullable FieldState redo() {
+ if (index < history.size() - 1) {
+ index++;
+ return history.get(index);
+ } else {
+ return null;
+ }
+ }
+
+ public record FieldState(String text, int cursorPos, int selectionLength) {}
+}