Skip to content

增加在添加游戏目录页面选择目录时自动填写名称 - #4289

Merged
Glavo merged 11 commits into
HMCL-dev:mainfrom
MinecraftYJQ:main
Jan 3, 2026
Merged

增加在添加游戏目录页面选择目录时自动填写名称#4289
Glavo merged 11 commits into
HMCL-dev:mainfrom
MinecraftYJQ:main

Conversation

@MinecraftYJQ

Copy link
Copy Markdown
Contributor

在 游戏目录>添加游戏目录 中选择游戏目录时会自动填写游戏目录的父级目录名,以方便用户新增游戏目录

@Glavo Glavo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 不要在新代码中使用 File,请使用 NIO API;
  2. 不应该通过比较 parentOldFolder 检测 profile 名称是否被显式设置了。你应该通过创建一个 binding 来跟踪 locationProperty 和 profile 的nameProperty 的变化。

Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java Outdated
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java Outdated
@Glavo

Glavo commented Dec 22, 2025

Copy link
Copy Markdown
Member

请解决代码和主线的冲突。

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an auto-fill feature for the profile name field when adding a new game directory. When a user selects a game directory, the profile name is automatically populated with the parent directory's name to improve user experience.

Key Changes:

  • Added automatic profile name suggestion based on the parent directory name when selecting a game directory
  • Implemented tracking of manual edits to prevent overwriting user-entered names
  • Changed imports from individual property imports to wildcard import

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

private final JFXTextField txtProfileName;
private final FileItem gameDir;
private final OptionToggleButton toggleUseRelativePath;
private boolean nameManuallyEdited = false;

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When editing an existing profile (profile != null), the nameManuallyEdited flag should be initialized to true. The current implementation leaves it as false, which means if the user changes the directory while editing an existing profile, the profile name will be unexpectedly overwritten with the parent directory name. This auto-fill feature should only apply when creating a new profile.

Copilot uses AI. Check for mistakes.
Path parent = newPath.getParent();

if (parent != null) {
Path suggestedName = parent.toAbsolutePath().getFileName();

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling toAbsolutePath() on the parent path before getting the file name is unnecessary and could produce unexpected results. The parent path is already obtained from the newPath, so you should get the file name directly from parent without converting to absolute path first. This could cause issues if the path resolution behaves differently than expected.

Suggested change
Path suggestedName = parent.toAbsolutePath().getFileName();
Path suggestedName = parent.getFileName();

Copilot uses AI. Check for mistakes.


locationProperty().addListener((observable, oldValue, newValue) -> {
if (nameManuallyEdited && !txtProfileName.getText().isEmpty())

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition logic is incorrect. The current code uses AND (&&) which means it will return early only if BOTH conditions are true. This means auto-fill will happen even when the name has been manually edited, as long as the text field is empty. The condition should use OR (||) instead: if the name was manually edited OR the text field is not empty, then skip auto-filling.

Suggested change
if (nameManuallyEdited && !txtProfileName.getText().isEmpty())
if (nameManuallyEdited || !txtProfileName.getText().isEmpty())

Copilot uses AI. Check for mistakes.
Comment on lines +164 to +167
txtProfileName.textProperty().addListener((observable, oldValue, newValue) -> {
if (txtProfileName.isFocused()) {
nameManuallyEdited = true;
}

Copilot AI Jan 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach to detect manual editing is unreliable. Checking if the text field is focused during a text change doesn't properly distinguish between user input and programmatic updates. The text property listener will fire when the auto-fill code calls setText(), and if the field happens to be focused at that moment, it will incorrectly mark the name as manually edited. Consider using a flag that's set only when user types in the field (e.g., using setOnKeyTyped) or track whether the setText call is coming from the auto-fill logic.

Suggested change
txtProfileName.textProperty().addListener((observable, oldValue, newValue) -> {
if (txtProfileName.isFocused()) {
nameManuallyEdited = true;
}
txtProfileName.setOnKeyTyped(event -> {
nameManuallyEdited = true;

Copilot uses AI. Check for mistakes.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java
@Glavo
Glavo merged commit acd41e5 into HMCL-dev:main Jan 3, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants