-
Notifications
You must be signed in to change notification settings - Fork 814
当标签过多时折叠溢出的标签 #5171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Glavo
wants to merge
3
commits into
HMCL-dev:main
Choose a base branch
from
Glavo:tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
当标签过多时折叠溢出的标签 #5171
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TagsBar.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| 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) { | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我找到了一个解决办法,之所以会出现标签没办法渲染,主要原因是部分情况下重新布局时,标签Label没有正确应用css,这导致Label的尺寸为0,可以强制applyCss来解决
可能的原因如下:
HMCL/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadListPage.java
Lines 558 to 561 in 5057524
在这里,由于选中组件导致updateControl被执行,因为导致标签被重新添加,会触发这个逻辑,导致tagLabel重建
但问题在于,选中本身也导致更新css,这导致选中列表时会同时更新css和更新tagLabel以及重新布局,这可能导致一些bug,导致在layoutChildren时新的tagLabels根本没有应用css。
我不能确定一定是因此产生的bug,但是我可以确认的是,重新布局时,新的Label确实没有正确应用css,这导致新的Label的尺寸为0