170 lines
6.1 KiB
Java
170 lines
6.1 KiB
Java
package io.gitlab.jfronny.inceptum.gtk.window;
|
|
|
|
import io.gitlab.jfronny.inceptum.common.InceptumConfig;
|
|
import io.gitlab.jfronny.inceptum.common.Utils;
|
|
import io.gitlab.jfronny.inceptum.gtk.GtkMenubar;
|
|
import io.gitlab.jfronny.inceptum.gtk.control.InstanceGridEntryFactory;
|
|
import io.gitlab.jfronny.inceptum.gtk.control.InstanceListEntryFactory;
|
|
import io.gitlab.jfronny.inceptum.gtk.menu.MenuBuilder;
|
|
import io.gitlab.jfronny.inceptum.gtk.util.I18n;
|
|
import io.gitlab.jfronny.inceptum.gtk.util.ListIndexModel;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.instance.*;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.launch.LaunchType;
|
|
import org.gnome.adw.Clamp;
|
|
import org.gnome.adw.StatusPage;
|
|
import org.gtk.gio.Menu;
|
|
import org.gtk.glib.GLib;
|
|
import org.gtk.gtk.*;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class MainWindow extends ApplicationWindow {
|
|
private final Button listButton;
|
|
private final Button gridButton;
|
|
private final Stack stack;
|
|
private final StatusPage empty;
|
|
private final Clamp listContainer;
|
|
private final GridView gridView;
|
|
private final List<Instance> instanceList;
|
|
private final ListIndexModel instanceListIndex;
|
|
|
|
public MainWindow(Application app) {
|
|
super(app);
|
|
|
|
HeaderBar header = new HeaderBar();
|
|
Button newButton = new Button();
|
|
newButton.iconName = "list-add-symbolic";
|
|
newButton.onClicked(() -> new NewInstanceWindow(app).show());
|
|
|
|
MenuButton accountsButton = new MenuButton();
|
|
accountsButton.iconName = "avatar-default-symbolic";
|
|
accountsButton.menuModel = GtkMenubar.accountsMenu.menu;
|
|
|
|
listButton = Button.newFromIconName("view-list-symbolic");
|
|
listButton.onClicked(() -> {
|
|
InceptumConfig.listView = true;
|
|
InceptumConfig.saveConfig();
|
|
generateWindowBody();
|
|
});
|
|
|
|
gridButton = Button.newFromIconName("view-grid-symbolic");
|
|
gridButton.onClicked(() -> {
|
|
InceptumConfig.listView = false;
|
|
InceptumConfig.saveConfig();
|
|
generateWindowBody();
|
|
});
|
|
|
|
//TODO search button like boxes
|
|
|
|
MenuBuilder uiMenu = new MenuBuilder(app, new Menu(), "hamburger");
|
|
uiMenu.button("support", () -> Utils.openWebBrowser(new URI("https://git.frohnmeyer-wds.de/JfMods/Inceptum/issues")));
|
|
uiMenu.button("preferences", () -> new LauncherSettingsWindow(app).show());
|
|
uiMenu.button("about", AboutWindow::createAndShow);
|
|
MenuButton menuButton = new MenuButton();
|
|
menuButton.iconName = "open-menu-symbolic";
|
|
menuButton.menuModel = uiMenu.menu;
|
|
|
|
header.packStart(newButton);
|
|
|
|
header.packEnd(menuButton);
|
|
header.packEnd(gridButton);
|
|
header.packEnd(listButton);
|
|
header.packEnd(accountsButton);
|
|
|
|
instanceList = new ArrayList<>();
|
|
instanceListIndex = new ListIndexModel(instanceList.size());
|
|
var singleSelection = new NoSelection(instanceListIndex);
|
|
|
|
ListView listView = new ListView(singleSelection, new InstanceListEntryFactory(app, instanceList));
|
|
listView.addCssClass("rich-list");
|
|
listView.showSeparators = true;
|
|
listView.onActivate(position -> {
|
|
// Double click
|
|
GtkMenubar.launch(instanceList.get(position), LaunchType.Client);
|
|
});
|
|
Frame frame = new Frame(null);
|
|
frame.child = listView;
|
|
frame.marginHorizontal = 24;
|
|
frame.marginVertical = 12;
|
|
frame.valign = Align.START;
|
|
listContainer = new Clamp();
|
|
listContainer.maximumSize = 900;
|
|
listContainer.child = frame;
|
|
gridView = new GridView(singleSelection, new InstanceGridEntryFactory(instanceList));
|
|
empty = new StatusPage();
|
|
empty.title = I18n.get("main.empty.title");
|
|
empty.description = I18n.get("main.empty.description");
|
|
//TODO empty.setIconName(new Str());
|
|
stack = new Stack();
|
|
stack.addChild(listContainer);
|
|
stack.addChild(gridView);
|
|
stack.addChild(empty);
|
|
|
|
ScrolledWindow scroll = new ScrolledWindow();
|
|
scroll.setPolicy(PolicyType.NEVER, PolicyType.AUTOMATIC);
|
|
scroll.child = stack;
|
|
|
|
setDefaultSize(360, 720);
|
|
title = "Inceptum";
|
|
titlebar = header;
|
|
showMenubar = false;
|
|
child = scroll;
|
|
|
|
generateWindowBody();
|
|
//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 {
|
|
InstanceListWatcher isw = new InstanceListWatcher();
|
|
addTickCallback((widget, clock) -> {
|
|
try {
|
|
if (isw.poll()) generateWindowBody();
|
|
} catch (IOException e) {
|
|
Utils.LOGGER.error("Could not run update task", e);
|
|
}
|
|
return GLib.SOURCE_CONTINUE;
|
|
}, null);
|
|
onCloseRequest(() -> {
|
|
try {
|
|
isw.close();
|
|
} catch (IOException ignored) {
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
private void generateWindowBody() {
|
|
if (listButton != null) listButton.visible = !InceptumConfig.listView;
|
|
if (gridButton != null) gridButton.visible = InceptumConfig.listView;
|
|
try {
|
|
// Unbind then clear
|
|
instanceListIndex.size = 0;
|
|
instanceList.clear();
|
|
|
|
// Add new entries
|
|
instanceList.addAll(InstanceList.ordered());
|
|
instanceListIndex.size = instanceList.size();
|
|
|
|
// Choose view for this amount of entries
|
|
if (InstanceList.isEmpty) stack.visibleChild = empty;
|
|
else if (InceptumConfig.listView) stack.visibleChild = listContainer;
|
|
else stack.visibleChild = gridView;
|
|
|
|
// This is called from a tick callback, so re-render
|
|
stack.queueResize();
|
|
stack.queueDraw();
|
|
} catch (IOException e) {
|
|
Utils.LOGGER.error("Could not generate window body", e);
|
|
}
|
|
}
|
|
}
|