Merge pull request #175 from nking07049925/fix_tab_switch_undo

Fix undo/redo when switching between tabs
This commit is contained in:
Ben Fry
2021-06-14 15:19:38 -04:00
committed by GitHub
2 changed files with 26 additions and 5 deletions
+17 -1
View File
@@ -25,6 +25,7 @@
package processing.app;
import java.io.*;
import java.util.Stack;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
@@ -64,7 +65,15 @@ public class SketchCode {
* Editor.undo will be set to this object when this code is the tab
* that's currently the front.
*/
private UndoManager undo = new UndoManager();
private final UndoManager undo = new UndoManager();
/**
* Caret positions for this tab.
* Editor.caretUndoStack and Editor.caretRedoStack will be set to these
* when this code is the tab that's currently the front.
*/
private final Stack<Integer> caretUndoStack = new Stack<>();
private final Stack<Integer> caretRedoStack = new Stack<>();
/** What was on top of the undo stack when last saved. */
// private UndoableEdit lastEdit;
@@ -238,6 +247,13 @@ public class SketchCode {
return undo;
}
public Stack<Integer> getCaretRedoStack() {
return caretRedoStack;
}
public Stack<Integer> getCaretUndoStack() {
return caretUndoStack;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+9 -4
View File
@@ -138,8 +138,11 @@ public abstract class Editor extends JFrame implements RunnerListener {
/** Menu Actions updated on the opening of the edit menu. */
protected List<UpdatableAction> editMenuUpdatable = new ArrayList<>();
/** The currently selected tab's undo manager */
/** The currently selected tab's undo manager and caret positions*/
private UndoManager undo;
// maintain caret position during undo operations
private Stack<Integer> caretUndoStack = new Stack<>();
private Stack<Integer> caretRedoStack = new Stack<>();
// used internally for every edit. Groups hotkey-event text manipulations and
// groups multi-character inputs into a single undos.
private CompoundEdit compoundEdit;
@@ -148,9 +151,6 @@ public abstract class Editor extends JFrame implements RunnerListener {
private TimerTask endUndoEvent;
// true if inserting text, false if removing text
private boolean isInserting;
// maintain caret position during undo operations
private final Stack<Integer> caretUndoStack = new Stack<>();
private final Stack<Integer> caretRedoStack = new Stack<>();
private FindReplace find;
JMenu toolsMenu;
@@ -1904,7 +1904,12 @@ public abstract class Editor extends JFrame implements RunnerListener {
// textarea.requestFocus(); // get the caret blinking
textarea.requestFocusInWindow(); // required for caret blinking
// end edits in the previous tab
endTextEditHistory();
// update the UndoManager and caret positions to the selected tab
this.undo = code.getUndo();
caretUndoStack = code.getCaretUndoStack();
caretRedoStack = code.getCaretRedoStack();
undoAction.updateUndoState();
redoAction.updateRedoState();
}