From d4bfe610a93e3aeca78143d452be663d45015d7b Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sun, 23 Jul 2023 09:04:29 +0100 Subject: [PATCH] Fix key handling in AppView - Now offering to menu if it has focus so that it can do its things. Essentially if menu is visible it take keys. - Relates #807 --- .../shell/component/view/control/AppView.java | 5 ++ .../component/view/control/AppViewTests.java | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/AppView.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/AppView.java index a32a5a05..5fc46137 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/AppView.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/control/AppView.java @@ -107,6 +107,11 @@ public class AppView extends BoxView { } return KeyHandler.resultOf(event, consumed, null); }; + + // if menu has focus, take from there + if (menu != null && menu.hasFocus()) { + return menu.getKeyHandler(); + } KeyHandler otherHandler = main != null ? main.getKeyHandler() : super.getKeyHandler(); return otherHandler.thenIfNotConsumed(handler); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/AppViewTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/AppViewTests.java index 4da93125..150d1655 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/AppViewTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/view/control/AppViewTests.java @@ -18,6 +18,9 @@ package org.springframework.shell.component.view.control; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.springframework.shell.component.view.event.KeyEvent.Key; +import org.springframework.shell.component.view.event.KeyHandler.KeyHandlerResult; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.never; @@ -63,6 +66,50 @@ class AppViewTests extends AbstractViewTests { } + @Nested + class Events { + + @Test + void shouldOfferKeysToMenuIfHavingFocus() { + BoxView smenu = spy(new BoxView()); + BoxView smain = spy(new BoxView()); + BoxView sstatus = spy(new BoxView()); + AppView sview = new AppView(smain, smenu, sstatus); + + smenu.focus(smenu, true); + + KeyHandlerResult result = handleKey(sview, Key.CursorRight); + assertThat(result).isNotNull().satisfies(r -> { + assertThat(r.event()).isNotNull(); + assertThat(r.consumed()).isFalse(); + assertThat(r.focus()).isNull(); + assertThat(r.capture()).isNull(); + }); + + verify(smain, never()).getKeyHandler(); + verify(smenu).getKeyHandler(); + } + + @Test + void shouldOfferKeysToMainIfMenuHaveNoFocus() { + BoxView smenu = spy(new BoxView()); + BoxView smain = spy(new BoxView()); + BoxView sstatus = spy(new BoxView()); + AppView sview = new AppView(smain, smenu, sstatus); + + KeyHandlerResult result = handleKey(sview, Key.CursorRight); + assertThat(result).isNotNull().satisfies(r -> { + assertThat(r.event()).isNotNull(); + assertThat(r.consumed()).isTrue(); + assertThat(r.focus()).isNull(); + assertThat(r.capture()).isNull(); + }); + + verify(smain).getKeyHandler(); + verify(smenu, never()).getKeyHandler(); + } + } + @Nested class Visibility {