Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: chrome/browser/resources/pdf/gesture_detector.js

Issue 2855953003: Handle long press in PDF documents. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/browser/resources/pdf/pdf.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * A class that listens for touch events and produces events when these 8 * A class that listens for touch events and produces events when these
9 * touches form gestures (e.g. pinching). 9 * touches form gestures (e.g. pinching).
10 */ 10 */
11 class GestureDetector { 11 class GestureDetector {
12 /** 12 /**
13 * Constructs a GestureDetector. 13 * Constructs a GestureDetector.
14 * @param {!Element} element The element to monitor for touch gestures. 14 * @param {!Element} element The element to monitor for touch gestures.
15 */ 15 */
16 constructor(element) { 16 constructor(element) {
17 this.element_ = element; 17 this.element_ = element;
18 18
19 this.element_.addEventListener( 19 this.element_.addEventListener(
20 'touchstart', this.onTouchStart_.bind(this), { passive: false }); 20 'touchstart', this.onTouchStart_.bind(this), { passive: false });
Kevin McNee 2017/05/02 23:18:18 Since we're no longer calling preventDefault in th
dsinclair 2017/05/03 14:18:40 Yea, I'm going to use your Cl for this bit, I'd do
21 this.element_.addEventListener( 21 this.element_.addEventListener(
22 'touchmove', this.onTouch_.bind(this), { passive: true }); 22 'touchmove', this.onTouch_.bind(this), { passive: false });
23 this.element_.addEventListener( 23 this.element_.addEventListener(
24 'touchend', this.onTouch_.bind(this), { passive: true }); 24 'touchend', this.onTouch_.bind(this), { passive: true });
25 this.element_.addEventListener( 25 this.element_.addEventListener(
26 'touchcancel', this.onTouch_.bind(this), { passive: true }); 26 'touchcancel', this.onTouch_.bind(this), { passive: true });
27 27
28 this.pinchStartEvent_ = null; 28 this.touchStartEvent_ = null;
29 this.lastEvent_ = null; 29 this.lastEvent_ = null;
30 30
31 this.listeners_ = new Map([ 31 this.listeners_ = new Map([
32 ['pinchstart', []], 32 ['pinchstart', []],
33 ['pinchupdate', []], 33 ['pinchupdate', []],
34 ['pinchend', []] 34 ['pinchend', []]
35 ]); 35 ]);
36 } 36 }
37 37
38 /** 38 /**
39 * Add a |listener| to be notified of |type| events. 39 * Add a |listener| to be notified of |type| events.
40 * @param {string} type The event type to be notified for. 40 * @param {string} type The event type to be notified for.
41 * @param {Function} listener The callback. 41 * @param {Function} listener The callback.
42 */ 42 */
43 addEventListener(type, listener) { 43 addEventListener(type, listener) {
44 if (this.listeners_.has(type)) { 44 if (this.listeners_.has(type)) {
45 this.listeners_.get(type).push(listener); 45 this.listeners_.get(type).push(listener);
46 } 46 }
47 } 47 }
48 48
49 /** 49 /**
50 * Returns true if the previous touch start was a two finger touch.
51 * @return {!bool} True if touch start was two finger.
Kevin McNee 2017/05/02 23:18:18 The ! is redundant since booleans are non-nullable
dsinclair 2017/05/03 14:18:40 Done.
52 */
53 isTwoFingerTouch() {
54 return this.touchStartEvent_ && this.touchStartEvent_.touches.length == 2;
Kevin McNee 2017/05/02 23:18:19 I notice that |touchStartEvent_| is never cleared
dsinclair 2017/05/03 14:18:40 Done.
55 }
56
57 /**
50 * Call the relevant listeners with the given |pinchEvent|. 58 * Call the relevant listeners with the given |pinchEvent|.
51 * @private 59 * @private
52 * @param {!Object} pinchEvent The event to notify the listeners of. 60 * @param {!Object} pinchEvent The event to notify the listeners of.
53 */ 61 */
54 notify_(pinchEvent) { 62 notify_(pinchEvent) {
55 let listeners = this.listeners_.get(pinchEvent.type); 63 let listeners = this.listeners_.get(pinchEvent.type);
56 64
57 for (let l of listeners) 65 for (let l of listeners)
58 l(pinchEvent); 66 l(pinchEvent);
59 } 67 }
60 68
61 /** 69 /**
62 * The callback for touchstart events on the element. 70 * The callback for touchstart events on the element.
63 * @private 71 * @private
64 * @param {!TouchEvent} event Touch event on the element. 72 * @param {!TouchEvent} event Touch event on the element.
65 */ 73 */
66 onTouchStart_(event) { 74 onTouchStart_(event) {
67 // We must preventDefault if there is a two finger touch. By doing so 75 this.touchStartEvent_ = event;
68 // native pinch-zoom does not interfere with our way of handling the event. 76
69 if (event.touches.length == 2) { 77 if (this.isTwoFingerTouch()) {
70 event.preventDefault();
71 this.pinchStartEvent_ = event;
72 this.lastEvent_ = event; 78 this.lastEvent_ = event;
73 this.notify_({ 79 this.notify_({
74 type: 'pinchstart', 80 type: 'pinchstart',
75 center: GestureDetector.center_(event) 81 center: GestureDetector.center_(event)
76 }); 82 });
77 } 83 }
78 } 84 }
79 85
80 /** 86 /**
81 * The callback for touch move, end, and cancel events on the element. 87 * The callback for touch move, end, and cancel events on the element.
82 * @private 88 * @private
83 * @param {!TouchEvent} event Touch event on the element. 89 * @param {!TouchEvent} event Touch event on the element.
84 */ 90 */
85 onTouch_(event) { 91 onTouch_(event) {
86 if (!this.pinchStartEvent_) 92 // Missing a lastEvent_ means we either haven't got a touchstart or we've
93 // finished handling the pinch already.
94 if (!this.lastEvent_ || !this.isTwoFingerTouch())
87 return; 95 return;
88 96
97 // Prevent default the touchmove to block the native pinch-zoom.
98 if (event.type == "touchmove")
99 event.preventDefault();
dsinclair 2017/05/02 19:44:05 This is very similar to https://codereview.chromiu
100
89 // Check if the pinch ends with the current event. 101 // Check if the pinch ends with the current event.
90 if (event.touches.length < 2 || 102 if (event.touches.length < 2 ||
91 this.lastEvent_.touches.length !== event.touches.length) { 103 this.lastEvent_.touches.length !== event.touches.length) {
92 let startScaleRatio = GestureDetector.pinchScaleRatio_( 104 let startScaleRatio = GestureDetector.pinchScaleRatio_(
93 this.lastEvent_, this.pinchStartEvent_); 105 this.lastEvent_, this.touchStartEvent_);
94 let center = GestureDetector.center_(this.lastEvent_); 106 let center = GestureDetector.center_(this.lastEvent_);
95 let endEvent = { 107 let endEvent = {
96 type: 'pinchend', 108 type: 'pinchend',
97 startScaleRatio: startScaleRatio, 109 startScaleRatio: startScaleRatio,
98 center: center 110 center: center
99 }; 111 };
100 this.pinchStartEvent_ = null; 112
101 this.lastEvent_ = null; 113 this.lastEvent_ = null;
102 this.notify_(endEvent); 114 this.notify_(endEvent);
103 return; 115 return;
104 } 116 }
105 117
106 let scaleRatio = GestureDetector.pinchScaleRatio_(event, this.lastEvent_); 118 let scaleRatio = GestureDetector.pinchScaleRatio_(event, this.lastEvent_);
107 let startScaleRatio = GestureDetector.pinchScaleRatio_( 119 let startScaleRatio = GestureDetector.pinchScaleRatio_(
108 event, this.pinchStartEvent_); 120 event, this.touchStartEvent_);
109 let center = GestureDetector.center_(event); 121 let center = GestureDetector.center_(event);
110 this.notify_({ 122 this.notify_({
111 type: 'pinchupdate', 123 type: 'pinchupdate',
112 scaleRatio: scaleRatio, 124 scaleRatio: scaleRatio,
113 direction: scaleRatio > 1.0 ? 'in' : 'out', 125 direction: scaleRatio > 1.0 ? 'in' : 'out',
114 startScaleRatio: startScaleRatio, 126 startScaleRatio: startScaleRatio,
115 center: center 127 center: center
116 }); 128 });
117 129
118 this.lastEvent_ = event; 130 this.lastEvent_ = event;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 */ 167 */
156 static center_(event) { 168 static center_(event) {
157 let touch1 = event.touches[0]; 169 let touch1 = event.touches[0];
158 let touch2 = event.touches[1]; 170 let touch2 = event.touches[1];
159 return { 171 return {
160 x: (touch1.clientX + touch2.clientX) / 2, 172 x: (touch1.clientX + touch2.clientX) / 2,
161 y: (touch1.clientY + touch2.clientY) / 2 173 y: (touch1.clientY + touch2.clientY) / 2
162 }; 174 };
163 } 175 }
164 }; 176 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/browser/resources/pdf/pdf.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698