Refactor AccountList and ProfileList

This commit is contained in:
huangyuhui
2018-09-05 23:20:20 +08:00
parent 94a71dc88f
commit 19725012fb
3 changed files with 30 additions and 8 deletions

View File

@@ -24,6 +24,8 @@ import javafx.scene.layout.Region;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.jackhuang.hmcl.Launcher; import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.Metadata; 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.Task;
import org.jackhuang.hmcl.task.TaskExecutor; import org.jackhuang.hmcl.task.TaskExecutor;
import org.jackhuang.hmcl.ui.account.AccountList; import org.jackhuang.hmcl.ui.account.AccountList;
@@ -81,15 +83,21 @@ public final class Controllers {
// FXThread // FXThread
public static AccountList getAccountListPage() { public static AccountList getAccountListPage() {
if (accountListPage == null) if (accountListPage == null) {
accountListPage = new AccountList(); accountListPage = new AccountList();
accountListPage.selectedAccountProperty().bindBidirectional(Accounts.selectedAccountProperty());
accountListPage.accountsProperty().bindContent(Accounts.accountsProperty());
}
return accountListPage; return accountListPage;
} }
// FXThread // FXThread
public static ProfileList getProfileListPage() { public static ProfileList getProfileListPage() {
if (profileListPage == null) if (profileListPage == null) {
profileListPage = new ProfileList(); profileListPage = new ProfileList();
profileListPage.selectedProfileProperty().bindBidirectional(Profiles.selectedProfileProperty());
profileListPage.profilesProperty().bindContent(Profiles.profilesProperty());
}
return profileListPage; return profileListPage;
} }

View File

@@ -24,7 +24,6 @@ import javafx.scene.control.Control;
import javafx.scene.control.Skin; import javafx.scene.control.Skin;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import org.jackhuang.hmcl.auth.Account; import org.jackhuang.hmcl.auth.Account;
import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.MappedObservableList; 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)); items.forEach(item -> item.selectedProperty().set(item.getAccount() == selected));
} }
}; };
private final ListProperty<Account> accounts = new SimpleListProperty<>(FXCollections.observableArrayList());
private ToggleGroup toggleGroup; private ToggleGroup toggleGroup;
private final ObservableList<AccountListItem> accountItems; private final ObservableList<AccountListItem> accountItems;
@@ -54,18 +54,25 @@ public class AccountList extends Control implements DecoratorPage {
toggleGroup = new ToggleGroup(); toggleGroup = new ToggleGroup();
accountItems = MappedObservableList.create( accountItems = MappedObservableList.create(
Accounts.accountsProperty(), accountsProperty(),
account -> new AccountListItem(toggleGroup, account)); account -> new AccountListItem(toggleGroup, account));
items.bindContent(accountItems); items.bindContent(accountItems);
selectedAccount.bindBidirectional(Accounts.selectedAccountProperty());
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> { toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
if (toggle == null || toggle.getUserData() == null) return; if (toggle == null || toggle.getUserData() == null) return;
selectedAccount.set(((AccountListItem) toggle.getUserData()).getAccount()); selectedAccount.set(((AccountListItem) toggle.getUserData()).getAccount());
}); });
} }
public ObjectProperty<Account> selectedAccountProperty() {
return selectedAccount;
}
public ListProperty<Account> accountsProperty() {
return accounts;
}
@Override @Override
protected Skin<?> createDefaultSkin() { protected Skin<?> createDefaultSkin() {
return new AccountListSkin(this); return new AccountListSkin(this);

View File

@@ -24,7 +24,6 @@ import javafx.scene.control.Control;
import javafx.scene.control.Skin; import javafx.scene.control.Skin;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage; import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.MappedObservableList; 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)); items.forEach(item -> item.selectedProperty().set(item.getProfile() == selected));
} }
}; };
private final ListProperty<Profile> profiles = new SimpleListProperty<>(FXCollections.observableArrayList());
private ToggleGroup toggleGroup; private ToggleGroup toggleGroup;
private final ObservableList<ProfileListItem> profileItems; private final ObservableList<ProfileListItem> profileItems;
@@ -54,18 +54,25 @@ public class ProfileList extends Control implements DecoratorPage {
toggleGroup = new ToggleGroup(); toggleGroup = new ToggleGroup();
profileItems = MappedObservableList.create( profileItems = MappedObservableList.create(
Profiles.profilesProperty(), profilesProperty(),
profile -> new ProfileListItem(toggleGroup, profile)); profile -> new ProfileListItem(toggleGroup, profile));
items.bindContent(profileItems); items.bindContent(profileItems);
selectedProfile.bindBidirectional(Profiles.selectedProfileProperty());
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> { toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
if (toggle == null || toggle.getUserData() == null) return; if (toggle == null || toggle.getUserData() == null) return;
selectedProfile.set(((ProfileListItem) toggle.getUserData()).getProfile()); selectedProfile.set(((ProfileListItem) toggle.getUserData()).getProfile());
}); });
} }
public ObjectProperty<Profile> selectedProfileProperty() {
return selectedProfile;
}
public ListProperty<Profile> profilesProperty() {
return profiles;
}
@Override @Override
protected Skin<?> createDefaultSkin() { protected Skin<?> createDefaultSkin() {
return new ProfileListSkin(this); return new ProfileListSkin(this);