From 19725012fb1c281fd788cebd4ee496538abaebbc Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Wed, 5 Sep 2018 23:20:20 +0800 Subject: [PATCH] Refactor AccountList and ProfileList --- .../java/org/jackhuang/hmcl/ui/Controllers.java | 12 ++++++++++-- .../org/jackhuang/hmcl/ui/account/AccountList.java | 13 ++++++++++--- .../org/jackhuang/hmcl/ui/profile/ProfileList.java | 13 ++++++++++--- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java index 3156b281d..73d29c4a0 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java @@ -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; } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java index 8be5ca8f7..441fe88a8 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountList.java @@ -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 accounts = new SimpleListProperty<>(FXCollections.observableArrayList()); private ToggleGroup toggleGroup; private final ObservableList 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 selectedAccountProperty() { + return selectedAccount; + } + + public ListProperty accountsProperty() { + return accounts; + } + @Override protected Skin createDefaultSkin() { return new AccountListSkin(this); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java index 51f1d6d28..0ce8804c1 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/profile/ProfileList.java @@ -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 profiles = new SimpleListProperty<>(FXCollections.observableArrayList()); private ToggleGroup toggleGroup; private final ObservableList 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 selectedProfileProperty() { + return selectedProfile; + } + + public ListProperty profilesProperty() { + return profiles; + } + @Override protected Skin createDefaultSkin() { return new ProfileListSkin(this);