Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/vr_shell/android_ui_gesture_target.h" | |
| 6 | |
| 7 #include "jni/MotionEventSynthesizer_jni.h" | |
| 8 | |
| 9 namespace vr_shell { | |
| 10 | |
| 11 AndroidUiGestureTarget::AndroidUiGestureTarget(jobject event_synthesizer, | |
| 12 float scroll_ratio) | |
| 13 : event_synthesizer_(event_synthesizer), scroll_ratio_(scroll_ratio) { | |
| 14 DCHECK(event_synthesizer_); | |
| 15 } | |
| 16 | |
| 17 AndroidUiGestureTarget::~AndroidUiGestureTarget() = default; | |
| 18 | |
| 19 void AndroidUiGestureTarget::DispatchWebInputEvent( | |
| 20 std::unique_ptr<blink::WebInputEvent> event) { | |
| 21 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 22 blink::WebMouseEvent* mouse; | |
| 23 blink::WebGestureEvent* gesture; | |
| 24 if (blink::WebInputEvent::isMouseEventType(event->type())) { | |
| 25 mouse = static_cast<blink::WebMouseEvent*>(event.get()); | |
| 26 } else { | |
| 27 gesture = static_cast<blink::WebGestureEvent*>(event.get()); | |
| 28 } | |
| 29 | |
| 30 switch (event->type()) { | |
| 31 case blink::WebGestureEvent::GestureScrollBegin: | |
| 32 DCHECK(gesture->data.scrollBegin.deltaHintUnits == | |
| 33 blink::WebGestureEvent::ScrollUnits::PrecisePixels); | |
| 34 scroll_x_ = (scroll_ratio_ * gesture->data.scrollBegin.deltaXHint); | |
| 35 scroll_y_ = (scroll_ratio_ * gesture->data.scrollBegin.deltaYHint); | |
| 36 SetPointer(env, 0, 0); | |
| 37 Inject(env, Action::ActionStart, gesture->timeStampSeconds()); | |
| 38 SetPointer(env, scroll_x_, scroll_y_); | |
| 39 // Send a move immediately so that we can't accidentally trigger a click. | |
| 40 Inject(env, Action::ActionMove, gesture->timeStampSeconds()); | |
| 41 break; | |
| 42 case blink::WebGestureEvent::GestureScrollEnd: | |
| 43 Inject(env, Action::ActionEnd, gesture->timeStampSeconds()); | |
| 44 break; | |
| 45 case blink::WebGestureEvent::GestureScrollUpdate: | |
| 46 scroll_x_ += (scroll_ratio_ * gesture->data.scrollUpdate.deltaX); | |
| 47 scroll_y_ += (scroll_ratio_ * gesture->data.scrollUpdate.deltaY); | |
| 48 SetPointer(env, scroll_x_, scroll_y_); | |
| 49 Inject(env, Action::ActionMove, gesture->timeStampSeconds()); | |
| 50 break; | |
| 51 case blink::WebGestureEvent::GestureTapDown: | |
| 52 SetPointer(env, gesture->x, gesture->y); | |
| 53 Inject(env, Action::ActionStart, gesture->timeStampSeconds()); | |
| 54 Inject(env, Action::ActionEnd, gesture->timeStampSeconds()); | |
| 55 break; | |
| 56 case blink::WebGestureEvent::GestureFlingCancel: | |
| 57 Inject(env, Action::ActionCancel, gesture->timeStampSeconds()); | |
| 58 break; | |
| 59 case blink::WebGestureEvent::GestureFlingStart: | |
| 60 // Flings are automatically generated for android UI. Ignore this input. | |
| 61 break; | |
| 62 case blink::WebMouseEvent::MouseEnter: | |
| 63 SetPointer(env, mouse->x, mouse->y); | |
| 64 Inject(env, Action::ActionHoverEnter, gesture->timeStampSeconds()); | |
| 65 break; | |
| 66 case blink::WebMouseEvent::MouseMove: | |
| 67 SetPointer(env, mouse->x, mouse->y); | |
| 68 Inject(env, Action::ActionHoverMove, gesture->timeStampSeconds()); | |
| 69 break; | |
| 70 case blink::WebMouseEvent::MouseLeave: | |
| 71 SetPointer(env, mouse->x, mouse->y); | |
| 72 Inject(env, Action::ActionHoverExit, gesture->timeStampSeconds()); | |
| 73 break; | |
| 74 default: | |
| 75 NOTREACHED(); | |
| 76 break; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void AndroidUiGestureTarget::SetPointer(JNIEnv* env, int x, int y) { | |
| 81 content::Java_MotionEventSynthesizer_setPointer(env, event_synthesizer_, 0, x, | |
| 82 y, 0); | |
| 83 } | |
| 84 | |
| 85 void AndroidUiGestureTarget::SetScrollDeltas(JNIEnv* env, | |
|
cjgrant
2017/02/16 15:29:34
nit: I think the style guide prefers packing as ma
mthiesse
2017/02/16 15:53:49
Apparently not, git cl format sets it to this.
| |
| 86 int x, | |
| 87 int y, | |
| 88 int dx, | |
| 89 int dy) { | |
| 90 content::Java_MotionEventSynthesizer_setScrollDeltas(env, event_synthesizer_, | |
| 91 x, y, dx, dy); | |
| 92 } | |
| 93 | |
| 94 void AndroidUiGestureTarget::Inject(JNIEnv* env, | |
| 95 Action action, | |
| 96 double time_in_seconds) { | |
| 97 content::Java_MotionEventSynthesizer_inject( | |
| 98 env, event_synthesizer_, static_cast<int>(action), 1, | |
| 99 static_cast<int64_t>(time_in_seconds * 1000.0)); | |
| 100 } | |
| 101 | |
| 102 } // namespace vr_shell | |
| OLD | NEW |