From 4a2d8cc9b0310946c27ff4febf9d3e7d3c8d0ea4 Mon Sep 17 00:00:00 2001 From: crschnick Date: Wed, 10 Jul 2024 04:43:30 +0000 Subject: [PATCH] Add typed selection model --- .../app/browser/file/BrowserFileListComp.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/browser/file/BrowserFileListComp.java b/app/src/main/java/io/xpipe/app/browser/file/BrowserFileListComp.java index 052052b84..354c70b7a 100644 --- a/app/src/main/java/io/xpipe/app/browser/file/BrowserFileListComp.java +++ b/app/src/main/java/io/xpipe/app/browser/file/BrowserFileListComp.java @@ -29,10 +29,7 @@ import javafx.scene.Node; import javafx.scene.control.*; import javafx.scene.control.skin.TableViewSkin; import javafx.scene.control.skin.VirtualFlow; -import javafx.scene.input.DragEvent; -import javafx.scene.input.KeyCode; -import javafx.scene.input.MouseButton; -import javafx.scene.input.MouseEvent; +import javafx.scene.input.*; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; @@ -60,6 +57,7 @@ public final class BrowserFileListComp extends SimpleComp { private static final PseudoClass DRAG_INTO_CURRENT = PseudoClass.getPseudoClass("drag-into-current"); private final BrowserFileListModel fileList; + private final StringProperty typedSelection = new SimpleStringProperty(""); public BrowserFileListComp(BrowserFileListModel fileList) { this.fileList = fileList; @@ -130,10 +128,40 @@ public final class BrowserFileListComp extends SimpleComp { prepareTableShortcuts(table); prepareTableEntries(table); prepareTableChanges(table, mtimeCol, modeCol); + prepareTypedSelectionModel(table); return table; } + private void prepareTypedSelectionModel(TableView table) { + table.addEventHandler(KeyEvent.KEY_PRESSED,event -> { + var typed = event.getText(); + if (typed.isEmpty()) { + return; + } + + var updated = typedSelection.get() + typed; + var find = fileList.getShown().getValue().stream().filter(browserEntry -> browserEntry.getFileName().toLowerCase().startsWith(updated.toLowerCase())).findFirst(); + if (find.isEmpty()) { + typedSelection.set(""); + table.getSelectionModel().clearSelection(); + event.consume(); + return; + } + + typedSelection.set(updated); + table.scrollTo(find.get()); + table.getSelectionModel().select(find.get()); + event.consume(); + }); + + table.addEventFilter(KeyEvent.KEY_PRESSED,event -> { + if (event.getCode() == KeyCode.ESCAPE) { + typedSelection.set(""); + } + }); + } + private void prepareTableSelectionModel(TableView table) { if (!fileList.getSelectionMode().isMultiple()) { table.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);