Add support for completable future to carry the Window result

This commit is contained in:
2023-11-02 11:21:09 +01:00
parent 85b4bd96e2
commit 4e07ac963c
4 changed files with 47 additions and 23 deletions

View File

@@ -13,7 +13,7 @@ public interface Window extends Widget {
<T extends Component> T reference(String ref, Class<T> type);
CompletableFuture<Window> getFuture();
CompletableFuture<Object> getFuture();
default void onOpen(WindowManager manager, Object[] args) {
// do nothing

View File

@@ -82,7 +82,7 @@ public final class WindowManager extends BaseWidget {
}
}
public CompletableFuture<Window> open(@NonNull Window window, Object... args) {
public CompletableFuture<Object> open(@NonNull Window window, Object... args) {
if (windows.isEmpty()) {
input.addKeyEventHandler(this::forwardKeyEventToTopWindow);
}
@@ -94,6 +94,10 @@ public final class WindowManager extends BaseWidget {
return window.getFuture();
}
public <T> CompletableFuture<T> openForResult(Class<T> resultClass, @NonNull Window window, Object... args) {
return open(window, args).thenApply(resultClass::cast);
}
private void forwardKeyEventToTopWindow(KeyEvent event) {
var topWindow = windows.peekLast();
if (topWindow != null) {
@@ -108,7 +112,7 @@ public final class WindowManager extends BaseWidget {
}
}
public void close() {
public void resolve(Object result) {
if (windows.isEmpty()) {
return;
}
@@ -121,7 +125,11 @@ public final class WindowManager extends BaseWidget {
input.removeKeyEventHandler(this::forwardKeyEventToTopWindow);
}
window.getFuture().complete(window);
window.getFuture().complete(result == null ? window : result);
}
public void close() {
resolve(null);
}
public int size() {

View File

@@ -37,6 +37,10 @@ public abstract class BaseContainer extends BaseComponent {
component.setParent(null);
}
public void removeAllChildren() {
this.children.clear();
}
protected float maxChildrenWidth() {
var theWidestChild = 0.0f;

View File

@@ -23,7 +23,7 @@ public abstract class BaseWindow extends BaseWidget implements Window {
protected WindowPosition windowPosition = WindowPosition.CENTER;
@Getter
protected CompletableFuture<Window> future;
protected CompletableFuture<Object> future;
protected BaseWindow(Context context, GUI gui, Map<String, Component> refs) {
this.context = context;
@@ -31,6 +31,18 @@ public abstract class BaseWindow extends BaseWidget implements Window {
this.refs = refs;
}
protected void close() {
if (manager != null) {
manager.close();
}
}
protected void resolve(Object result) {
if (manager != null) {
manager.resolve(result);
}
}
@Override
public void setContent(Component component) {
if (this.content != null) {