mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-15 14:56:05 -04:00
Refactor ProfileList
This commit is contained in:
parent
7da5b8fbc8
commit
c03a0feb75
@ -19,48 +19,23 @@ package org.jackhuang.hmcl.ui.profile;
|
||||
|
||||
import javafx.beans.property.*;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import org.jackhuang.hmcl.setting.Profile;
|
||||
import org.jackhuang.hmcl.ui.Controllers;
|
||||
import org.jackhuang.hmcl.ui.ListPage;
|
||||
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
||||
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
|
||||
|
||||
import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
|
||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||
import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.createSelectedItemPropertyFor;
|
||||
|
||||
public class ProfileList extends ListPage<ProfileListItem> implements DecoratorPage {
|
||||
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage"));
|
||||
private ObjectProperty<Profile> selectedProfile = new SimpleObjectProperty<Profile>() {
|
||||
{
|
||||
itemsProperty().addListener(onInvalidating(this::invalidated));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidated() {
|
||||
Profile selected = get();
|
||||
itemsProperty().forEach(item -> item.selectedProperty().set(item.getProfile() == selected));
|
||||
}
|
||||
};
|
||||
private final ListProperty<Profile> profiles = new SimpleListProperty<>(FXCollections.observableArrayList());
|
||||
|
||||
private ToggleGroup toggleGroup;
|
||||
private final ObservableList<ProfileListItem> profileItems;
|
||||
private ObjectProperty<Profile> selectedProfile;
|
||||
|
||||
public ProfileList() {
|
||||
toggleGroup = new ToggleGroup();
|
||||
|
||||
profileItems = MappedObservableList.create(
|
||||
profilesProperty(),
|
||||
profile -> new ProfileListItem(toggleGroup, profile));
|
||||
|
||||
itemsProperty().bindContent(profileItems);
|
||||
|
||||
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
|
||||
if (toggle == null || toggle.getUserData() == null) return;
|
||||
selectedProfile.set(((ProfileListItem) toggle.getUserData()).getProfile());
|
||||
});
|
||||
setItems(MappedObservableList.create(profilesProperty(), ProfileListItem::new));
|
||||
selectedProfile = createSelectedItemPropertyFor(getItems(), Profile.class);
|
||||
}
|
||||
|
||||
public ObjectProperty<Profile> selectedProfileProperty() {
|
||||
|
@ -17,30 +17,26 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.ui.profile;
|
||||
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
import javafx.scene.control.Control;
|
||||
import javafx.scene.control.Skin;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import javafx.scene.control.ToggleButton;
|
||||
|
||||
import org.jackhuang.hmcl.setting.Profile;
|
||||
import org.jackhuang.hmcl.setting.Profiles;
|
||||
|
||||
public class ProfileListItem extends Control {
|
||||
public class ProfileListItem extends ToggleButton {
|
||||
private final Profile profile;
|
||||
private final ToggleGroup toggleGroup;
|
||||
private final StringProperty title = new SimpleStringProperty();
|
||||
private final StringProperty subtitle = new SimpleStringProperty();
|
||||
private final BooleanProperty selected = new SimpleBooleanProperty();
|
||||
|
||||
public ProfileListItem(ToggleGroup toggleGroup, Profile profile) {
|
||||
public ProfileListItem(Profile profile) {
|
||||
this.profile = profile;
|
||||
this.toggleGroup = toggleGroup;
|
||||
getStyleClass().clear();
|
||||
setUserData(profile);
|
||||
|
||||
title.set(Profiles.getProfileDisplayName(profile));
|
||||
subtitle.set(profile.getGameDir().toString());
|
||||
selected.set(Profiles.selectedProfileProperty().get() == profile);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -48,27 +44,35 @@ public class ProfileListItem extends Control {
|
||||
return new ProfileListItemSkin(this);
|
||||
}
|
||||
|
||||
public ToggleGroup getToggleGroup() {
|
||||
return toggleGroup;
|
||||
public void remove() {
|
||||
Profiles.getProfiles().remove(profile);
|
||||
}
|
||||
|
||||
public Profile getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title.get();
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title.set(title);
|
||||
}
|
||||
|
||||
public StringProperty titleProperty() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getSubtitle() {
|
||||
return subtitle.get();
|
||||
}
|
||||
|
||||
public void setSubtitle(String subtitle) {
|
||||
this.subtitle.set(subtitle);
|
||||
}
|
||||
|
||||
public StringProperty subtitleProperty() {
|
||||
return subtitle;
|
||||
}
|
||||
|
||||
public BooleanProperty selectedProperty() {
|
||||
return selected;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
Profiles.getProfiles().remove(profile);
|
||||
}
|
||||
}
|
||||
|
@ -41,9 +41,7 @@ public class ProfileListItemSkin extends SkinBase<ProfileListItem> {
|
||||
|
||||
JFXRadioButton chkSelected = new JFXRadioButton();
|
||||
BorderPane.setAlignment(chkSelected, Pos.CENTER);
|
||||
chkSelected.setUserData(skinnable);
|
||||
chkSelected.selectedProperty().bindBidirectional(skinnable.selectedProperty());
|
||||
chkSelected.setToggleGroup(skinnable.getToggleGroup());
|
||||
root.setLeft(chkSelected);
|
||||
|
||||
HBox center = new HBox();
|
||||
|
Loading…
x
Reference in New Issue
Block a user