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 Screencast.InputModel = class extends SDK.SDKModel { |
| 6 /** |
| 7 * @param {!SDK.Target} target |
| 8 */ |
| 9 constructor(target) { |
| 10 super(target); |
| 11 this._inputAgent = target.inputAgent(); |
| 12 /** @type {?number} */ |
| 13 this._activeTouchOffsetTop = null; |
| 14 this._activeTouchParams = null; |
| 15 } |
| 16 |
| 17 /** |
| 18 * @param {!Event} event |
| 19 */ |
| 20 emitKeyEvent(event) { |
| 21 var type; |
| 22 switch (event.type) { |
| 23 case 'keydown': |
| 24 type = 'keyDown'; |
| 25 break; |
| 26 case 'keyup': |
| 27 type = 'keyUp'; |
| 28 break; |
| 29 case 'keypress': |
| 30 type = 'char'; |
| 31 break; |
| 32 default: |
| 33 return; |
| 34 } |
| 35 |
| 36 var text = event.type === 'keypress' ? String.fromCharCode(event.charCode) :
undefined; |
| 37 this._inputAgent.invoke_dispatchKeyEvent({ |
| 38 type: type, |
| 39 modifiers: this._modifiersForEvent(event), |
| 40 timestamp: event.timeStamp / 1000, |
| 41 text: text, |
| 42 unmodifiedText: text ? text.toLowerCase() : undefined, |
| 43 keyIdentifier: event.keyIdentifier, |
| 44 code: event.code, |
| 45 key: event.key, |
| 46 windowsVirtualKeyCode: event.keyCode, |
| 47 nativeVirtualKeyCode: event.keyCode, |
| 48 autoRepeat: false, |
| 49 isKeypad: false, |
| 50 isSystemKey: false |
| 51 }); |
| 52 } |
| 53 |
| 54 /** |
| 55 * @param {!Event} event |
| 56 * @param {number} offsetTop |
| 57 * @param {number} zoom |
| 58 */ |
| 59 emitTouchFromMouseEvent(event, offsetTop, zoom) { |
| 60 var buttons = {0: 'none', 1: 'left', 2: 'middle', 3: 'right'}; |
| 61 var types = { |
| 62 'mousedown': 'mousePressed', |
| 63 'mouseup': 'mouseReleased', |
| 64 'mousemove': 'mouseMoved', |
| 65 'mousewheel': 'mouseWheel' |
| 66 }; |
| 67 if (!(event.type in types) || !(event.which in buttons)) |
| 68 return; |
| 69 if (event.type !== 'mousewheel' && buttons[event.which] === 'none') |
| 70 return; |
| 71 |
| 72 if (event.type === 'mousedown' || this._activeTouchOffsetTop === null) |
| 73 this._activeTouchOffsetTop = offsetTop; |
| 74 |
| 75 var x = Math.round(event.offsetX / zoom); |
| 76 var y = Math.round(event.offsetY / zoom); |
| 77 y = Math.round(y - this._activeTouchOffsetTop); |
| 78 var params = { |
| 79 type: types[event.type], |
| 80 x: x, |
| 81 y: y, |
| 82 modifiers: this._modifiersForEvent(event), |
| 83 timestamp: event.timeStamp / 1000, |
| 84 button: buttons[event.which], |
| 85 clickCount: 0 |
| 86 }; |
| 87 if (event.type === 'mousewheel') { |
| 88 params.deltaX = event.wheelDeltaX / zoom; |
| 89 params.deltaY = event.wheelDeltaY / zoom; |
| 90 } else { |
| 91 this._activeTouchParams = params; |
| 92 } |
| 93 if (event.type === 'mouseup') |
| 94 this._activeTouchOffsetTop = null; |
| 95 this._inputAgent.invoke_emulateTouchFromMouseEvent(params); |
| 96 } |
| 97 |
| 98 cancelTouch() { |
| 99 if (this._activeTouchOffsetTop !== null) { |
| 100 var params = this._activeTouchParams; |
| 101 this._activeTouchParams = null; |
| 102 params.type = 'mouseReleased'; |
| 103 this._inputAgent.invoke_emulateTouchFromMouseEvent(params); |
| 104 } |
| 105 } |
| 106 |
| 107 /** |
| 108 * @param {!Event} event |
| 109 * @return {number} |
| 110 */ |
| 111 _modifiersForEvent(event) { |
| 112 return (event.altKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.metaKey ? 4
: 0) | (event.shiftKey ? 8 : 0); |
| 113 } |
| 114 }; |
| 115 |
| 116 SDK.SDKModel.register(Screencast.InputModel, SDK.Target.Capability.Input, false)
; |
OLD | NEW |