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

166 lines
6.2 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.ListIndexModel;
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;
private final ListIndexModel 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();
2023-01-13 19:34:29 +01:00
newButton.iconName = "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();
2023-01-13 19:34:29 +01:00
accountsButton.iconName = "avatar-default-symbolic";
accountsButton.menuModel = GtkMenubar.accountsMenu.menu;
2022-09-29 18:19:43 +02:00
listButton = new Button();
2023-01-13 19:34:29 +01:00
listButton.iconName = "view-list-symbolic";
listButton.onClicked(() -> {
2022-09-29 18:19:43 +02:00
InceptumConfig.listView = true;
InceptumConfig.saveConfig();
generateWindowBody();
});
gridButton = new Button();
2023-01-13 19:34:29 +01:00
gridButton.iconName = "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-12-18 20:46:46 +01:00
uiMenu.button("support", () -> Utils.openWebBrowser(new URI("https://git.frohnmeyer-wds.de/JfMods/Inceptum/issues")));
2022-09-29 18:19:43 +02:00
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();
2023-01-13 19:34:29 +01:00
menuButton.iconName = "open-menu-symbolic";
menuButton.menuModel = uiMenu.menu;
2022-09-29 18:19:43 +02:00
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 ListIndexModel(instanceList.size());
var singleSelection = new SingleSelection(instanceListIndex);
2022-09-30 18:07:18 +02:00
2022-10-19 20:28:49 +02:00
listView = new Clamp();
listView.maximumSize = 900;
2023-01-13 19:34:29 +01:00
listView.child = new ListView(singleSelection, new InstanceListEntryFactory(instanceList, this));
listView.child.marginHorizontal = 24;
listView.child.marginVertical = 12;
listView.child.addCssClass("content");
listView.child.valign = Align.START;
gridView = new GridView(singleSelection, 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(() -> {
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(InstanceList.INSTANCE_CONFIG_NAME));
2022-09-30 18:07:18 +02:00
}
}
}
if (instancesChanged) generateWindowBody();
return true;
});
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());
2023-01-13 14:20:16 +01:00
instanceListIndex.size = instanceList.size();
2022-09-30 18:07:18 +02:00
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
}
}