fix(download): not showing download mod progress. Closes #1456.

This commit is contained in:
huanghongxun 2022-05-22 15:31:33 +08:00
parent 59f6837914
commit a230027974
17 changed files with 102 additions and 50 deletions

View File

@ -92,8 +92,7 @@ public final class LauncherHelper {
this.launchingStepsPane.setTitle(i18n("version.launch")); this.launchingStepsPane.setTitle(i18n("version.launch"));
} }
private final TaskExecutorDialogPane launchingStepsPane = new TaskExecutorDialogPane(it -> { private final TaskExecutorDialogPane launchingStepsPane = new TaskExecutorDialogPane(TaskCancellationAction.NORMAL);
});
public void setTestMode() { public void setTestMode() {
launcherVisibility = LauncherVisibility.KEEP; launcherVisibility = LauncherVisibility.KEEP;
@ -197,10 +196,10 @@ public final class LauncherHelper {
if (launcherVisibility == LauncherVisibility.CLOSE) if (launcherVisibility == LauncherVisibility.CLOSE)
Launcher.stopApplication(); Launcher.stopApplication();
else else
launchingStepsPane.setCancel(it -> { launchingStepsPane.setCancel(new TaskCancellationAction(it -> {
process.stop(); process.stop();
it.fireEvent(new DialogCloseEvent()); it.fireEvent(new DialogCloseEvent());
}); }));
} else { } else {
Platform.runLater(() -> { Platform.runLater(() -> {
launchingStepsPane.fireEvent(new DialogCloseEvent()); launchingStepsPane.fireEvent(new DialogCloseEvent());
@ -598,8 +597,7 @@ public final class LauncherHelper {
private static CompletableFuture<JavaVersion> downloadJavaImpl(GameJavaVersion javaVersion, DownloadProvider downloadProvider) { private static CompletableFuture<JavaVersion> downloadJavaImpl(GameJavaVersion javaVersion, DownloadProvider downloadProvider) {
CompletableFuture<JavaVersion> future = new CompletableFuture<>(); CompletableFuture<JavaVersion> future = new CompletableFuture<>();
TaskExecutorDialogPane javaDownloadingPane = new TaskExecutorDialogPane(it -> { TaskExecutorDialogPane javaDownloadingPane = new TaskExecutorDialogPane(TaskCancellationAction.NORMAL);
});
TaskExecutor executor = JavaRepository.downloadJava(javaVersion, downloadProvider) TaskExecutor executor = JavaRepository.downloadJava(javaVersion, downloadProvider)
.whenComplete(Schedulers.javafx(), (downloadedJava, exception) -> { .whenComplete(Schedulers.javafx(), (downloadedJava, exception) -> {

View File

@ -52,13 +52,13 @@ import org.jackhuang.hmcl.ui.versions.VersionPage;
import org.jackhuang.hmcl.util.FutureCallback; import org.jackhuang.hmcl.util.FutureCallback;
import org.jackhuang.hmcl.util.Lazy; import org.jackhuang.hmcl.util.Lazy;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.JavaVersion; import org.jackhuang.hmcl.util.platform.JavaVersion;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig; import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig;
@ -263,11 +263,7 @@ public final class Controllers {
return pane.getCompletableFuture(); return pane.getCompletableFuture();
} }
public static TaskExecutorDialogPane taskDialog(TaskExecutor executor, String title) { public static TaskExecutorDialogPane taskDialog(TaskExecutor executor, String title, TaskCancellationAction onCancel) {
return taskDialog(executor, title, null);
}
public static TaskExecutorDialogPane taskDialog(TaskExecutor executor, String title, Consumer<Region> onCancel) {
TaskExecutorDialogPane pane = new TaskExecutorDialogPane(onCancel); TaskExecutorDialogPane pane = new TaskExecutorDialogPane(onCancel);
pane.setTitle(title); pane.setTitle(title);
pane.setExecutor(executor); pane.setExecutor(executor);
@ -275,7 +271,7 @@ public final class Controllers {
return pane; return pane;
} }
public static TaskExecutorDialogPane taskDialog(Task<?> task, String title, Consumer<Region> onCancel) { public static TaskExecutorDialogPane taskDialog(Task<?> task, String title, TaskCancellationAction onCancel) {
TaskExecutor executor = task.executor(); TaskExecutor executor = task.executor();
TaskExecutorDialogPane pane = new TaskExecutorDialogPane(onCancel); TaskExecutorDialogPane pane = new TaskExecutorDialogPane(onCancel);
pane.setTitle(title); pane.setTitle(title);

View File

@ -22,12 +22,13 @@ import javafx.application.Platform;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.TaskExecutor; import org.jackhuang.hmcl.task.TaskExecutor;
import org.jackhuang.hmcl.task.TaskListener; import org.jackhuang.hmcl.task.TaskListener;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jetbrains.annotations.NotNull;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -37,7 +38,7 @@ import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
public class TaskExecutorDialogPane extends StackPane { public class TaskExecutorDialogPane extends StackPane {
private TaskExecutor executor; private TaskExecutor executor;
private Consumer<Region> onCancel; private TaskCancellationAction onCancel;
private final Consumer<FileDownloadTask.SpeedEvent> speedEventHandler; private final Consumer<FileDownloadTask.SpeedEvent> speedEventHandler;
@FXML @FXML
@ -49,14 +50,14 @@ public class TaskExecutorDialogPane extends StackPane {
@FXML @FXML
private TaskListPane taskListPane; private TaskListPane taskListPane;
public TaskExecutorDialogPane(Consumer<Region> cancel) { public TaskExecutorDialogPane(@NotNull TaskCancellationAction cancel) {
FXUtils.loadFXML(this, "/assets/fxml/task-dialog.fxml"); FXUtils.loadFXML(this, "/assets/fxml/task-dialog.fxml");
setCancel(cancel); setCancel(cancel);
btnCancel.setOnAction(e -> { btnCancel.setOnAction(e -> {
Optional.ofNullable(executor).ifPresent(TaskExecutor::cancel); Optional.ofNullable(executor).ifPresent(TaskExecutor::cancel);
onCancel.accept(this); onCancel.getCancellationAction().accept(this);
}); });
speedEventHandler = speedEvent -> { speedEventHandler = speedEvent -> {
@ -113,7 +114,7 @@ public class TaskExecutorDialogPane extends StackPane {
lblTitle.setText(currentState); lblTitle.setText(currentState);
} }
public void setCancel(Consumer<Region> onCancel) { public void setCancel(TaskCancellationAction onCancel) {
this.onCancel = onCancel; this.onCancel = onCancel;
runInFX(() -> btnCancel.setDisable(onCancel == null)); runInFX(() -> btnCancel.setDisable(onCancel == null));

View File

@ -195,7 +195,7 @@ public final class TaskListPane extends StackPane {
if (task instanceof Task.CountTask) { if (task instanceof Task.CountTask) {
runInFX(() -> { runInFX(() -> {
stageNodes.stream() stageNodes.stream()
.filter(x -> x.stage.equals(((Task.CountTask) task).getCountStage())) .filter(x -> x.stage.equals(((Task<?>.CountTask) task).getCountStage()))
.findAny() .findAny()
.ifPresent(StageNode::count); .ifPresent(StageNode::count);
}); });

View File

@ -31,7 +31,6 @@ import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutor;
import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG; import org.jackhuang.hmcl.ui.SVG;
@ -41,13 +40,13 @@ import org.jackhuang.hmcl.ui.animation.TransitionPane;
import org.jackhuang.hmcl.ui.construct.AdvancedListBox; import org.jackhuang.hmcl.ui.construct.AdvancedListBox;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane; import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.TabHeader; import org.jackhuang.hmcl.ui.construct.TabHeader;
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage; import org.jackhuang.hmcl.ui.decorator.DecoratorAnimatedPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.ui.versions.*; import org.jackhuang.hmcl.ui.versions.*;
import org.jackhuang.hmcl.ui.wizard.Navigation; import org.jackhuang.hmcl.ui.wizard.Navigation;
import org.jackhuang.hmcl.ui.wizard.WizardController; import org.jackhuang.hmcl.ui.wizard.WizardController;
import org.jackhuang.hmcl.ui.wizard.WizardProvider; import org.jackhuang.hmcl.ui.wizard.WizardProvider;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -172,10 +171,7 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage
} }
Path dest = runDirectory.resolve(subdirectoryName).resolve(result); Path dest = runDirectory.resolve(subdirectoryName).resolve(result);
TaskExecutorDialogPane downloadingPane = new TaskExecutorDialogPane(it -> { Controllers.taskDialog(Task.composeAsync(() -> {
});
TaskExecutor executor = Task.composeAsync(() -> {
FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest.toFile()); FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest.toFile());
task.setName(file.getName()); task.setName(file.getName());
return task; return task;
@ -189,11 +185,7 @@ public class DownloadPage extends DecoratorAnimatedPage implements DecoratorPage
} else { } else {
Controllers.showToast(i18n("install.success")); Controllers.showToast(i18n("install.success"));
} }
}).executor(false); }), i18n("message.downloading"), TaskCancellationAction.NORMAL);
downloadingPane.setExecutor(executor, true);
Controllers.dialog(downloadingPane);
executor.start();
resolve.run(); resolve.run();
}, file.getFile().getFilename()); }, file.getFile().getFilename());

View File

@ -31,6 +31,7 @@ import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.wizard.WizardController; import org.jackhuang.hmcl.ui.wizard.WizardController;
import org.jackhuang.hmcl.ui.wizard.WizardPage; import org.jackhuang.hmcl.ui.wizard.WizardPage;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.gson.JsonUtils; import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
@ -106,7 +107,7 @@ public final class ModpackSelectionPage extends StackPane implements WizardPage
} else { } else {
reject.accept(e.getMessage()); reject.accept(e.getMessage());
} }
}).executor(true), i18n("message.downloading")); }).executor(true), i18n("message.downloading"), TaskCancellationAction.NORMAL);
} else { } else {
// otherwise we still consider the file as modpack zip file // otherwise we still consider the file as modpack zip file
// since casually the url may not ends with ".zip" // since casually the url may not ends with ".zip"
@ -124,7 +125,8 @@ public final class ModpackSelectionPage extends StackPane implements WizardPage
reject.accept(e.getMessage()); reject.accept(e.getMessage());
} }
}).executor(true), }).executor(true),
i18n("message.downloading") i18n("message.downloading"),
TaskCancellationAction.NORMAL
); );
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -40,6 +40,7 @@ import org.jackhuang.hmcl.ui.download.ModpackInstallWizardProvider;
import org.jackhuang.hmcl.ui.versions.GameAdvancedListItem; import org.jackhuang.hmcl.ui.versions.GameAdvancedListItem;
import org.jackhuang.hmcl.ui.versions.Versions; import org.jackhuang.hmcl.ui.versions.Versions;
import org.jackhuang.hmcl.upgrade.UpdateChecker; import org.jackhuang.hmcl.upgrade.UpdateChecker;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.CompressingUtils; import org.jackhuang.hmcl.util.io.CompressingUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
@ -198,7 +199,7 @@ public class RootPage extends DecoratorAnimatedPage implements DecoratorPage {
modpack) modpack)
.executor()) .executor())
.thenAcceptAsync(Schedulers.javafx(), executor -> { .thenAcceptAsync(Schedulers.javafx(), executor -> {
Controllers.taskDialog(executor, i18n("modpack.installing")); Controllers.taskDialog(executor, i18n("modpack.installing"), TaskCancellationAction.NO_CANCEL);
executor.start(); executor.start();
}).start(); }).start();
} }

