From 6ca6429114d63d406972dae825b360dc952b6822 Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 25 Nov 2025 15:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=83=AF=E6=80=A7=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E9=94=99=E4=BD=8D=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#4857)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jackhuang/hmcl/ui/ScrollUtils.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/ScrollUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ScrollUtils.java index 72b2076dd..faf72d8dc 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/ScrollUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/ScrollUtils.java @@ -52,8 +52,8 @@ final class ScrollUtils { } } - private ScrollUtils() { - } + private static final double DEFAULT_SPEED = 1.0; + private static final double DEFAULT_TRACK_PAD_ADJUSTMENT = 7.0; /** * Determines if the given ScrollEvent comes from a trackpad. @@ -68,16 +68,10 @@ final class ScrollUtils { * @see ScrollEvent#getDeltaY() */ public static boolean isTrackPad(ScrollEvent event, ScrollDirection scrollDirection) { - switch (scrollDirection) { - case UP: - case DOWN: - return Math.abs(event.getDeltaY()) < 10; - case LEFT: - case RIGHT: - return Math.abs(event.getDeltaX()) < 10; - default: - return false; - } + return switch (scrollDirection) { + case UP, DOWN -> Math.abs(event.getDeltaY()) < 10; + case LEFT, RIGHT -> Math.abs(event.getDeltaX()) < 10; + }; } /** @@ -117,7 +111,7 @@ final class ScrollUtils { * default speed value of 1. */ public static void addSmoothScrolling(ScrollPane scrollPane) { - addSmoothScrolling(scrollPane, 1); + addSmoothScrolling(scrollPane, DEFAULT_SPEED); } /** @@ -126,7 +120,7 @@ final class ScrollUtils { * with a default trackPadAdjustment of 7. */ public static void addSmoothScrolling(ScrollPane scrollPane, double speed) { - addSmoothScrolling(scrollPane, speed, 7); + addSmoothScrolling(scrollPane, speed, DEFAULT_TRACK_PAD_ADJUSTMENT); } /** @@ -143,12 +137,12 @@ final class ScrollUtils { /// @author Glavo public static void addSmoothScrolling(VirtualFlow virtualFlow) { - addSmoothScrolling(virtualFlow, 1); + addSmoothScrolling(virtualFlow, DEFAULT_SPEED); } /// @author Glavo public static void addSmoothScrolling(VirtualFlow virtualFlow, double speed) { - addSmoothScrolling(virtualFlow, speed, 7); + addSmoothScrolling(virtualFlow, speed, DEFAULT_TRACK_PAD_ADJUSTMENT); } /// @author Glavo @@ -180,16 +174,16 @@ final class ScrollUtils { } }; if (scrollPane.getContent().getParent() != null) { - scrollPane.getContent().getParent().addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler); + scrollPane.getContent().getParent().addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler); scrollPane.getContent().getParent().addEventHandler(ScrollEvent.ANY, scrollHandler); } scrollPane.getContent().parentProperty().addListener((observable, oldValue, newValue) -> { if (oldValue != null) { - oldValue.removeEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler); + oldValue.removeEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler); oldValue.removeEventHandler(ScrollEvent.ANY, scrollHandler); } if (newValue != null) { - newValue.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler); + newValue.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler); newValue.addEventHandler(ScrollEvent.ANY, scrollHandler); } }); @@ -250,7 +244,7 @@ final class ScrollUtils { event.consume(); } }; - virtualFlow.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler); + virtualFlow.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler); virtualFlow.addEventFilter(ScrollEvent.ANY, scrollHandler); timeline.getKeyFrames().add(new KeyFrame(DURATION, event -> { @@ -278,4 +272,7 @@ final class ScrollUtils { })); timeline.setCycleCount(Animation.INDEFINITE); } + + private ScrollUtils() { + } }