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._deferredMouseEventOffsetTop = null; | |
14 this._deferredMouseEventParams = null; | |
15 } | |
16 | |
17 /** | |
18 * @param {!Event} event | |
19 */ | |
20 dispatchKeyEvent(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 dispatchTouchEventFromMouse(event, offsetTop, zoom) { | |
60 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.
| |
61 const 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._deferredMouseEventOffsetTop === null ) | |
73 this._deferredMouseEventOffsetTop = offsetTop; | |
74 | |
75 var modifiers = | |
76 (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.
| |
77 | |
78 var x = Math.round(event.offsetX / zoom); | |
79 var y = Math.round(event.offsetY / zoom); | |
80 y = Math.round(y - this._deferredMouseEventOffsetTop); | |
81 var params = { | |
82 type: types[event.type], | |
83 x: x, | |
84 y: y, | |
85 modifiers: modifiers, | |
86 timestamp: event.timeStamp / 1000, | |
87 button: buttons[event.which], | |
88 clickCount: 0 | |
89 }; | |
90 if (event.type === 'mousewheel') { | |
91 params.deltaX = event.wheelDeltaX / zoom; | |
92 params.deltaY = event.wheelDeltaY / zoom; | |
93 } else { | |
94 this._deferredMouseEventParams = params; | |
95 } | |
96 if (event.type === 'mouseup') | |
97 this._deferredMouseEventOffsetTop = null; | |
98 this._inputAgent.invoke_emulateTouchFromMouseEvent(params); | |
99 } | |
100 | |
101 dispatchTouchOnBlur() { | |
lushnikov
2017/04/12 01:27:54
cancelTouch
dgozman
2017/04/12 18:57:43
Done.
| |
102 if (this._deferredMouseEventOffsetTop !== null) { | |
103 var params = this._deferredMouseEventParams; | |
104 this._deferredMouseEventParams = null; | |
105 params.type = 'mouseReleased'; | |
106 this._inputAgent.invoke_emulateTouchFromMouseEvent(params); | |
107 } | |
108 } | |
109 | |
110 /** | |
111 * @param {!Event} event | |
112 * @return {number} | |
113 */ | |
114 _modifiersForEvent(event) { | |
115 var modifiers = 0; | |
116 if (event.altKey) | |
117 modifiers = 1; | |
118 if (event.ctrlKey) | |
119 modifiers += 2; | |
120 if (event.metaKey) | |
121 modifiers += 4; | |
122 if (event.shiftKey) | |
123 modifiers += 8; | |
124 return modifiers; | |
125 } | |
126 }; | |
127 | |
128 SDK.SDKModel.register(Screencast.InputModel, SDK.Target.Capability.Input, false) ; | |
OLD | NEW |