2022-09-26 19:09:02 +02:00
|
|
|
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;
|
2022-09-29 16:28:05 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2022-09-26 19:09:02 +02:00
|
|
|
|
|
|
|
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);
|
2022-09-29 16:28:05 +02:00
|
|
|
simpleDialog(message, title, MessageType.ERROR, ButtonsType.CLOSE, null, null);
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showError(String message, Throwable t) {
|
2022-09-29 16:28:05 +02:00
|
|
|
simpleDialog(StringFormatter.toString(t), message, MessageType.ERROR, ButtonsType.CLOSE, null, null);
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showInfo(String message, String title) {
|
|
|
|
Utils.LOGGER.info(message);
|
2022-09-29 16:28:05 +02:00
|
|
|
simpleDialog(message, title, MessageType.INFO, ButtonsType.CLOSE, null, null);
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel) {
|
|
|
|
Utils.LOGGER.info(message);
|
2022-09-29 16:28:05 +02:00
|
|
|
simpleDialog(message, title, MessageType.QUESTION, ButtonsType.OK_CANCEL, ok, cancel);
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-09-29 16:28:05 +02:00
|
|
|
public void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) {
|
|
|
|
//TODO spacing
|
2022-09-30 18:07:18 +02:00
|
|
|
//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();
|
|
|
|
});
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void showLoginRefreshPrompt(MicrosoftAccount account) {
|
|
|
|
//TODO
|
|
|
|
}
|
2022-09-29 16:28:05 +02:00
|
|
|
|
|
|
|
private void simpleDialog(String markup, String title, int type, int buttons, Runnable ok, Runnable cancel) {
|
2022-09-30 18:07:18 +02:00
|
|
|
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();
|
|
|
|
});
|
2022-09-29 16:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-09-26 19:09:02 +02:00
|
|
|
}
|