Inceptum/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/window/dialog/ProcessStateWatcherWindow.java

58 lines
1.7 KiB
Java

package io.gitlab.jfronny.inceptum.imgui.window.dialog;
import imgui.ImGui;
import io.gitlab.jfronny.commons.throwable.ThrowingConsumer;
import io.gitlab.jfronny.inceptum.imgui.window.Window;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.atomic.AtomicBoolean;
public class ProcessStateWatcherWindow extends Window {
private final ProcessState state;
private final Runnable cancel;
private final AtomicBoolean canceled = new AtomicBoolean(false);
public ProcessStateWatcherWindow(String title, String errorMessage, ProcessState state, ThrowingConsumer<AtomicBoolean, ?> executor, @Nullable Runnable cancel) {
super(title);
this.state = state;
this.cancel = cancel;
new Thread(() -> {
try {
executor.accept(canceled);
} catch (Throwable e) {
canceled.set(true);
LauncherEnv.showError(errorMessage, e);
} finally {
close();
}
}).start();
}
public ProcessState getState() {
return state;
}
@Override
public void draw() {
ImGui.progressBar(state.progress);
ImGui.textUnformatted(state.currentStep);
if (cancel != null && ImGui.button("Cancel")) {
canceled.set(true);
close();
}
}
@Override
public void close() {
super.close();
if (canceled.get() && cancel != null) cancel.run();
}
@Override
public boolean isCloseable() {
return false;
}
}