blob: 3328c162af44c417b19a95a29ed0a07a0fa13db6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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) {}
}
|