From adfa7287f8da3b3ebbeb389a8927e1324847f619 Mon Sep 17 00:00:00 2001 From: Burning_TNT Date: Tue, 1 Aug 2023 14:10:36 +0800 Subject: [PATCH] Enhance TwoLineListItem --- .../hmcl/ui/construct/TwoLineListItem.java | 165 +++++++++++++++--- .../hmcl/ui/versions/DownloadListPage.java | 1 + .../hmcl/ui/versions/DownloadPage.java | 3 + HMCL/src/main/resources/assets/css/root.css | 38 ++-- 4 files changed, 169 insertions(+), 38 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TwoLineListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TwoLineListItem.java index 51c98e1596..a7ddf27fcc 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TwoLineListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/TwoLineListItem.java @@ -17,35 +17,56 @@ */ package org.jackhuang.hmcl.ui.construct; +import javafx.animation.Animation; +import javafx.animation.Interpolator; +import javafx.animation.TranslateTransition; +import javafx.application.Platform; import javafx.beans.binding.Bindings; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; +import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Node; import javafx.scene.control.Label; import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; +import javafx.scene.shape.Rectangle; +import javafx.util.Duration; +import org.jackhuang.hmcl.setting.ConfigHolder; import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.util.AggregatedObservableList; +import org.jackhuang.hmcl.util.javafx.BindingMapping; import org.jackhuang.hmcl.util.javafx.MappedObservableList; public class TwoLineListItem extends VBox { private static final String DEFAULT_STYLE_CLASS = "two-line-list-item"; + private static final double TITLE_PART = 0.7D; private final StringProperty title = new SimpleStringProperty(this, "title"); private final ObservableList tags = FXCollections.observableArrayList(); private final StringProperty subtitle = new SimpleStringProperty(this, "subtitle"); - private final ObservableList tagLabels; - private final AggregatedObservableList firstLineChildren; + private final Label titleLabel; + private final HBox tagBox; + private final Label subTitleLabel; + + private final BooleanProperty titleTranslatePlaying = new SimpleBooleanProperty(false); + private final BooleanProperty tagTranslatePlaying = new SimpleBooleanProperty(false); + private final BooleanProperty subTitleTranslatePlaying = new SimpleBooleanProperty(false); public TwoLineListItem(String titleString, String subtitleString) { this(); title.set(titleString); subtitle.set(subtitleString); + + Platform.runLater(this::reLayout); } public TwoLineListItem() { @@ -54,28 +75,55 @@ public class TwoLineListItem extends VBox { HBox firstLine = new HBox(); firstLine.getStyleClass().add("first-line"); - Label lblTitle = new Label(); - lblTitle.getStyleClass().add("title"); - lblTitle.textProperty().bind(title); + { + Pane titlePane = new Pane(); + titlePane.getStyleClass().add("title-pane"); - tagLabels = MappedObservableList.create(tags, tag -> { - Label tagLabel = new Label(); - tagLabel.getStyleClass().add("tag"); - tagLabel.setText(tag); - HBox.setMargin(tagLabel, new Insets(0, 8, 0, 0)); - return tagLabel; - }); - firstLineChildren = new AggregatedObservableList<>(); - firstLineChildren.appendList(FXCollections.singletonObservableList(lblTitle)); - firstLineChildren.appendList(tagLabels); - Bindings.bindContent(firstLine.getChildren(), firstLineChildren.getAggregatedList()); + titleLabel = new Label(); + titleLabel.getStyleClass().add("title"); + titleLabel.textProperty().bind(BindingMapping.of(title).map(s -> s + " | " + s)); - Label lblSubtitle = new Label(); - lblSubtitle.getStyleClass().add("subtitle"); - lblSubtitle.textProperty().bind(subtitle); + Rectangle titleClip = new Rectangle(0, 0, titleLabel.getWidth() / 2 - 10, titleLabel.getHeight()); + titleClip.widthProperty().bind(BindingMapping.of(titleLabel.widthProperty()).map(w -> w.doubleValue() / 2 - 10)); + titlePane.setClip(titleClip); + titlePane.getChildren().setAll(titleLabel); - HBox secondLine = new HBox(); - secondLine.getChildren().setAll(lblSubtitle); + Pane tagLabelsPane = new Pane(); + tagLabelsPane.getStyleClass().add("tag-pane"); + + tagBox = new HBox(); + AggregatedObservableList tagLabels = new AggregatedObservableList<>(); + for (int i = 0; i < 2; i++) { + tagLabels.appendList(MappedObservableList.create(tags, tag -> { + Label tagLabel = new Label(); + tagLabel.getStyleClass().add("tag"); + tagLabel.setText(tag); + HBox.setMargin(tagLabel, new Insets(0, 8, 0, 0)); + return tagLabel; + })); + } + Bindings.bindContent(tagBox.getChildren(), tagLabels.getAggregatedList()); + + Rectangle tagClip = new Rectangle(0, 0, tagBox.getWidth() / 2 - 2, tagBox.getHeight()); + tagClip.widthProperty().bind(BindingMapping.of(tagBox.widthProperty()).map(w -> w.doubleValue() / 2 - 2)); + tagLabelsPane.setClip(tagClip); + tagLabelsPane.getChildren().setAll(tagBox); + + firstLine.getChildren().setAll(titlePane, tagLabelsPane); + } + + Pane secondLine = new Pane(); + { + subTitleLabel = new Label(); + subTitleLabel.getStyleClass().add("subtitle"); + subTitleLabel.textProperty().bind(BindingMapping.of(subtitle).map(s -> s + " | " + s)); + + Rectangle subTitleClip = new Rectangle(0, 0, subTitleLabel.getWidth() / 2 - 2, 80000); + subTitleClip.widthProperty().bind(BindingMapping.of(subTitleLabel.widthProperty()).map(w -> w.doubleValue() / 2 - 2)); + secondLine.setClip(subTitleClip); + + secondLine.getChildren().setAll(subTitleLabel); + } getChildren().setAll(firstLine, secondLine); @@ -85,6 +133,81 @@ public class TwoLineListItem extends VBox { }); getStyleClass().add(DEFAULT_STYLE_CLASS); + + title.addListener(observable -> Platform.runLater(this::reLayout)); + subtitle.addListener(observable -> Platform.runLater(this::reLayout)); + tags.addListener((ListChangeListener) c -> Platform.runLater(this::reLayout)); + this.widthProperty().addListener(observable -> Platform.runLater(this::reLayout)); + + TranslateTransition titleTranslateTransition = new TranslateTransition(Duration.seconds(0.1D), titleLabel); + titleTranslateTransition.setFromX(0); + titleTranslateTransition.durationProperty().bind(BindingMapping.of(titleLabel.widthProperty()).map(w -> Duration.seconds((w.doubleValue() / 2D + 1) / 30D))); + titleTranslateTransition.toXProperty().bind(BindingMapping.of(titleLabel.widthProperty()).map(w -> -w.doubleValue() / 2D - 1)); + titleTranslateTransition.setInterpolator(Interpolator.LINEAR); + titleTranslateTransition.setCycleCount(Animation.INDEFINITE); + titleTranslatePlaying.addListener(generateTranslateListener(titleTranslateTransition)); + + TranslateTransition tagTranslateTransition = new TranslateTransition(Duration.seconds(0.1D), tagBox); + tagTranslateTransition.setFromX(0); + tagTranslateTransition.durationProperty().bind(BindingMapping.of(tagBox.widthProperty()).map(w -> Duration.seconds(w.doubleValue() / 2D / 30D))); + tagTranslateTransition.toXProperty().bind(BindingMapping.of(tagBox.widthProperty()).map(w -> -w.doubleValue() / 2D)); + tagTranslateTransition.setInterpolator(Interpolator.LINEAR); + tagTranslateTransition.setCycleCount(Animation.INDEFINITE); + tagTranslatePlaying.addListener(generateTranslateListener(tagTranslateTransition)); + + TranslateTransition subTitleTranslateTransition = new TranslateTransition(Duration.seconds(0.1D), subTitleLabel); + subTitleTranslateTransition.setFromX(0); + subTitleTranslateTransition.durationProperty().bind(BindingMapping.of(subTitleLabel.widthProperty()).map(w -> Duration.seconds(w.doubleValue() / 2D / 30D))); + subTitleTranslateTransition.toXProperty().bind(BindingMapping.of(subTitleLabel.widthProperty()).map(w -> (-w.doubleValue() - 8) / 2D)); + subTitleTranslateTransition.setInterpolator(Interpolator.LINEAR); + subTitleTranslateTransition.setCycleCount(Animation.INDEFINITE); + subTitleTranslatePlaying.addListener(generateTranslateListener(subTitleTranslateTransition)); + + Rectangle mainClip = new Rectangle(0, 0, this.getWidth(), this.getHeight()); + mainClip.widthProperty().bind(this.widthProperty()); + mainClip.heightProperty().bind(this.heightProperty()); + this.setClip(mainClip); + + this.minWidthProperty().set(0); + } + + private static ChangeListener generateTranslateListener(TranslateTransition translateTransition) { + if (ConfigHolder.config().isAnimationDisabled()) { + return (observable, oldValue, newValue) -> {}; + } else { + return (observable, oldValue, newValue) -> { + if (oldValue.booleanValue() != newValue.booleanValue()) { + if (newValue) { + translateTransition.play(); + } else { + translateTransition.jumpTo(Duration.ZERO); + translateTransition.stop(); + } + } + }; + } + } + + public void reLayout() { + this.layout(); + + double titleMaxWidth = tags.size() == 0 ? this.getWidth() : this.getWidth() * TITLE_PART; + double titleWidth = Math.min(titleLabel.getWidth() / 2, titleMaxWidth); + double tagMaxWidth = this.getWidth() - titleWidth - this.paddingProperty().get().getLeft() - this.paddingProperty().get().getRight() - 5; + double tagWidth = Math.min(tagBox.getWidth() / 2, tagMaxWidth); + + FXUtils.setLimitWidth((Pane) titleLabel.getParent(), titleWidth); + Rectangle titleClip = new Rectangle(0, 0, titleWidth - 10, titleLabel.getHeight()); + titleLabel.getParent().setClip(titleClip); + + titleTranslatePlaying.set(titleLabel.getWidth() / 2 > titleMaxWidth); + + Rectangle tagClip = new Rectangle(0, 0, tagWidth - 2, tagBox.getHeight()); + tagBox.getParent().setClip(tagClip); + + tagTranslatePlaying.set(tagBox.getWidth() / 2 > tagMaxWidth); + + subTitleTranslatePlaying.set(subTitleLabel.getWidth() / 2 > this.getWidth()); } public String getTitle() { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadListPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadListPage.java index 07d21e1ac5..76ad09c577 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadListPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadListPage.java @@ -402,6 +402,7 @@ public class DownloadListPage extends Control implements DecoratorPage, VersionP pane.getChildren().add(container); container.getChildren().setAll(FXUtils.limitingSize(imageView, 40, 40), content); + HBox.setHgrow(content, Priority.ALWAYS); } @Override diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadPage.java index e20b06a329..2102864200 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/DownloadPage.java @@ -264,12 +264,14 @@ public class DownloadPage extends Control implements DecoratorPage { JFXHyperlink openMcmodButton = new JFXHyperlink(i18n("mods.mcmod")); openMcmodButton.setExternalLink(getSkinnable().translations.getMcmodUrl(getSkinnable().mod)); descriptionPane.getChildren().add(openMcmodButton); + openMcmodButton.setMinWidth(Region.USE_PREF_SIZE); runInFX(() -> FXUtils.installFastTooltip(openMcmodButton, i18n("mods.mcmod"))); if (StringUtils.isNotBlank(getSkinnable().mod.getMcbbs())) { JFXHyperlink openMcbbsButton = new JFXHyperlink(i18n("mods.mcbbs")); openMcbbsButton.setExternalLink(ModManager.getMcbbsUrl(getSkinnable().mod.getMcbbs())); descriptionPane.getChildren().add(openMcbbsButton); + openMcbbsButton.setMinWidth(Region.USE_PREF_SIZE); runInFX(() -> FXUtils.installFastTooltip(openMcbbsButton, i18n("mods.mcbbs"))); } } @@ -277,6 +279,7 @@ public class DownloadPage extends Control implements DecoratorPage { JFXHyperlink openUrlButton = new JFXHyperlink(control.page.getLocalizedOfficialPage()); openUrlButton.setExternalLink(getSkinnable().addon.getPageUrl()); descriptionPane.getChildren().add(openUrlButton); + openUrlButton.setMinWidth(Region.USE_PREF_SIZE); runInFX(() -> FXUtils.installFastTooltip(openUrlButton, control.page.getLocalizedOfficialPage())); } diff --git a/HMCL/src/main/resources/assets/css/root.css b/HMCL/src/main/resources/assets/css/root.css index aea759d2a9..c21b81abca 100644 --- a/HMCL/src/main/resources/assets/css/root.css +++ b/HMCL/src/main/resources/assets/css/root.css @@ -155,12 +155,12 @@ -fx-padding: 0 0 0 10; } -.advanced-list-item > .rippler-container > .container > .two-line-list-item > .first-line > .title { +.advanced-list-item > .rippler-container > .container > .two-line-list-item > .first-line > .title-pane > .title { -fx-font-size: 13; -fx-text-alignment: justify; } -.advanced-list-item > .rippler-container > .container > .two-line-list-item > HBox > .subtitle { +.advanced-list-item > .rippler-container > .container > .two-line-list-item > Pane > .subtitle { -fx-font-size: 10; -fx-text-alignment: justify; } @@ -169,7 +169,7 @@ -fx-background-color: -fx-base-rippler-color; } -.advanced-list-item:selected > .rippler-container > .container > .two-line-list-item > .first-line > .title { +.advanced-list-item:selected > .rippler-container > .container > .two-line-list-item > .first-line > .title-pane > .title { -fx-text-fill: -fx-base-color; -fx-font-weight: bold; } @@ -182,11 +182,11 @@ -fx-padding: 0 0 0 0; } -.profile-list-item > .rippler-container > BorderPane > .two-line-list-item > .first-line > .title { +.profile-list-item > .rippler-container > BorderPane > .two-line-list-item > .first-line > .title-pane > .title { -fx-font-size: 13; } -.profile-list-item > .rippler-container > BorderPane > .two-line-list-item > HBox > .subtitle { +.profile-list-item > .rippler-container > BorderPane > .two-line-list-item > Pane > .subtitle { -fx-font-size: 10; } @@ -198,7 +198,7 @@ -fx-background-color: -fx-base-rippler-color; } -.profile-list-item:selected > .rippler-container > BorderPane > .two-line-list-item > .first-line > .title { +.profile-list-item:selected > .rippler-container > BorderPane > .two-line-list-item > .first-line > .title-pane > .title { -fx-text-fill: -fx-base-color; -fx-font-weight: bold; } @@ -261,22 +261,26 @@ -fx-padding: 4 0 4 0; } -.two-line-list-item > .first-line > .title { +.two-line-list-item > .first-line > .title-pane { + -fx-overflow: hidden; +} + +.two-line-list-item > .first-line > .title-pane > .title { -fx-text-fill: #292929; -fx-font-size: 15px; -fx-padding: 0 8 0 0; } -.two-line-list-item > HBox > .subtitle { - -fx-text-fill: rgba(0, 0, 0, 0.5); +.two-line-list-item > .first-line > .tag-pane > HBox > .tag { + -fx-text-fill: -fx-base-color; + -fx-background-color: -fx-base-rippler-color; + -fx-padding: 2; -fx-font-weight: normal; -fx-font-size: 12px; } -.two-line-list-item > .first-line > .tag { - -fx-text-fill: -fx-base-color; - -fx-background-color: -fx-base-rippler-color; - -fx-padding: 2; +.two-line-list-item > Pane > .subtitle { + -fx-text-fill: rgba(0, 0, 0, 0.5); -fx-font-weight: normal; -fx-font-size: 12px; } @@ -285,13 +289,13 @@ } -.two-line-item-second-large > .first-line > .title, .two-line-item-second-large-title { +.two-line-item-second-large > .first-line > .title-pane > .title, .two-line-item-second-large-title { -fx-text-fill: rgba(0, 0, 0, 0.5); -fx-font-weight: normal; -fx-font-size: 12px; } -.two-line-item-second-large > HBox > .subtitle { +.two-line-item-second-large > Pane > .subtitle { -fx-text-fill: #292929; -fx-font-size: 15px; } @@ -306,8 +310,8 @@ -fx-text-fill: white; } -.bubble > HBox > .two-line-list-item > .first-line > .title, -.bubble > HBox > .two-line-list-item > HBox > .subtitle { +.bubble > HBox > .two-line-list-item > .first-line > .title-pane > .title, +.bubble > HBox > .two-line-list-item > Pane > .subtitle { -fx-text-fill: white; }