Skip to content

Commit 9b9471d

Browse files
authored
惰性初始化 JFXRippler 属性 (#5208)
1 parent b1402d3 commit 9b9471d

1 file changed

Lines changed: 85 additions & 70 deletions

File tree

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

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public enum RipplerMask {
7878
protected Node control;
7979

8080
protected static final double RIPPLE_MAX_RADIUS = 300;
81-
82-
private boolean forceOverlay = false;
83-
private final Interpolator rippleInterpolator = Interpolator.SPLINE(0.0825,
81+
private static final Interpolator RIPPLE_INTERPOLATOR = Interpolator.SPLINE(0.0825,
8482
0.3025,
8583
0.0875,
8684
0.9975); //0.1, 0.54, 0.28, 0.95);
8785

86+
private boolean forceOverlay = false;
87+
8888
/// creates empty rippler node
8989
public JFXRippler() {
9090
this(null, RipplerMask.RECT, RipplerPos.FRONT);
@@ -414,15 +414,15 @@ void createOverlay() {
414414
overlayRect.setClip(getMask());
415415
getChildren().add(0, overlayRect);
416416
overlayRect.fillProperty().bind(Bindings.createObjectBinding(() -> {
417-
if (ripplerFill.get() instanceof Color) {
418-
return new Color(((Color) ripplerFill.get()).getRed(),
419-
((Color) ripplerFill.get()).getGreen(),
420-
((Color) ripplerFill.get()).getBlue(),
417+
if (getRipplerFill() instanceof Color fill) {
418+
return new Color(fill.getRed(),
419+
fill.getGreen(),
420+
fill.getBlue(),
421421
0.2);
422422
} else {
423423
return Color.TRANSPARENT;
424424
}
425-
}, ripplerFill));
425+
}, ripplerFillProperty()));
426426
}
427427
}
428428

@@ -472,8 +472,8 @@ private final class Ripple extends Circle {
472472
private Ripple(double centerX, double centerY) {
473473
super(centerX,
474474
centerY,
475-
ripplerRadius.get().doubleValue() == Region.USE_COMPUTED_SIZE ?
476-
computeRippleRadius() : ripplerRadius.get().doubleValue(), null);
475+
getRipplerRadius() == Region.USE_COMPUTED_SIZE ?
476+
computeRippleRadius() : getRipplerRadius(), null);
477477
setCache(true);
478478
setCacheHint(CacheHint.SPEED);
479479
setCacheShape(true);
@@ -483,55 +483,55 @@ private Ripple(double centerX, double centerY) {
483483
KeyValue[] inKeyValues = new KeyValue[isRipplerRecenter() ? 4 : 2];
484484
outKeyValues = new KeyValue[isRipplerRecenter() ? 5 : 3];
485485

486-
inKeyValues[0] = new KeyValue(scaleXProperty(), 0.9, rippleInterpolator);
487-
inKeyValues[1] = new KeyValue(scaleYProperty(), 0.9, rippleInterpolator);
486+
inKeyValues[0] = new KeyValue(scaleXProperty(), 0.9, RIPPLE_INTERPOLATOR);
487+
inKeyValues[1] = new KeyValue(scaleYProperty(), 0.9, RIPPLE_INTERPOLATOR);
488488

489-
outKeyValues[0] = new KeyValue(this.scaleXProperty(), 1, rippleInterpolator);
490-
outKeyValues[1] = new KeyValue(this.scaleYProperty(), 1, rippleInterpolator);
491-
outKeyValues[2] = new KeyValue(this.opacityProperty(), 0, rippleInterpolator);
489+
outKeyValues[0] = new KeyValue(this.scaleXProperty(), 1, RIPPLE_INTERPOLATOR);
490+
outKeyValues[1] = new KeyValue(this.scaleYProperty(), 1, RIPPLE_INTERPOLATOR);
491+
outKeyValues[2] = new KeyValue(this.opacityProperty(), 0, RIPPLE_INTERPOLATOR);
492492

493493
if (isRipplerRecenter()) {
494494
double dx = (control.getLayoutBounds().getWidth() / 2 - centerX) / 1.55;
495495
double dy = (control.getLayoutBounds().getHeight() / 2 - centerY) / 1.55;
496496
inKeyValues[2] = outKeyValues[3] = new KeyValue(translateXProperty(),
497497
Math.signum(dx) * Math.min(Math.abs(dx),
498498
this.getRadius() / 2),
499-
rippleInterpolator);
499+
RIPPLE_INTERPOLATOR);
500500
inKeyValues[3] = outKeyValues[4] = new KeyValue(translateYProperty(),
501501
Math.signum(dy) * Math.min(Math.abs(dy),
502502
this.getRadius() / 2),
503-
rippleInterpolator);
503+
RIPPLE_INTERPOLATOR);
504504
}
505505
inAnimation = new Timeline(new KeyFrame(Duration.ZERO,
506506
new KeyValue(scaleXProperty(),
507507
0,
508-
rippleInterpolator),
508+
RIPPLE_INTERPOLATOR),
509509
new KeyValue(scaleYProperty(),
510510
0,
511-
rippleInterpolator),
511+
RIPPLE_INTERPOLATOR),
512512
new KeyValue(translateXProperty(),
513513
0,
514-
rippleInterpolator),
514+
RIPPLE_INTERPOLATOR),
515515
new KeyValue(translateYProperty(),
516516
0,
517-
rippleInterpolator),
517+
RIPPLE_INTERPOLATOR),
518518
new KeyValue(opacityProperty(),
519519
1,
520-
rippleInterpolator)
520+
RIPPLE_INTERPOLATOR)
521521
), new KeyFrame(Duration.millis(900), inKeyValues));
522522

