增加在添加游戏目录页面选择目录时自动填写名称 - #4289
Conversation
Glavo
left a comment
There was a problem hiding this comment.
- 不要在新代码中使用
File,请使用 NIO API; - 不应该通过比较
parentOldFolder检测 profile 名称是否被显式设置了。你应该通过创建一个 binding 来跟踪locationProperty和 profile 的nameProperty的变化。
|
请解决代码和主线的冲突。 |
# Conflicts: # HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
| Path parent = newPath.getParent(); | ||
|
|
||
| if (parent != null) { | ||
| Path suggestedName = parent.toAbsolutePath().getFileName(); |
There was a problem hiding this comment.
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.
| Path suggestedName = parent.toAbsolutePath().getFileName(); | |
| Path suggestedName = parent.getFileName(); |
|
|
||
|
|
||
| locationProperty().addListener((observable, oldValue, newValue) -> { | ||
| if (nameManuallyEdited && !txtProfileName.getText().isEmpty()) |
There was a problem hiding this comment.
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.
| if (nameManuallyEdited && !txtProfileName.getText().isEmpty()) | |
| if (nameManuallyEdited || !txtProfileName.getText().isEmpty()) |
| txtProfileName.textProperty().addListener((observable, oldValue, newValue) -> { | ||
| if (txtProfileName.isFocused()) { | ||
| nameManuallyEdited = true; | ||
| } |
There was a problem hiding this comment.
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.
| txtProfileName.textProperty().addListener((observable, oldValue, newValue) -> { | |
| if (txtProfileName.isFocused()) { | |
| nameManuallyEdited = true; | |
| } | |
| txtProfileName.setOnKeyTyped(event -> { | |
| nameManuallyEdited = true; |
There was a problem hiding this comment.
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.
在 游戏目录>添加游戏目录 中选择游戏目录时会自动填写游戏目录的父级目录名,以方便用户新增游戏目录