package io.gitlab.jfronny.inceptum.gtk; import org.gtk.gtk.*; 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 ok, Runnable cancel) { //TODO spacing //TODO run on main thread GtkMain.schedule(() -> { Dialog dialog = new Dialog(); if (dialogParent != null) dialog.transientFor = dialogParent; dialog.modal = true; if (dialogParent != null) dialog.destroyWithParent = true; dialog.title = prompt; Box box = dialog.contentArea; box.append(new Label(details)); Entry entry = new Entry(); Editable entryEditable = (Editable) entry; entryEditable.text = defaultValue; box.append(entry); dialog.addButton(I18n.get("ok"), ResponseType.OK.getValue()); dialog.addButton(I18n.get("cancel"), ResponseType.CANCEL.getValue()); dialog.onResponse(processResponses(dialog, () -> ok.accept(entryEditable.text.toString()), cancel)); dialog.show(); }); } @Override public void showLoginRefreshPrompt(MicrosoftAccount account) { //TODO } private void simpleDialog(String markup, String title, MessageType type, ButtonsType buttons, Runnable ok, Runnable cancel) { GtkMain.schedule(() -> { MessageDialog dialog = new MessageDialog(dialogParent, 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 Dialog.Response processResponses(Dialog dialog, @Nullable Runnable ok, @Nullable Runnable cancel) { return response_id -> { switch (ResponseType.of(response_id)) { 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: " + response_id); } }; } }