Skip to content

Commit

Permalink
Suppport feature #2554. Fix the width of task progess bar. (#2573)
Browse files Browse the repository at this point in the history
* Suppport feature #2554. Fix the width of task progess bar.

* Simply codes.

* Fix checkstyle.

---------

Co-authored-by: burningtnt <[email protected]“>
  • Loading branch information
burningtnt and burningtnt authored Jan 7, 2024
1 parent f558be7 commit 5b7149b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
14 changes: 9 additions & 5 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
import org.jackhuang.hmcl.mod.ModpackCompletionException;
import org.jackhuang.hmcl.mod.ModpackConfiguration;
import org.jackhuang.hmcl.mod.ModpackProvider;
import org.jackhuang.hmcl.setting.*;
import org.jackhuang.hmcl.setting.DownloadProviders;
import org.jackhuang.hmcl.setting.LauncherVisibility;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.VersionSetting;
import org.jackhuang.hmcl.task.*;
import org.jackhuang.hmcl.ui.*;
import org.jackhuang.hmcl.ui.construct.*;
Expand Down Expand Up @@ -216,9 +219,10 @@ private void launch0() {
Controllers.dialog(i18n("version.launch_script.success", scriptFile.getAbsolutePath()));
});
}
}).thenRunAsync(() -> {
launchingLatch.await();
}).withStage("launch.state.waiting_launching"))
}).withFakeProgress(
i18n("message.doing"),
() -> launchingLatch.getCount() == 0, 6.95
).withStage("launch.state.waiting_launching"))
.withStagesHint(Lang.immutableListOf(
"launch.state.java",
"launch.state.dependencies",
Expand Down Expand Up @@ -617,7 +621,7 @@ private static CompletableFuture<JavaVersion> downloadJava(String gameVersion, G
/**
* Directly start java downloading.
*
* @param javaVersion target Java version
* @param javaVersion target Java version
* @param downloadProvider download provider
* @return JavaVersion, null if we failed to download java, failed if an error occurred when downloading.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private class ProgressListNode extends BorderPane {
private final Label title = new Label();
private final Label state = new Label();
private final DoubleBinding binding = Bindings.createDoubleBinding(() ->
getWidth() - getPadding().getLeft() - getPadding().getRight(),
getWidth() - getPadding().getLeft() - getPadding().getRight() - getInsets().getLeft() - getInsets().getRight(),
paddingProperty(), widthProperty());

public ProgressListNode(Task<?> task) {
Expand Down
52 changes: 47 additions & 5 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
Expand Down Expand Up @@ -806,9 +807,11 @@ public <E1 extends Exception, E2 extends Exception> Task<Void> whenComplete(Exec
}

public Task<T> withStage(String stage) {
StageTask task = new StageTask();
task.setStage(stage);
return task;
return new StageTask(stage);
}

public Task<T> withFakeProgress(String name, BooleanSupplier done, double k) {
return new FakeProgressTask(done, k).setExecutor(Schedulers.defaultScheduler()).setName(name).setSignificance(TaskSignificance.MAJOR);
}

public Task<T> withStagesHint(List<String> stages) {
Expand Down Expand Up @@ -1100,15 +1103,54 @@ public boolean isRelyingOnDependents() {
}
}

public class StageTask extends Task<T> {
private final class StageTask extends Task<T> {
private StageTask(String stage) {
this.setStage(stage);
}

@Override
public Collection<Task<?>> getDependents() {
return Collections.singleton(Task.this);
}

@Override
public void execute() throws Exception {
public void execute() {
setResult(Task.this.getResult());
}
}

private final class FakeProgressTask extends Task<T> {
private static final double MAX_VALUE = 0.98D;

private final BooleanSupplier done;

private final double k;

private FakeProgressTask(BooleanSupplier done, double k) {
this.done = done;
this.k = k;
}

@Override
public Collection<Task<?>> getDependents() {
return Collections.singleton(Task.this);
}

@Override
public void execute() throws InterruptedException {
if (!done.getAsBoolean()) {
updateProgress(0.0D);

final long start = System.currentTimeMillis();
final double k2 = k / MAX_VALUE;
while (!done.getAsBoolean()) {
updateProgressImmediately(-k / ((System.currentTimeMillis() - start) / 1000D + k2) + MAX_VALUE);

Thread.sleep(1000);
}
}

updateProgress(1.0D);
setResult(Task.this.getResult());
}
}
Expand Down

0 comments on commit 5b7149b

Please sign in to comment.