Skip to content

Commit ae51634

Browse files
Change preferences dialog behaviour so esc key press is not consumed by cell-based controls (ListView/TableView/TreeView/TreeTableView)
1 parent 4fd2ed0 commit ae51634

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogView.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import java.util.Locale;
44
import java.util.Optional;
55

6+
import javafx.application.Platform;
67
import javafx.fxml.FXML;
78
import javafx.scene.control.ButtonType;
89
import javafx.scene.control.ListView;
910
import javafx.scene.control.ScrollPane;
1011
import javafx.scene.control.ToggleButton;
1112
import javafx.scene.input.KeyCode;
13+
import javafx.scene.input.KeyEvent;
1214

1315
import org.jabref.gui.DialogService;
1416
import org.jabref.gui.icon.IconTheme;
@@ -115,6 +117,58 @@ private void initialize() {
115117
memoryStickMode.selectedProperty().bindBidirectional(viewModel.getMemoryStickProperty());
116118

117119
viewModel.setValues();
120+
// Delay execution until JavaFX has completed layout and the Scene object is attached to the DialogPane
121+
Platform.runLater(this::installGlobalCloseBehavior);
122+
}
123+
124+
private void installGlobalCloseBehavior() {
125+
var scene = getDialogPane().getScene();
126+
127+
// Capture events BEFORE child nodes
128+
scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
129+
if (!preferences.getKeyBindingRepository().checkKeyCombinationEquality(KeyBinding.CLOSE, e)) {
130+
return;
131+
}
132+
133+
// If any cell-based control is editing (ListView/TableView/TreeView/TreeTableView),
134+
// record that the close input is for canceling edit; DO NOT consume, let the control cancel.
135+
if (isAnyCellControlEditing()) {
136+
return;
137+
}
138+
139+
// No editing in progress --> close dialog
140+
closeDialog();
141+
e.consume();
142+
});
143+
}
144+
145+
private boolean isAnyCellControlEditing() {
146+
var root = getDialogPane();
147+
148+
for (var n : root.lookupAll(".list-view")) {
149+
if (n instanceof javafx.scene.control.ListView<?> lv && lv.getEditingIndex() != -1) {
150+
return true;
151+
}
152+
}
153+
154+
for (var n : root.lookupAll(".table-view")) {
155+
if (n instanceof javafx.scene.control.TableView<?> tv && tv.getEditingCell() != null) {
156+
return true;
157+
}
158+
}
159+
160+
for (var n : root.lookupAll(".tree-view")) {
161+
if (n instanceof javafx.scene.control.TreeView<?> trv && trv.getEditingItem() != null) {
162+
return true;
163+
}
164+
}
165+
166+
for (var n : root.lookupAll(".tree-table-view")) {
167+
if (n instanceof javafx.scene.control.TreeTableView<?> ttv && ttv.getEditingCell() != null) {
168+
return true;
169+
}
170+
}
171+
return false;
118172
}
119173

120174
@FXML

0 commit comments

Comments
 (0)