Skip to content

Commit 4cc3679

Browse files
authored
Use wheel event type instead of deprected mousewheel (#9971)
Fixes the velocity detection problem mentioned in #9671 and #8806. This is a bit more direct than https://gwt-review.googlesource.com/c/gwt/+/3171 as it removes the deprecated events where possible. Tested in FF and Chrome using a mouse. Fixes #8009
1 parent 97934f7 commit 4cc3679

File tree

9 files changed

+52
-63
lines changed

9 files changed

+52
-63
lines changed

user/src/com/google/gwt/dom/client/BrowserEvents.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public class BrowserEvents {
5353
public static final String MOUSEOUT = "mouseout";
5454
public static final String MOUSEOVER = "mouseover";
5555
public static final String MOUSEUP = "mouseup";
56+
@Deprecated
5657
public static final String MOUSEWHEEL = "mousewheel";
58+
public static final String WHEEL = "wheel";
5759
public static final String PROGRESS = "progress";
5860
public static final String SCROLL = "scroll";
5961
public static final String TOUCHCANCEL = "touchcancel";

user/src/com/google/gwt/dom/client/DOMImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public native boolean eventGetMetaKey(NativeEvent evt) /*-{
138138
return !!evt.metaKey;
139139
}-*/;
140140

141-
public abstract int eventGetMouseWheelVelocityY(NativeEvent evt);
141+
public int eventGetMouseWheelVelocityY(NativeEvent evt) {
142+
return (int) Math.signum(evt.getDeltaY());
143+
}
142144

143145
public abstract EventTarget eventGetRelatedTarget(NativeEvent nativeEvent);
144146

user/src/com/google/gwt/dom/client/DOMImplMozilla.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ public NativeEvent createKeyPressEvent(Document doc, boolean ctrlKey,
124124
shiftKey, metaKey, 0, charCode);
125125
}
126126

127-
@Override
128-
public native int eventGetMouseWheelVelocityY(NativeEvent evt) /*-{
129-
return evt.detail || 0;
130-
}-*/;
131-
132127
@Override
133128
public native EventTarget eventGetRelatedTarget(NativeEvent evt) /*-{
134129
// Hack around Mozilla bug 497780 (relatedTarget sometimes returns XUL

user/src/com/google/gwt/dom/client/DOMImplStandardBase.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,6 @@ public native EventTarget eventGetCurrentTarget(NativeEvent event) /*-{
204204
return event.currentTarget || $wnd;
205205
}-*/;
206206

207-
@Override
208-
public native int eventGetMouseWheelVelocityY(NativeEvent evt) /*-{
209-
return Math.round(-evt.wheelDelta / 40) || 0;
210-
}-*/;
211-
212207
@Override
213208
public int getAbsoluteLeft(Element elem) {
214209
ClientRect rect = getBoundingClientRect(elem);

user/src/com/google/gwt/dom/client/NativeEvent.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,40 @@ public final boolean getMetaKey() {
158158
}
159159

160160
/**
161-
* Gets the velocity of the mouse wheel associated with the event along the Y
161+
* Gets the sign of the velocity of the mouse wheel associated with the event along the Y
162162
* axis.
163163
* <p>
164-
* The velocity of the event is an artificial measurement for relative
165-
* comparisons of wheel activity. It is affected by some non-browser factors,
166-
* including choice of input hardware and mouse acceleration settings. The
167-
* sign of the velocity measurement agrees with the screen coordinate system;
168-
* negative values are towards the origin and positive values are away from
169-
* the origin. Standard scrolling speed is approximately ten units per event.
164+
* In previous versions of GWT this returned the velocity normalized to a small integer.
165+
* Unfortunately for some devices such normalization rounded non-trivial velocities to 0.
166+
* To maintain compatibility with that implementation, this still returns an integer,
167+
* but using the sign function
170168
* </p>
171169
*
172-
* @return The velocity of the mouse wheel.
170+
* @return The velocity of the mouse wheel: -1 for scrolling up, 1 for scrolling down,
171+
* 0 if not scrolled at all
172+
* @deprecated use getDeltaY() instead
173173
*/
174+
@Deprecated
174175
public final int getMouseWheelVelocityY() {
175176
return DOMImpl.impl.eventGetMouseWheelVelocityY(this);
176177
}
177178

179+
/**
180+
* Gets the native velocity of the mouse wheel in Y direction.
181+
* @return The velocity of the mouse wheel
182+
*/
183+
public final native double getDeltaY() /*-{
184+
return this.deltaY;
185+
}-*/;
186+
187+
/**
188+
* Gets the native velocity of the mouse wheel in X direction.
189+
* @return The velocity of the mouse wheel
190+
*/
191+
public final native double getDeltaX() /*-{
192+
return this.deltaX;
193+
}-*/;
194+
178195
/**
179196
* Gets the related target for this event.
180197
*

user/src/com/google/gwt/event/dom/client/MouseWheelEvent.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ public class MouseWheelEvent extends MouseEvent<MouseWheelHandler> {
2727
* this event.
2828
*/
2929
private static final Type<MouseWheelHandler> TYPE = new Type<MouseWheelHandler>(
30-
BrowserEvents.MOUSEWHEEL, new MouseWheelEvent());
31-
32-
static {
33-
/**
34-
* Hidden type used to ensure DOMMouseScroll gets registered in the type map.
35-
* This is the special name used on Mozilla browsers for what everyone else
36-
* calls 'mousewheel'.
37-
*/
38-
new Type<MouseWheelHandler>("DOMMouseScroll", new MouseWheelEvent());
39-
}
30+
BrowserEvents.WHEEL, new MouseWheelEvent());
4031

4132
/**
4233
* Gets the event type associated with mouse wheel events.
@@ -61,18 +52,30 @@ public final Type<MouseWheelHandler> getAssociatedType() {
6152
}
6253

6354
/**
64-
* Get the change in the mouse wheel position along the Y-axis; negative if
65-
* the mouse wheel is moving north (toward the top of the screen) or positive
55+
* Get the sign of the change in the mouse wheel position along the Y-axis; -1 if
56+
* the mouse wheel is moving north (toward the top of the screen) or 1
6657
* if the mouse wheel is moving south (toward the bottom of the screen).
6758
*
68-
* Note that delta values are not normalized across browsers or OSes.
69-
*
70-
* @return the delta of the mouse wheel along the y axis
59+
* @return the sign of the delta of the mouse wheel along the y axis
60+
* @deprecated use getNativeDeltaY() instead
7161
*/
62+
@Deprecated
7263
public int getDeltaY() {
7364
return getNativeEvent().getMouseWheelVelocityY();
7465
}
7566

67+
/**
68+
* Get the change in the mouse wheel position along the Y-axis; -1 if
69+
* the mouse wheel is moving north (toward the top of the screen) or 1
70+
* if the mouse wheel is moving south (toward the bottom of the screen).
71+
* Note that the return values are not normalized for browsers and OSs.
72+
*
73+
* @return the sign of the delta of the mouse wheel along the y axis
74+
*/
75+
public double getNativeDeltaY() {
76+
return getNativeEvent().getDeltaY();
77+
}
78+
7679
/**
7780
* Convenience method that returns <code>true</code> if {@link #getDeltaY()}
7881
* is a negative value (ie, the velocity is directed toward the top of the
@@ -81,7 +84,7 @@ public int getDeltaY() {
8184
* @return true if the velocity is directed toward the top of the screen
8285
*/
8386
public boolean isNorth() {
84-
return getDeltaY() < 0;
87+
return getNativeDeltaY() < 0;
8588
}
8689

8790
/**
@@ -92,7 +95,7 @@ public boolean isNorth() {
9295
* @return true if the velocity is directed toward the bottom of the screen
9396
*/
9497
public boolean isSouth() {
95-
return getDeltaY() > 0;
98+
return getNativeDeltaY() > 0;
9699
}
97100

98101
@Override

user/src/com/google/gwt/user/client/impl/DOMImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public native int eventGetTypeInt(String eventType) /*-{
102102
case "scroll": return 0x04000;
103103
case "error": return 0x10000;
104104
case "mousewheel": return 0x20000;
105-
case "DOMMouseScroll": return 0x20000;
105+
case "wheel": return 0x20000;
106106
case "contextmenu": return 0x40000;
107107
case "paste": return 0x80000;
108108
case "touchstart": return 0x100000;

user/src/com/google/gwt/user/client/impl/DOMImplMozilla.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,11 @@
1515
*/
1616
package com.google.gwt.user.client.impl;
1717

18-
import com.google.gwt.dom.client.Element;
19-
2018
/**
2119
* Mozilla implementation of StandardBrowser.
2220
*/
2321
class DOMImplMozilla extends DOMImplStandard {
2422

25-
static {
26-
addMozillaCaptureEventDispatchers();
27-
}
28-
29-
@SuppressWarnings("deprecation")
30-
private static native void addMozillaCaptureEventDispatchers() /*-{
31-
@com.google.gwt.user.client.impl.DOMImplStandard::captureEventDispatchers['DOMMouseScroll'] =
32-
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchCapturedMouseEvent(*);
33-
}-*/;
34-
35-
@Override
36-
public void sinkEvents(Element elem, int bits) {
37-
super.sinkEvents(elem, bits);
38-
sinkEventsMozilla(elem, bits);
39-
}
40-
41-
@SuppressWarnings("deprecation")
42-
public native void sinkEventsMozilla(Element elem, int bits) /*-{
43-
if (bits & 0x20000) {
44-
elem.addEventListener('DOMMouseScroll', @com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent, false);
45-
}
46-
}-*/;
47-
4823
@Override
4924
protected void initEventSystem() {
5025
super.initEventSystem();

user/src/com/google/gwt/user/client/impl/DOMImplStandard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ protected native void sinkEventsImpl(Element elem, int bits) /*-{
292292
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchUnhandledEvent : null;
293293
if (chMask & 0x10000) elem.onerror = (bits & 0x10000) ?
294294
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent : null;
295-
if (chMask & 0x20000) elem.onmousewheel = (bits & 0x20000) ?
295+
if (chMask & 0x20000) elem.onwheel = (bits & 0x20000) ?
296296
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent : null;
297297
if (chMask & 0x40000) elem.oncontextmenu = (bits & 0x40000) ?
298298
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent : null;

0 commit comments

Comments
 (0)