From acd41e5885b1298b9d270119a7ff89ea7b9b0c64 Mon Sep 17 00:00:00 2001 From: Dime <122196845+MinecraftYJQ@users.noreply.github.com> Date: Sat, 3 Jan 2026 20:19:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=B7=BB=E5=8A=A0=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E9=A1=B5=E9=9D=A2=E9=80=89=E6=8B=A9=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=97=B6=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=86=99=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=20(#4289)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ciilu <109708109+Ciilu@users.noreply.github.com> Co-authored-by: Glavo --- .../hmcl/ui/profile/ProfilePage.java | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java index 91ea0b5ef..899fe7b70 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java @@ -22,10 +22,9 @@ import com.jfoenix.controls.JFXTextField; import com.jfoenix.validation.RequiredFieldValidator; import com.jfoenix.validation.base.ValidatorBase; import javafx.beans.binding.Bindings; -import javafx.beans.property.ReadOnlyObjectProperty; -import javafx.beans.property.ReadOnlyObjectWrapper; -import javafx.beans.property.SimpleStringProperty; -import javafx.beans.property.StringProperty; +import javafx.beans.property.*; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; @@ -44,6 +43,7 @@ import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.io.FileUtils; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.util.Optional; @@ -53,7 +53,6 @@ public final class ProfilePage extends BorderPane implements DecoratorPage { private final ReadOnlyObjectWrapper state = new ReadOnlyObjectWrapper<>(); private final StringProperty location; private final Profile profile; - private final JFXTextField txtProfileName; private final FileItem gameDir; private final OptionToggleButton toggleUseRelativePath; @@ -147,6 +146,38 @@ public final class ProfilePage extends BorderPane implements DecoratorPage { () -> !txtProfileName.validate() || StringUtils.isBlank(getLocation()), txtProfileName.textProperty(), location)); } + + ChangeListener locationChangeListener = (observable, oldValue, newValue) -> { + Path newPath; + try { + newPath = FileUtils.toAbsolute(Path.of(newValue)); + } catch (InvalidPathException ignored) { + return; + } + + if (!".minecraft".equals(FileUtils.getName(newPath))) + return; + + Path parent = newPath.getParent(); + if (parent == null) + return; + + String suggestedName = FileUtils.getName(parent); + if (!suggestedName.isBlank()) { + txtProfileName.setText(suggestedName); + } + }; + locationProperty().addListener(locationChangeListener); + + txtProfileName.textProperty().addListener(new ChangeListener<>() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + if (txtProfileName.isFocused()) { + txtProfileName.textProperty().removeListener(this); + locationProperty().removeListener(locationChangeListener); + } + } + }); } private void onSave() {