523523
setScaleX(0);
524524
setScaleY(0);
525-
if (ripplerFill.get() instanceof Color) {
526-
Color circleColor = new Color(((Color) ripplerFill.get()).getRed(),
527-
((Color) ripplerFill.get()).getGreen(),
528-
((Color) ripplerFill.get()).getBlue(),
525+
if (getRipplerFill() instanceof Color fill) {
526+
Color circleColor = new Color(fill.getRed(),
527+
fill.getGreen(),
528+
fill.getBlue(),
529529
0.3);
530530
setStroke(circleColor);
531531
setFill(circleColor);
532532
} else {
533-
setStroke(ripplerFill.get());
534-
setFill(ripplerFill.get());
533+
setStroke(getRipplerFill());
534+
setFill(getRipplerFill());
535535
}
536536
}
537537
}
@@ -584,110 +584,125 @@ private void initialize() {
584584
* the ripple recenter property, by default it's false.
585585
* if true the ripple effect will show gravitational pull to the center of its control
586586
*/
587-
private final StyleableObjectProperty<Boolean> ripplerRecenter = new SimpleStyleableObjectProperty<>(
588-
StyleableProperties.RIPPLER_RECENTER,
589-
JFXRippler.this,
590-
"ripplerRecenter",
591-
false);
587+
private StyleableBooleanProperty ripplerRecenter;
592588

593-
public Boolean isRipplerRecenter() {
589+
public boolean isRipplerRecenter() {
594590
return ripplerRecenter != null && ripplerRecenter.get();
595591
}
596592

597-
public StyleableObjectProperty<Boolean> ripplerRecenterProperty() {
593+
public StyleableBooleanProperty ripplerRecenterProperty() {
594+
if (this.ripplerRecenter == null) {
595+
this.ripplerRecenter = new SimpleStyleableBooleanProperty(
596+
StyleableProperties.RIPPLER_RECENTER,
597+
JFXRippler.this,
598+
"ripplerRecenter",
599+
false);
600+
}
598601
return this.ripplerRecenter;
599602
}
600603

601-
public void setRipplerRecenter(Boolean radius) {
602-
this.ripplerRecenter.set(radius);
604+
public void setRipplerRecenter(boolean recenter) {
605+
ripplerRecenterProperty().set(recenter);
603606
}
604607

605608
/**
606609
* the ripple radius size, by default it will be automatically computed.
607610
*/
608-
private final StyleableObjectProperty<Number> ripplerRadius = new SimpleStyleableObjectProperty<>(
609-
StyleableProperties.RIPPLER_RADIUS,
610-
JFXRippler.this,
611-
"ripplerRadius",
612-
Region.USE_COMPUTED_SIZE);
611+
private StyleableDoubleProperty ripplerRadius;
613612

614-
public Number getRipplerRadius() {
613+
public double getRipplerRadius() {
615614
return ripplerRadius == null ? Region.USE_COMPUTED_SIZE : ripplerRadius.get();
616615
}
617616

618-
public StyleableObjectProperty<Number> ripplerRadiusProperty() {
617+
public StyleableDoubleProperty ripplerRadiusProperty() {
618+
if (this.ripplerRadius == null) {
619+
this.ripplerRadius = new SimpleStyleableDoubleProperty(
620+
StyleableProperties.RIPPLER_RADIUS,
621+
JFXRippler.this,
622+
"ripplerRadius",
623+
Region.USE_COMPUTED_SIZE);
624+
}
619625
return this.ripplerRadius;
620626
}
621627

622-
public void setRipplerRadius(Number radius) {
623-
this.ripplerRadius.set(radius);
628+
public void setRipplerRadius(double radius) {
629+
ripplerRadiusProperty().set(radius);
624630
}
625631

632+
private static final Color DEFAULT_RIPPLER_FILL = Color.rgb(0, 200, 255);
633+
626634
/**
627635
* the default color of the ripple effect
628636
*/
629-
private final StyleableObjectProperty<Paint> ripplerFill = new SimpleStyleableObjectProperty<>(StyleableProperties.RIPPLER_FILL,
630-
JFXRippler.this,
631-
"ripplerFill",
632-
Color.rgb(0,
633-
200,
634-
255));
637+
private StyleableObjectProperty<Paint> ripplerFill;
635638

636639
public Paint getRipplerFill() {
637-
return ripplerFill == null ? Color.rgb(0, 200, 255) : ripplerFill.get();
640+
return ripplerFill == null ? DEFAULT_RIPPLER_FILL : ripplerFill.get();
638641
}
639642

640643
public StyleableObjectProperty<Paint> ripplerFillProperty() {
644+
if (this.ripplerFill == null) {
645+
this.ripplerFill = new SimpleStyleableObjectProperty<>(StyleableProperties.RIPPLER_FILL,
646+
JFXRippler.this,
647+
"ripplerFill",
648+
DEFAULT_RIPPLER_FILL);
649+
}
641650
return this.ripplerFill;
642651
}
643652

644653
public void setRipplerFill(Paint color) {
645-
this.ripplerFill.set(color);
654+
ripplerFillProperty().set(color);
646655
}
647656

648657
/// mask property used for clipping the rippler.
649658
/// can be either CIRCLE/RECT
650-
private final StyleableObjectProperty<RipplerMask> maskType = new SimpleStyleableObjectProperty<>(
651-
StyleableProperties.MASK_TYPE,
652-
JFXRippler.this,
653-
"maskType",
654-
RipplerMask.RECT);
659+
private StyleableObjectProperty<RipplerMask> maskType;
655660

656661
public RipplerMask getMaskType() {
657662
return maskType == null ? RipplerMask.RECT : maskType.get();
658663
}
659664

660665
public StyleableObjectProperty<RipplerMask> maskTypeProperty() {
666+
if (this.maskType == null) {
667+
this.maskType = new SimpleStyleableObjectProperty<>(
668+
StyleableProperties.MASK_TYPE,
669+
JFXRippler.this,
670+
"maskType",
671+
RipplerMask.RECT);
672+
}
661673
return this.maskType;
662674
}
663675

664676
public void setMaskType(RipplerMask type) {
665-
this.maskType.set(type);
677+
if (this.maskType != null || type != RipplerMask.RECT)
678+
maskTypeProperty().set(type);
666679
}
667680

668681
/**
669682
* the ripple disable, by default it's false.
670683
* if true the ripple effect will be hidden
671684
*/
672-
private final StyleableBooleanProperty ripplerDisabled = new SimpleStyleableBooleanProperty(
673-
StyleableProperties.RIPPLER_DISABLED,
674-
JFXRippler.this,
675-
"ripplerDisabled",
676-
false);
685+
private StyleableBooleanProperty ripplerDisabled;
677686

678-
public Boolean isRipplerDisabled() {
687+
public boolean isRipplerDisabled() {
679688
return ripplerDisabled != null && ripplerDisabled.get();
680689
}
681690

682691
public StyleableBooleanProperty ripplerDisabledProperty() {
692+
if (this.ripplerDisabled == null) {
693+
this.ripplerDisabled = new SimpleStyleableBooleanProperty(
694+
StyleableProperties.RIPPLER_DISABLED,
695+
JFXRippler.this,
696+
"ripplerDisabled",
697+
false);
698+
}
683699
return this.ripplerDisabled;
684700
}
685701

686-
public void setRipplerDisabled(Boolean disabled) {
687-
this.ripplerDisabled.set(disabled);
702+
public void setRipplerDisabled(boolean disabled) {
703+
ripplerDisabledProperty().set(disabled);
688704
}
689705

690-
691706
/**
692707
* indicates whether the ripple effect is infront of or behind the node
693708
*/
@@ -734,7 +749,7 @@ public StyleableProperty<Boolean> getStyleableProperty(JFXRippler control) {
734749
};
735750
private static final CssMetaData<JFXRippler, Paint> RIPPLER_FILL =
736751
new CssMetaData<>("-jfx-rippler-fill",
737-
PaintConverter.getInstance(), Color.rgb(0, 200, 255)) {
752+
PaintConverter.getInstance(), DEFAULT_RIPPLER_FILL) {
738753
@Override
739754
public boolean isSettable(JFXRippler control) {
740755
return control.ripplerFill == null || !control.ripplerFill.isBound();

0 commit comments

Comments
 (0)