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

Unified Diff: third_party/WebKit/Source/devtools/front_end/screencast/InputModel.js

Issue 2815003002: [DevTools] Split InputModel out of screencast (Closed)
Patch Set: Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/screencast/InputModel.js
diff --git a/third_party/WebKit/Source/devtools/front_end/screencast/InputModel.js b/third_party/WebKit/Source/devtools/front_end/screencast/InputModel.js
new file mode 100644
index 0000000000000000000000000000000000000000..0c2f1f0f51ac9b7db77988f2bb238445944c7c38
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/front_end/screencast/InputModel.js
@@ -0,0 +1,128 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+Screencast.InputModel = class extends SDK.SDKModel {
+ /**
+ * @param {!SDK.Target} target
+ */
+ constructor(target) {
+ super(target);
+ this._inputAgent = target.inputAgent();
+ /** @type {?number} */
+ this._deferredMouseEventOffsetTop = null;
+ this._deferredMouseEventParams = null;
+ }
+
+ /**
+ * @param {!Event} event
+ */
+ dispatchKeyEvent(event) {
+ var type;
+ switch (event.type) {
+ case 'keydown':
+ type = 'keyDown';
+ break;
+ case 'keyup':
+ type = 'keyUp';
+ break;
+ case 'keypress':
+ type = 'char';
+ break;
+ default:
+ return;
+ }
+
+ var text = event.type === 'keypress' ? String.fromCharCode(event.charCode) : undefined;
+ this._inputAgent.invoke_dispatchKeyEvent({
+ type: type,
+ modifiers: this._modifiersForEvent(event),
+ timestamp: event.timeStamp / 1000,
+ text: text,
+ unmodifiedText: text ? text.toLowerCase() : undefined,
+ keyIdentifier: event.keyIdentifier,
+ code: event.code,
+ key: event.key,
+ windowsVirtualKeyCode: event.keyCode,
+ nativeVirtualKeyCode: event.keyCode,
+ autoRepeat: false,
+ isKeypad: false,
+ isSystemKey: false
+ });
+ }
+
+ /**
+ * @param {!Event} event
+ * @param {number} offsetTop
+ * @param {number} zoom
+ */
+ dispatchTouchEventFromMouse(event, offsetTop, zoom) {
+ const buttons = {0: 'none', 1: 'left', 2: 'middle', 3: 'right'};
lushnikov 2017/04/12 01:27:54 s/const/var
dgozman 2017/04/12 18:57:43 Done.
+ const types = {
+ 'mousedown': 'mousePressed',
+ 'mouseup': 'mouseReleased',
+ 'mousemove': 'mouseMoved',
+ 'mousewheel': 'mouseWheel'
+ };
+ if (!(event.type in types) || !(event.which in buttons))
+ return;
+ if (event.type !== 'mousewheel' && buttons[event.which] === 'none')
+ return;
+
+ if (event.type === 'mousedown' || this._deferredMouseEventOffsetTop === null)
+ this._deferredMouseEventOffsetTop = offsetTop;
+
+ var modifiers =
+ (event.altKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.metaKey ? 4 : 0) | (event.shiftKey ? 8 : 0);
lushnikov 2017/04/12 01:27:54 you can use _modifiersForEvent!
dgozman 2017/04/12 18:57:43 Done.
+
+ var x = Math.round(event.offsetX / zoom);
+ var y = Math.round(event.offsetY / zoom);
+ y = Math.round(y - this._deferredMouseEventOffsetTop);
+ var params = {
+ type: types[event.type],
+ x: x,
+ y: y,
+ modifiers: modifiers,
+ timestamp: event.timeStamp / 1000,
+ button: buttons[event.which],
+ clickCount: 0
+ };
+ if (event.type === 'mousewheel') {
+ params.deltaX = event.wheelDeltaX / zoom;
+ params.deltaY = event.wheelDeltaY / zoom;
+ } else {
+ this._deferredMouseEventParams = params;
+ }
+ if (event.type === 'mouseup')
+ this._deferredMouseEventOffsetTop = null;
+ this._inputAgent.invoke_emulateTouchFromMouseEvent(params);
+ }
+
+ dispatchTouchOnBlur() {
lushnikov 2017/04/12 01:27:54 cancelTouch
dgozman 2017/04/12 18:57:43 Done.
+ if (this._deferredMouseEventOffsetTop !== null) {
+ var params = this._deferredMouseEventParams;
+ this._deferredMouseEventParams = null;
+ params.type = 'mouseReleased';
+ this._inputAgent.invoke_emulateTouchFromMouseEvent(params);
+ }
+ }
+
+ /**
+ * @param {!Event} event
+ * @return {number}
+ */
+ _modifiersForEvent(event) {
+ var modifiers = 0;
+ if (event.altKey)
+ modifiers = 1;
+ if (event.ctrlKey)
+ modifiers += 2;
+ if (event.metaKey)
+ modifiers += 4;
+ if (event.shiftKey)
+ modifiers += 8;
+ return modifiers;
+ }
+};
+
+SDK.SDKModel.register(Screencast.InputModel, SDK.Target.Capability.Input, false);

Powered by Google App Engine
This is Rietveld 408576698