Make StatusBarView work with hotkeys
- StatusItem can now define a hotkey which is then bound to its action. - In catalog app replace use of raw key event to item's hotkey for status bar visibility. - Various doc updates. - Relates #826
This commit is contained in:
@@ -135,7 +135,10 @@ public class AppView extends BoxView {
|
||||
|
||||
@Override
|
||||
public KeyHandler getHotKeyHandler() {
|
||||
return menu != null ? menu.getHotKeyHandler() : super.getHotKeyHandler();
|
||||
KeyHandler mainHandler = main != null ? main.getHotKeyHandler() : super.getHotKeyHandler();
|
||||
KeyHandler menuHandler = menu != null ? menu.getHotKeyHandler() : super.getHotKeyHandler();
|
||||
KeyHandler statusHandler = status != null ? status.getHotKeyHandler() : super.getHotKeyHandler();
|
||||
return mainHandler.thenIfNotConsumed(menuHandler).thenIfNotConsumed(statusHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -121,6 +121,7 @@ public class StatusBarView extends BoxView {
|
||||
public void setItems(List<StatusItem> items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(items);
|
||||
registerHotKeys();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,6 +133,17 @@ public class StatusBarView extends BoxView {
|
||||
return items;
|
||||
}
|
||||
|
||||
private void registerHotKeys() {
|
||||
getItems().stream()
|
||||
.filter(item -> item.getHotKey() != null)
|
||||
.forEach(item -> {
|
||||
Runnable action = item.getAction();
|
||||
if (action != null) {
|
||||
registerHotKeyBinding(item.getHotKey(), action);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link StatusItem} represents an item in a {@link StatusBarView}.
|
||||
*/
|
||||
@@ -139,14 +151,32 @@ public class StatusBarView extends BoxView {
|
||||
|
||||
private String title;
|
||||
private Runnable action;
|
||||
private Integer hotKey;
|
||||
|
||||
public StatusItem(String title) {
|
||||
this(title, null);
|
||||
}
|
||||
|
||||
public StatusItem(String title, Runnable action) {
|
||||
this(title, action, null);
|
||||
}
|
||||
|
||||
public StatusItem(String title, Runnable action, Integer hotKey) {
|
||||
this.title = title;
|
||||
this.action = action;
|
||||
this.hotKey = hotKey;
|
||||
}
|
||||
|
||||
public static StatusItem of(String title) {
|
||||
return new StatusItem(title);
|
||||
}
|
||||
|
||||
public static StatusItem of(String title, Runnable action) {
|
||||
return new StatusItem(title, action);
|
||||
}
|
||||
|
||||
public static StatusItem of(String title, Runnable action, Integer hotKey) {
|
||||
return new StatusItem(title, action, hotKey);
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
@@ -157,8 +187,18 @@ public class StatusBarView extends BoxView {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(Runnable action) {
|
||||
public StatusItem setAction(Runnable action) {
|
||||
this.action = action;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getHotKey() {
|
||||
return hotKey;
|
||||
}
|
||||
|
||||
public StatusItem setHotKey(Integer hotKey) {
|
||||
this.hotKey = hotKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.shell.component.view.control.StatusBarView.StatusBarViewOpenSelectedItemEvent;
|
||||
import org.springframework.shell.component.view.control.StatusBarView.StatusItem;
|
||||
import org.springframework.shell.component.view.event.KeyEvent.Key;
|
||||
import org.springframework.shell.component.view.event.MouseEvent;
|
||||
import org.springframework.shell.component.view.event.MouseHandler.MouseHandlerResult;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
@@ -51,8 +52,23 @@ class StatusBarViewTests extends AbstractViewTests {
|
||||
|
||||
view = new StatusBarView(Arrays.asList(new StatusItem("item1")));
|
||||
assertThat(view.getItems()).hasSize(1);
|
||||
|
||||
view = new StatusBarView(Arrays.asList(StatusItem.of("item1")));
|
||||
assertThat(view.getItems()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hotkeys() {
|
||||
StatusItem item;
|
||||
|
||||
item = StatusItem.of("title");
|
||||
assertThat(item.getHotKey()).isNull();
|
||||
item.setHotKey(Key.f);
|
||||
assertThat(item.getHotKey()).isEqualTo(Key.f);
|
||||
|
||||
item = StatusItem.of("title").setHotKey(Key.f);
|
||||
assertThat(item.getHotKey()).isEqualTo(Key.f);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
Reference in New Issue
Block a user