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

96 lines
3.8 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.inceptum.gtk;
import ch.bailu.gtk.GTK;
import ch.bailu.gtk.gtk.*;
import ch.bailu.gtk.type.Str;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.api.account.MicrosoftAccount;
import java.io.IOException;
import java.util.function.Consumer;
public enum GtkEnvBackend implements LauncherEnv.EnvBackend { //TODO test
INSTANCE;
public Window dialogParent = null;
@Override
public void showError(String message, String title) {
Utils.LOGGER.error(message);
MessageDialog dialog = new MessageDialog(dialogParent, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, MessageType.ERROR, ButtonsType.CLOSE, null);
dialog.setTitle(new Str(title));
dialog.setMarkup(new Str(message));
dialog.show();
}
@Override
public void showError(String message, Throwable t) {
Utils.LOGGER.error(message, t);
MessageDialog dialog = new MessageDialog(dialogParent, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, MessageType.ERROR, ButtonsType.CLOSE, null);
dialog.setTitle(new Str(message));
dialog.setMarkup(new Str(StringFormatter.toString(t)));
dialog.show();
}
@Override
public void showInfo(String message, String title) {
Utils.LOGGER.info(message);
MessageDialog dialog = new MessageDialog(dialogParent, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, MessageType.INFO, ButtonsType.CLOSE, null);
dialog.setTitle(new Str(title));
dialog.setMarkup(new Str(message));
dialog.show();
}
@Override
public void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel) {
Utils.LOGGER.info(message);
MessageDialog dialog = new MessageDialog(dialogParent, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, MessageType.QUESTION, ButtonsType.OK_CANCEL, null);
dialog.setTitle(new Str(title));
dialog.setMarkup(new Str(message));
dialog.onResponse(response_id -> {
if (response_id == ResponseType.OK) ok.run();
else if (response_id == ResponseType.CANCEL) cancel.run();
else {
Utils.LOGGER.error("Unexpected response type: " + response_id);
cancel.run();
}
});
dialog.show();
}
@Override
public void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) throws IOException {
Dialog dialog = new Dialog();
if (dialogParent != null) dialog.setParent(dialogParent);
dialog.setModal(GTK.TRUE);
dialog.setDestroyWithParent(GTK.TRUE);
dialog.setTitle(new Str(prompt));
Box box = new Box(Orientation.VERTICAL, 0);
box.append(new Label(new Str(details)));
Entry entry = new Entry();
Editable entryEditable = new Editable(entry.cast());
entryEditable.setText(new Str(defaultValue));
box.append(entry);
dialog.setChild(box);
dialog.addButton(I18n.str("ok"), ResponseType.OK);
dialog.addButton(I18n.str("cancel"), ResponseType.CANCEL);
dialog.onResponse(response_id -> {
if (response_id == ResponseType.OK) ok.accept(entryEditable.getText().toString());
else if (response_id == ResponseType.CANCEL) cancel.run();
else {
Utils.LOGGER.error("Unexpected response type: " + response_id);
cancel.run();
}
});
dialog.show();
}
@Override
public void showLoginRefreshPrompt(MicrosoftAccount account) {
//TODO
}
}