Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion HMCL/src/main/java/com/jfoenix/controls/JFXRippler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javafx.css.converter.BooleanConverter;
import javafx.css.converter.PaintConverter;
import javafx.css.converter.SizeConverter;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.CacheHint;
import javafx.scene.Group;
Expand All @@ -45,6 +46,9 @@
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
import org.jackhuang.hmcl.theme.Themes;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.animation.Motion;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -76,6 +80,8 @@ public enum RipplerMask {
protected RippleGenerator rippler;
protected Pane ripplerPane;
protected Node control;
private Animation coverAnimation;
private Rectangle hoverOverlay;

protected static final double RIPPLE_MAX_RADIUS = 300;
private static final Interpolator RIPPLE_INTERPOLATOR = Interpolator.SPLINE(0.0825,
Expand Down Expand Up @@ -128,14 +134,50 @@ public JFXRippler(Node control, RipplerMask mask, RipplerPos pos) {
setCache(true);
setCacheHint(CacheHint.SPEED);
setCacheShape(true);

EventHandler<MouseEvent> mouseEventHandler;
if (AnimationUtils.isAnimationEnabled()) {
mouseEventHandler = event -> {
if (coverAnimation != null) {
coverAnimation.stop();
}

boolean isEntered = event.getEventType() == MouseEvent.MOUSE_ENTERED;
Color onSurface = Themes.getColorScheme().getOnSurface();
hoverOverlay.setFill(Color.color(onSurface.getRed(), onSurface.getGreen(), onSurface.getBlue(), 0.04));
Comment on lines +146 to +147
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The JFXRippler class now has a direct dependency on org.jackhuang.hmcl.theme.Themes. As a general-purpose UI component, it should ideally remain agnostic of the application's specific theme logic. Hardcoding this dependency makes the component less reusable and breaks the separation of concerns. Consider using a CSS-stylable property or a dedicated hoverFill property that can be configured from the outside.


coverAnimation = new Timeline(new KeyFrame(Motion.SHORT4,
new KeyValue(hoverOverlay.opacityProperty(), isEntered ? 1 : 0, isEntered ? Motion.EASE_IN : Motion.EASE_OUT)));
Comment thread
CiiLu marked this conversation as resolved.
coverAnimation.play();
};
Comment thread
CiiLu marked this conversation as resolved.
} else {
mouseEventHandler = event ->
interpolateBackground(event.getEventType() == MouseEvent.MOUSE_ENTERED ? 1 : 0);
}

addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEventHandler);
addEventHandler(MouseEvent.MOUSE_EXITED, mouseEventHandler);
}

private void interpolateBackground(double frac) {
if (hoverOverlay == null) return;
Color onSurface = Themes.getColorScheme().getOnSurface();
hoverOverlay.setFill(Color.color(onSurface.getRed(), onSurface.getGreen(), onSurface.getBlue(), 0.04));
hoverOverlay.setOpacity(frac);
}
Comment thread
CiiLu marked this conversation as resolved.

protected final void createRippleUI() {
// create rippler panels
rippler = new RippleGenerator();
ripplerPane = new StackPane();
ripplerPane.setMouseTransparent(true);
ripplerPane.getChildren().add(rippler);

hoverOverlay = new Rectangle();
hoverOverlay.setManaged(false);
hoverOverlay.setCache(true);
hoverOverlay.setCacheHint(CacheHint.SPEED);
hoverOverlay.setOpacity(0);
ripplerPane.getChildren().addAll(hoverOverlay, rippler);
getChildren().add(ripplerPane);
}

Expand Down Expand Up @@ -561,6 +603,17 @@ private void resetClip() {
protected void resetRippler() {
resetOverLay();
resetClip();

if (hoverOverlay != null && control != null) {
Bounds bounds = control.getBoundsInParent();
double diffMinX = Math.abs(control.getBoundsInLocal().getMinX() - control.getLayoutBounds().getMinX());
double diffMinY = Math.abs(control.getBoundsInLocal().getMinY() - control.getLayoutBounds().getMinY());
hoverOverlay.setX(bounds.getMinX() + diffMinX - snappedLeftInset());
hoverOverlay.setY(bounds.getMinY() + diffMinY - snappedTopInset());
hoverOverlay.setWidth(control.getLayoutBounds().getWidth());
hoverOverlay.setHeight(control.getLayoutBounds().getHeight());
hoverOverlay.setClip(getMask());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

resetRippler is called frequently during layout changes. Calling getMask() here on every pulse is inefficient because it creates a new Node object each time. This can lead to excessive object allocation and GC pressure during UI transitions (e.g., when the control is resizing). Consider caching the mask node or only updating it when the mask type or significant bounds change.

}
}

/***************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@
package org.jackhuang.hmcl.ui.construct;

import com.jfoenix.controls.JFXRippler;
import javafx.animation.Transition;
import javafx.css.*;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.css.converter.PaintConverter;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import org.jackhuang.hmcl.theme.Themes;
import org.jackhuang.hmcl.ui.animation.AnimationUtils;
import org.jackhuang.hmcl.ui.animation.Motion;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -63,8 +60,6 @@ protected Node getMask() {
}
};

private Transition coverAnimation;

public RipplerContainer(Node container) {
this.container = container;

Expand All @@ -90,61 +85,6 @@ public RipplerContainer(Node container) {
shape.widthProperty().bind(widthProperty());
shape.heightProperty().bind(heightProperty());
setShape(shape);

EventHandler<MouseEvent> mouseEventHandler;
if (AnimationUtils.isAnimationEnabled()) {
mouseEventHandler = event -> {
if (coverAnimation != null) {
coverAnimation.stop();
coverAnimation = null;
}

if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
coverAnimation = new Transition() {
{
setCycleDuration(Motion.SHORT4);
setInterpolator(Motion.EASE_IN);
}

@Override
protected void interpolate(double frac) {
interpolateBackground(frac);
}
};
} else {
coverAnimation = new Transition() {
{
setCycleDuration(Motion.SHORT4);
setInterpolator(Motion.EASE_OUT);
}

@Override
protected void interpolate(double frac) {
interpolateBackground(1 - frac);
}
};
}

coverAnimation.play();
};
} else {
mouseEventHandler = event ->
interpolateBackground(event.getEventType() == MouseEvent.MOUSE_ENTERED ? 1 : 0);
}

addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEventHandler);
addEventHandler(MouseEvent.MOUSE_EXITED, mouseEventHandler);
}

private void interpolateBackground(double frac) {
if (frac < 0.01) {
setBackground(null);
} else {
Color onSurface = Themes.getColorScheme().getOnSurface();
setBackground(new Background(new BackgroundFill(
Color.color(onSurface.getRed(), onSurface.getGreen(), onSurface.getBlue(), frac * 0.04),
CornerRadii.EMPTY, Insets.EMPTY)));
}
}

protected void updateChildren() {
Expand Down