feat: continue launching after adding account

This commit is contained in:
Haowei Wen
2021-09-06 21:55:16 +08:00
parent dc2911f7d8
commit 3051332165
2 changed files with 47 additions and 47 deletions

View File

@@ -17,7 +17,6 @@
*/ */
package org.jackhuang.hmcl.ui.main; package org.jackhuang.hmcl.ui.main;
import javafx.application.Platform;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.SkinBase; import javafx.scene.control.SkinBase;
import javafx.scene.layout.BorderPane; 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.Controllers;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.account.AccountAdvancedListItem; 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.AdvancedListBox;
import org.jackhuang.hmcl.ui.construct.AdvancedListItem; import org.jackhuang.hmcl.ui.construct.AdvancedListItem;
import org.jackhuang.hmcl.ui.construct.TabHeader; import org.jackhuang.hmcl.ui.construct.TabHeader;
@@ -140,13 +138,7 @@ public class RootPage extends DecoratorTabPage {
// first item in left sidebar // first item in left sidebar
AccountAdvancedListItem accountListItem = new AccountAdvancedListItem(); AccountAdvancedListItem accountListItem = new AccountAdvancedListItem();
accountListItem.setOnAction(e -> { accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage()));
Controllers.navigate(Controllers.getAccountListPage());
if (Accounts.getAccounts().isEmpty()) {
Controllers.dialog(new CreateAccountPane());
}
});
accountListItem.accountProperty().bind(Accounts.selectedAccountProperty()); accountListItem.accountProperty().bind(Accounts.selectedAccountProperty());
// second item in left sidebar // 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 boolean checkedModpack = false;
private void onRefreshedVersions(HMCLGameRepository repository) { private void onRefreshedVersions(HMCLGameRepository repository) {
@@ -257,7 +228,7 @@ public class RootPage extends DecoratorTabPage {
.thenApplyAsync(modpack -> ModpackHelper .thenApplyAsync(modpack -> ModpackHelper
.getInstallTask(repository.getProfile(), modpackFile, modpack.getName(), .getInstallTask(repository.getProfile(), modpackFile, modpack.getName(),
modpack) modpack)
.withRunAsync(Schedulers.javafx(), this::checkAccount).executor()) .executor())
.thenAcceptAsync(Schedulers.javafx(), executor -> { .thenAcceptAsync(Schedulers.javafx(), executor -> {
Controllers.taskDialog(executor, i18n("modpack.installing")); Controllers.taskDialog(executor, i18n("modpack.installing"));
executor.start(); executor.start();
@@ -265,8 +236,6 @@ public class RootPage extends DecoratorTabPage {
} }
} }
} }
checkAccount();
}); });
} }
} }

View File

@@ -18,7 +18,11 @@
package org.jackhuang.hmcl.ui.versions; package org.jackhuang.hmcl.ui.versions;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import javafx.application.Platform;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.download.game.GameAssetDownloadTask; import org.jackhuang.hmcl.download.game.GameAssetDownloadTask;
import org.jackhuang.hmcl.game.GameDirectoryType; import org.jackhuang.hmcl.game.GameDirectoryType;
import org.jackhuang.hmcl.game.GameRepository; 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.task.TaskExecutor;
import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.FXUtils; 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.MessageDialogPane;
import org.jackhuang.hmcl.ui.construct.PromptDialogPane; import org.jackhuang.hmcl.ui.construct.PromptDialogPane;
import org.jackhuang.hmcl.ui.construct.Validator; import org.jackhuang.hmcl.ui.construct.Validator;
@@ -50,6 +56,7 @@ import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; 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) { public static void generateLaunchScript(Profile profile, String id) {
if (checkForLaunching(profile, id)) { if (!checkVersionForLaunching(profile, id))
return;
ensureSelectedAccount(account -> {
GameRepository repository = profile.getRepository(); GameRepository repository = profile.getRepository();
FileChooser chooser = new FileChooser(); FileChooser chooser = new FileChooser();
if (repository.getRunDirectory(id).isDirectory()) if (repository.getRunDirectory(id).isDirectory())
@@ -196,33 +205,55 @@ public final class Versions {
: new FileChooser.ExtensionFilter(i18n("extension.sh"), "*.sh")); : new FileChooser.ExtensionFilter(i18n("extension.sh"), "*.sh"));
File file = chooser.showSaveDialog(Controllers.getStage()); File file = chooser.showSaveDialog(Controllers.getStage());
if (file != null) 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) { public static void launch(Profile profile, String id) {
if (checkForLaunching(profile, id)) if (!checkVersionForLaunching(profile, id))
new LauncherHelper(profile, Accounts.getSelectedAccount(), id).launch(); return;
ensureSelectedAccount(account -> {
new LauncherHelper(profile, account, id).launch();
});
} }
public static void testGame(Profile profile, String id) { public static void testGame(Profile profile, String id) {
if (checkForLaunching(profile, id)) { if (!checkVersionForLaunching(profile, id))
LauncherHelper helper = new LauncherHelper(profile, Accounts.getSelectedAccount(), id); return;
ensureSelectedAccount(account -> {
LauncherHelper helper = new LauncherHelper(profile, account, id);
helper.setTestMode(); helper.setTestMode();
helper.launch(); helper.launch();
} });
} }
private static boolean checkForLaunching(Profile profile, String id) { private static boolean checkVersionForLaunching(Profile profile, String id) {
if (Accounts.getSelectedAccount() == null) if (id == null || !profile.getRepository().isLoaded() || !profile.getRepository().hasVersion(id)) {
Controllers.getRootPage().checkAccountForcibly();
else if (id == null || !profile.getRepository().isLoaded() || !profile.getRepository().hasVersion(id))
Controllers.dialog(i18n("version.empty.launch"), i18n("launch.failed"), MessageDialogPane.MessageType.ERROR, () -> { Controllers.dialog(i18n("version.empty.launch"), i18n("launch.failed"), MessageDialogPane.MessageType.ERROR, () -> {
Controllers.navigate(Controllers.getGameListPage()); Controllers.navigate(Controllers.getGameListPage());
}); });
else
return true;
return false; return false;
} else {
return true;
}
}
private static void ensureSelectedAccount(Consumer<Account> 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) { public static void modifyGlobalSettings(Profile profile) {