fix #1054: Avoid incorrect UI layout caused by long strings

This commit is contained in:
Glavo 2021-09-22 13:27:07 +08:00 committed by Yuhui Huang
parent cbe9f7474e
commit bcaf3de164
2 changed files with 23 additions and 6 deletions

View File

@ -29,6 +29,7 @@ import javafx.scene.Node;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Toggle; import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
@ -128,7 +129,8 @@ public class MultiFileItem<T> extends ComponentSublist {
protected final T data; protected final T data;
public Option(String title, T data) { public Option(String title, T data) {
this.title = title; this.data = data; this.title = title;
this.data = data;
} }
public T getData() { public T getData() {
@ -160,7 +162,14 @@ public class MultiFileItem<T> extends ComponentSublist {
pane.setLeft(left); pane.setLeft(left);
if (StringUtils.isNotBlank(subtitle)) { if (StringUtils.isNotBlank(subtitle)) {
Label right = new Label(subtitle); Optional<String> shortSubtitle = StringUtils.truncate(subtitle);
Label right;
if (shortSubtitle.isPresent()) {
right = new Label(shortSubtitle.get());
right.setTooltip(new Tooltip(subtitle));
} else {
right = new Label(subtitle);
}
BorderPane.setAlignment(right, Pos.CENTER_RIGHT); BorderPane.setAlignment(right, Pos.CENTER_RIGHT);
right.setWrapText(true); right.setWrapText(true);
right.getStyleClass().add("subtitle-label"); right.getStyleClass().add("subtitle-label");

View File

@ -21,10 +21,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Collection; import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -245,6 +242,17 @@ public final class StringUtils {
return result.toString(); return result.toString();
} }
public static int MAX_SHORT_STRING_LENGTH = 77;
public static Optional<String> truncate(String str) {
if (str.length() <= MAX_SHORT_STRING_LENGTH) {
return Optional.empty();
}
final int halfLength = (MAX_SHORT_STRING_LENGTH - 5) / 2;
return Optional.of(str.substring(0, halfLength) + " ... " + str.substring(str.length() - halfLength));
}
/** /**
* Class for computing the longest common subsequence between strings. * Class for computing the longest common subsequence between strings.
*/ */