Refactor ComponentList

This commit is contained in:
huangyuhui 2018-09-03 23:42:42 +08:00
parent 7e08cfedd6
commit d9d20c969a
6 changed files with 46 additions and 40 deletions

View File

@ -109,9 +109,9 @@ public abstract class SettingsView extends StackPane {
content.getChildren().setAll(chkUpdateStable, chkUpdateDev, noteWrapper); content.getChildren().setAll(chkUpdateStable, chkUpdateDev, noteWrapper);
updatePane.addChildren(content); updatePane.getContent().add(content);
} }
settingsPane.addChildren(updatePane); settingsPane.getContent().add(updatePane);
} }
{ {
@ -122,7 +122,7 @@ public abstract class SettingsView extends StackPane {
fileCommonLocation.setHasSubtitle(true); fileCommonLocation.setHasSubtitle(true);
fileCommonLocation.setCustomText("settings.custom"); fileCommonLocation.setCustomText("settings.custom");
settingsPane.addChildren(fileCommonLocation); settingsPane.getContent().add(fileCommonLocation);
} }
{ {
@ -132,7 +132,7 @@ public abstract class SettingsView extends StackPane {
backgroundItem.setHasSubtitle(true); backgroundItem.setHasSubtitle(true);
backgroundItem.setCustomText(I18n.i18n("settings.custom")); backgroundItem.setCustomText(I18n.i18n("settings.custom"));
settingsPane.addChildren(backgroundItem); settingsPane.getContent().add(backgroundItem);
} }
{ {
@ -169,7 +169,7 @@ public abstract class SettingsView extends StackPane {
); );
downloadSourcePane.setRight(cboDownloadSource); downloadSourcePane.setRight(cboDownloadSource);
} }
settingsPane.addChildren(downloadSourcePane); settingsPane.getContent().add(downloadSourcePane);
} }
{ {
@ -183,7 +183,7 @@ public abstract class SettingsView extends StackPane {
FXUtils.setLimitWidth(cboLanguage, 400); FXUtils.setLimitWidth(cboLanguage, 400);
languagePane.setRight(cboLanguage); languagePane.setRight(cboLanguage);
settingsPane.addChildren(languagePane); settingsPane.getContent().add(languagePane);
} }
{ {
@ -309,8 +309,8 @@ public abstract class SettingsView extends StackPane {
} }
proxyWrapper.getChildren().add(proxyPane); proxyWrapper.getChildren().add(proxyPane);
} }
proxyList.addChildren(proxyWrapper); proxyList.getContent().add(proxyWrapper);
settingsPane.addChildren(proxyList); settingsPane.getContent().add(proxyList);
} }
{ {
@ -324,13 +324,13 @@ public abstract class SettingsView extends StackPane {
themeColorPickerContainer.setMinHeight(30); themeColorPickerContainer.setMinHeight(30);
themePane.setRight(themeColorPickerContainer); themePane.setRight(themeColorPickerContainer);
settingsPane.addChildren(themePane); settingsPane.getContent().add(themePane);
} }
{ {
VBox fontPane = new VBox(); VBox fontPane = new VBox();
fontPane.setSpacing(5); fontPane.setSpacing(5);
settingsPane.addChildren(fontPane); settingsPane.getContent().add(fontPane);
{ {
BorderPane borderPane = new BorderPane(); BorderPane borderPane = new BorderPane();
@ -462,7 +462,7 @@ public abstract class SettingsView extends StackPane {
gridPane.getChildren().add(label); gridPane.getChildren().add(label);
} }
aboutPane.getChildren().setAll(gridPane); aboutPane.getChildren().setAll(gridPane);
settingsPane.addChildren(aboutPane); settingsPane.getContent().add(aboutPane);
} }
rootPane.getChildren().add(settingsPane); rootPane.getChildren().add(settingsPane);
} }

View File

