feat: press esc to go back

This commit is contained in:
Haowei Wen
2021-08-27 23:24:57 +08:00
parent a85d3b504f
commit 8230c28084

View File

@@ -57,6 +57,7 @@ import static java.util.logging.Level.WARNING;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.ui.FXUtils.newImage; import static org.jackhuang.hmcl.ui.FXUtils.newImage;
import static org.jackhuang.hmcl.ui.FXUtils.onEscPressed;
import static org.jackhuang.hmcl.util.Logging.LOG; import static org.jackhuang.hmcl.util.Logging.LOG;
import static org.jackhuang.hmcl.util.io.FileUtils.getExtension; import static org.jackhuang.hmcl.util.io.FileUtils.getExtension;
@@ -86,33 +87,39 @@ public class DecoratorController {
setupAuthlibInjectorDnD(); setupAuthlibInjectorDnD();
// pass key events to current dialog // pass key events to current dialog / current page
decorator.addEventFilter(KeyEvent.ANY, e -> { decorator.addEventFilter(KeyEvent.ANY, e -> {
if (dialogPane == null || !dialogPane.peek().isPresent()) {
return;
}
Node currentDialog = dialogPane.peek().get();
if (!(e.getTarget() instanceof Node)) { if (!(e.getTarget() instanceof Node)) {
return; return; // event source can't be determined
} }
boolean targetInDialog = false; Node newTarget;
if (dialogPane != null && dialogPane.peek().isPresent()) {
newTarget = dialogPane.peek().get(); // current dialog
} else {
newTarget = navigator.getCurrentPage(); // current page
}
boolean needsRedirect = true;
Node t = (Node) e.getTarget(); Node t = (Node) e.getTarget();
while (t != null) { while (t != null) {
if (t == currentDialog) { if (t == newTarget) {
targetInDialog = true; // current event target is in newTarget
needsRedirect = false;
break; break;
} }
t = t.getParent(); t = t.getParent();
} }
if (targetInDialog) { if (!needsRedirect) {
return; return;
} }
e.consume(); e.consume();
currentDialog.fireEvent(e.copyFor(e.getSource(), currentDialog)); newTarget.fireEvent(e.copyFor(e.getSource(), newTarget));
}); });
// press ESC to go back
onEscPressed(navigator, this::back);
} }
public Decorator getDecorator() { public Decorator getDecorator() {
@@ -229,10 +236,15 @@ public class DecoratorController {
if (navigator.getCurrentPage() instanceof DecoratorPage) { if (navigator.getCurrentPage() instanceof DecoratorPage) {
DecoratorPage page = (DecoratorPage) navigator.getCurrentPage(); DecoratorPage page = (DecoratorPage) navigator.getCurrentPage();
if (page.back()) if (page.back()) {
navigator.close(); if (navigator.canGoBack()) {
navigator.close();
}
}
} else { } else {
navigator.close(); if (navigator.canGoBack()) {
navigator.close();
}
} }
} }