From fbb5a03e5d5af600307facdf376193c9eabb1f7c Mon Sep 17 00:00:00 2001 From: Haowei Wen Date: Sun, 13 Jun 2021 01:30:32 +0800 Subject: [PATCH] feat: select authlib-injector server after added * The most recently added server will be at the top. * In Add Account dialog, after adding an authlib-injector server, it will be automatically selected. --- .../hmcl/ui/account/AddAccountPane.java | 19 ++++++++++--------- .../account/AddAuthlibInjectorServerPane.java | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java index 6b30d150f..074cae9db 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAccountPane.java @@ -1,6 +1,6 @@ /* * Hello Minecraft! Launcher - * Copyright (C) 2020 huangyuhui and contributors + * Copyright (C) 2021 huangyuhui and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -87,8 +87,8 @@ public class AddAccountPane extends StackPane { cboServers.setCellFactory(jfxListCellFactory(server -> new TwoLineListItem(server.getName(), server.getUrl()))); cboServers.setConverter(stringConverter(AuthlibInjectorServer::getName)); Bindings.bindContent(cboServers.getItems(), config().getAuthlibInjectorServers()); - cboServers.getItems().addListener(onInvalidating(this::selectDefaultServer)); - selectDefaultServer(); + cboServers.getItems().addListener(onInvalidating(this::resetServerSelection)); + resetServerSelection(); cboType.getItems().setAll(Accounts.FACTORIES); cboType.setConverter(stringConverter(Accounts::getLocalizedLoginTypeName)); @@ -174,12 +174,13 @@ public class AddAccountPane extends StackPane { return unmodifiableList(result); } - /** - * Selects the first server if no server is selected. - */ - private void selectDefaultServer() { - if (!cboServers.getItems().isEmpty() && cboServers.getSelectionModel().isEmpty()) { - cboServers.getSelectionModel().select(0); + private void resetServerSelection() { + if (!cboServers.getItems().isEmpty()) { + Platform.runLater(() -> { + // the selection will not be updated as expected + // if we call it immediately + cboServers.getSelectionModel().selectFirst(); + }); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAuthlibInjectorServerPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAuthlibInjectorServerPane.java index b45540d9d..4a5c91c65 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAuthlibInjectorServerPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AddAuthlibInjectorServerPane.java @@ -1,6 +1,6 @@ /* * Hello Minecraft! Launcher - * Copyright (C) 2020 huangyuhui and contributors + * Copyright (C) 2021 huangyuhui and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -129,7 +129,7 @@ public class AddAuthlibInjectorServerPane extends StackPane implements DialogAwa @FXML private void onAddFinish() { if (!config().getAuthlibInjectorServers().contains(serverBeingAdded)) { - config().getAuthlibInjectorServers().add(serverBeingAdded); + config().getAuthlibInjectorServers().add(0, serverBeingAdded); } fireEvent(new DialogCloseEvent()); }