Improve selection model of list view:

* Clear selection if an empty space is clicked
* only open context menu if an item is selected
This commit is contained in:
Armin Schrenk
2021-03-30 11:25:22 +02:00
parent 2be7a050a4
commit 3b4f6276b5
3 changed files with 17 additions and 1 deletions

View File

@@ -25,6 +25,8 @@ import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.util.Arrays;
import java.util.Optional;
@@ -90,6 +92,19 @@ public class VaultListController implements FxController {
}
}
});
vaultList.addEventFilter(MouseEvent.MOUSE_RELEASED, this::deselect);
vaultList.addEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, request -> {
if (selectedVault.get() == null) {
request.consume();
}
});
}
private void deselect(MouseEvent released) {
if (released.getY() > (vaultList.getItems().size() * vaultList.fixedCellSizeProperty().get())) {
vaultList.getSelectionModel().clearSelection();
released.consume();
}
}
private void selectedVaultDidChange(@SuppressWarnings("unused") ObservableValue<? extends Vault> observableValue, @SuppressWarnings("unused") Vault oldValue, Vault newValue) {

View File

@@ -15,7 +15,7 @@
fx:controller="org.cryptomator.ui.mainwindow.VaultListController"
minWidth="206">
<StackPane VBox.vgrow="ALWAYS">
<ListView fx:id="vaultList" editable="true">
<ListView fx:id="vaultList" editable="true" fixedCellSize="60">
<contextMenu>
<ContextMenu>
<items>

View File

@@ -13,6 +13,7 @@
prefWidth="200"
spacing="12"
alignment="CENTER_LEFT">
<!-- Remark Check the containing list view for a fixed cell size before editing height properties -->
<padding>
<Insets topRightBottomLeft="12"/>
</padding>