Skip to content
Draft
Show file tree
Hide file tree
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 @@ -10,7 +10,6 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
Expand All @@ -21,7 +20,7 @@

public class IconedTwoLineListItem extends HBox {
private final StringProperty title = new SimpleStringProperty(this, "title");
private final ObservableList<Label> tags = FXCollections.observableArrayList();
private final ObservableList<TagsBar.Tag> tags = FXCollections.observableArrayList();
private final StringProperty subtitle = new SimpleStringProperty(this, "subtitle");
private final StringProperty externalLink = new SimpleStringProperty(this, "externalLink");
private final ObjectProperty<Image> image = new SimpleObjectProperty<>(this, "image");
Expand Down Expand Up @@ -62,7 +61,7 @@ public void setTitle(String title) {
this.title.set(title);
}

public ObservableList<Label> getTags() {
public ObservableList<TagsBar.Tag> getTags() {
return tags;
}

Expand Down
264 changes: 264 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TagsBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2020 huangyuhui <[email protected]> and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.ui.construct;

import javafx.beans.InvalidationListener;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

import java.util.ArrayList;
import java.util.List;

/**
* A horizontal bar that displays multiple text tags.
* When there is not enough space, it collapses the rightmost tags into a "+N" indicator
* with a tooltip showing the collapsed tag contents.
*/
public class TagsBar extends Pane {
private static final String DEFAULT_STYLE_CLASS = "tags-bar";
private static final double TAG_SPACING = 8.0;

private final ObservableList<Tag> tags = FXCollections.observableArrayList();
private final List<Label> tagLabels = new ArrayList<>();
private final Text collapsedIndicator = new Text();
private final Text measureText = new Text(); // For measuring width without affecting display
private final Tooltip collapsedTooltip = new Tooltip();

private int visibleTagCount = 0;

public TagsBar() {
getStyleClass().add(DEFAULT_STYLE_CLASS);

collapsedIndicator.getStyleClass().add("collapsed-indicator");
collapsedIndicator.setManaged(false);
collapsedIndicator.setVisible(false);
getChildren().add(collapsedIndicator);

// Copy style from collapsedIndicator for accurate measurement
measureText.getStyleClass().add("collapsed-indicator");

tags.addListener((ListChangeListener<Tag>) c -> {
rebuildTagLabels();
requestLayout();
});

tags.addListener((InvalidationListener) obs -> requestLayout());
}

private void rebuildTagLabels() {
// Remove old labels
getChildren().removeAll(tagLabels);
tagLabels.clear();

// Create new labels
for (Tag tag : tags) {
Label label = new Label();
label.setText(tag.text());
label.setManaged(false);
label.getStyleClass().add(tag.warning() ? "tag-warning" : "tag");
tagLabels.add(label);
}

// Add new labels
getChildren().addAll(tagLabels);

// Ensure collapsed indicator is on top
collapsedIndicator.toFront();
Copy link
Contributor

@Mine-diamond Mine-diamond Jan 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
collapsedIndicator.toFront();
collapsedIndicator.toFront();
this.applyCss();

我找到了一个解决办法,之所以会出现标签没办法渲染,主要原因是部分情况下重新布局时,标签Label没有正确应用css,这导致Label的尺寸为0,可以强制applyCss来解决

可能的原因如下:

content.getTags().clear();
dataItem.getCategories().stream()
.map(category -> getSkinnable().getLocalizedCategory(category))
.forEach(content::addTag);

在这里,由于选中组件导致updateControl被执行,因为导致标签被重新添加,会触发这个逻辑,导致tagLabel重建

    private void rebuildTagLabels() {
        // Remove old labels
        getChildren().removeAll(tagLabels);
        tagLabels.clear();

        // Create new labels
        for (Tag tag : tags) {
            Label label = new Label();
            label.setText(tag.text());
            label.setManaged(false);
            label.getStyleClass().add(tag.warning() ? "tag-warning" : "tag");
            tagLabels.add(label);
        }
        ...

但问题在于,选中本身也导致更新css,这导致选中列表时会同时更新css和更新tagLabel以及重新布局,这可能导致一些bug,导致在layoutChildren时新的tagLabels根本没有应用css。
我不能确定一定是因此产生的bug,但是我可以确认的是,重新布局时,新的Label确实没有正确应用css,这导致新的Label的尺寸为0

}

public ObservableList<Tag> getTags() {
return tags;
}

public void addTag(String text) {
tags.add(new Tag(text, false));
}

public void addTagWarning(String text) {
tags.add(new Tag(text, true));
}

public Tooltip getCollapsedTooltip() {
return collapsedTooltip;
}

public Text getCollapsedIndicator() {
return collapsedIndicator;
}

@Override
protected double computeMinWidth(double height) {
// Minimum width is the width of the collapsed indicator "+N" if there are tags
if (tags.isEmpty()) {
return 0;
}
// Use measureText to avoid affecting collapsedIndicator's display
measureText.setText("+" + tags.size());
return snapSizeX(measureText.prefWidth(-1));
}

@Override
protected double computePrefWidth(double height) {
if (tags.isEmpty()) {
return 0;
}
double width = 0;
for (Label label : tagLabels) {
width += label.prefWidth(-1);
}
// Add spacing between tags
width += TAG_SPACING * (tagLabels.size() - 1);
return snapSizeX(width);
}

@Override
protected double computeMinHeight(double width) {
return computePrefHeight(width);
}

@Override
protected double computePrefHeight(double width) {
double maxHeight = 0;
for (Label label : tagLabels) {
maxHeight = Math.max(maxHeight, label.prefHeight(-1));
}
if (maxHeight == 0) {
measureText.setText("+1");
maxHeight = measureText.prefHeight(-1);
}
return snapSizeY(maxHeight);
}

@Override
protected void layoutChildren() {
if (tagLabels.isEmpty()) {
collapsedIndicator.setVisible(false);
return;
}

double availableWidth = getWidth();
double height = getHeight();

// Calculate widths of all tags
double[] tagWidths = new double[tagLabels.size()];
for (int i = 0; i < tagLabels.size(); i++) {
tagWidths[i] = tagLabels.get(i).prefWidth(-1);
}

// Pre-calculate collapsed indicator widths for all possible hidden counts using measureText
double[] collapsedWidths = new double[tagLabels.size() + 1];
for (int n = 1; n <= tagLabels.size(); n++) {
measureText.setText("+" + n);
collapsedWidths[n] = measureText.prefWidth(-1);
}

// Calculate how many tags can fit
visibleTagCount = 0;
double currentWidth = 0;

// Try to fit as many tags as possible
for (int i = 0; i < tagLabels.size(); i++) {
double tagWidth = tagWidths[i];
double spacing = (visibleTagCount > 0) ? TAG_SPACING : 0;

if (i < tagLabels.size() - 1) {
// Not the last tag
// If tag i fits but next doesn't, we need space for "+(size-i-1)"
int hiddenIfNextFails = tagLabels.size() - i - 1;
double collapsedWidthIfNextFails = collapsedWidths[hiddenIfNextFails] + TAG_SPACING;

// Check if we can fit tag i + collapsed indicator for remaining tags
if (currentWidth + spacing + tagWidth + collapsedWidthIfNextFails <= availableWidth) {
currentWidth += spacing + tagWidth;
visibleTagCount++;
} else {
// Tag i doesn't fit with its associated indicator
break;
}
} else {
// This is the last tag - no collapsed indicator needed if it fits
if (currentWidth + spacing + tagWidth <= availableWidth) {
currentWidth += spacing + tagWidth;
visibleTagCount++;
}
}
}

// Determine if we need to show collapsed indicator
int hiddenCount = tagLabels.size() - visibleTagCount;
boolean showingCollapsedIndicator = hiddenCount > 0;

// Layout visible tags
double x = 0;
for (int i = 0; i < tagLabels.size(); i++) {
Label label = tagLabels.get(i);
if (i < visibleTagCount) {
label.setVisible(true);
double labelWidth = tagWidths[i];
layoutInArea(label, x, 0, labelWidth, height, 0, HPos.LEFT, VPos.CENTER);
x += labelWidth + TAG_SPACING;
} else {
label.setVisible(false);
}
}

// Layout collapsed indicator
if (showingCollapsedIndicator) {
collapsedIndicator.setText("+" + hiddenCount);
collapsedIndicator.setVisible(true);
double indicatorWidth = collapsedWidths[hiddenCount];

// Ensure indicator doesn't exceed available width
double indicatorX = Math.min(x, availableWidth - indicatorWidth);
indicatorX = Math.max(0, indicatorX); // Don't go negative

layoutInArea(collapsedIndicator, indicatorX, 0, indicatorWidth, height, 0, HPos.LEFT, VPos.CENTER);

// Update tooltip with hidden tags
updateCollapsedTooltip();
} else {
collapsedIndicator.setVisible(false);
}
}

private void updateCollapsedTooltip() {
StringBuilder sb = new StringBuilder();
for (int i = visibleTagCount; i < tagLabels.size(); i++) {
if (!sb.isEmpty()) {
sb.append("\n");
}
sb.append(tags.get(i).text());
}
collapsedTooltip.setText(sb.toString());
}


/**
* A tag with its text content and warning flag.
*/
public record Tag(String text, boolean warning) {
}
}

Loading