Skip to content

Commit

Permalink
fix: escape key not working on auto-selected grid search box
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Mar 14, 2024
1 parent 9ffd4e4 commit 9c32ca3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,44 @@ && 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).
final boolean isAutoSelected = !canLoseFocus;
return isAutoSelected;
}
return false;
}

private boolean handleFocusToggle() {
private void toggleFocus() {
setFocused(!isFocused());
saveHistory();
return true;
}

private void saveHistory() {
Expand Down

0 comments on commit 9c32ca3

Please sign in to comment.