View File

@ -40,6 +40,7 @@ import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.HMCLService; import org.jackhuang.hmcl.util.HMCLService;
import org.jackhuang.hmcl.util.Result; import org.jackhuang.hmcl.util.Result;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.ChecksumMismatchException; import org.jackhuang.hmcl.util.io.ChecksumMismatchException;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -196,7 +197,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
Controllers.showToast(i18n("multiplayer.download.success")); Controllers.showToast(i18n("multiplayer.download.success"));
} }
}).executor(); }).executor();
Controllers.taskDialog(executor, i18n("multiplayer.download")); Controllers.taskDialog(executor, i18n("multiplayer.download"), TaskCancellationAction.NORMAL);
executor.start(); executor.start();
} else { } else {
setDisabled(false); setDisabled(false);

View File

@ -51,6 +51,7 @@ import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.SimpleMultimap; import org.jackhuang.hmcl.util.SimpleMultimap;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.i18n.I18n; import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import org.jackhuang.hmcl.util.versioning.VersionNumber; import org.jackhuang.hmcl.util.versioning.VersionNumber;
@ -203,9 +204,13 @@ public class DownloadPage extends Control implements DecoratorPage {
return; return;
} }
Controllers.taskDialog( Controllers.taskDialog(Task.composeAsync(() -> {
new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest, file.getFile().getIntegrityCheck()).executor(true), FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(file.getFile().getUrl()), dest, file.getFile().getIntegrityCheck());
i18n("message.downloading") task.setName(file.getName());
return task;
}),
i18n("message.downloading"),
TaskCancellationAction.NORMAL
); );
} }

