Skip to content

Commit 0c4805d

Browse files
authored
使用 LinePane 简化代码 (#5360)
1 parent 27972b9 commit 0c4805d

File tree

6 files changed

+287
-131
lines changed

6 files changed

+287
-131
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,8 @@ public static JFXButton newToggleButton4(SVG icon) {
12991299
return button;
13001300
}
13011301

1302-
public static Label newSafeTruncatedLabel(String text) {
1303-
Label label = new Label(text);
1302+
public static Label newSafeTruncatedLabel() {
1303+
Label label = new Label();
13041304
label.setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
13051305
showTooltipWhenTruncated(label);
13061306
return label;

HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/ComponentList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private static final class Skin extends ControlSkinBase<ComponentList> {
142142
if (node.getProperties().containsKey("ComponentList.vgrow")) {
143143
VBox.setVgrow(cell, (Priority) node.getProperties().get("ComponentList.vgrow"));
144144
}
145-
if (node instanceof LineButtonBase || node.getProperties().containsKey("ComponentList.noPadding")) {
145+
if (node instanceof LineButtonBase || node instanceof LinePane || node.getProperties().containsKey("ComponentList.noPadding")) {
146146
cell.getStyleClass().add("no-padding");
147147
}
148148
return cell;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.ui.construct;
19+
20+
import javafx.beans.property.SimpleStringProperty;
21+
import javafx.beans.property.StringProperty;
22+
import javafx.beans.property.StringPropertyBase;
23+
import javafx.geometry.Insets;
24+
import javafx.geometry.Pos;
25+
import javafx.scene.control.Label;
26+
import javafx.scene.layout.BorderPane;
27+
import javafx.scene.layout.Region;
28+
import javafx.scene.layout.VBox;
29+
30+
/// @author Glavo
31+
public class LinePane extends BorderPane {
32+
33+
private static final Insets PADDING = new Insets(8, 8, 8, 16);
34+
35+
private final Label titleLabel;
36+
37+
public LinePane() {
38+
this.setPadding(PADDING);
39+
this.setMinHeight(48);
40+
41+
this.titleLabel = new Label();
42+
this.setCenter(titleLabel);
43+
BorderPane.setAlignment(titleLabel, Pos.CENTER_LEFT);
44+
titleLabel.textProperty().bind(titleProperty());
45+
titleLabel.getStyleClass().add("title");
46+
}
47+
48+
private final StringProperty title = new SimpleStringProperty(this, "title");
49+
50+
public StringProperty titleProperty() {
51+
return title;
52+
}
53+
54+
public String getTitle() {
55+
return titleProperty().get();
56+
}
57+
58+
public void setTitle(String title) {
59+
this.titleProperty().set(title);
60+
}
61+
62+
private StringProperty subtitle;
63+
64+
public StringProperty subtitleProperty() {
65+
if (subtitle == null) {
66+
subtitle = new StringPropertyBase() {
67+
private VBox left;
68+
private Label subtitleLabel;
69+
70+
@Override
71+
public String getName() {
72+
return "subtitle";
73+
}
74+
75+
@Override
76+
public Object getBean() {
77+
return LinePane.this;
78+
}
79+
80+
@Override
81+
protected void invalidated() {
82+
String subtitle = get();
83+
if (subtitle != null && !subtitle.isEmpty()) {
84+
if (left == null) {
85+
left = new VBox();
86+
left.setMouseTransparent(true);
87+
left.setAlignment(Pos.CENTER_LEFT);
88+
89+
subtitleLabel = new Label();
90+
subtitleLabel.setWrapText(true);
91+
subtitleLabel.setMinHeight(Region.USE_PREF_SIZE);
92+
subtitleLabel.getStyleClass().add("subtitle");
93+
}
94+
subtitleLabel.setText(subtitle);
95+
left.getChildren().setAll(titleLabel, subtitleLabel);
96+
LinePane.this.setCenter(left);
97+
} else if (left != null) {
98+
subtitleLabel.setText(null);
99+
LinePane.this.setCenter(titleLabel);
100+
}
101+
}
102+
};
103+
}
104+
105+
return subtitle;
106+
}
107+
108+
public String getSubtitle() {
109+
return subtitleProperty().get();
110+
}
111+
112+
public void setSubtitle(String subtitle) {
113+
subtitleProperty().set(subtitle);
114+
}
115+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Hello Minecraft! Launcher
3+
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
package org.jackhuang.hmcl.ui.construct;
19+
20+
import javafx.beans.property.StringProperty;
21+
import javafx.beans.property.StringPropertyBase;
22+
import javafx.geometry.Insets;
23+
import javafx.geometry.Pos;
24+
import javafx.scene.control.Label;
25+
import javafx.scene.layout.BorderPane;
26+
import org.jackhuang.hmcl.ui.FXUtils;
27+
28+
/// @author Glavo
29+
public final class LineTextPane extends LinePane {
30+
31+
private static final String DEFAULT_STYLE_CLASS = "line-label-pane";
32+
33+
public LineTextPane() {
34+
this.getStyleClass().add(DEFAULT_STYLE_CLASS);
35+
}
36+
37+
private StringProperty text;
38+
39+
public StringProperty textProperty() {
40+
if (text == null) {
41+
text = new StringPropertyBase() {
42+
private static final Insets LABEL_MARGIN = new Insets(0, 8, 0, 16);
43+
44+
private Label rightLabel;
45+
46+
@Override
47+
public Object getBean() {
48+
return LineTextPane.this;
49+
}
50+
51+
@Override
52+
public String getName() {
53+
return "text";
54+
}
55+
56+
@Override
57+
protected void invalidated() {
58+
String text = get();
59+
if (text == null || text.isEmpty()) {
60+
if (rightLabel != null)
61+
rightLabel.setText(null);
62+
LineTextPane.this.setRight(null);
63+
} else {
64+
if (rightLabel == null) {
65+
rightLabel = FXUtils.newSafeTruncatedLabel();
66+
FXUtils.copyOnDoubleClick(rightLabel);
67+
BorderPane.setMargin(rightLabel, LABEL_MARGIN);
68+
BorderPane.setAlignment(rightLabel, Pos.CENTER_RIGHT);
69+
}
70+
rightLabel.setText(text);
71+
LineTextPane.this.setRight(rightLabel);
72+
}
73+
}
74+
};
75+
}
76+
return text;
77+
}
78+
79+
public String getText() {
80+
return textProperty().get();
81+
}
82+
83+
public void setText(String text) {
84+
textProperty().set(text);
85+
}
86+
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/main/JavaInstallPage.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import com.jfoenix.controls.JFXTextField;
2222
import javafx.beans.property.SimpleStringProperty;
2323
import javafx.beans.property.StringProperty;
24-
import javafx.geometry.Insets;
2524
import javafx.geometry.Pos;
26-
import javafx.scene.control.Label;
2725
import javafx.scene.control.SkinBase;
2826
import javafx.scene.layout.BorderPane;
2927
import javafx.scene.layout.VBox;
@@ -110,17 +108,14 @@ private static final class Skin extends SkinBase<JavaInstallPage> {
110108

111109

112110
{
113-
BorderPane namePane = new BorderPane();
111+
var namePane = new LinePane();
114112
{
115-
Label label = new Label(i18n("java.install.name"));
116-
BorderPane.setAlignment(label, Pos.CENTER_LEFT);
117-
namePane.setLeft(label);
113+
namePane.setTitle(i18n("java.install.name"));
118114

119115
nameField = new JFXTextField();
120116
nameField.textProperty().bindBidirectional(control.nameProperty);
121117
FXUtils.setLimitWidth(nameField, 200);
122118
BorderPane.setAlignment(nameField, Pos.CENTER_RIGHT);
123-
BorderPane.setMargin(nameField, new Insets(0, 0, 12, 0));
124119
namePane.setRight(nameField);
125120
nameField.setValidators(
126121
new RequiredValidator(),
@@ -176,16 +171,9 @@ private static final class Skin extends SkinBase<JavaInstallPage> {
176171
}
177172

178173
private void addInfo(String name, String value) {
179-
BorderPane pane = new BorderPane();
180-
181-
pane.setLeft(new Label(name));
182-
183-
Label valueLabel = FXUtils.newSafeTruncatedLabel(value);
184-
FXUtils.copyOnDoubleClick(valueLabel);
185-
BorderPane.setMargin(valueLabel, new Insets(0, 0, 0, 16));
186-
BorderPane.setAlignment(valueLabel, Pos.CENTER_RIGHT);
187-
pane.setCenter(valueLabel);
188-
174+
LineTextPane pane = new LineTextPane();
175+
pane.setTitle(name);
176+
pane.setText(value);
189177
this.componentList.getContent().add(pane);
190178
}
191179
}

0 commit comments

Comments
 (0)