From 4e2d5cac9e3ca9783989b3e67ce16373576a2dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B1=BC=E5=89=8D=E4=B8=89=E7=99=BE?= <169084442+fishdry1000@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:49:23 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=BD=AC=E6=8D=A2=20TaskCancellationA?= =?UTF-8?q?ction=20=E4=B8=BA=E5=87=BD=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=20(#6991)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Glavo --- .../jackhuang/hmcl/game/LauncherHelper.java | 6 ++-- .../ui/construct/TaskExecutorDialogPane.java | 4 +-- .../TaskExecutorDialogWizardDisplayer.java | 5 ++- .../hmcl/util/TaskCancellationAction.java | 33 +++++++++++-------- 4 files changed, 26 insertions(+), 22 deletions(-) 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; - } }