@ -18,6 +18,7 @@
package org.jackhuang.hmcl.ui.construct; package org.jackhuang.hmcl.ui.construct;
import javafx.beans.DefaultProperty; import javafx.beans.DefaultProperty;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
@ -26,12 +27,15 @@ import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import org.jackhuang.hmcl.util.MappedObservableList;
@DefaultProperty("content") @DefaultProperty("content")
public class ComponentList extends StackPane { public class ComponentList extends Control {
private final VBox vbox = new VBox();
private final StringProperty title = new SimpleStringProperty(this, "title", "Group"); private final StringProperty title = new SimpleStringProperty(this, "title", "Group");
private final StringProperty subtitle = new SimpleStringProperty(this, "subtitle", ""); private final StringProperty subtitle = new SimpleStringProperty(this, "subtitle", "");
private final IntegerProperty depth = new SimpleIntegerProperty(this, "depth", 0); private final IntegerProperty depth = new SimpleIntegerProperty(this, "depth", 0);
@ -39,31 +43,9 @@ public class ComponentList extends StackPane {
public final ObservableList<Node> content = FXCollections.observableArrayList(); public final ObservableList<Node> content = FXCollections.observableArrayList();
public ComponentList() { public ComponentList() {
getChildren().setAll(vbox);
content.addListener((ListChangeListener<? super Node>) change -> {
while (change.next()) {
for (int i = change.getFrom(); i < change.getTo(); ++i)
addChildren(change.getList().get(i));
}
});
getStyleClass().add("options-list"); getStyleClass().add("options-list");
} }
public void addChildren(Node node) {
StackPane child = new StackPane();
child.getChildren().add(new ComponentListCell(node));
if (vbox.getChildren().isEmpty())
child.getStyleClass().add("options-list-item-ahead");
else
child.getStyleClass().add("options-list-item");
child.getProperties().put("node", node);
vbox.getChildren().add(child);
}
public void removeChild(Node node) {
vbox.getChildren().removeIf(node1 -> node1.getProperties().get("node") == node);
}
public String getTitle() { public String getTitle() {
return title.get(); return title.get();
} }
@ -111,4 +93,28 @@ public class ComponentList extends StackPane {
public ObservableList<Node> getContent() { public ObservableList<Node> getContent() {
return content; return content;
} }
@Override
protected javafx.scene.control.Skin<?> createDefaultSkin() {
return new Skin(this);
}
protected static class Skin extends SkinBase<ComponentList> {
protected Skin(ComponentList control) {
super(control);
VBox vbox = new VBox();
Bindings.bindContent(vbox.getChildren(),
MappedObservableList.create(control.getContent(), this::mapper));
getChildren().setAll(vbox);
}
private Node mapper(Node node) {
StackPane child = new StackPane();
child.getChildren().add(new ComponentListCell(node));
child.getStyleClass().add("options-list-item");
return child;
}
}
} }

View File

@ -40,13 +40,13 @@ import org.jackhuang.hmcl.ui.SVG;
/** /**
* @author huangyuhui * @author huangyuhui
*/ */
public class ComponentListCell extends StackPane { class ComponentListCell extends StackPane {
private final Node content; private final Node content;
private Animation expandAnimation; private Animation expandAnimation;
private Rectangle clipRect; private Rectangle clipRect;
private final BooleanProperty expanded = new SimpleBooleanProperty(this, "expanded", false); private final BooleanProperty expanded = new SimpleBooleanProperty(this, "expanded", false);
public ComponentListCell(Node content) { ComponentListCell(Node content) {
this.content = content; this.content = content;
updateLayout(); updateLayout();

View File

@ -57,7 +57,7 @@ public class MultiColorItem extends ComponentList {
if (hasCustom) if (hasCustom)
pane.getChildren().add(custom); pane.getChildren().add(custom);
addChildren(pane); getContent().add(pane);
group.selectedToggleProperty().addListener((a, b, newValue) -> { group.selectedToggleProperty().addListener((a, b, newValue) -> {
if (toggleSelectedListener != null) if (toggleSelectedListener != null)

View File

@ -106,7 +106,7 @@ public class MultiFileItem<T> extends ComponentList {
if (hasCustom) if (hasCustom)
pane.getChildren().add(custom); pane.getChildren().add(custom);
addChildren(pane); getContent().add(pane);
group.selectedToggleProperty().addListener((a, b, newValue) -> { group.selectedToggleProperty().addListener((a, b, newValue) -> {
if (toggleSelectedListener != null) if (toggleSelectedListener != null)

View File

@ -152,7 +152,7 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag
this.versionId = versionId; this.versionId = versionId;
if (versionId == null) { if (versionId == null) {
componentList.removeChild(iconPickerItem); componentList.getContent().remove(iconPickerItem);
rootPane.getChildren().remove(settingsTypePane); rootPane.getChildren().remove(settingsTypePane);
chkEnableSpecificSettings.setSelected(true); chkEnableSpecificSettings.setSelected(true);
} }