Skip to content

Commit 9437ed8

Browse files
authored
Activate context menu on key press (#4004)
1 parent 1a7f968 commit 9437ed8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import java.util.function.BiConsumer;
44
import java.util.function.Function;
55

6+
import javafx.geometry.Bounds;
7+
import javafx.geometry.Point2D;
68
import javafx.scene.control.ContextMenu;
79
import javafx.scene.control.TableRow;
810
import javafx.scene.control.TableView;
911
import javafx.scene.control.TreeTableCell;
1012
import javafx.scene.input.DragEvent;
13+
import javafx.scene.input.KeyCode;
14+
import javafx.scene.input.KeyEvent;
1115
import javafx.scene.input.MouseDragEvent;
1216
import javafx.scene.input.MouseEvent;
1317
import javafx.util.Callback;
@@ -101,6 +105,23 @@ public TableRow<S> call(TableView<S> tableView) {
101105
}
102106
event.consume();
103107
});
108+
109+
// Activate context menu if user presses the "context menu" key
110+
tableView.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
111+
boolean rowFocused = !row.isEmpty() && tableView.getFocusModel().getFocusedIndex() == row.getIndex();
112+
if (event.getCode() == KeyCode.CONTEXT_MENU && rowFocused) {
113+
// Get center of focused cell
114+
Bounds anchorBounds = row.getBoundsInParent();
115+
double x = anchorBounds.getMinX() + anchorBounds.getWidth() / 2;
116+
double y = anchorBounds.getMinY() + anchorBounds.getHeight() / 2;
117+
Point2D screenPosition = row.getParent().localToScreen(x, y);
118+
119+
if (row.getContextMenu() == null) {
120+
row.setContextMenu(contextMenuFactory.apply(row.getItem()));
121+
}
122+
row.getContextMenu().show(row, screenPosition.getX(), screenPosition.getY());
123+
}
124+
});
104125
}
105126

106127
if (toOnDragDetected != null) {

0 commit comments

Comments
 (0)