Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.jackhuang.hmcl.ui.construct;

import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty;
import javafx.beans.property.StringPropertyBase;
Expand All @@ -26,10 +25,10 @@
import javafx.css.PseudoClass;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;

public class TwoLineListItem extends VBox {
private static final String DEFAULT_STYLE_CLASS = "two-line-list-item";
Expand Down Expand Up @@ -168,36 +167,28 @@ public ObservableList<Label> getTags() {
var tagsBox = new HBox(8);
tagsBox.getStyleClass().add("tags");
tagsBox.setAlignment(Pos.CENTER_LEFT);
HBox.setHgrow(tagsBox, Priority.ALWAYS);
Bindings.bindContent(tagsBox.getChildren(), tags);
var isNotEmpty = Bindings.isNotEmpty(tags);
tagsBox.managedProperty().bind(isNotEmpty);
tagsBox.visibleProperty().bind(isNotEmpty);

// Clip overflow tags, matching the previous ScrollPane behavior.
var clip = new Rectangle();
tagsBox.setClip(clip);
tagsBox.widthProperty().addListener((obs, old, val) -> clip.setWidth(val.doubleValue()));
tagsBox.heightProperty().addListener((obs, old, val) -> clip.setHeight(val.doubleValue()));

var scrollPane = new ScrollPane(tagsBox);
scrollPane.setMinSize(0, 0);
HBox.setHgrow(scrollPane, Priority.ALWAYS);
lblTitle.setMinWidth(Label.USE_PREF_SIZE);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
var expectedHeight = Bindings.createDoubleBinding(
() -> {
if (tags.isEmpty()) return 0.0;
double h = tagsBox.prefHeight(-1);
return h > 0 ? h : Double.NaN;
},
tags, tagsBox.heightProperty()
);

scrollPane.minHeightProperty().bind(expectedHeight);
scrollPane.prefHeightProperty().bind(expectedHeight);
scrollPane.maxHeightProperty().bind(expectedHeight);
firstLine.getChildren().setAll(lblTitle, scrollPane);

tags.addListener((InvalidationListener) ignored -> scrollPane.requestLayout());
firstLine.getChildren().setAll(lblTitle, tagsBox);
}
return tags;
}

public void addTag(String tag, PseudoClass pseudoClass) {
var tagLabel = new Label(tag);
tagLabel.getStyleClass().add("tag");
tagLabel.setMinWidth(Label.USE_PREF_SIZE);
if (pseudoClass != null)
tagLabel.pseudoClassStateChanged(pseudoClass, true);
getTags().add(tagLabel);
Expand Down