• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17(function() {
18// TODO(b/375326606): consider contribution on
19// upstream(https://github.com/xtermjs/xterm.js/issues/3727)
20let convertTouchToMouse = false;
21function touchHandler(event) {
22  const contextmenuByTouch =
23      event.type === 'contextmenu' && event.pointerType === 'touch';
24  // Only proceed for long touches (contextmenu) or when converting touch to
25  // mouse
26  if (!contextmenuByTouch && !convertTouchToMouse) {
27    return;
28  }
29
30  const touch = event.changedTouches ? event.changedTouches[0] : event;
31
32  let type;
33  switch (event.type) {
34    case 'contextmenu':
35      convertTouchToMouse = true;
36      type = 'mousedown';
37      break;
38    case 'touchmove':
39      type = 'mousemove';
40      break;
41    case 'touchend':
42      convertTouchToMouse = false;
43      type = 'mouseup';
44      break;
45    default:
46      convertTouchToMouse = false;
47      return;
48  }
49
50  const simulatedEvent = new MouseEvent(type, {
51    bubbles: true,
52    cancelable: true,
53    view: window,
54    detail: 1,
55    screenX: touch.screenX,
56    screenY: touch.screenY,
57    clientX: touch.clientX,
58    clientY: touch.clientY,
59    button: 0,  // left click
60  });
61
62  touch.target.dispatchEvent(simulatedEvent);
63
64  // Prevent default behavior for touch events (except contextmenu)
65  if (event.type !== 'contextmenu') {
66    event.preventDefault();
67    event.stopPropagation();
68  }
69}
70const eventOptions = {
71  capture: true,
72  passive: false
73};
74document.addEventListener('touchstart', touchHandler, eventOptions);
75document.addEventListener('touchmove', touchHandler, eventOptions);
76document.addEventListener('touchend', touchHandler, eventOptions);
77document.addEventListener('touchcancel', touchHandler, eventOptions);
78document.addEventListener('contextmenu', touchHandler, eventOptions);
79})();