diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/RootPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/RootPage.java index 6d14c88b6..c0e17e2f2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/RootPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/RootPage.java @@ -17,7 +17,6 @@ */ package org.jackhuang.hmcl.ui.main; -import javafx.application.Platform; import javafx.scene.Node; import javafx.scene.control.SkinBase; import javafx.scene.layout.BorderPane; @@ -34,7 +33,6 @@ import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.account.AccountAdvancedListItem; -import org.jackhuang.hmcl.ui.account.CreateAccountPane; import org.jackhuang.hmcl.ui.construct.AdvancedListBox; import org.jackhuang.hmcl.ui.construct.AdvancedListItem; import org.jackhuang.hmcl.ui.construct.TabHeader; @@ -140,13 +138,7 @@ public class RootPage extends DecoratorTabPage { // first item in left sidebar AccountAdvancedListItem accountListItem = new AccountAdvancedListItem(); - accountListItem.setOnAction(e -> { - Controllers.navigate(Controllers.getAccountListPage()); - - if (Accounts.getAccounts().isEmpty()) { - Controllers.dialog(new CreateAccountPane()); - } - }); + accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage())); accountListItem.accountProperty().bind(Accounts.selectedAccountProperty()); // second item in left sidebar @@ -220,27 +212,6 @@ public class RootPage extends DecoratorTabPage { } - // ==== Accounts ==== - - private boolean checkedAccont = false; - - public void checkAccount() { - if (checkedAccont) - return; - checkedAccont = true; - checkAccountForcibly(); - } - - public void checkAccountForcibly() { - if (Accounts.getAccounts().isEmpty()) - Platform.runLater(this::addNewAccount); - } - - private void addNewAccount() { - Controllers.dialog(new CreateAccountPane()); - } - // ==== - private boolean checkedModpack = false; private void onRefreshedVersions(HMCLGameRepository repository) { @@ -257,7 +228,7 @@ public class RootPage extends DecoratorTabPage { .thenApplyAsync(modpack -> ModpackHelper .getInstallTask(repository.getProfile(), modpackFile, modpack.getName(), modpack) - .withRunAsync(Schedulers.javafx(), this::checkAccount).executor()) + .executor()) .thenAcceptAsync(Schedulers.javafx(), executor -> { Controllers.taskDialog(executor, i18n("modpack.installing")); executor.start(); @@ -265,8 +236,6 @@ public class RootPage extends DecoratorTabPage { } } } - - checkAccount(); }); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java index 55812f1f4..1cd3c1271 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java @@ -18,7 +18,11 @@ package org.jackhuang.hmcl.ui.versions; import com.jfoenix.controls.JFXButton; + +import javafx.application.Platform; import javafx.stage.FileChooser; + +import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.download.game.GameAssetDownloadTask; import org.jackhuang.hmcl.game.GameDirectoryType; import org.jackhuang.hmcl.game.GameRepository; @@ -33,6 +37,8 @@ import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.TaskExecutor; import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.FXUtils; +import org.jackhuang.hmcl.ui.account.CreateAccountPane; +import org.jackhuang.hmcl.ui.construct.DialogCloseEvent; import org.jackhuang.hmcl.ui.construct.MessageDialogPane; import org.jackhuang.hmcl.ui.construct.PromptDialogPane; import org.jackhuang.hmcl.ui.construct.Validator; @@ -50,6 +56,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; import java.util.logging.Level; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; @@ -185,7 +192,9 @@ public final class Versions { } public static void generateLaunchScript(Profile profile, String id) { - if (checkForLaunching(profile, id)) { + if (!checkVersionForLaunching(profile, id)) + return; + ensureSelectedAccount(account -> { GameRepository repository = profile.getRepository(); FileChooser chooser = new FileChooser(); if (repository.getRunDirectory(id).isDirectory()) @@ -196,33 +205,55 @@ public final class Versions { : new FileChooser.ExtensionFilter(i18n("extension.sh"), "*.sh")); File file = chooser.showSaveDialog(Controllers.getStage()); if (file != null) - new LauncherHelper(profile, Accounts.getSelectedAccount(), id).makeLaunchScript(file); - } + new LauncherHelper(profile, account, id).makeLaunchScript(file); + }); } public static void launch(Profile profile, String id) { - if (checkForLaunching(profile, id)) - new LauncherHelper(profile, Accounts.getSelectedAccount(), id).launch(); + if (!checkVersionForLaunching(profile, id)) + return; + ensureSelectedAccount(account -> { + new LauncherHelper(profile, account, id).launch(); + }); } public static void testGame(Profile profile, String id) { - if (checkForLaunching(profile, id)) { - LauncherHelper helper = new LauncherHelper(profile, Accounts.getSelectedAccount(), id); + if (!checkVersionForLaunching(profile, id)) + return; + ensureSelectedAccount(account -> { + LauncherHelper helper = new LauncherHelper(profile, account, id); helper.setTestMode(); helper.launch(); - } + }); } - private static boolean checkForLaunching(Profile profile, String id) { - if (Accounts.getSelectedAccount() == null) - Controllers.getRootPage().checkAccountForcibly(); - else if (id == null || !profile.getRepository().isLoaded() || !profile.getRepository().hasVersion(id)) + private static boolean checkVersionForLaunching(Profile profile, String id) { + if (id == null || !profile.getRepository().isLoaded() || !profile.getRepository().hasVersion(id)) { Controllers.dialog(i18n("version.empty.launch"), i18n("launch.failed"), MessageDialogPane.MessageType.ERROR, () -> { Controllers.navigate(Controllers.getGameListPage()); }); - else + return false; + } else { return true; - return false; + } + } + + private static void ensureSelectedAccount(Consumer action) { + Account account = Accounts.getSelectedAccount(); + if (account == null) { + CreateAccountPane dialog = new CreateAccountPane(); + dialog.addEventHandler(DialogCloseEvent.CLOSE, e -> { + Account newAccount = Accounts.getSelectedAccount(); + if (newAccount == null) { + // user cancelled operation + } else { + Platform.runLater(() -> action.accept(newAccount)); + } + }); + Controllers.dialog(dialog); + } else { + action.accept(account); + } } public static void modifyGlobalSettings(Profile profile) {