chore: 转换 TaskCancellationAction 为函数式接口 (#6991)

Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
鱼前三百
2026-10-16 20:49:23 +08:00
committed by GitHub
parent 452d030817
commit 4e2d5cac9e
4 changed files with 26 additions and 22 deletions
@@ -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;
@@ -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 -> {
@@ -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")) {
@@ -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<TaskExecutorDialogPane>) null);
public static TaskCancellationAction NORMAL = new TaskCancellationAction(() -> {
});
@NotNullByDefault
@FunctionalInterface
public interface TaskCancellationAction extends Consumer<TaskExecutorDialogPane> {
private final Consumer<TaskExecutorDialogPane> 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<TaskExecutorDialogPane> cancellationAction) {
this.cancellationAction = cancellationAction;
}
public Consumer<TaskExecutorDialogPane> getCancellationAction() {
return cancellationAction;
}
}