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

190 lines
7.5 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.inceptum.gtk;
import ch.bailu.gtk.GTK;
import ch.bailu.gtk.gio.ApplicationFlags;
import ch.bailu.gtk.gio.Menu;
import ch.bailu.gtk.gtk.*;
import ch.bailu.gtk.type.Str;
import ch.bailu.gtk.type.Strs;
import io.gitlab.jfronny.inceptum.common.*;
import io.gitlab.jfronny.inceptum.gtk.menu.MenuBuilder;
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.api.account.*;
import io.gitlab.jfronny.inceptum.launcher.util.InstanceList;
import io.gitlab.jfronny.inceptum.launcher.system.install.Steps;
import io.gitlab.jfronny.inceptum.launcher.system.launch.InstanceLauncher;
import java.io.IOException;
import java.net.URI;
import java.util.*;
public class GtkMain {
public static final Str ID = new Str("io.gitlab.jfronny.inceptum");
public static final Str TITLE = new Str("Inceptum");
public static final int WIDTH = 720 / 2;
public static final int HEIGHT = 1440 / 2;
private static MenuBuilder launchMenu;
private static MenuBuilder accountsMenu;
private static Button listButton;
private static Button gridButton;
public static void main(String[] args) throws IOException {
LauncherEnv.initialize(GtkEnvBackend.INSTANCE);
Utils.LOGGER.info("Launching Inceptum v" + BuildMetadata.VERSION);
Utils.LOGGER.info("Loading from " + MetaHolder.BASE_PATH);
//TODO look into java-gtk samples for window architecture
int statusCode = -1;
try {
statusCode = showGui(args);
} finally {
LauncherEnv.terminate();
System.exit(statusCode);
}
}
public static int showGui(String[] args) throws IOException {
var app = new Application(ID, ApplicationFlags.FLAGS_NONE);
app.onActivate(() -> {
var menu = new MenuBuilder(app);
var file = menu.submenu("file");
file.button("new", GtkMain::showNewInstanceDialog);
file.button("redownload", () -> {
//TODO
});
file.button("exit", app::quit);
launchMenu = menu.submenu("launch");
generateLaunchMenu();
accountsMenu = menu.submenu("account");
generateAccountsMenu();
var help = menu.submenu("help");
help.button("about", GtkMain::showAboutDialog);
help.button("log", () -> {
//TODO
});
var header = new HeaderBar();
var newButton = new Button();
newButton.setIconName(new Str("list-add-symbolic"));
newButton.onClicked(GtkMain::showNewInstanceDialog);
var accountsButton = new MenuButton();
accountsButton.setIconName(new Str("avatar-default-symbolic"));
accountsButton.setPopover(accountsMenu.asPopover());
listButton = new Button();
listButton.setIconName(new Str("view-list-symbolic"));
listButton.onClicked(() -> {
InceptumConfig.listView = true;
InceptumConfig.saveConfig();
generateWindowBody();
});
gridButton = new Button();
gridButton.setIconName(new Str("view-grid-symbolic"));
gridButton.onClicked(() -> {
InceptumConfig.listView = false;
InceptumConfig.saveConfig();
generateWindowBody();
});
var uiMenu = new MenuBuilder(app, new Menu(), "hamburger");
uiMenu.button("support", () -> Utils.openWebBrowser(new URI("https://gitlab.com/jfmods/inceptum/-/issues")));
uiMenu.button("preferences", () -> {}); //TODO preferences UI inspired by boxes
uiMenu.button("about", GtkMain::showAboutDialog);
var menuButton = new MenuButton();
menuButton.setIconName(new Str("open-menu-symbolic"));
menuButton.setPopover(uiMenu.asPopover());
header.packStart(newButton);
header.packEnd(menuButton);
header.packEnd(gridButton);
header.packEnd(listButton);
header.packEnd(accountsButton);
var window = new ApplicationWindow(app);
window.setDefaultSize(WIDTH, HEIGHT);
window.setTitle(TITLE);
window.setTitlebar(header);
window.setShowMenubar(GTK.FALSE);
window.setChild(new TextView());
generateWindowBody();
window.show();
GtkEnvBackend.INSTANCE.dialogParent = window;
window.onCloseRequest(() -> {
GtkEnvBackend.INSTANCE.dialogParent = null;
return GTK.FALSE;
});
});
return app.run(args.length, new Strs(args));
}
private static void generateWindowBody() {
if (listButton != null) listButton.setVisible(GTK.is(!InceptumConfig.listView));
if (gridButton != null) gridButton.setVisible(GTK.is(InceptumConfig.listView));
//TODO create list/grid view
}
private static void showAboutDialog() {
AboutDialog dialog = new AboutDialog();
dialog.setProgramName(new Str("Inceptum"));
dialog.setCopyright(new Str("Copyright (C) 2021 JFronny"));
dialog.setVersion(new Str(BuildMetadata.VERSION.toString()));
dialog.setLicenseType(License.MIT_X11);
dialog.setLicense(I18n.str("about.license"));
dialog.setWebsiteLabel(I18n.str("about.contact"));
dialog.setWebsite(new Str("https://jfronny.gitlab.io/contact.html"));
if (!BuildMetadata.IS_PUBLIC) {
dialog.setComments(I18n.str("about.unsupported-build"));
}
int vm = Runtime.version().feature();
dialog.setSystemInformation(I18n.str(BuildMetadata.VM_VERSION == vm ? "about.jvm" : "about.jvm.unsupported", vm));
//TODO setLogo
dialog.show();
}
private static void showNewInstanceDialog() {
//TODO
}
private static void generateLaunchMenu() {
Objects.requireNonNull(launchMenu);
launchMenu.clear();
try {
InstanceList.forEachLaunchable(entry -> {
Utils.LOGGER.info("Entry " + entry.toString());
launchMenu.literalButton(entry.id(), entry.toString(), () -> {
try {
Steps.reDownload(entry.path(), Steps.createProcessState());
} catch (IOException e) {
Utils.LOGGER.error("Could not redownload instance, trying to start anyways", e);
}
InstanceLauncher.launchClient(entry.path(), entry.meta());
//TODO figure out why this crashes
});
});
} catch (IOException e) {
Utils.LOGGER.error("Could not generate launch menu", e);
}
}
private static void generateAccountsMenu() {
Objects.requireNonNull(accountsMenu);
accountsMenu.clear();
accountsMenu.button("new", () -> {
//TODO
});
accountsMenu.button("manage", () -> {
//TODO UI to add/remove accounts
});
List<MicrosoftAccount> accounts = new ArrayList<>(AccountManager.getAccounts());
accounts.add(null);
accountsMenu.literalRadio("account", accounts.get(AccountManager.getSelectedIndex()), accounts, (i, acc) -> {
if (acc == null) return I18n.get("account.none");
return acc.minecraftUsername;
}, AccountManager::switchAccount);
}
}