Skip to content

Commit 0adff9a

Browse files
authored
Added overwrite mode (#1051)
1 parent cb963f7 commit 0adff9a

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledAreaBehavior.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static org.fxmisc.wellbehaved.event.template.InputMapTemplate.*;
99
import static org.reactfx.EventStreams.*;
1010

11-
import java.util.Arrays;
1211
import java.util.function.Predicate;
1312

1413
import javafx.event.Event;
@@ -90,7 +89,9 @@ class GenericStyledAreaBehavior {
9089
consume(keyPressed(SHORTCUT_Z), (b, e) -> b.view.undo()),
9190
consume(
9291
anyOf(keyPressed(SHORTCUT_Y), keyPressed(SHORTCUT_SHIFT_Z)),
93-
(b, e) -> b.view.redo())
92+
(b, e) -> b.view.redo()),
93+
// insert/overwrite
94+
consume(keyPressed(INSERT), GenericStyledAreaBehavior::toggelOverwriteMode)
9495
);
9596
InputMapTemplate<GenericStyledAreaBehavior, KeyEvent> edits = when(b -> b.view.isEditable(), editsBase);
9697

@@ -299,6 +300,11 @@ private enum DragState {
299300

300301
private final GenericStyledArea<?, ?, ?> view;
301302

303+
/**
304+
* Indicates weather the area is in overwrite or insert mode.
305+
*/
306+
private boolean overwriteMode = false;
307+
302308
/**
303309
* Indicates whether an existing selection is being dragged by the user.
304310
*/
@@ -353,7 +359,19 @@ private void keyTyped(KeyEvent event) {
353359
return;
354360
}
355361

356-
view.replaceSelection(text);
362+
IndexRange range = view.getSelection();
363+
int start = range.getStart();
364+
int end = range.getEnd();
365+
366+
if (overwriteMode && start == end) {
367+
end = Math.min(end+1, view.getLength());
368+
}
369+
370+
view.replaceText(start, end, text);
371+
}
372+
373+
private void toggelOverwriteMode(KeyEvent ignore) {
374+
overwriteMode = !overwriteMode;
357375
}
358376

359377
private void deleteBackward(KeyEvent ignore) {

0 commit comments

Comments
 (0)