"Fix" input dialog

This commit is contained in:
Johannes Frohnmeyer 2022-09-29 16:28:05 +02:00
parent eb3fb83673
commit b0a6146d49
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 77 additions and 38 deletions

View File

@ -8,6 +8,7 @@ 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.io.IOException;
import java.util.function.Consumer;
@ -20,71 +21,43 @@ public enum GtkEnvBackend implements LauncherEnv.EnvBackend { //TODO test
@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();
simpleDialog(message, title, MessageType.ERROR, ButtonsType.CLOSE, null, null);
}
@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();
simpleDialog(StringFormatter.toString(t), message, MessageType.ERROR, ButtonsType.CLOSE, null, null);
}
@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();
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);
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();
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) throws IOException {
public void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) {
//TODO spacing
Dialog dialog = new Dialog();
if (dialogParent != null) dialog.setParent(dialogParent);
if (dialogParent != null) dialog.setTransientFor(dialogParent);
dialog.setModal(GTK.TRUE);
dialog.setDestroyWithParent(GTK.TRUE);
if (dialogParent != null) dialog.setDestroyWithParent(GTK.TRUE);
dialog.setTitle(new Str(prompt));
Box box = new Box(Orientation.VERTICAL, 0);
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.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.onResponse(processResponses(dialog, () -> ok.accept(entryEditable.getText().toString()), cancel));
dialog.show();
}
@ -92,4 +65,29 @@ public enum GtkEnvBackend implements LauncherEnv.EnvBackend { //TODO test
public void showLoginRefreshPrompt(MicrosoftAccount account) {
//TODO
}
private void simpleDialog(String markup, String title, int type, int buttons, Runnable ok, Runnable cancel) {
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);
}
};
}
}

View File

@ -0,0 +1,41 @@
package io.gitlab.jfronny.inceptum.gtk;
import ch.bailu.gtk.GTK;
import ch.bailu.gtk.gio.ApplicationFlags;
import ch.bailu.gtk.gtk.*;
import ch.bailu.gtk.type.Str;
import ch.bailu.gtk.type.Strs;
public class TestStart {
public static void main(String[] args) {
var app = new Application(GtkMain.ID, ApplicationFlags.FLAGS_NONE);
app.onActivate(() -> {
var button = Button.newWithLabelButton(new Str("Test"));
button.onClicked(TestStart::test);
var window = new ApplicationWindow(app);
window.setDefaultSize(200, 50);
window.setTitle(GtkMain.TITLE);
window.setChild(button);
window.show();
GtkEnvBackend.INSTANCE.dialogParent = window;
window.onCloseRequest(() -> {
GtkEnvBackend.INSTANCE.dialogParent = null;
return GTK.FALSE;
});
});
System.exit(app.run(args.length, new Strs(args)));
}
private static void test() {
GtkEnvBackend backend = GtkEnvBackend.INSTANCE;
// backend.showInfo("Some message", "Title");
// backend.showError("Yes!", "AAee");
// backend.showError("Nes!", new ArrayIndexOutOfBoundsException("Top 500 cheese"));
// backend.showOkCancel("Some Message\nYes!", "TitL", () -> backend.showInfo("Pressed OK", "OK"), () -> backend.showError("Pressed CANCEL", "CANCEL"));
// backend.getInput("This is a prompt", """
// These are some extremely interesting and detailed (hence the name) details!
// This is a second line, since these details
// are just THAT important!""", "Default", value -> backend.showInfo("Received " + value, "Input"), () -> backend.showInfo("Got no input :/", "Input"));
}
}