mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-16 07:16:27 -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.beans.property.*;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.scene.control.ToggleGroup;
|
|
||||||
import org.jackhuang.hmcl.setting.Profile;
|
import org.jackhuang.hmcl.setting.Profile;
|
||||||
import org.jackhuang.hmcl.ui.Controllers;
|
import org.jackhuang.hmcl.ui.Controllers;
|
||||||
import org.jackhuang.hmcl.ui.ListPage;
|
import org.jackhuang.hmcl.ui.ListPage;
|
||||||
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
||||||
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
|
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.i18n.I18n.i18n;
|
||||||
|
import static org.jackhuang.hmcl.util.javafx.SelectedItemProperties.createSelectedItemPropertyFor;
|
||||||
|
|
||||||
public class ProfileList extends ListPage<ProfileListItem> implements DecoratorPage {
|
public class ProfileList extends ListPage<ProfileListItem> implements DecoratorPage {
|
||||||
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage"));
|
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 final ListProperty<Profile> profiles = new SimpleListProperty<>(FXCollections.observableArrayList());
|
||||||
|
private ObjectProperty<Profile> selectedProfile;
|
||||||
private ToggleGroup toggleGroup;
|
|
||||||
private final ObservableList<ProfileListItem> profileItems;
|
|
||||||
|
|
||||||
public ProfileList() {
|
public ProfileList() {
|
||||||
toggleGroup = new ToggleGroup();
|
setItems(MappedObservableList.create(profilesProperty(), ProfileListItem::new));
|
||||||
|
selectedProfile = createSelectedItemPropertyFor(getItems(), Profile.class);
|
||||||
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());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectProperty<Profile> selectedProfileProperty() {
|
public ObjectProperty<Profile> selectedProfileProperty() {
|
||||||
|
@ -17,30 +17,26 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.ui.profile;
|
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.SimpleStringProperty;
|
||||||
import javafx.beans.property.StringProperty;
|
import javafx.beans.property.StringProperty;
|
||||||
import javafx.scene.control.Control;
|
|
||||||
import javafx.scene.control.Skin;
|
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.Profile;
|
||||||
import org.jackhuang.hmcl.setting.Profiles;
|
import org.jackhuang.hmcl.setting.Profiles;
|
||||||
|
|
||||||
public class ProfileListItem extends Control {
|
public class ProfileListItem extends ToggleButton {
|
||||||
private final Profile profile;
|
private final Profile profile;
|
||||||
private final ToggleGroup toggleGroup;
|
|
||||||
private final StringProperty title = new SimpleStringProperty();
|
private final StringProperty title = new SimpleStringProperty();
|
||||||
private final StringProperty subtitle = 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.profile = profile;
|
||||||
this.toggleGroup = toggleGroup;
|
getStyleClass().clear();
|
||||||
|
setUserData(profile);
|
||||||
|
|
||||||
title.set(Profiles.getProfileDisplayName(profile));
|
title.set(Profiles.getProfileDisplayName(profile));
|
||||||
subtitle.set(profile.getGameDir().toString());
|
subtitle.set(profile.getGameDir().toString());
|
||||||
selected.set(Profiles.selectedProfileProperty().get() == profile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -48,27 +44,35 @@ public class ProfileListItem extends Control {
|
|||||||
return new ProfileListItemSkin(this);
|
return new ProfileListItemSkin(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ToggleGroup getToggleGroup() {
|
public void remove() {
|
||||||
return toggleGroup;
|
Profiles.getProfiles().remove(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Profile getProfile() {
|
public Profile getProfile() {
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title.set(title);
|
||||||
|
}
|
||||||
|
|
||||||
public StringProperty titleProperty() {
|
public StringProperty titleProperty() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSubtitle() {
|
||||||
|
return subtitle.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubtitle(String subtitle) {
|
||||||
|
this.subtitle.set(subtitle);
|
||||||
|
}
|
||||||
|
|
||||||
public StringProperty subtitleProperty() {
|
public StringProperty subtitleProperty() {
|
||||||
return subtitle;
|
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();
|
JFXRadioButton chkSelected = new JFXRadioButton();
|
||||||
BorderPane.setAlignment(chkSelected, Pos.CENTER);
|
BorderPane.setAlignment(chkSelected, Pos.CENTER);
|
||||||
chkSelected.setUserData(skinnable);
|
|
||||||
chkSelected.selectedProperty().bindBidirectional(skinnable.selectedProperty());
|
chkSelected.selectedProperty().bindBidirectional(skinnable.selectedProperty());
|
||||||
chkSelected.setToggleGroup(skinnable.getToggleGroup());
|
|
||||||
root.setLeft(chkSelected);
|
root.setLeft(chkSelected);
|
||||||
|
|
||||||
HBox center = new HBox();
|
HBox center = new HBox();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user