Refactor ProfileList & AccountList

This commit is contained in:
huangyuhui
2018-09-07 01:03:59 +08:00
parent 60143d5299
commit c3cd0dd8a0
5 changed files with 56 additions and 115 deletions

View File

@@ -0,0 +1,40 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2017 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.ui;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
public abstract class ListPage<T extends Node> extends Control {
private final ListProperty<T> items = new SimpleListProperty<>(FXCollections.observableArrayList());
public abstract void add();
public ListProperty<T> itemsProperty() {
return items;
}
@Override
protected Skin<?> createDefaultSkin() {
return new ListPageSkin(this);
}
}

View File

@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.ui.account;
package org.jackhuang.hmcl.ui;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXScrollPane;
@@ -27,12 +27,10 @@ import javafx.scene.control.SkinBase;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
public class AccountListSkin extends SkinBase<AccountList> {
public class ListPageSkin extends SkinBase<ListPage> {
public AccountListSkin(AccountList skinnable) {
public ListPageSkin(ListPage<?> skinnable) {
super(skinnable);
StackPane root = new StackPane();
@@ -65,7 +63,7 @@ public class AccountListSkin extends SkinBase<AccountList> {
btnAdd.getStyleClass().setAll("jfx-button-raised-round");
btnAdd.setButtonType(JFXButton.ButtonType.RAISED);
btnAdd.setGraphic(SVG.plus(Theme.whiteFillBinding(), -1, -1));
btnAdd.setOnMouseClicked(e -> skinnable.addNewAccount());
btnAdd.setOnMouseClicked(e -> skinnable.add());
vBox.getChildren().setAll(btnAdd);
}

View File

@@ -20,29 +20,27 @@ package org.jackhuang.hmcl.ui.account;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.ui.Controllers;
import org.jackhuang.hmcl.ui.ListPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.MappedObservableList;
import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public class AccountList extends Control implements DecoratorPage {
public class AccountList extends ListPage<AccountListItem> implements DecoratorPage {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("account.manage"));
private final ListProperty<AccountListItem> items = new SimpleListProperty<>(FXCollections.observableArrayList());
private ObjectProperty<Account> selectedAccount = new SimpleObjectProperty<Account>() {
{
items.addListener(onInvalidating(this::invalidated));
itemsProperty().addListener(onInvalidating(this::invalidated));
}
@Override
protected void invalidated() {
Account selected = get();
items.forEach(item -> item.selectedProperty().set(item.getAccount() == selected));
itemsProperty().forEach(item -> item.selectedProperty().set(item.getAccount() == selected));
}
};
private final ListProperty<Account> accounts = new SimpleListProperty<>(FXCollections.observableArrayList());
@@ -57,7 +55,7 @@ public class AccountList extends Control implements DecoratorPage {
accountsProperty(),
account -> new AccountListItem(toggleGroup, account));
items.bindContent(accountItems);
itemsProperty().bindContent(accountItems);
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
if (toggle == null || toggle.getUserData() == null) return;
@@ -74,18 +72,10 @@ public class AccountList extends Control implements DecoratorPage {
}
@Override
protected Skin<?> createDefaultSkin() {
return new AccountListSkin(this);
}
public void addNewAccount() {
public void add() {
Controllers.dialog(new AddAccountPane());
}
public ListProperty<AccountListItem> itemsProperty() {
return items;
}
@Override
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();

View File

@@ -20,29 +20,27 @@ package org.jackhuang.hmcl.ui.profile;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.ui.Controllers;
import org.jackhuang.hmcl.ui.ListPage;
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
import org.jackhuang.hmcl.util.MappedObservableList;
import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public class ProfileList extends Control implements DecoratorPage {
public class ProfileList extends ListPage<ProfileListItem> implements DecoratorPage {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper(i18n("profile.manage"));
private final ListProperty<ProfileListItem> items = new SimpleListProperty<>(FXCollections.observableArrayList());
private ObjectProperty<Profile> selectedProfile = new SimpleObjectProperty<Profile>() {
{
items.addListener(onInvalidating(this::invalidated));
itemsProperty().addListener(onInvalidating(this::invalidated));
}
@Override
protected void invalidated() {
Profile selected = get();
items.forEach(item -> item.selectedProperty().set(item.getProfile() == selected));
itemsProperty().forEach(item -> item.selectedProperty().set(item.getProfile() == selected));
}
};
private final ListProperty<Profile> profiles = new SimpleListProperty<>(FXCollections.observableArrayList());
@@ -57,7 +55,7 @@ public class ProfileList extends Control implements DecoratorPage {
profilesProperty(),
profile -> new ProfileListItem(toggleGroup, profile));
items.bindContent(profileItems);
itemsProperty().bindContent(profileItems);
toggleGroup.selectedToggleProperty().addListener((o, a, toggle) -> {
if (toggle == null || toggle.getUserData() == null) return;
@@ -74,18 +72,10 @@ public class ProfileList extends Control implements DecoratorPage {
}
@Override
protected Skin<?> createDefaultSkin() {
return new ProfileListSkin(this);
}
public void addNewProfile() {
public void add() {
Controllers.navigate(new ProfilePage(null));
}
public ListProperty<ProfileListItem> itemsProperty() {
return items;
}
@Override
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();

View File

@@ -1,77 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2017 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.ui.profile;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXScrollPane;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
public class ProfileListSkin extends SkinBase<ProfileList> {
public ProfileListSkin(ProfileList skinnable) {
super(skinnable);
StackPane root = new StackPane();
ScrollPane scrollPane = new ScrollPane();
{
scrollPane.setFitToWidth(true);
VBox accountList = new VBox();
accountList.maxWidthProperty().bind(scrollPane.widthProperty());
accountList.setSpacing(10);
accountList.setStyle("-fx-padding: 10 10 10 10;");
Bindings.bindContent(accountList.getChildren(), skinnable.itemsProperty());
scrollPane.setContent(accountList);
JFXScrollPane.smoothScrolling(scrollPane);
}
VBox vBox = new VBox();
{
vBox.setAlignment(Pos.BOTTOM_RIGHT);
vBox.setPickOnBounds(false);
vBox.setPadding(new Insets(15));
vBox.setSpacing(15);
JFXButton btnAdd = new JFXButton();
FXUtils.setLimitWidth(btnAdd, 40);
FXUtils.setLimitHeight(btnAdd, 40);
btnAdd.getStyleClass().setAll("jfx-button-raised-round");
btnAdd.setButtonType(JFXButton.ButtonType.RAISED);
btnAdd.setGraphic(SVG.plus(Theme.whiteFillBinding(), -1, -1));
btnAdd.setOnMouseClicked(e -> skinnable.addNewProfile());
vBox.getChildren().setAll(btnAdd);
}
root.getChildren().setAll(scrollPane, vBox);
getChildren().setAll(root);
}
}