View File

@ -31,6 +31,7 @@ import org.jackhuang.hmcl.task.TaskListener;
import org.jackhuang.hmcl.ui.*; import org.jackhuang.hmcl.ui.*;
import org.jackhuang.hmcl.ui.download.UpdateInstallerWizardProvider; import org.jackhuang.hmcl.ui.download.UpdateInstallerWizardProvider;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@ -161,7 +162,7 @@ public class InstallerListPage extends ListPageBase<InstallerItem> implements Ve
}); });
} }
}); });
Controllers.taskDialog(executor, i18n("install.installer.install_offline")); Controllers.taskDialog(executor, i18n("install.installer.install_offline"), TaskCancellationAction.NO_CANCEL);
executor.start(); executor.start();
} }

View File

@ -35,6 +35,7 @@ import org.jackhuang.hmcl.ui.ListPageBase;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane; import org.jackhuang.hmcl.ui.construct.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.PageAware; import org.jackhuang.hmcl.ui.construct.PageAware;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
@ -194,8 +195,7 @@ public final class ModListPage extends ListPageBase<ModListPageSkin.ModInfoObjec
} }
}) })
.withStagesHint(Collections.singletonList("mods.check_updates")), .withStagesHint(Collections.singletonList("mods.check_updates")),
i18n("update.checking"), pane -> { i18n("update.checking"), TaskCancellationAction.NORMAL);
});
} }
public void download() { public void download() {

View File

@ -39,6 +39,7 @@ import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.construct.*; import org.jackhuang.hmcl.ui.construct.*;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.Pair; import org.jackhuang.hmcl.util.Pair;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.i18n.I18n; import org.jackhuang.hmcl.util.i18n.I18n;
import java.net.URL; import java.net.URL;
@ -146,8 +147,7 @@ public class ModUpdatesPage extends BorderPane implements DecoratorPage {
} }
}), }),
i18n("mods.check_updates.update"), i18n("mods.check_updates.update"),
t -> { TaskCancellationAction.NORMAL);
});
} }
@Override @Override

