From cf9fedbc2042513ab77b19285ae83afbc28d5d08 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Thu, 14 Mar 2024 21:51:12 +0100 Subject: [PATCH] fix: escape key not working on auto-selected grid search box --- CHANGELOG.md | 1 + .../grid/screen/AbstractGridScreen.java | 5 ++-- .../support/widget/SearchFieldWidget.java | 27 ++++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 216ab2159..d5dae18c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Fixed External Storage not displaying empty allowlist warning. - Fixed incrementing starting from 1 in amount screens not having an intended off-by-one. - Fixed problems moving network devices with "Carry On" mod. +- Fixed escape key not working on auto-selected Grid search box. ## [2.0.0-milestone.3.3] - 2024-02-17 diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/screen/AbstractGridScreen.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/screen/AbstractGridScreen.java index bcee47e1c..a46f2213e 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/screen/AbstractGridScreen.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/grid/screen/AbstractGridScreen.java @@ -596,13 +596,12 @@ public boolean charTyped(final char unknown1, final int unknown2) { @Override public boolean keyPressed(final int key, final int scanCode, final int modifiers) { // First do the prevent sorting. - // Order matters. In Auto-selected mode, the search field will swallow the SHIFT key. + // Order matters. In auto-selected mode, the search field will swallow the SHIFT key. if (hasShiftDown() && Platform.INSTANCE.getConfig().getGrid().isPreventSortingWhileShiftIsDown()) { getMenu().getView().setPreventSorting(true); } - if (searchField != null - && (searchField.keyPressed(key, scanCode, modifiers) || searchField.canConsumeInput())) { + if (searchField != null && searchField.keyPressed(key, scanCode, modifiers)) { return true; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/widget/SearchFieldWidget.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/widget/SearchFieldWidget.java index ca7ed0b72..b67a008fc 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/widget/SearchFieldWidget.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/support/widget/SearchFieldWidget.java @@ -44,40 +44,43 @@ && mouseX < getX() + width @Override public boolean keyPressed(final int keyCode, final int scanCode, final int modifier) { final boolean canLoseFocus = Platform.INSTANCE.canEditBoxLoseFocus(this); - if (isFocused() && handleKeyCode(keyCode, canLoseFocus)) { - return true; + // The search field takes control over everything if it's active and focused. + // Calculate this here because "shouldMoveControlToParent" may change the focus. + final boolean havingControl = isActive() && isFocused(); + // Sometimes pressing a special key (like ESC) should return control to the parent. + if (havingControl && shouldMoveControlToParent(keyCode, canLoseFocus)) { + return false; } if (Platform.INSTANCE.isKeyDown(KeyMappings.INSTANCE.getFocusSearchBar()) && canLoseFocus) { - return handleFocusToggle(); + toggleFocus(); } - return super.keyPressed(keyCode, scanCode, modifier); + // Call the parent to process more special characters. + super.keyPressed(keyCode, scanCode, modifier); + return havingControl; } - private boolean handleKeyCode(final int keyCode, - final boolean canLoseFocus) { + private boolean shouldMoveControlToParent(final int keyCode, final boolean canLoseFocus) { if (keyCode == GLFW.GLFW_KEY_UP || keyCode == GLFW.GLFW_KEY_DOWN) { final String newValue = keyCode == GLFW.GLFW_KEY_UP ? history.older() : history.newer(); setValue(newValue); - return true; } else if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) { saveHistory(); if (canLoseFocus) { setFocused(false); } - return true; } else if (keyCode == GLFW.GLFW_KEY_ESCAPE) { saveHistory(); setFocused(false); - // If we can't lose focus, "fail" and let bubble it up so that the screen can be closed. - return canLoseFocus; + // If we are autoselected, we need to move control back to the parent straight away. + // If we are not autoselected, we can just unfocus (which will require another ESC press to close). + return !canLoseFocus; } return false; } - private boolean handleFocusToggle() { + private void toggleFocus() { setFocused(!isFocused()); saveHistory(); - return true; } private void saveHistory() {