Refactor AccountList and ProfileList
This commit is contained in:
@@ -24,6 +24,8 @@ import javafx.scene.layout.Region;
|
||||
import javafx.stage.Stage;
|
||||
import org.jackhuang.hmcl.Launcher;
|
||||
import org.jackhuang.hmcl.Metadata;
|
||||
import org.jackhuang.hmcl.setting.Accounts;
|
||||
import org.jackhuang.hmcl.setting.Profiles;
|
||||
import org.jackhuang.hmcl.task.Task;
|
||||
import org.jackhuang.hmcl.task.TaskExecutor;
|
||||
import org.jackhuang.hmcl.ui.account.AccountList;
|
||||
@@ -81,15 +83,21 @@ public final class Controllers {
|
||||
|
||||
// FXThread
|
||||
public static AccountList getAccountListPage() {
|
||||
if (accountListPage == null)
|
||||
if (accountListPage == null) {
|
||||
accountListPage = new AccountList();
|
||||
accountListPage.selectedAccountProperty().bindBidirectional(Accounts.selectedAccountProperty());
|
||||
accountListPage.accountsProperty().bindContent(Accounts.accountsProperty());
|
||||
}
|
||||
return accountListPage;
|
||||
}
|
||||
|
||||
// FXThread
|
||||
public static ProfileList getProfileListPage() {
|
||||
if (profileListPage == null)
|
||||
if (profileListPage == null) {
|
||||
profileListPage = new ProfileList();
|
||||
profileListPage.selectedProfileProperty().bindBidirectional(Profiles.selectedProfileProperty());
|
||||
profileListPage.profilesProperty().bindContent(Profiles.profilesProperty());
|
||||
}
|
||||
return profileListPage;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import javafx.scene.control.Control;
|
||||
import javafx.scene.control.Skin;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import org.jackhuang.hmcl.auth.Account;
|
||||
import org.jackhuang.hmcl.setting.Accounts;
|
||||
import org.jackhuang.hmcl.ui.Controllers;
|
||||
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
||||
import org.jackhuang.hmcl.util.MappedObservableList;
|
||||
@@ -46,6 +45,7 @@ public class AccountList extends Control implements DecoratorPage {
|
||||
items.forEach(item -> item.selectedProperty().set(item.getAccount() == selected));
|
||||
}
|
||||
};
|
||||
private final ListProperty<Account> accounts = new SimpleListProperty<>(FXCollections.observableArrayList());
|
||||
|
||||
private ToggleGroup toggleGroup;
|
||||
private final ObservableList<AccountListItem> accountItems;
|
||||
@@ -54,18 +54,25 @@ public class AccountList extends Control implements DecoratorPage {
|
||||
toggleGroup = new ToggleGroup();
|
||||
|
||||
accountItems = MappedObservableList.create(
|
||||
Accounts.accountsProperty(),
|
||||
accountsProperty(),
|
||||
account -> new AccountListItem(toggleGroup, account));
|
||||
|
||||
items.bindContent(accountItems);
|
||||
|
||||
selectedAccount.bindBidirectional(Accounts.selectedAccountProperty());
|
||||
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
|
||||
if (toggle == null || toggle.getUserData() == null) return;
|
||||
selectedAccount.set(((AccountListItem) toggle.getUserData()).getAccount());
|
||||
});
|
||||
}
|
||||
|
||||
public ObjectProperty<Account> selectedAccountProperty() {
|
||||
return selectedAccount;
|
||||
}
|
||||
|
||||
public ListProperty<Account> accountsProperty() {
|
||||
return accounts;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Skin<?> createDefaultSkin() {
|
||||
return new AccountListSkin(this);
|
||||
|
||||
@@ -24,7 +24,6 @@ import javafx.scene.control.Control;
|
||||
import javafx.scene.control.Skin;
|
||||
import javafx.scene.control.ToggleGroup;
|
||||
import org.jackhuang.hmcl.setting.Profile;
|
||||
import org.jackhuang.hmcl.setting.Profiles;
|
||||
import org.jackhuang.hmcl.ui.Controllers;
|
||||
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
||||
import org.jackhuang.hmcl.util.MappedObservableList;
|
||||
@@ -46,6 +45,7 @@ public class ProfileList extends Control implements DecoratorPage {
|
||||
items.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;
|
||||
@@ -54,18 +54,25 @@ public class ProfileList extends Control implements DecoratorPage {
|
||||
toggleGroup = new ToggleGroup();
|
||||
|
||||
profileItems = MappedObservableList.create(
|
||||
Profiles.profilesProperty(),
|
||||
profilesProperty(),
|
||||
profile -> new ProfileListItem(toggleGroup, profile));
|
||||
|
||||
items.bindContent(profileItems);
|
||||
|
||||
selectedProfile.bindBidirectional(Profiles.selectedProfileProperty());
|
||||
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
|
||||
if (toggle == null || toggle.getUserData() == null) return;
|
||||
selectedProfile.set(((ProfileListItem) toggle.getUserData()).getProfile());
|
||||
});
|
||||
}
|
||||
|
||||
public ObjectProperty<Profile> selectedProfileProperty() {
|
||||
return selectedProfile;
|
||||
}
|
||||
|
||||
public ListProperty<Profile> profilesProperty() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Skin<?> createDefaultSkin() {
|
||||
return new ProfileListSkin(this);
|
||||
|
||||
Reference in New Issue
Block a user