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

161 lines
5.9 KiB
Java
Raw Normal View History

2022-09-29 18:19:43 +02:00
package io.gitlab.jfronny.inceptum.gtk.window;
import io.gitlab.jfronny.inceptum.gtk.util.ListIndex;
import org.gnome.adw.Clamp;
import org.gnome.adw.StatusPage;
import org.gtk.gio.Menu;
import org.gtk.glib.GLib;
import org.gtk.gtk.*;
2022-09-30 18:07:18 +02:00
import io.gitlab.jfronny.inceptum.common.*;
2022-09-29 18:19:43 +02:00
import io.gitlab.jfronny.inceptum.gtk.GtkMenubar;
2022-09-30 18:07:18 +02:00
import io.gitlab.jfronny.inceptum.gtk.control.InstanceGridEntryFactory;
import io.gitlab.jfronny.inceptum.gtk.control.InstanceListEntryFactory;
2022-09-29 18:19:43 +02:00
import io.gitlab.jfronny.inceptum.gtk.menu.MenuBuilder;
2022-09-30 18:07:18 +02:00
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
import io.gitlab.jfronny.inceptum.launcher.system.instance.InstanceList;
2022-09-29 18:19:43 +02:00
2022-09-30 18:07:18 +02:00
import java.io.IOException;
2022-09-29 18:19:43 +02:00
import java.net.URI;
2022-09-30 18:07:18 +02:00
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import static java.nio.file.StandardWatchEventKinds.*;
2022-09-29 18:19:43 +02:00
public class MainWindow extends ApplicationWindow {
private final Button listButton;
private final Button gridButton;
2022-09-30 18:07:18 +02:00
private final Stack stack;
2022-10-19 20:28:49 +02:00
private final StatusPage empty;
private final Clamp listView;
2022-09-30 18:07:18 +02:00
private final GridView gridView;
private final List<Instance> instanceList;
2022-09-30 18:07:18 +02:00
private final ListIndex instanceListIndex;
2022-09-29 18:19:43 +02:00
public MainWindow(Application app) {
super(app);
2022-09-30 18:07:18 +02:00
HeaderBar header = new HeaderBar();
Button newButton = new Button();
newButton.setIconName("list-add-symbolic");
newButton.onClicked($ -> NewInstanceWindow.createAndShow());
2022-09-29 18:19:43 +02:00
2022-09-30 18:07:18 +02:00
MenuButton accountsButton = new MenuButton();
accountsButton.setIconName("avatar-default-symbolic");
2022-09-29 18:19:43 +02:00
accountsButton.setPopover(GtkMenubar.accountsMenu.asPopover());
listButton = new Button();
listButton.setIconName("view-list-symbolic");
listButton.onClicked($ -> {
2022-09-29 18:19:43 +02:00
InceptumConfig.listView = true;
InceptumConfig.saveConfig();
generateWindowBody();
});
gridButton = new Button();
gridButton.setIconName("view-grid-symbolic");
gridButton.onClicked($ -> {
2022-09-29 18:19:43 +02:00
InceptumConfig.listView = false;
InceptumConfig.saveConfig();
generateWindowBody();
});
2022-09-30 18:07:18 +02:00
//TODO search button like boxes
MenuBuilder uiMenu = new MenuBuilder(app, new Menu(), "hamburger");
2022-09-29 18:19:43 +02:00
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", AboutWindow::createAndShow);
2022-09-30 18:07:18 +02:00
MenuButton menuButton = new MenuButton();
menuButton.setIconName("open-menu-symbolic");
2022-09-29 18:19:43 +02:00
menuButton.setPopover(uiMenu.asPopover());
header.packStart(newButton);
header.packEnd(menuButton);
header.packEnd(gridButton);
header.packEnd(listButton);
header.packEnd(accountsButton);
2022-09-30 18:07:18 +02:00
instanceList = new ArrayList<>();
instanceListIndex = new ListIndex();
2022-10-19 20:28:49 +02:00
listView = new Clamp();
listView.maximumSize = 900;
listView.child = new ListView(instanceListIndex.inSelectionModel(), new InstanceListEntryFactory(instanceList));
2022-09-30 18:07:18 +02:00
gridView = new GridView(instanceListIndex.inSelectionModel(), new InstanceGridEntryFactory(instanceList));
2022-10-19 20:28:49 +02:00
empty = new StatusPage();
empty.title = I18n.get("main.empty.title");
empty.description = I18n.get("main.empty.description");
2022-09-30 18:07:18 +02:00
//TODO empty.setIconName(new Str());
stack = new Stack();
stack.addChild(listView);
stack.addChild(gridView);
stack.addChild(empty);
2022-09-30 18:07:18 +02:00
ScrolledWindow scroll = new ScrolledWindow();
scroll.setPolicy(PolicyType.NEVER, PolicyType.AUTOMATIC);
scroll.child = stack;
2022-09-30 18:07:18 +02:00
2022-09-29 18:19:43 +02:00
setDefaultSize(360, 720);
title = "Inceptum";
titlebar = header;
showMenubar = false;
child = scroll;
2022-09-29 18:19:43 +02:00
generateWindowBody();
2022-09-30 18:07:18 +02:00
//TODO DropTarget to add mods/instances
try {
setupDirWatcher();
} catch (IOException e) {
Utils.LOGGER.error("Could not set up watch service, live updates of the instance dir will be unavailable", e);
}
}
private void setupDirWatcher() throws IOException { //TODO test (including after lock state change)
WatchService ws = FileSystems.getDefault().newWatchService();
MetaHolder.INSTANCE_DIR.register(ws, ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE);
int source = GLib.idleAdd(data -> {
2022-09-30 18:07:18 +02:00
//TODO watch instance dirs for locks
WatchKey key = ws.poll();
boolean instancesChanged = false;
if (key != null) {
for (WatchEvent<?> event : key.pollEvents()) {
if (event.context() instanceof Path p) {
p = MetaHolder.INSTANCE_DIR.resolve(p);
instancesChanged |= Files.exists(p.resolve("instance.json"));
}
}
}
if (instancesChanged) generateWindowBody();
return true;
2022-09-30 18:07:18 +02:00
}, null);
onCloseRequest($ -> {
2022-09-30 18:07:18 +02:00
try {
ws.close();
} catch (IOException ignored) {
}
GLib.sourceRemove(source);
return false;
2022-09-30 18:07:18 +02:00
});
2022-09-29 18:19:43 +02:00
}
private void generateWindowBody() {
if (listButton != null) listButton.visible = !InceptumConfig.listView;
if (gridButton != null) gridButton.visible = InceptumConfig.listView;
2022-09-30 18:07:18 +02:00
try {
instanceList.clear();
instanceList.addAll(InstanceList.ordered());
2022-09-30 18:07:18 +02:00
instanceListIndex.setSize(instanceList.size());
if (InstanceList.isEmpty) stack.visibleChild = empty;
else if (InceptumConfig.listView) stack.visibleChild = listView;
else stack.visibleChild = gridView;
2022-09-30 18:07:18 +02:00
} catch (IOException e) {
Utils.LOGGER.error("Could not generate window body", e);
}
2022-09-29 18:19:43 +02:00
}
}