diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index 62b9588146..a1ce26124e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -305,10 +305,10 @@ public final class LauncherHelper { if (launcherVisibility == LauncherVisibility.CLOSE) Launcher.stopApplication(); else - launchingStepsPane.setCancel(new TaskCancellationAction(it -> { + launchingStepsPane.setCancel(it -> { process.stop(); it.fireEvent(new DialogCloseEvent()); - })); + }); } else { runLater(() -> { launchingStepsPane.fireEvent(new DialogCloseEvent()); @@ -746,7 +746,7 @@ public final class LauncherHelper { } future.completeExceptionally(new CancellationException()); } - }), i18n("download.java"), new TaskCancellationAction(() -> future.completeExceptionally(new CancellationException()))); + }), i18n("download.java"), it -> future.completeExceptionally(new CancellationException())); }, () -> future.completeExceptionally(new CancellationException())).build()); return future; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java index 943d487f65..0c11cb2a33 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TaskExecutorDialogPane.java @@ -85,11 +85,11 @@ public class TaskExecutorDialogPane extends BorderPane { setCancel(cancel); - btnCancel.setDisable(onCancel.getCancellationAction() == null); + btnCancel.setDisable(!onCancel.isEnabled()); btnCancel.setOnAction(e -> { if (executor != null) executor.cancel(); - onCancel.getCancellationAction().accept(this); + onCancel.accept(this); }); speedEventHandler = FetchTask.SPEED_EVENT.registerWeak(speedEvent -> { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/wizard/TaskExecutorDialogWizardDisplayer.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/wizard/TaskExecutorDialogWizardDisplayer.java index 5e2e6beb61..ace3dd3310 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/wizard/TaskExecutorDialogWizardDisplayer.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/wizard/TaskExecutorDialogWizardDisplayer.java @@ -27,7 +27,6 @@ import org.jackhuang.hmcl.ui.construct.MessageDialogPane.MessageType; import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane; import org.jackhuang.hmcl.util.SettingsMap; import org.jackhuang.hmcl.util.StringUtils; -import org.jackhuang.hmcl.util.TaskCancellationAction; import org.jetbrains.annotations.Nullable; import java.util.Queue; @@ -46,7 +45,7 @@ public abstract class TaskExecutorDialogWizardDisplayer extends AbstractWizardDi @Override public void handleTask(SettingsMap settings, Task task) { @Nullable WizardProvider.TaskCleanup cleanup = settings.remove(WizardProvider.TaskCleanup.KEY); - TaskExecutorDialogPane pane = new TaskExecutorDialogPane(new TaskCancellationAction(it -> { + TaskExecutorDialogPane pane = new TaskExecutorDialogPane(it -> { if (cleanup != null) { // Keep the installation modal until onStop has released its repository draft. it.setCancel(null); @@ -54,7 +53,7 @@ public abstract class TaskExecutorDialogWizardDisplayer extends AbstractWizardDi } it.fireEvent(new DialogCloseEvent()); onEnd(); - })); + }); pane.setTitle(i18n("message.doing")); if (settings.containsKey("title")) { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/TaskCancellationAction.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/TaskCancellationAction.java index 7360287663..7ba2b0bae0 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/TaskCancellationAction.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/TaskCancellationAction.java @@ -18,25 +18,30 @@ package org.jackhuang.hmcl.util; import org.jackhuang.hmcl.ui.construct.TaskExecutorDialogPane; +import org.jetbrains.annotations.NotNullByDefault; import java.util.function.Consumer; -public final class TaskCancellationAction { - public static TaskCancellationAction NO_CANCEL = new TaskCancellationAction((Consumer) null); - public static TaskCancellationAction NORMAL = new TaskCancellationAction(() -> { - }); +@NotNullByDefault +@FunctionalInterface +public interface TaskCancellationAction extends Consumer { - private final Consumer cancellationAction; + TaskCancellationAction NO_CANCEL = new TaskCancellationAction() { + @Override + public boolean isEnabled() { + return false; + } - public TaskCancellationAction(Runnable cancellationAction) { - this.cancellationAction = it -> cancellationAction.run(); + @Override + public void accept(TaskExecutorDialogPane taskExecutorDialogPane) { + } + }; + + TaskCancellationAction NORMAL = ignored -> { + }; + + default boolean isEnabled() { + return true; } - public TaskCancellationAction(Consumer cancellationAction) { - this.cancellationAction = cancellationAction; - } - - public Consumer getCancellationAction() { - return cancellationAction; - } }