From caf85d2a924e6707d7d7448a2a148d984feae42f Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Tue, 2 Oct 2018 18:06:24 +0800 Subject: [PATCH] Fix showing a gray horizontal line in component list cell --- .../hmcl/ui/construct/ComponentList.java | 14 ++--- .../hmcl/ui/construct/ComponentListCell.java | 5 +- .../construct/ListFirstElementListener.java | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ListFirstElementListener.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentList.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentList.java index 40e364d61..c0258854d 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentList.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentList.java @@ -1,7 +1,7 @@ /* * Hello Minecraft! Launcher. * Copyright (C) 2018 huangyuhui - * + * * 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 @@ -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; - } } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentListCell.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentListCell.java index 2ae9b3dc3..3f6983e8e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentListCell.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentListCell.java @@ -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(); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ListFirstElementListener.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ListFirstElementListener.java new file mode 100644 index 000000000..1c49cd8b6 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ListFirstElementListener.java @@ -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 implements ListChangeListener { + + private final Consumer first, last; + + public ListFirstElementListener(Consumer first, Consumer last) { + this.first = first; + this.last = last; + } + + @Override + public void onChanged(Change c) { + ObservableList 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 void observe(ObservableList list, Consumer first, Consumer 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)); + } +}