From b3355266572deef1a5c3e494ad162c592383e455 Mon Sep 17 00:00:00 2001 From: isXander Date: Mon, 14 Aug 2023 23:27:45 +0100 Subject: More-or-less complete API for YACL auto-gen --- .../isxander/yacl3/gui/utils/UndoRedoHelper.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java (limited to 'common/src/main/java/dev/isxander/yacl3/gui/utils') 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 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) {} +} -- cgit