Fix typed selection

This commit is contained in:
crschnick 2024-07-25 07:01:50 +00:00
parent 17069267da
commit a4b7d42f83

View file

@ -136,40 +136,7 @@ public final class BrowserFileListComp extends SimpleComp {
private void prepareTypedSelectionModel(TableView<BrowserEntry> table) { private void prepareTypedSelectionModel(TableView<BrowserEntry> table) {
AtomicReference<Instant> lastFail = new AtomicReference<>(); AtomicReference<Instant> lastFail = new AtomicReference<>();
table.addEventHandler(KeyEvent.KEY_PRESSED, event -> { table.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
var typed = event.getText(); updateTypedSelection(table, lastFail, event);
if (typed.isEmpty()) {
return;
}
var updated = typedSelection.get() + typed;
var found = fileList.getShown().getValue().stream()
.filter(browserEntry ->
browserEntry.getFileName().toLowerCase().startsWith(updated.toLowerCase()))
.findFirst();
if (found.isEmpty()) {
if (lastFail.get() == null) {
lastFail.set(Instant.now());
}
var inCooldown = Duration.between(lastFail.get(), Instant.now()).toMillis() < 1000;
if (inCooldown) {
lastFail.set(Instant.now());
event.consume();
return;
} else {
lastFail.set(null);
typedSelection.set(typed);
table.getSelectionModel().clearSelection();
event.consume();
return;
}
}
lastFail.set(null);
typedSelection.set(updated);
table.scrollTo(found.get());
table.getSelectionModel()
.clearAndSelect(fileList.getShown().getValue().indexOf(found.get()));
event.consume();
}); });
table.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> { table.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
@ -190,6 +157,43 @@ public final class BrowserFileListComp extends SimpleComp {
}); });
} }
private void updateTypedSelection(TableView<BrowserEntry> table, AtomicReference<Instant> lastType, KeyEvent event) {
var typed = event.getText();
if (typed.isEmpty()) {
return;
}
System.out.println(typedSelection.get() + " vs " + typed);
var updated = typedSelection.get() + typed;
var found = fileList.getShown().getValue().stream()
.filter(browserEntry ->
browserEntry.getFileName().toLowerCase().startsWith(updated.toLowerCase()))
.findFirst();
if (found.isEmpty()) {
var inCooldown = lastType.get() != null && Duration.between(lastType.get(), Instant.now()).toMillis() < 1000;
if (inCooldown) {
System.out.println("cool");
lastType.set(Instant.now());
event.consume();
return;
} else {
System.out.println("cancel");
lastType.set(null);
typedSelection.set("");
table.getSelectionModel().clearSelection();
updateTypedSelection(table, lastType, event);
return;
}
}
System.out.println("norm");
lastType.set(Instant.now());
typedSelection.set(updated);
table.scrollTo(found.get());
table.getSelectionModel().clearAndSelect(fileList.getShown().getValue().indexOf(found.get()));
event.consume();
}
private void prepareTableSelectionModel(TableView<BrowserEntry> table) { private void prepareTableSelectionModel(TableView<BrowserEntry> table) {
if (!fileList.getSelectionMode().isMultiple()) { if (!fileList.getSelectionMode().isMultiple()) {
table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);