2022-09-26 19:09:02 +02:00
|
|
|
package io.gitlab.jfronny.inceptum.gtk;
|
|
|
|
|
|
|
|
import ch.bailu.gtk.GTK;
|
|
|
|
import ch.bailu.gtk.gio.ApplicationFlags;
|
2022-09-29 17:53:49 +02:00
|
|
|
import ch.bailu.gtk.gio.Menu;
|
2022-09-26 19:09:02 +02:00
|
|
|
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;
|
2022-09-29 17:53:49 +02:00
|
|
|
import java.net.URI;
|
2022-09-26 19:09:02 +02:00
|
|
|
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;
|
2022-09-29 17:53:49 +02:00
|
|
|
private static Button listButton;
|
|
|
|
private static Button gridButton;
|
2022-09-26 19:09:02 +02:00
|
|
|
|
|
|
|
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");
|
2022-09-29 17:53:49 +02:00
|
|
|
file.button("new", GtkMain::showNewInstanceDialog);
|
2022-09-26 19:09:02 +02:00
|
|
|
file.button("redownload", () -> {
|
|
|
|
//TODO
|
|
|
|
});
|
2022-09-29 17:53:49 +02:00
|
|
|
file.button("exit", app::quit);
|
2022-09-26 19:09:02 +02:00
|
|
|
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();
|
2022-09-29 17:53:49 +02:00
|
|
|
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);
|
2022-09-26 19:09:02 +02:00
|
|
|
|
|
|
|
var window = new ApplicationWindow(app);
|
|
|
|
window.setDefaultSize(WIDTH, HEIGHT);
|
|
|
|
window.setTitle(TITLE);
|
|
|
|
window.setTitlebar(header);
|
2022-09-29 17:53:49 +02:00
|
|
|
window.setShowMenubar(GTK.FALSE);
|
2022-09-26 19:09:02 +02:00
|
|
|
window.setChild(new TextView());
|
|
|
|
|
2022-09-29 17:53:49 +02:00
|
|
|
generateWindowBody();
|
2022-09-26 19:09:02 +02:00
|
|
|
|
|
|
|
window.show();
|
|
|
|
GtkEnvBackend.INSTANCE.dialogParent = window;
|
|
|
|
window.onCloseRequest(() -> {
|
|
|
|
GtkEnvBackend.INSTANCE.dialogParent = null;
|
|
|
|
return GTK.FALSE;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return app.run(args.length, new Strs(args));
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:53:49 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:09:02 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:53:49 +02:00
|
|
|
private static void showNewInstanceDialog() {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2022-09-26 19:09:02 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|