From 2ad3e7a29152d051414c216bd4919d966dc21dcd Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Sun, 21 Aug 2022 21:26:20 +0800 Subject: [PATCH] fix(account): Authlib-Injector server may be unknown when in CreateAcountPane tab mode. --- .../hmcl/ui/account/CreateAccountPane.java | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java index 908c73a77..eae5bc90b 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/CreateAccountPane.java @@ -362,6 +362,7 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { private final AccountFactory factory; private @Nullable AuthlibInjectorServer server; + private @Nullable JFXComboBox cboServers; private @Nullable JFXTextField txtUsername; private @Nullable JFXPasswordField txtPassword; private @Nullable JFXTextField txtUUID; @@ -404,10 +405,46 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { add(boxServers, 1, rowIndex); rowIndex++; - } + } else if (factory instanceof AuthlibInjectorAccountFactory) { + Label lblServers = new Label(i18n("account.injector.server")); + setHalignment(lblServers, HPos.LEFT); + add(lblServers, 0, rowIndex); - if (factory instanceof AuthlibInjectorAccountFactory) { - throw new IllegalArgumentException("Use BoundAuthlibInjectorAccountFactory instead"); + cboServers = new JFXComboBox<>(); + cboServers.setCellFactory(jfxListCellFactory(server -> new TwoLineListItem(server.getName(), server.getUrl()))); + cboServers.setConverter(stringConverter(AuthlibInjectorServer::getName)); + bindContent(cboServers.getItems(), config().getAuthlibInjectorServers()); + cboServers.getItems().addListener(onInvalidating( + () -> Platform.runLater( // the selection will not be updated as expected if we call it immediately + cboServers.getSelectionModel()::selectFirst))); + cboServers.getSelectionModel().selectFirst(); + cboServers.setPromptText(i18n("account.injector.empty")); + BooleanBinding noServers = createBooleanBinding(cboServers.getItems()::isEmpty, cboServers.getItems()); + classPropertyFor(cboServers, "jfx-combo-box-warning").bind(noServers); + classPropertyFor(cboServers, "jfx-combo-box").bind(noServers.not()); + HBox.setHgrow(cboServers, Priority.ALWAYS); + HBox.setMargin(cboServers, new Insets(0, 10, 0, 0)); + cboServers.setMaxWidth(Double.MAX_VALUE); + + HBox linksContainer = new HBox(); + linksContainer.setAlignment(Pos.CENTER); + onChangeAndOperate(cboServers.valueProperty(), server -> { + this.server = server; + linksContainer.getChildren().setAll(createHyperlinks(server)); + }); + linksContainer.setMinWidth(USE_PREF_SIZE); + + JFXButton btnAddServer = new JFXButton(); + btnAddServer.setGraphic(SVG.plus(Theme.blackFillBinding(), 20, 20)); + btnAddServer.getStyleClass().add("toggle-icon4"); + btnAddServer.setOnAction(e -> { + Controllers.dialog(new AddAuthlibInjectorServerPane()); + }); + + HBox boxServers = new HBox(cboServers, linksContainer, btnAddServer); + add(boxServers, 1, rowIndex); + + rowIndex++; } if (factory.getLoginType().requiresUsername) { @@ -511,6 +548,8 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { valid = new BooleanBinding() { { + if (cboServers != null) + bind(cboServers.valueProperty()); if (txtUsername != null) bind(txtUsername.textProperty()); if (txtPassword != null) @@ -521,6 +560,8 @@ public class CreateAccountPane extends JFXDialogLayout implements DialogAware { @Override protected boolean computeValue() { + if (cboServers != null && cboServers.getValue() == null) + return false; if (txtUsername != null && !txtUsername.validate()) return false; if (txtPassword != null && !txtPassword.validate())