InputSource.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.WheelSource = exports.PointerSource = exports.KeySource = exports.NoneSource = exports.SourceType = void 0;
  20. const protocol_js_1 = require("../../../protocol/protocol.js");
  21. exports.SourceType = protocol_js_1.Input.SourceActionsType;
  22. class NoneSource {
  23. type = exports.SourceType.None;
  24. }
  25. exports.NoneSource = NoneSource;
  26. class KeySource {
  27. type = exports.SourceType.Key;
  28. pressed = new Set();
  29. // This is a bitfield that matches the modifiers parameter of
  30. // https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchKeyEvent
  31. #modifiers = 0;
  32. get modifiers() {
  33. return this.#modifiers;
  34. }
  35. get alt() {
  36. return (this.#modifiers & 1) === 1;
  37. }
  38. set alt(value) {
  39. this.#setModifier(value, 1);
  40. }
  41. get ctrl() {
  42. return (this.#modifiers & 2) === 2;
  43. }
  44. set ctrl(value) {
  45. this.#setModifier(value, 2);
  46. }
  47. get meta() {
  48. return (this.#modifiers & 4) === 4;
  49. }
  50. set meta(value) {
  51. this.#setModifier(value, 4);
  52. }
  53. get shift() {
  54. return (this.#modifiers & 8) === 8;
  55. }
  56. set shift(value) {
  57. this.#setModifier(value, 8);
  58. }
  59. #setModifier(value, bit) {
  60. if (value) {
  61. this.#modifiers |= bit;
  62. }
  63. else {
  64. this.#modifiers &= ~bit;
  65. }
  66. }
  67. }
  68. exports.KeySource = KeySource;
  69. class PointerSource {
  70. type = exports.SourceType.Pointer;
  71. subtype;
  72. pointerId;
  73. pressed = new Set();
  74. x = 0;
  75. y = 0;
  76. constructor(id, subtype) {
  77. this.pointerId = id;
  78. this.subtype = subtype;
  79. }
  80. // This is a bitfield that matches the buttons parameter of
  81. // https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent
  82. get buttons() {
  83. let buttons = 0;
  84. for (const button of this.pressed) {
  85. switch (button) {
  86. case 0:
  87. buttons |= 1;
  88. break;
  89. case 1:
  90. buttons |= 4;
  91. break;
  92. case 2:
  93. buttons |= 2;
  94. break;
  95. case 3:
  96. buttons |= 8;
  97. break;
  98. case 4:
  99. buttons |= 16;
  100. break;
  101. }
  102. }
  103. return buttons;
  104. }
  105. // --- Platform-specific state starts here ---
  106. // Input.dispatchMouseEvent doesn't know the concept of double click, so we
  107. // need to create it like for OSes:
  108. // https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:ui/events/event.cc;l=479
  109. static #DOUBLE_CLICK_TIME_MS = 500;
  110. static #MAX_DOUBLE_CLICK_RADIUS = 2;
  111. #clickCount = 0;
  112. #lastClick;
  113. setClickCount(context) {
  114. if (!this.#lastClick ||
  115. // The click needs to be within a certain amount of ms.
  116. context.timeStamp - this.#lastClick.timeStamp >
  117. PointerSource.#DOUBLE_CLICK_TIME_MS ||
  118. // The click needs to be within a square radius.
  119. Math.abs(this.#lastClick.x - context.x) >
  120. PointerSource.#MAX_DOUBLE_CLICK_RADIUS ||
  121. Math.abs(this.#lastClick.y - context.y) >
  122. PointerSource.#MAX_DOUBLE_CLICK_RADIUS) {
  123. this.#clickCount = 0;
  124. }
  125. ++this.#clickCount;
  126. this.#lastClick = context;
  127. }
  128. get clickCount() {
  129. return this.#clickCount;
  130. }
  131. }
  132. exports.PointerSource = PointerSource;
  133. class WheelSource {
  134. type = exports.SourceType.Wheel;
  135. }
  136. exports.WheelSource = WheelSource;
  137. //# sourceMappingURL=InputSource.js.map