-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop next change from merging with previous one after inactive period.
- Loading branch information
1 parent
c73aacb
commit 400cade
Showing
2 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
richtextfx/src/main/java/org/fxmisc/richtext/util/UndoManagerInactivityWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package org.fxmisc.richtext.util; | ||
|
||
import javafx.beans.value.ObservableBooleanValue; | ||
import org.fxmisc.undo.UndoManager; | ||
import org.reactfx.EventStream; | ||
import org.reactfx.Subscription; | ||
import org.reactfx.value.Val; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* A wrapper around an {@link UndoManager} that prevents the next emitted change from merging with the previous | ||
* one after a period of inactivity (i.e., the UndoManager's {@code changeSource} has not emitted an event | ||
* after a specified period of time. | ||
* | ||
* @param <C> the type of change the UndoManager can undo/redo | ||
*/ | ||
final class UndoManagerInactivityWrapper<C> implements UndoManager<C> { | ||
|
||
private final UndoManager<C> delegate; | ||
private final Subscription subscription; | ||
|
||
/** | ||
* Wraps an {@link UndoManager} and prevents the next emitted change from merging with the previous one | ||
* after a period of inactivity (i.e., the {@code changeSource} has not emitted an event for | ||
* {@code preventMergeDelay}). <b>Note:</b> there is no check that insures that the {@code changeSource} | ||
* parameter is the same one used by the {@code undoManager} parameter | ||
*/ | ||
public UndoManagerInactivityWrapper(UndoManager<C> undoManager, EventStream<C> changeSource, Duration preventMergeDelay) { | ||
this.delegate = undoManager; | ||
subscription = changeSource.successionEnds(preventMergeDelay).subscribe(ignore -> preventMerge()); | ||
} | ||
|
||
@Override | ||
public boolean undo() { | ||
return delegate.undo(); | ||
} | ||
|
||
@Override | ||
public boolean redo() { | ||
return delegate.redo(); | ||
} | ||
|
||
@Override | ||
public Val<Boolean> undoAvailableProperty() { | ||
return delegate.undoAvailableProperty(); | ||
} | ||
|
||
@Override | ||
public boolean isUndoAvailable() { | ||
return delegate.isUndoAvailable(); | ||
} | ||
|
||
@Override | ||
public Val<C> nextToUndoProperty() { | ||
return delegate.nextToUndoProperty(); | ||
} | ||
|
||
@Override | ||
public C getNextToUndo() { | ||
return delegate.getNextToUndo(); | ||
} | ||
|
||
@Override | ||
public Val<C> nextToRedoProperty() { | ||
return delegate.nextToRedoProperty(); | ||
} | ||
|
||
@Override | ||
public C getNextToRedo() { | ||
return delegate.getNextToRedo(); | ||
} | ||
|
||
@Override | ||
public Val<Boolean> redoAvailableProperty() { | ||
return delegate.redoAvailableProperty(); | ||
} | ||
|
||
@Override | ||
public boolean isRedoAvailable() { | ||
return delegate.isRedoAvailable(); | ||
} | ||
|
||
@Override | ||
public ObservableBooleanValue performingActionProperty() { | ||
return delegate.performingActionProperty(); | ||
} | ||
|
||
@Override | ||
public boolean isPerformingAction() { | ||
return delegate.isPerformingAction(); | ||
} | ||
|
||
@Override | ||
public void preventMerge() { | ||
delegate.preventMerge(); | ||
} | ||
|
||
@Override | ||
public void forgetHistory() { | ||
delegate.forgetHistory(); | ||
} | ||
|
||
@Override | ||
public UndoPosition getCurrentPosition() { | ||
return delegate.getCurrentPosition(); | ||
} | ||
|
||
@Override | ||
public void mark() { | ||
delegate.mark(); | ||
} | ||
|
||
@Override | ||
public ObservableBooleanValue atMarkedPositionProperty() { | ||
return delegate.atMarkedPositionProperty(); | ||
} | ||
|
||
@Override | ||
public boolean isAtMarkedPosition() { | ||
return delegate.isAtMarkedPosition(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
subscription.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters