Inceptum/launcher-gtk/src/main/java/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.java

99 lines
3.8 KiB
Java

package io.gitlab.jfronny.inceptum.gtk;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.gtk.window.dialog.MicrosoftLoginDialog;
import io.gitlab.jfronny.inceptum.gtk.window.dialog.StringInputDialog;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.api.account.MicrosoftAccount;
import org.gnome.gtk.*;
import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
public enum GtkEnvBackend implements LauncherEnv.EnvBackend {
INSTANCE;
public Window dialogParent = null;
@Override
public void showError(String message, String title) {
Utils.LOGGER.error(message);
simpleDialog(message, title, MessageType.ERROR, ButtonsType.CLOSE, null, null);
}
@Override
public void showError(String message, Throwable t) {
simpleDialog(StringFormatter.toString(t), message, MessageType.ERROR, ButtonsType.CLOSE, null, null);
}
@Override
public void showInfo(String message, String title) {
Utils.LOGGER.info(message);
simpleDialog(message, title, MessageType.INFO, ButtonsType.CLOSE, null, null);
}
@Override
public void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel) {
Utils.LOGGER.info(message);
simpleDialog(message, title, MessageType.QUESTION, ButtonsType.OK_CANCEL, ok, cancel);
}
@Override
public void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) {
GtkMain.schedule(() -> {
DialogFlags flags = DialogFlags.DESTROY_WITH_PARENT;
if (dialogParent != null) flags = flags.or(DialogFlags.MODAL);
StringInputDialog dialog = new StringInputDialog(
dialogParent,
flags,
MessageType.QUESTION,
ButtonsType.OK_CANCEL,
details,
defaultValue
);
dialog.title = prompt;
dialog.onResponse(processResponses(dialog, () -> ok.accept(dialog.input), cancel));
dialog.show();
});
}
@Override
public void showLoginRefreshPrompt(MicrosoftAccount account) {
GtkMain.schedule(() -> {
new MicrosoftLoginDialog(dialogParent, account).show();
});
}
private void simpleDialog(String markup, String title, MessageType type, ButtonsType buttons, Runnable ok, Runnable cancel) {
GtkMain.schedule(() -> {
simpleDialog(dialogParent, markup, title, type, buttons, ok, cancel);
});
}
public static void simpleDialog(Window parent, String markup, String title, MessageType type, ButtonsType buttons, @Nullable Runnable ok, @Nullable Runnable cancel) {
MessageDialog dialog = new MessageDialog(parent, DialogFlags.MODAL.or(DialogFlags.DESTROY_WITH_PARENT), type, buttons, null);
dialog.title = title;
dialog.markup = markup;
dialog.onResponse(processResponses(dialog, ok, cancel));
dialog.show();
}
private static Dialog.Response processResponses(Dialog dialog, @Nullable Runnable ok, @Nullable Runnable cancel) {
return responseId -> {
switch (ResponseType.of(responseId)) {
case OK -> {
dialog.close();
if (ok != null) ok.run();
}
case CLOSE, CANCEL -> {
dialog.close();
if (cancel != null) cancel.run();
}
case DELETE_EVENT -> dialog.destroy();
default -> Utils.LOGGER.error("Unexpected response type: " + responseId);
}
};
}
}