Inceptum/launcher-gtk/src/main/java/io/gitlab/jfronny/inceptum/gtk/GtkEnvBackend.java
JFronny 5dd3c4b0a2
Some checks failed
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline failed
Reformatting, instance ordering, "Inceptum" version name
2022-11-04 17:02:24 +01:00

98 lines
3.8 KiB
Java

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 org.jetbrains.annotations.Nullable;
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);
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) {
//TODO spacing
//TODO run on main thread
GtkMain.schedule(() -> {
Dialog dialog = new Dialog();
if (dialogParent != null) dialog.setTransientFor(dialogParent);
dialog.setModal(GTK.TRUE);
if (dialogParent != null) dialog.setDestroyWithParent(GTK.TRUE);
dialog.setTitle(new Str(prompt));
Box box = dialog.getContentArea();
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.addButton(I18n.str("ok"), ResponseType.OK);
dialog.addButton(I18n.str("cancel"), ResponseType.CANCEL);
dialog.onResponse(processResponses(dialog, () -> ok.accept(entryEditable.getText().toString()), cancel));
dialog.show();
});
}
@Override
public void showLoginRefreshPrompt(MicrosoftAccount account) {
//TODO
}
private void simpleDialog(String markup, String title, int type, int buttons, Runnable ok, Runnable cancel) {
GtkMain.schedule(() -> {
MessageDialog dialog = new MessageDialog(dialogParent, DialogFlags.MODAL | DialogFlags.DESTROY_WITH_PARENT, type, buttons, null);
dialog.setTitle(new Str(title));
dialog.setMarkup(new Str(markup));
dialog.onResponse(processResponses(dialog, ok, cancel));
dialog.show();
});
}
private Dialog.OnResponse processResponses(Dialog dialog, @Nullable Runnable ok, @Nullable Runnable cancel) {
return response_id -> {
switch (response_id) {
case ResponseType.OK -> {
dialog.close();
if (ok != null) ok.run();
}
case ResponseType.CLOSE, ResponseType.CANCEL -> {
dialog.close();
if (cancel != null) cancel.run();
}
case ResponseType.DELETE_EVENT -> dialog.destroy();
default -> Utils.LOGGER.error("Unexpected response type: " + response_id);
}
};
}
}