-
Notifications
You must be signed in to change notification settings - Fork 869
悬停效果改为在 JFXRippler 中实现 #5757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
悬停效果改为在 JFXRippler 中实现 #5757
Changes from all commits
3fc11b9
b863bca
3de4af0
b3d2410
dbde4e4
74cca51
1c3999f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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, | ||
|
|
@@ -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)); | ||
|
|
||
| coverAnimation = new Timeline(new KeyFrame(Motion.SHORT4, | ||
| new KeyValue(hoverOverlay.opacityProperty(), isEntered ? 1 : 0, isEntered ? Motion.EASE_IN : Motion.EASE_OUT))); | ||
|
CiiLu marked this conversation as resolved.
|
||
| coverAnimation.play(); | ||
| }; | ||
|
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); | ||
| } | ||
|
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); | ||
| } | ||
|
|
||
|
|
@@ -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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| /*************************************************************************** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
JFXRipplerclass now has a direct dependency onorg.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 dedicatedhoverFillproperty that can be configured from the outside.