Skip to content

Commit 6ca6429

Browse files
authored
修复惯性滚动可能导致弹出菜单错位的问题 (#4857)
1 parent 4a3d330 commit 6ca6429

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/ui/ScrollUtils.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public int intDirection() {
5252
}
5353
}
5454

55-
private ScrollUtils() {
56-
}
55+
private static final double DEFAULT_SPEED = 1.0;
56+
private static final double DEFAULT_TRACK_PAD_ADJUSTMENT = 7.0;
5757

5858
/**
5959
* Determines if the given ScrollEvent comes from a trackpad.
@@ -68,16 +68,10 @@ private ScrollUtils() {
6868
* @see ScrollEvent#getDeltaY()
6969
*/
7070
public static boolean isTrackPad(ScrollEvent event, ScrollDirection scrollDirection) {
71-
switch (scrollDirection) {
72-
case UP:
73-
case DOWN:
74-
return Math.abs(event.getDeltaY()) < 10;
75-
case LEFT:
76-
case RIGHT:
77-
return Math.abs(event.getDeltaX()) < 10;
78-
default:
79-
return false;
80-
}
71+
return switch (scrollDirection) {
72+
case UP, DOWN -> Math.abs(event.getDeltaY()) < 10;
73+
case LEFT, RIGHT -> Math.abs(event.getDeltaX()) < 10;
74+
};
8175
}
8276

8377
/**
@@ -117,7 +111,7 @@ public static ScrollDirection determineScrollDirection(ScrollEvent event) {
117111
* default speed value of 1.
118112
*/
119113
public static void addSmoothScrolling(ScrollPane scrollPane) {
120-
addSmoothScrolling(scrollPane, 1);
114+
addSmoothScrolling(scrollPane, DEFAULT_SPEED);
121115
}
122116

123117
/**
@@ -126,7 +120,7 @@ public static void addSmoothScrolling(ScrollPane scrollPane) {
126120
* with a default trackPadAdjustment of 7.
127121
*/
128122
public static void addSmoothScrolling(ScrollPane scrollPane, double speed) {
129-
addSmoothScrolling(scrollPane, speed, 7);
123+
addSmoothScrolling(scrollPane, speed, DEFAULT_TRACK_PAD_ADJUSTMENT);
130124
}
131125

132126
/**
@@ -143,12 +137,12 @@ public static void addSmoothScrolling(ScrollPane scrollPane, double speed, doubl
143137

144138
/// @author Glavo
145139
public static void addSmoothScrolling(VirtualFlow<?> virtualFlow) {
146-
addSmoothScrolling(virtualFlow, 1);
140+
addSmoothScrolling(virtualFlow, DEFAULT_SPEED);
147141
}
148142

149143
/// @author Glavo
150144
public static void addSmoothScrolling(VirtualFlow<?> virtualFlow, double speed) {
151-
addSmoothScrolling(virtualFlow, speed, 7);
145+
addSmoothScrolling(virtualFlow, speed, DEFAULT_TRACK_PAD_ADJUSTMENT);
152146
}
153147

154148
/// @author Glavo
@@ -180,16 +174,16 @@ private static void smoothScroll(ScrollPane scrollPane, double speed, double tra
180174
}
181175
};
182176
if (scrollPane.getContent().getParent() != null) {
183-
scrollPane.getContent().getParent().addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler);
177+
scrollPane.getContent().getParent().addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler);
184178
scrollPane.getContent().getParent().addEventHandler(ScrollEvent.ANY, scrollHandler);
185179
}
186180
scrollPane.getContent().parentProperty().addListener((observable, oldValue, newValue) -> {
187181
if (oldValue != null) {
188-
oldValue.removeEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler);
182+
oldValue.removeEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler);
189183
oldValue.removeEventHandler(ScrollEvent.ANY, scrollHandler);
190184
}
191185
if (newValue != null) {
192-
newValue.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler);
186+
newValue.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler);
193187
newValue.addEventHandler(ScrollEvent.ANY, scrollHandler);
194188
}
195189
});
@@ -250,7 +244,7 @@ private static void smoothScroll(VirtualFlow<?> virtualFlow, double speed, doubl
250244
event.consume();
251245
}
252246
};
253-
virtualFlow.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseHandler);
247+
virtualFlow.addEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler);
254248
virtualFlow.addEventFilter(ScrollEvent.ANY, scrollHandler);
255249

256250
timeline.getKeyFrames().add(new KeyFrame(DURATION, event -> {
@@ -278,4 +272,7 @@ private static void smoothScroll(VirtualFlow<?> virtualFlow, double speed, doubl
278272
}));
279273
timeline.setCycleCount(Animation.INDEFINITE);
280274
}
275+
276+
private ScrollUtils() {
277+
}
281278
}

0 commit comments

Comments
 (0)