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); <T extends Component> T reference(String ref, Class<T> type);
CompletableFuture<Window> getFuture(); CompletableFuture<Object> getFuture();
default void onOpen(WindowManager manager, Object[] args) { default void onOpen(WindowManager manager, Object[] args) {
// do nothing // 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()) { if (windows.isEmpty()) {
input.addKeyEventHandler(this::forwardKeyEventToTopWindow); input.addKeyEventHandler(this::forwardKeyEventToTopWindow);
} }
@@ -94,6 +94,10 @@ public final class WindowManager extends BaseWidget {
return window.getFuture(); 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) { private void forwardKeyEventToTopWindow(KeyEvent event) {
var topWindow = windows.peekLast(); var topWindow = windows.peekLast();
if (topWindow != null) { if (topWindow != null) {
@@ -108,7 +112,7 @@ public final class WindowManager extends BaseWidget {
} }
} }
public void close() { public void resolve(Object result) {
if (windows.isEmpty()) { if (windows.isEmpty()) {
return; return;
} }
@@ -121,7 +125,11 @@ public final class WindowManager extends BaseWidget {
input.removeKeyEventHandler(this::forwardKeyEventToTopWindow); input.removeKeyEventHandler(this::forwardKeyEventToTopWindow);
} }
window.getFuture().complete(window); window.getFuture().complete(result == null ? window : result);
}
public void close() {
resolve(null);
} }
public int size() { public int size() {
@@ -175,48 +183,48 @@ public final class WindowManager extends BaseWidget {
private void drawWindow(Screen screen, Window window, GUI gui) { private void drawWindow(Screen screen, Window window, GUI gui) {
switch (window.getWindowPosition()) { switch (window.getWindowPosition()) {
case TOP -> window.setPosition( case TOP -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
window.getMarginTop() window.getMarginTop()
); );
case TOP_RIGHT -> window.setPosition( case TOP_RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
window.getMarginTop() window.getMarginTop()
); );
case RIGHT -> window.setPosition( case RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
case BOTTOM_RIGHT -> window.setPosition( case BOTTOM_RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case BOTTOM -> window.setPosition( case BOTTOM -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case BOTTOM_LEFT -> window.setPosition( case BOTTOM_LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case LEFT -> window.setPosition( case LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
case TOP_LEFT -> window.setPosition( case TOP_LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
window.getMarginTop() window.getMarginTop()
); );
case CENTER -> window.setPosition( case CENTER -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
} }

View File

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

View File

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