View File

@ -44,6 +44,7 @@ import org.jackhuang.hmcl.ui.download.ModpackInstallWizardProvider;
import org.jackhuang.hmcl.ui.export.ExportWizardProvider; import org.jackhuang.hmcl.ui.export.ExportWizardProvider;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
@ -52,6 +53,7 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
@ -91,13 +93,16 @@ public final class Versions {
.whenComplete(Schedulers.javafx(), e -> { .whenComplete(Schedulers.javafx(), e -> {
if (e == null) { if (e == null) {
Controllers.getDecorator().startWizard(new ModpackInstallWizardProvider(profile, modpack.toFile())); Controllers.getDecorator().startWizard(new ModpackInstallWizardProvider(profile, modpack.toFile()));
} else if (e instanceof CancellationException) {
Controllers.showToast(i18n("message.cancelled"));
} else { } else {
Controllers.dialog( Controllers.dialog(
i18n("install.failed.downloading.detail", file.getFile().getUrl()) + "\n" + StringUtils.getStackTrace(e), i18n("install.failed.downloading.detail", file.getFile().getUrl()) + "\n" + StringUtils.getStackTrace(e),
i18n("download.failed"), MessageDialogPane.MessageType.ERROR); i18n("download.failed"), MessageDialogPane.MessageType.ERROR);
} }
}).executor(true), }).executor(true),
i18n("message.downloading") i18n("message.downloading"),
TaskCancellationAction.NORMAL
); );
} }
@ -172,7 +177,7 @@ public final class Versions {
public static void updateGameAssets(Profile profile, String version) { public static void updateGameAssets(Profile profile, String version) {
TaskExecutor executor = new GameAssetDownloadTask(profile.getDependency(), profile.getRepository().getVersion(version), GameAssetDownloadTask.DOWNLOAD_INDEX_FORCIBLY, true) TaskExecutor executor = new GameAssetDownloadTask(profile.getDependency(), profile.getRepository().getVersion(version), GameAssetDownloadTask.DOWNLOAD_INDEX_FORCIBLY, true)
.executor(); .executor();
Controllers.taskDialog(executor, i18n("version.manage.redownload_assets_index")); Controllers.taskDialog(executor, i18n("version.manage.redownload_assets_index"), TaskCancellationAction.NO_CANCEL);
executor.start(); executor.start();
} }

View File

@ -26,6 +26,7 @@ import org.jackhuang.hmcl.ui.construct.DialogCloseEvent;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType; import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane; import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
@ -41,10 +42,10 @@ public abstract class TaskExecutorDialogWizardDisplayer extends AbstractWizardDi
@Override @Override
public void handleTask(Map<String, Object> settings, Task<?> task) { public void handleTask(Map<String, Object> settings, Task<?> task) {
TaskExecutorDialogPane pane = new TaskExecutorDialogPane(it -> { TaskExecutorDialogPane pane = new TaskExecutorDialogPane(new TaskCancellationAction(it -> {
it.fireEvent(new DialogCloseEvent()); it.fireEvent(new DialogCloseEvent());
onEnd(); onEnd();
}); }));
pane.setTitle(i18n("message.doing")); pane.setTitle(i18n("message.doing"));
if (settings.containsKey("title")) { if (settings.containsKey("title")) {

View File

@ -29,6 +29,7 @@ import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.UpgradeDialog; import org.jackhuang.hmcl.ui.UpgradeDialog;
import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType; import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.io.JarUtils; import org.jackhuang.hmcl.util.io.JarUtils;
import org.jackhuang.hmcl.util.platform.JavaVersion; import org.jackhuang.hmcl.util.platform.JavaVersion;
@ -39,6 +40,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -100,8 +102,9 @@ public final class UpdateHandler {
Task<?> task = new HMCLDownloadTask(version, downloaded); Task<?> task = new HMCLDownloadTask(version, downloaded);
TaskExecutor executor = task.executor(); TaskExecutor executor = task.executor(false);
Controllers.taskDialog(executor, i18n("message.downloading")); Controllers.taskDialog(executor, i18n("message.downloading"), TaskCancellationAction.NORMAL);
executor.start();
thread(() -> { thread(() -> {
boolean success = executor.test(); boolean success = executor.test();
@ -121,8 +124,12 @@ public final class UpdateHandler {
} else { } else {
Exception e = executor.getException(); Exception e = executor.getException();
LOG.log(Level.WARNING, "Failed to update to " + version, e); LOG.log(Level.WARNING, "Failed to update to " + version, e);
if (e instanceof CancellationException) {
Platform.runLater(() -> Controllers.showToast(i18n("message.cancelled")));
} else {
Platform.runLater(() -> Controllers.dialog(e.toString(), i18n("update.failed"), MessageType.ERROR)); Platform.runLater(() -> Controllers.dialog(e.toString(), i18n("update.failed"), MessageType.ERROR));
} }
}
}); });
})); }));
} }

View File

@ -0,0 +1,42 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2022 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util;
import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane;
import java.util.function.Consumer;
public class TaskCancellationAction {
public static TaskCancellationAction NO_CANCEL = new TaskCancellationAction((Consumer<TaskExecutorDialogPane>) null);
public static TaskCancellationAction NORMAL = new TaskCancellationAction(() -> {
});
private final Consumer<TaskExecutorDialogPane> cancellationAction;
public TaskCancellationAction(Runnable cancellationAction) {
this.cancellationAction = it -> cancellationAction.run();
}
public TaskCancellationAction(Consumer<TaskExecutorDialogPane> cancellationAction) {
this.cancellationAction = cancellationAction;
}
public Consumer<TaskExecutorDialogPane> getCancellationAction() {
return cancellationAction;
}
}

View File

@ -119,7 +119,7 @@
} }
.radio-button-title-label { .radio-button-title-label {
-fx-font-size: 16.0px; -fx-font-size: 16px;
-fx-padding: 14.0 0.0 -20.0 0.0; -fx-padding: 14.0 0.0 -20.0 0.0;
-fx-text-fill: rgba(0.0, 0.0, 0.0, 0.87); -fx-text-fill: rgba(0.0, 0.0, 0.0, 0.87);
} }