Fix showing a gray horizontal line in component list cell

This commit is contained in:
huanghongxun 2018-10-02 18:06:24 +08:00
parent 030b577dee
commit caf85d2a92
3 changed files with 65 additions and 13 deletions

View File

@ -106,18 +106,14 @@ public class ComponentList extends Control {
protected Skin(ComponentList control) {
super(control);
list = MappedObservableList.create(control.getContent(), this::mapper);
list = MappedObservableList.create(control.getContent(), ComponentListCell::new);
ListFirstElementListener.observe(list,
first -> first.getStyleClass().setAll("options-list-item-ahead"),
last -> last.getStyleClass().setAll("options-list-item"));
VBox vbox = new VBox();
Bindings.bindContent(vbox.getChildren(), list);
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

@ -122,10 +122,7 @@ class ComponentListCell extends StackPane {
VBox container = new VBox();
container.setStyle("-fx-padding: 8 0 0 0;");
FXUtils.setLimitHeight(container, 0);
Rectangle clipRect = new Rectangle();
clipRect.widthProperty().bind(container.widthProperty());
clipRect.heightProperty().bind(container.heightProperty());
container.setClip(clipRect);
FXUtils.setOverflowHidden(container, true);
container.getChildren().setAll(content);
VBox holder = new VBox();

View File

@ -0,0 +1,59 @@
package org.jackhuang.hmcl.ui.construct;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import java.util.function.Consumer;
public class ListFirstElementListener<T> implements ListChangeListener<T> {
private final Consumer<? super T> first, last;
public ListFirstElementListener(Consumer<? super T> first, Consumer<? super T> last) {
this.first = first;
this.last = last;
}
@Override
public void onChanged(Change<? extends T> c) {
ObservableList<? extends T> list = c.getList();
if (list.isEmpty()) return;
while (c.next()) {
int from = c.getFrom();
int to = c.getTo();
if (c.wasPermutated()) {
for (int i = from; i < to; ++i) {
int newIdx = c.getPermutation(i);
if (i == 0 && newIdx != 0)
last.accept(list.get(newIdx));
else if (i != 0 && newIdx == 0)
first.accept(list.get(newIdx));
}
} else {
if (c.wasRemoved()) {
if (!list.isEmpty() && from == 0)
first.accept(list.get(0));
}
if (c.wasAdded()) {
for (int i = from; i < to; ++i) {
if (i == 0)
first.accept(list.get(0));
else
last.accept(list.get(i));
}
if (list.size() > to)
last.accept(list.get(to));
}
}
}
}
public static <T> void observe(ObservableList<? extends T> list, Consumer<? super T> first, Consumer<? super T> last) {
for (int i = 0; i < list.size(); ++i) {
if (i == 0) first.accept(list.get(i));
else last.accept(list.get(i));
}
list.addListener(new ListFirstElementListener<>(first, last));
}
}