Skip to content

Commit 730d9b4

Browse files
authored
更新 JFXSpinner 默认样式 (#5803)
1 parent c7b316e commit 730d9b4

File tree

3 files changed

+22
-126
lines changed

3 files changed

+22
-126
lines changed

HMCL/src/main/java/com/jfoenix/controls/JFXSpinner.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ArrayList;
3232
import java.util.Collections;
3333
import java.util.List;
34+
import java.util.concurrent.ThreadLocalRandom;
3435

3536
/// JFXSpinner is the material design implementation of a loading spinner.
3637
///
@@ -71,10 +72,10 @@ protected Skin<?> createDefaultSkin() {
7172
/// this control.
7273
private static final String DEFAULT_STYLE_CLASS = "jfx-spinner";
7374

74-
private static final double DEFAULT_RADIUS = 12.0;
75+
private static final double DEFAULT_RADIUS = 16.0;
7576

7677
/**
77-
* specifies the radius of the spinner node, by default it's set to `12.0`
78+
* specifies the radius of the spinner node, by default it's set to `16.0`
7879
*/
7980
private StyleableDoubleProperty radius;
8081

@@ -96,25 +97,8 @@ public final void setRadius(final double radius) {
9697
this.radiusProperty().set(radius);
9798
}
9899

99-
/// specifies from which angle the spinner should start spinning
100-
private StyleableDoubleProperty startingAngle;
101-
102-
public final StyleableDoubleProperty startingAngleProperty() {
103-
if (this.startingAngle == null) {
104-
startingAngle = new SimpleStyleableDoubleProperty(StyleableProperties.STARTING_ANGLE,
105-
JFXSpinner.this,
106-
"startingAngle",
107-
0.0);
108-
}
109-
return this.startingAngle;
110-
}
111-
112-
public final double getStartingAngle() {
113-
return startingAngle != null ? startingAngle.get() : 0.0;
114-
}
115-
116-
public final void setStartingAngle(final double startingAngle) {
117-
this.startingAngleProperty().set(startingAngle);
100+
public double getStartingAngle() {
101+
return 360 - ThreadLocalRandom.current().nextDouble() * 720;
118102
}
119103

120104
private static final class StyleableProperties {
@@ -132,26 +116,12 @@ public StyleableDoubleProperty getStyleableProperty(JFXSpinner control) {
132116
}
133117
};
134118

135-
private static final CssMetaData<JFXSpinner, Number> STARTING_ANGLE =
136-
new CssMetaData<>("-jfx-starting-angle",
137-
SizeConverter.getInstance(), 0.0) {
138-
@Override
139-
public boolean isSettable(JFXSpinner control) {
140-
return control.startingAngle == null || !control.startingAngle.isBound();
141-
}
142-
143-
@Override
144-
public StyleableDoubleProperty getStyleableProperty(JFXSpinner control) {
145-
return control.startingAngleProperty();
146-
}
147-
};
148-
149119
private static final List<CssMetaData<? extends Styleable, ?>> CHILD_STYLEABLES;
150120

151121
static {
152122
final List<CssMetaData<? extends Styleable, ?>> styleables =
153123
new ArrayList<>(ProgressIndicator.getClassCssMetaData());
154-
Collections.addAll(styleables, RADIUS, STARTING_ANGLE);
124+
Collections.addAll(styleables, RADIUS);
155125
CHILD_STYLEABLES = List.copyOf(styleables);
156126
}
157127
}

HMCL/src/main/java/com/jfoenix/skins/JFXSpinnerSkin.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.jfoenix.skins;
2121

2222
import com.jfoenix.controls.JFXSpinner;
23-
import com.jfoenix.utils.JFXNodeUtils;
2423
import com.jfoenix.utils.TreeShowingProperty;
2524
import javafx.animation.Interpolator;
2625
import javafx.animation.KeyFrame;
@@ -36,8 +35,6 @@
3635
import javafx.scene.shape.Arc;
3736
import javafx.scene.shape.Rectangle;
3837
import javafx.scene.shape.StrokeLineCap;
39-
import javafx.scene.text.Font;
40-
import javafx.scene.text.Text;
4138
import javafx.util.Duration;
4239

4340
/// JFXSpinner material design skin
@@ -47,6 +44,8 @@
4744
/// @since 2017-09-25
4845
public class JFXSpinnerSkin extends SkinBase<JFXSpinner> {
4946

47+
private static final double DEFAULT_STROKE_WIDTH = 4;
48+
5049
private static final Color GREEN_COLOR = Color.valueOf("#0F9D58");
5150
private static final Color RED_COLOR = Color.valueOf("#db4437");
5251
private static final Color YELLOW_COLOR = Color.valueOf("#f4b400");
@@ -62,37 +61,36 @@ public class JFXSpinnerSkin extends SkinBase<JFXSpinner> {
6261
private final StackPane arcPane;
6362
private final Rectangle fillRect;
6463
private double arcLength = -1;
65-
private final Text text;
64+
65+
private final double startingAngle;
6666

6767
public JFXSpinnerSkin(JFXSpinner control) {
6868
super(control);
6969

7070
this.control = control;
7171
this.treeShowingProperty = new TreeShowingProperty(control);
72+
this.startingAngle = control.getStartingAngle();
7273

7374
arc = new Arc();
7475
arc.setManaged(false);
7576
arc.setStartAngle(0);
7677
arc.setLength(180);
7778
arc.getStyleClass().setAll("arc");
7879
arc.setFill(Color.TRANSPARENT);
79-
arc.setStrokeWidth(3);
80+
arc.setStrokeWidth(DEFAULT_STROKE_WIDTH);
8081
arc.setStrokeLineCap(StrokeLineCap.ROUND);
8182

8283
track = new Arc();
8384
track.setManaged(false);
8485
track.setStartAngle(0);
8586
track.setLength(360);
86-
track.setStrokeWidth(3);
87+
track.setStrokeWidth(DEFAULT_STROKE_WIDTH);
8788
track.getStyleClass().setAll("track");
8889
track.setFill(Color.TRANSPARENT);
8990

9091
fillRect = new Rectangle();
9192
fillRect.setFill(Color.TRANSPARENT);
92-
text = new Text();
93-
text.setStyle("-fx-font-size:null");
94-
text.getStyleClass().setAll("text", "percentage");
95-
final Group group = new Group(fillRect, track, arc, text);
93+
final Group group = new Group(fillRect, track, arc);
9694
group.setManaged(false);
9795
arcPane = new StackPane(group);
9896
arcPane.setPrefSize(50, 50);
@@ -109,7 +107,7 @@ private void initialize() {
109107
if (getSkinnable().isIndeterminate()) {
110108
if (timeline == null) {
111109
createTransition();
112-
if (JFXNodeUtils.isTreeShowing(getSkinnable())) {
110+
if (treeShowingProperty.get()) {
113111
timeline.play();
114112
}
115113
}
@@ -125,22 +123,22 @@ private KeyFrame[] getKeyFrames(double angle, double duration, Paint color) {
125123
frames[0] = new KeyFrame(Duration.seconds(duration),
126124
new KeyValue(arc.lengthProperty(), 5, Interpolator.LINEAR),
127125
new KeyValue(arc.startAngleProperty(),
128-
angle + 45 + control.getStartingAngle(),
126+
angle + 45 + startingAngle,
129127
Interpolator.LINEAR));
130128
frames[1] = new KeyFrame(Duration.seconds(duration + 0.4),
131129
new KeyValue(arc.lengthProperty(), 250, Interpolator.LINEAR),
132130
new KeyValue(arc.startAngleProperty(),
133-
angle + 90 + control.getStartingAngle(),
131+
angle + 90 + startingAngle,
134132
Interpolator.LINEAR));
135133
frames[2] = new KeyFrame(Duration.seconds(duration + 0.7),
136134
new KeyValue(arc.lengthProperty(), 250, Interpolator.LINEAR),
137135
new KeyValue(arc.startAngleProperty(),
138-
angle + 135 + control.getStartingAngle(),
136+
angle + 135 + startingAngle,
139137
Interpolator.LINEAR));
140138
frames[3] = new KeyFrame(Duration.seconds(duration + 1.1),
141139
new KeyValue(arc.lengthProperty(), 5, Interpolator.LINEAR),
142140
new KeyValue(arc.startAngleProperty(),
143-
angle + 435 + control.getStartingAngle(),
141+
angle + 435 + startingAngle,
144142
Interpolator.LINEAR),
145143
new KeyValue(arc.strokeProperty(), color, Interpolator.EASE_BOTH));
146144
return frames;
@@ -230,14 +228,6 @@ protected void layoutChildren(double contentX, double contentY, double contentWi
230228

231229
if (!getSkinnable().isIndeterminate()) {
232230
arc.setLength(arcLength);
233-
if (text.isVisible()) {
234-
final double progress = control.getProgress();
235-
int intProgress = (int) Math.round(progress * 100.0);
236-
Font font = text.getFont();
237-
text.setFont(Font.font(font.getFamily(), radius / 1.7));
238-
text.setText((progress > 1 ? 100 : intProgress) + "%");
239-
text.relocate((arcSize - text.getLayoutBounds().getWidth()) / 2, (arcSize - text.getLayoutBounds().getHeight()) / 2);
240-
}
241231
}
242232
}
243233

@@ -281,7 +271,7 @@ private void createTransition() {
281271
KeyFrame endingFrame = new KeyFrame(Duration.seconds(5.6),
282272
new KeyValue(arc.lengthProperty(), 5, Interpolator.LINEAR),
283273
new KeyValue(arc.startAngleProperty(),
284-
1845 + control.getStartingAngle(),
274+
1845 + startingAngle,
285275
Interpolator.LINEAR));
286276

287277
if (timeline != null) {

HMCL/src/main/resources/assets/css/root.css

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,80 +1452,16 @@
14521452
-fx-stroke: -monet-primary-container;
14531453
}
14541454

1455-
.jfx-spinner:determinate .percentage {
1456-
-fx-fill: -monet-on-surface-variant;
1457-
}
1458-
1459-
.first-spinner {
1460-
-jfx-radius: 20;
1461-
}
1462-
1463-
.first-spinner .arc {
1464-
-fx-stroke-width: 5.0;
1465-
}
1466-
14671455
.small-spinner {
1468-
-jfx-radius: 10;
1456+
-jfx-radius: 9;
14691457
}
14701458

14711459
.small-spinner .arc {
14721460
-fx-stroke-width: 3.0;
14731461
}
14741462

14751463
.small-spinner-pane .jfx-spinner {
1476-
-jfx-radius: 10;
1477-
}
1478-
1479-
.small-spinner-pane .jfx-spinner .arc {
1480-
-fx-stroke-width: 3.0;
1481-
}
1482-
1483-
.second-spinner {
1484-
-jfx-radius: 30;
1485-
}
1486-
1487-
.second-spinner .arc {
1488-
-fx-stroke-width: 5.0;
1489-
}
1490-
1491-
.third-spinner {
1492-
-jfx-radius: 40;
1493-
}
1494-
1495-
.third-spinner .arc {
1496-
-fx-stroke-width: 5.0;
1497-
}
1498-
1499-
.fourth-spinner {
1500-
-jfx-radius: 50;
1501-
}
1502-
1503-
.fourth-spinner .arc {
1504-
-fx-stroke-width: 5.0;
1505-
}
1506-
1507-
.fifth-spinner {
1508-
-jfx-radius: 60;
1509-
}
1510-
1511-
.fifth-spinner .arc {
1512-
-fx-stroke-width: 5.0;
1513-
}
1514-
1515-
.sixth-spinner {
1516-
-jfx-radius: 70;
1517-
}
1518-
1519-
.sixth-spinner .arc {
1520-
-fx-stroke-width: 5.0;
1521-
}
1522-
1523-
.seventh-spinner {
1524-
-jfx-radius: 80;
1525-
}
1526-
1527-
.seventh-spinner .arc {
1528-
-fx-stroke-width: 5.0;
1464+
-jfx-radius: 9;
15291465
}
15301466

15311467
/*******************************************************************************

0 commit comments

Comments
 (0)