-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ViewComponent can take view and drive it as non-fullscreen - ViewDoneEvent which InputView now uses - ComponentUiCommands is a sample where we add ideas for views in flow components - Allow View to set eventloop - Relates #850
- Loading branch information
Showing
6 changed files
with
201 additions
and
8 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
spring-shell-core/src/main/java/org/springframework/shell/component/ViewComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.shell.component; | ||
|
||
import org.jline.terminal.Terminal; | ||
|
||
import org.springframework.messaging.Message; | ||
import org.springframework.shell.component.view.TerminalUI; | ||
import org.springframework.shell.component.view.control.View; | ||
import org.springframework.shell.component.view.control.ViewDoneEvent; | ||
import org.springframework.shell.component.view.event.EventLoop; | ||
import org.springframework.shell.component.view.message.ShellMessageBuilder; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Handles view execution in a non-fullscreen setup. | ||
* | ||
* @author Janne Valkealahti | ||
*/ | ||
public class ViewComponent { | ||
|
||
private final Terminal terminal; | ||
private final View view; | ||
private EventLoop eventLoop; | ||
|
||
public ViewComponent(Terminal terminal, View view) { | ||
Assert.notNull(terminal, "terminal must be set"); | ||
Assert.notNull(view, "view must be set"); | ||
this.terminal = terminal; | ||
this.view = view; | ||
} | ||
|
||
/** | ||
* Run a view execution loop. | ||
*/ | ||
public void run() { | ||
TerminalUI ui = new TerminalUI(terminal); | ||
eventLoop = ui.getEventLoop(); | ||
eventLoop.onDestroy(eventLoop.viewEvents(ViewDoneEvent.class, view) | ||
.subscribe(event -> { | ||
exit(); | ||
} | ||
)); | ||
view.setEventLoop(eventLoop); | ||
ui.setRoot(view, false); | ||
ui.run(); | ||
} | ||
|
||
/** | ||
* Request exit from an execution loop. | ||
*/ | ||
public void exit() { | ||
if (eventLoop == null) { | ||
return; | ||
} | ||
Message<String> msg = ShellMessageBuilder.withPayload("int") | ||
.setEventType(EventLoop.Type.SYSTEM) | ||
.setPriority(0) | ||
.build(); | ||
eventLoop.dispatch(msg); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ll-core/src/main/java/org/springframework/shell/component/view/control/ViewDoneEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.shell.component.view.control; | ||
|
||
/** | ||
* Spesialisation of a {@link ViewEvent} indicating that a {@link View} is done. | ||
* | ||
* @author Janne Valkealahti | ||
*/ | ||
public interface ViewDoneEvent extends ViewEvent { | ||
|
||
/** | ||
* Create a generic event with empty view args. | ||
* | ||
* @param view the view | ||
* @return a generic view done event | ||
*/ | ||
static ViewDoneEvent of(View view) { | ||
return new GenericViewDoneEvent(view, ViewEventArgs.EMPTY); | ||
} | ||
|
||
record GenericViewDoneEvent(View view, ViewEventArgs args) implements ViewDoneEvent { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ommands/src/main/java/org/springframework/shell/samples/standard/ComponentUiCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.shell.samples.standard; | ||
|
||
import org.springframework.shell.command.annotation.Command; | ||
import org.springframework.shell.component.ViewComponent; | ||
import org.springframework.shell.component.view.control.InputView; | ||
import org.springframework.shell.standard.AbstractShellComponent; | ||
|
||
@Command | ||
public class ComponentUiCommands extends AbstractShellComponent { | ||
|
||
@Command(command = "componentui string") | ||
public String stringInput() { | ||
InputView view = new InputView(); | ||
view.setRect(0, 0, 10, 1); | ||
ViewComponent component = new ViewComponent(getTerminal(), view); | ||
component.run(); | ||
String input = view.getInputText(); | ||
return String.format("Input was '%s'", input); | ||
} | ||
|
||
} |