Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,36 @@
import javafx.beans.property.StringProperty;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Skin;

import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jetbrains.annotations.NotNull;

public class ProfileListItem extends RadioButton {
private final Profile profile;
private final StringProperty title = new SimpleStringProperty();
private final StringProperty subtitle = new SimpleStringProperty();

public ProfileListItem(Profile profile) {
public ProfileListItem(@NotNull Profile profile) {
this.profile = profile;
getStyleClass().setAll("profile-list-item", "navigation-drawer-item");
setUserData(profile);

title.set(Profiles.getProfileDisplayName(profile));
subtitle.set(profile.getGameDir().toString());

profile.nameProperty().addListener((obs, oldVal, newVal) -> title.set(Profiles.getProfileDisplayName(profile)));
Comment thread
NWMA-FYWF marked this conversation as resolved.
Outdated
profile.gameDirProperty().addListener((obs, oldVal, newVal) -> subtitle.set(profile.getGameDir().toString()));

FXUtils.onSecondaryButtonClicked(this, () -> ProfileListPopupMenu.show(this, profile));
}

@Override
protected Skin<?> createDefaultSkin() {
return new ProfileListItemSkin(this);
}

/// Removes this profile from the list.
public void remove() {
Profiles.getProfiles().remove(profile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2020 huangyuhui <huanghongxun2008@126.com> 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.ui.profile;

import com.jfoenix.controls.JFXPopup;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.construct.IconedMenuItem;
import org.jackhuang.hmcl.ui.construct.PopupMenu;

import static org.jackhuang.hmcl.util.i18n.I18n.i18n;

/// Popup menu for ProfileListItem.
public final class ProfileListPopupMenu extends StackPane {
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.

medium

ProfileListPopupMenu 类继承了 StackPane 并定义了构造函数,但在静态方法 show 中并没有使用该类的实例,而是直接创建了 PopupMenu。这导致继承和构造函数变得多余。建议将其改为工具类(私有化构造函数并移除继承),或者让其逻辑更加一致(例如让该类继承 PopupMenu 并在 show 中实例化)。

Copy link
Copy Markdown
Member

@Glavo Glavo May 11, 2026

Choose a reason for hiding this comment

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

我完全没理解这个 ProfileListPopupMenu 类是干什么的,为什么不是定义一个辅助方法,而是要这样一个继承 StackPane 的类并且完全没用上。


/// Shows the popup menu for the given profile item.
public static void show(Node owner, Profile profile) {
PopupMenu menu = new PopupMenu();
JFXPopup popup = new JFXPopup(menu);
menu.getContent().add(new IconedMenuItem(
org.jackhuang.hmcl.ui.SVG.EDIT,
i18n("button.edit"),
() -> Controllers.navigate(new ProfilePage(profile)),
popup));
popup.show(owner, JFXPopup.PopupVPosition.BOTTOM, JFXPopup.PopupHPosition.LEFT, 0, 0);
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.

medium

建议使用 FXUtils.determineOptimalPopupPosition 来动态计算弹出位置,以防止菜单在某些情况下(如靠近屏幕底部时)超出屏幕边界。

        JFXPopup.PopupVPosition vPosition = org.jackhuang.hmcl.ui.FXUtils.determineOptimalPopupPosition(owner, popup);
        popup.show(owner, vPosition, JFXPopup.PopupHPosition.LEFT, 0, 0);

}

public ProfileListPopupMenu() {
getStyleClass().add("popup-menu-content");
}
}
22 changes: 16 additions & 6 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfilePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class ProfilePage extends BorderPane implements DecoratorPage {
private final JFXTextField txtProfileName;
private final LineFileChooserButton gameDir;
private final LineToggleButton toggleUseRelativePath;
private ChangeListener<String> locationChangeListener;
private ChangeListener<String> txtProfileNameChangeListener;

/**
* @param profile null if creating a new profile.
Expand Down Expand Up @@ -90,7 +92,7 @@ public ProfilePage(Profile profile) {
txtProfileName.getValidators().add(validator);
BorderPane.setMargin(txtProfileName, new Insets(8, 0, 8, 0));

txtProfileName.setText(profileDisplayName);
txtProfileName.setText(profile == null ? "" : profile.getName());
txtProfileName.getValidators().add(new ValidatorBase() {
{
setMessage(i18n("profile.already_exists"));
Expand All @@ -99,7 +101,9 @@ public ProfilePage(Profile profile) {
@Override
protected void eval() {
JFXTextField control = (JFXTextField) this.getSrcControl();
hasErrors.set(Profiles.getProfiles().stream().anyMatch(profile -> profile.getName().equals(control.getText())));
hasErrors.set(Profiles.getProfiles().stream()
.filter(p -> p != profile)
.anyMatch(p -> p.getName().equals(control.getText())));
}
});
}
Expand Down Expand Up @@ -144,7 +148,7 @@ protected void eval() {
txtProfileName.textProperty(), location));
}

ChangeListener<String> locationChangeListener = (observable, oldValue, newValue) -> {
locationChangeListener = (observable, oldValue, newValue) -> {
Comment thread
NWMA-FYWF marked this conversation as resolved.
Path newPath;
try {
newPath = FileUtils.toAbsolute(Path.of(newValue));
Expand All @@ -164,20 +168,26 @@ protected void eval() {
txtProfileName.setText(suggestedName);
}
};
locationProperty().addListener(locationChangeListener);
if (profile == null) {
locationProperty().addListener(locationChangeListener);
}

txtProfileName.textProperty().addListener(new ChangeListener<>() {
txtProfileNameChangeListener = new ChangeListener<>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (txtProfileName.isFocused()) {
txtProfileName.textProperty().removeListener(this);
locationProperty().removeListener(locationChangeListener);
}
}
});
};
txtProfileName.textProperty().addListener(txtProfileNameChangeListener);
}

private void onSave() {
locationProperty().removeListener(locationChangeListener);
txtProfileName.textProperty().removeListener(txtProfileNameChangeListener);

if (profile != null) {
profile.setName(txtProfileName.getText());
Comment thread
NWMA-FYWF marked this conversation as resolved.
profile.setUseRelativePath(toggleUseRelativePath.isSelected());
Expand Down