Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve status bar with stat generation progress #183

Merged
merged 6 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions enigma-cli/src/main/java/org/quiltmc/enigma/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static JarIndex loadJar(Path jar) throws IOException {
Logger.info("Reading JAR...");
JarClassProvider classProvider = new JarClassProvider(jar);
JarIndex index = JarIndex.empty();
index.indexJar(classProvider.getClassNames(), new CachingClassProvider(classProvider), ProgressListener.none());
index.indexJar(classProvider.getClassNames(), new CachingClassProvider(classProvider), ProgressListener.createEmpty());

return index;
}
Expand Down Expand Up @@ -274,29 +274,29 @@ protected static <T> void writeDebugDelta(DeltaTrackingTree<T> tree, Path output
Logger.debug("Wrote debug output to {}", debugOutput.toAbsolutePath());
}

public static class ConsoleProgressListener implements ProgressListener {
public static class ConsoleProgressListener extends ProgressListener {
private static final int REPORT_TIME = 5000; // 5s

private int totalWork;
private long startTime;
private long lastReportTime;

@Override
public void init(int totalWork, String title) {
this.totalWork = totalWork;
super.init(totalWork, title);
this.startTime = System.currentTimeMillis();
this.lastReportTime = this.startTime;
Logger.info(title);
}

@Override
public void step(int numDone, String message) {
public void step(int workDone, String message) {
super.step(workDone, message);
long now = System.currentTimeMillis();
boolean isLastUpdate = numDone == this.totalWork;
boolean isLastUpdate = workDone == this.totalWork;
boolean shouldReport = isLastUpdate || now - this.lastReportTime > REPORT_TIME;

if (shouldReport) {
int percent = numDone * 100 / this.totalWork;
int percent = workDone * 100 / this.totalWork;
Logger.info("\tProgress: {}%", percent);
this.lastReportTime = now;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public static void run(Path leftFile, Path rightFile, String resultFormat, Path

Utils.delete(resultFile);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(result, resultFile, ProgressListener.none(), saveParameters);
writer.write(result, resultFile, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public static void run(Path source, String resultFormat, Path output) throws Map

Utils.delete(output);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);
writer.write(mappings, output, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void run(Path jarIn, Path mappingsIn, Path mappingsOut) throws Exc

Logger.info("Dropping invalid mappings...");

project.dropMappings(ProgressListener.none());
project.dropMappings(ProgressListener.createEmpty());

Logger.info("Writing mappings...");

Expand All @@ -75,6 +75,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
}

MappingSaveParameters saveParameters = project.getEnigma().getProfile().getMappingSaveParameters();
format.write(project.getRemapper().getMappings(), mappingsOut, ProgressListener.none(), saveParameters);
format.write(project.getRemapper().getMappings(), mappingsOut, ProgressListener.createEmpty(), saveParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static void run(Path jar, Path source, Path result, String resultFormat,

Logger.info("Reading mappings...");
MappingSaveParameters saveParameters = new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF, false);
EntryTree<EntryMapping> sourceMappings = readMappings(source, ProgressListener.none());
EntryTree<EntryMapping> sourceMappings = readMappings(source, ProgressListener.createEmpty());

EntryTree<EntryMapping> resultMappings = exec(jarIndex, sourceMappings, fillAll, debug);

Logger.info("Writing mappings...");
Utils.delete(result);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(resultMappings, result, ProgressListener.none(), saveParameters);
writer.write(resultMappings, result, ProgressListener.createEmpty(), saveParameters);

if (debug) {
writeDebugDelta((DeltaTrackingTree<EntryMapping>) resultMappings, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void run(Path inJar, Path source, Path output, String resultFormat
Utils.delete(output);
MappingSaveParameters saveParameters = new MappingSaveParameters(enigma.getProfile().getMappingSaveParameters().fileNameFormat(), true);
MappingsWriter writer = MappingCommandsUtil.getWriter(resultFormat);
writer.write(mappings, output, ProgressListener.none(), saveParameters);
writer.write(mappings, output, ProgressListener.createEmpty(), saveParameters);

if (debug) {
writeDebugDelta(mappings, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void test() throws Exception {
Path resultFile = Files.createTempFile("mapSpecializedMethods", ".mappings");
MapSpecializedMethodsCommand.run(JAR, MAPPINGS, MappingFormat.ENIGMA_FILE.name(), resultFile);

EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.none());
EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.createEmpty());

assertNotNull(result.findNode(BASE_CLASS));
assertEquals("foo", getName(result, BASE_FOO_1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void main(String[] args) {
EnigmaProfile profile = EnigmaProfile.read(profileFile);
Enigma enigma = Enigma.builder().setProfile(profile).build();
Logger.info("Indexing Jar...");
EnigmaProject project = enigma.openJar(jar, new ClasspathClassProvider(), ProgressListener.none());
EnigmaProject project = enigma.openJar(jar, new ClasspathClassProvider(), ProgressListener.createEmpty());

MappingFormat mappingFormat = MappingFormat.parseFromFile(mappingsFile);
EntryRemapper mappings;
Expand Down Expand Up @@ -154,7 +154,7 @@ public synchronized void stop() {
}

private void saveMappings() {
this.mappingFormat.write(this.getRemapper().getMappings(), this.getRemapper().takeMappingDelta(), this.mappingsFile, ProgressListener.none(), this.profile.getMappingSaveParameters());
this.mappingFormat.write(this.getRemapper().getMappings(), this.getRemapper().takeMappingDelta(), this.mappingsFile, ProgressListener.createEmpty(), this.profile.getMappingSaveParameters());
this.log.flush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class NetworkTest {
@BeforeAll
public static void startServer() throws IOException {
Enigma enigma = Enigma.create();
EnigmaProject project = enigma.openJar(JAR, new ClasspathClassProvider(), ProgressListener.none());
EnigmaProject project = enigma.openJar(JAR, new ClasspathClassProvider(), ProgressListener.createEmpty());

checksum = Utils.zipSha1(JAR);
remapper = project.getRemapper();
Expand Down
2 changes: 1 addition & 1 deletion enigma-swing/src/main/java/org/quiltmc/enigma/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public boolean isOffline() {
*/
public void updateUiState() {
this.menuBar.updateUiState();
this.connectionStatusLabel.setText(I18n.translate(this.connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected"));
this.connectionStatusLabel.setText(I18n.translate("status.prefix") + I18n.translate(this.connectionState == ConnectionState.NOT_CONNECTED ? "status.disconnected" : "status.connected"));
}

public void retranslateUi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.quiltmc.enigma.api.analysis.tree.ClassReferenceTreeNode;
import org.quiltmc.enigma.api.analysis.EntryReference;
import org.quiltmc.enigma.api.analysis.tree.FieldReferenceTreeNode;
import org.quiltmc.enigma.gui.dialog.CrashDialog;
import org.quiltmc.enigma.gui.network.IntegratedEnigmaClient;
import org.quiltmc.enigma.impl.analysis.IndexTreeBuilder;
import org.quiltmc.enigma.api.analysis.tree.MethodImplementationsTreeNode;
Expand Down Expand Up @@ -78,6 +79,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -167,8 +169,15 @@ public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
this.refreshClasses();
this.chp.invalidateJavadoc();
this.statsGenerator = new StatsGenerator(this.project);
new Thread(() -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already inside another secondary thread, and I feel like we have a better way to spawn threads instead of new Thread()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it executor time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what the best way of doing this would be, this is kinda a bodge that just got left in because I don't know what's better

ProgressListener progressListener = ProgressListener.createEmpty();
this.gui.getMainWindow().getStatusBar().syncWith(progressListener);
this.statsGenerator.generate(progressListener, EnumSet.allOf(StatType.class), false);
}).start();
} catch (MappingParseException e) {
JOptionPane.showMessageDialog(this.gui.getFrame(), e.getMessage());
} catch (Exception e) {
CrashDialog.show(e);
}
});
}
Expand Down Expand Up @@ -227,7 +236,7 @@ public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) {
public void closeMappings() {
if (this.project == null) return;

this.project.setMappings(null, ProgressListener.none());
this.project.setMappings(null, ProgressListener.createEmpty());

this.gui.setMappingsFile(null);
this.refreshClasses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.util.GridBagConstraintsBuilder;
import org.quiltmc.enigma.gui.util.GuiUtil;
import org.quiltmc.enigma.gui.util.ProgressBar;
import org.quiltmc.enigma.gui.util.ScaleUtil;
import org.quiltmc.enigma.util.I18n;

Expand All @@ -16,17 +17,18 @@
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ProgressDialog implements ProgressListener, AutoCloseable {
public class ProgressDialog extends ProgressBar implements AutoCloseable {
private final JDialog dialog;
private final JLabel labelTitle = new JLabel();
private final JLabel labelText = GuiUtil.unboldLabel(new JLabel());
private final JProgressBar progress = new JProgressBar();

public ProgressDialog(JFrame parent) {
JLabel labelTitle = new JLabel();
this.addTitleSetListener(labelTitle::setText);
JLabel labelText = GuiUtil.unboldLabel(new JLabel());
this.addMessageUpdateListener((message, done) -> labelText.setText(message));

// init frame
this.dialog = new JDialog(parent, String.format(I18n.translate("progress.operation"), Enigma.NAME));
Container pane = this.dialog.getContentPane();
Expand All @@ -38,15 +40,15 @@ public ProgressDialog(JFrame parent) {
.fill(GridBagConstraints.BOTH)
.weight(1.0, 0.0);

pane.add(this.labelTitle, cb.pos(0, 0).build());
pane.add(this.labelText, cb.pos(0, 1).build());
pane.add(this.progress, cb.pos(0, 2).weight(1.0, 1.0).build());
pane.add(labelTitle, cb.pos(0, 0).build());
pane.add(labelText, cb.pos(0, 1).build());
pane.add(this.delegate, cb.pos(0, 2).weight(1.0, 1.0).build());

// Set label text since otherwise the label height is 0, which makes the
// window size get set incorrectly
this.labelTitle.setText("Idle");
this.labelText.setText("Idle");
this.progress.setPreferredSize(ScaleUtil.getDimension(0, 20));
labelTitle.setText("Idle");
labelText.setText("Idle");
this.delegate.setPreferredSize(ScaleUtil.getDimension(0, 20));

// show the frame
this.dialog.setResizable(false);
Expand Down Expand Up @@ -101,29 +103,6 @@ public void close() {
SwingUtilities.invokeLater(this.dialog::dispose);
}

@Override
public void init(int totalWork, String title) {
SwingUtilities.invokeLater(() -> {
this.labelTitle.setText(title);
this.progress.setMinimum(0);
this.progress.setMaximum(totalWork);
this.progress.setValue(0);
});
}

@Override
public void step(int numDone, String message) {
SwingUtilities.invokeLater(() -> {
this.labelText.setText(message);
if (numDone != -1) {
this.progress.setValue(numDone);
this.progress.setIndeterminate(false);
} else {
this.progress.setIndeterminate(true);
}
});
}

public interface ProgressRunnable {
void run(ProgressListener listener) throws Exception;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.gui.dialog;

import org.quiltmc.enigma.api.stats.StatsGenerator;
import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.config.Config;
import org.quiltmc.enigma.api.stats.ProjectStatsResult;
Expand All @@ -26,10 +27,22 @@

public class StatsDialog {
public static void show(Gui gui) {
ProgressDialog.runOffThread(gui, listener -> {
ProjectStatsResult result = gui.getController().getStatsGenerator().getResult(false);
SwingUtilities.invokeLater(() -> show(gui, result, ""));
});
StatsGenerator generator = gui.getController().getStatsGenerator();
ProjectStatsResult nullableResult = generator.getResultNullable();

if (nullableResult == null) {
ProgressDialog.runOffThread(gui, listener -> {
// hook into current stat generation progress
if (generator.getOverallProgress() != null) {
listener.sync(generator.getOverallProgress());
}

ProjectStatsResult result = gui.getController().getStatsGenerator().getResult(false);
SwingUtilities.invokeLater(() -> show(gui, result, ""));
});
} else {
SwingUtilities.invokeLater(() -> show(gui, nullableResult, ""));
}
}

public static void show(Gui gui, ProjectStatsResult result, String packageName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.quiltmc.enigma.gui.element;

import org.quiltmc.enigma.api.ProgressListener;
import org.quiltmc.enigma.gui.util.ProgressBar;
import org.quiltmc.enigma.util.I18n;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
Expand All @@ -18,6 +23,7 @@ public class StatusBar {
private final JPanel leftPanel = new JPanel(new GridLayout(1, 1, 0, 0));
private final JPanel components = new JPanel();
private final JPanel permanentComponents = new JPanel();
private final ProgressBar progressBar = new ProgressBar();

private final JLabel temporaryMessage = new JLabel();
private final Timer timer = new Timer(0, e -> this.clearMessage());
Expand All @@ -28,6 +34,19 @@ public StatusBar() {
this.components.setLayout(new BoxLayout(this.components, BoxLayout.LINE_AXIS));
this.permanentComponents.setLayout(new BoxLayout(this.permanentComponents, BoxLayout.LINE_AXIS));
this.permanentComponents.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JLabel progressMessage = new JLabel();
AtomicReference<String> title = new AtomicReference<>("");
this.progressBar.addTitleSetListener(title::set);
this.progressBar.addMessageUpdateListener((message, done) -> {
progressMessage.setText(title + ": " + message);

if (done) {
progressMessage.setText(I18n.translate("status.idle"));
}
});

this.addPermanentComponent(this.progressBar.asJProgressBar());
this.addPermanentComponent(progressMessage);

this.leftPanel.add(this.components);
this.temporaryMessage.setHorizontalTextPosition(JLabel.LEFT);
Expand Down Expand Up @@ -69,6 +88,10 @@ public void showMessage(String message, int timeout) {
}
}

public void syncWith(ProgressListener listener) {
this.progressBar.sync(listener);
}

/**
* Clears any currently displayed temporary message.
*/
Expand Down Expand Up @@ -118,6 +141,10 @@ public void removeComponent(Component comp) {
* @param comp the component to add
*/
public void addPermanentComponent(Component comp) {
if (this.permanentComponents.getComponents().length != 0) {
this.permanentComponents.add(new JLabel(" | "));
}

this.permanentComponents.add(comp);
}

Expand Down
Loading
Loading