优化陶瓦联机用户须知对话框 (#4686)

This commit is contained in:
Glavo
2025-10-18 20:25:00 +08:00
committed by GitHub
parent e34ec9f801
commit c4823b3e55
3 changed files with 37 additions and 26 deletions

View File

@@ -64,6 +64,7 @@ import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.io.FileUtils; import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.Architecture; import org.jackhuang.hmcl.util.platform.Architecture;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.Nullable;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
@@ -433,26 +434,34 @@ public final class Controllers {
dialog(new MessageDialogPane.Builder(text, title, type).actionOrCancel(actionButton, cancel).build()); dialog(new MessageDialogPane.Builder(text, title, type).actionOrCancel(actionButton, cancel).build());
} }
public static void confirmActionDanger(String text, String title, Runnable resolve, Runnable cancel) { public static void confirmWithCountdown(String text, String title, int seconds, MessageType messageType,
JFXButton btnYes = new JFXButton(i18n("button.ok")); @Nullable Runnable ok, @Nullable Runnable cancel) {
btnYes.getStyleClass().add("dialog-error"); if (seconds <= 0)
btnYes.setOnAction(e -> resolve.run()); throw new IllegalArgumentException("Seconds must be greater than 0");
btnYes.setDisable(true);
int countdown = 10; JFXButton btnOk = new JFXButton(i18n("button.ok"));
KeyFrame[] keyFrames = new KeyFrame[countdown + 1]; btnOk.getStyleClass().add(messageType == MessageType.WARNING || messageType == MessageType.ERROR
for (int i = 0; i < countdown; i++) { ? "dialog-error"
: "dialog-accept");
if (ok != null)
btnOk.setOnAction(e -> ok.run());
btnOk.setDisable(true);
KeyFrame[] keyFrames = new KeyFrame[seconds + 1];
for (int i = 0; i < seconds; i++) {
keyFrames[i] = new KeyFrame(Duration.seconds(i), keyFrames[i] = new KeyFrame(Duration.seconds(i),
new KeyValue(btnYes.textProperty(), i18n("button.ok.countdown", countdown - i))); new KeyValue(btnOk.textProperty(), i18n("button.ok.countdown", seconds - i)));
} }
keyFrames[countdown] = new KeyFrame(Duration.seconds(countdown), keyFrames[seconds] = new KeyFrame(Duration.seconds(seconds),
new KeyValue(btnYes.textProperty(), i18n("button.ok")), new KeyValue(btnOk.textProperty(), i18n("button.ok")),
new KeyValue(btnYes.disableProperty(), false)); new KeyValue(btnOk.disableProperty(), false));
Timeline timeline = new Timeline(keyFrames); Timeline timeline = new Timeline(keyFrames);
confirmAction(text, title, MessageType.WARNING, btnYes, () -> { confirmAction(text, title, messageType, btnOk, () -> {
timeline.stop(); timeline.stop();
cancel.run(); if (cancel != null)
cancel.run();
}); });
timeline.play(); timeline.play();
} }

View File

@@ -264,10 +264,12 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware {
}; };
if (factory instanceof OfflineAccountFactory && username != null && (!USERNAME_CHECKER_PATTERN.matcher(username).matches() || username.length() > 16)) { if (factory instanceof OfflineAccountFactory && username != null && (!USERNAME_CHECKER_PATTERN.matcher(username).matches() || username.length() > 16)) {
Controllers.confirmActionDanger(i18n("account.methods.offline.name.invalid"), i18n("message.warning"), doCreate, () -> { Controllers.confirmWithCountdown(i18n("account.methods.offline.name.invalid"), i18n("message.warning"), 10,
body.setDisable(false); MessageDialogPane.MessageType.WARNING,
spinner.hideSpinner(); doCreate, () -> {
}); body.setDisable(false);
spinner.hideSpinner();
});
} else { } else {
doCreate.run(); doCreate.run();
} }

View File

@@ -138,13 +138,13 @@ public class TerracottaControllerPage extends StackPane {
TerracottaState state = UI_STATE.get(), next; TerracottaState state = UI_STATE.get(), next;
if (state instanceof TerracottaState.Uninitialized || state instanceof TerracottaState.Preparing preparing && preparing.hasInstallFence()) { if (state instanceof TerracottaState.Uninitialized || state instanceof TerracottaState.Preparing preparing && preparing.hasInstallFence()) {
if (state instanceof TerracottaState.Uninitialized uninitialized && !uninitialized.hasLegacy()) { if (state instanceof TerracottaState.Uninitialized uninitialized && !uninitialized.hasLegacy()) {
Controllers.confirmActionDanger(i18n("terracotta.confirm.desc"), i18n("terracotta.confirm.title"), () -> { Controllers.confirmWithCountdown(i18n("terracotta.confirm.desc"), i18n("terracotta.confirm.title"), 5,
TerracottaState.Preparing s = TerracottaManager.install(path); MessageDialogPane.MessageType.INFO, () -> {
if (s != null) { TerracottaState.Preparing s = TerracottaManager.install(path);
UI_STATE.set(s); if (s != null) {
} UI_STATE.set(s);
}, () -> { }
}); }, null);
return; return;
} }
@@ -202,7 +202,7 @@ public class TerracottaControllerPage extends StackPane {
} }
} }
} else { } else {
Controllers.confirmActionDanger(i18n("terracotta.confirm.desc"), i18n("terracotta.confirm.title"), () -> { Controllers.confirmWithCountdown(i18n("terracotta.confirm.desc"), i18n("terracotta.confirm.title"), 5, MessageDialogPane.MessageType.INFO, () -> {
globalConfig().setTerracottaAgreementVersion(1); globalConfig().setTerracottaAgreementVersion(1);
TerracottaState.Preparing s = TerracottaManager.install(null); TerracottaState.Preparing s = TerracottaManager.install(null);
if (s != null) { if (s != null) {