@xterm/xterm 6.1.0-beta.184 → 6.1.0-beta.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
3
  * @license MIT
4
4
  */
5
- import { IBufferService, ICoreService, ICoreMouseService, IOptionsService } from 'common/services/Services';
5
+ import { IMouseStateService } from 'common/services/Services';
6
6
  import { ICoreMouseProtocol, ICoreMouseEvent, CoreMouseEncoding, CoreMouseEventType, CoreMouseButton, CoreMouseAction } from 'common/Types';
7
7
  import { Disposable } from 'common/Lifecycle';
8
8
  import { Emitter } from 'common/Event';
@@ -151,7 +151,7 @@ const DEFAULT_ENCODINGS: { [key: string]: CoreMouseEncoding } = {
151
151
  };
152
152
 
153
153
  /**
154
- * CoreMouseService
154
+ * MouseStateService
155
155
  *
156
156
  * Provides mouse tracking reports with different protocols and encodings.
157
157
  * - protocols: NONE (default), X10, VT200, DRAG, ANY
@@ -166,25 +166,21 @@ const DEFAULT_ENCODINGS: { [key: string]: CoreMouseEncoding } = {
166
166
  * a tracking report to the backend based on protocol and encoding limitations.
167
167
  * To send a mouse event call `triggerMouseEvent`.
168
168
  */
169
- export class CoreMouseService extends Disposable implements ICoreMouseService {
169
+ export class MouseStateService extends Disposable implements IMouseStateService {
170
170
  public serviceBrand: any;
171
171
 
172
172
  private _protocols: { [name: string]: ICoreMouseProtocol } = {};
173
173
  private _encodings: { [name: string]: CoreMouseEncoding } = {};
174
174
  private _activeProtocol: string = '';
175
175
  private _activeEncoding: string = '';
176
- private _lastEvent: ICoreMouseEvent | null = null;
177
- private _wheelPartialScroll: number = 0;
176
+ private _customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined;
178
177
 
179
178
  private readonly _onProtocolChange = this._register(new Emitter<CoreMouseEventType>());
180
179
  public readonly onProtocolChange = this._onProtocolChange.event;
181
180
 
182
- constructor(
183
- @IBufferService private readonly _bufferService: IBufferService,
184
- @ICoreService private readonly _coreService: ICoreService,
185
- @IOptionsService private readonly _optionsService: IOptionsService
186
- ) {
181
+ constructor() {
187
182
  super();
183
+
188
184
  // register default protocols and encodings
189
185
  for (const name of Object.keys(DEFAULT_PROTOCOLS)) this.addProtocol(name, DEFAULT_PROTOCOLS[name]);
190
186
  for (const name of Object.keys(DEFAULT_ENCODINGS)) this.addEncoding(name, DEFAULT_ENCODINGS[name]);
@@ -230,136 +226,29 @@ export class CoreMouseService extends Disposable implements ICoreMouseService {
230
226
  public reset(): void {
231
227
  this.activeProtocol = 'NONE';
232
228
  this.activeEncoding = 'DEFAULT';
233
- this._lastEvent = null;
234
- this._wheelPartialScroll = 0;
235
229
  }
236
230
 
237
- /**
238
- * Processes a wheel event, accounting for partial scrolls for trackpad, mouse scrolls.
239
- * This prevents hyper-sensitive scrolling in alt buffer.
240
- */
241
- public consumeWheelEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number {
242
- // Do nothing if it's not a vertical scroll event
243
- if (ev.deltaY === 0 || ev.shiftKey) {
244
- return 0;
245
- }
246
-
247
- if (cellHeight === undefined || dpr === undefined) {
248
- return 0;
249
- }
250
-
251
- const targetWheelEventPixels = cellHeight / dpr;
252
- let amount = this._applyScrollModifier(ev.deltaY, ev);
253
-
254
- if (ev.deltaMode === WheelEvent.DOM_DELTA_PIXEL) {
255
- amount /= (targetWheelEventPixels + 0.0); // Prevent integer division
256
-
257
- const isLikelyTrackpad = Math.abs(ev.deltaY) < 50;
258
- if (isLikelyTrackpad) {
259
- amount *= 0.3;
260
- }
261
-
262
- this._wheelPartialScroll += amount;
263
- amount = Math.floor(Math.abs(this._wheelPartialScroll)) * (this._wheelPartialScroll > 0 ? 1 : -1);
264
- this._wheelPartialScroll %= 1;
265
- } else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
266
- amount *= this._bufferService.rows;
267
- }
268
- return amount;
231
+ public setCustomWheelEventHandler(customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined): void {
232
+ this._customWheelEventHandler = customWheelEventHandler;
269
233
  }
270
234
 
271
- private _applyScrollModifier(amount: number, ev: WheelEvent): number {
272
- // Multiply the scroll speed when the modifier key is pressed
273
- if (ev.altKey || ev.ctrlKey || ev.shiftKey) {
274
- return amount * this._optionsService.rawOptions.fastScrollSensitivity * this._optionsService.rawOptions.scrollSensitivity;
275
- }
276
- return amount * this._optionsService.rawOptions.scrollSensitivity;
235
+ public allowCustomWheelEvent(ev: WheelEvent): boolean {
236
+ return this._customWheelEventHandler ? this._customWheelEventHandler(ev) !== false : true;
277
237
  }
278
238
 
279
- /**
280
- * Triggers a mouse event to be sent.
281
- *
282
- * Returns true if the event passed all protocol restrictions and a report
283
- * was sent, otherwise false. The return value may be used to decide whether
284
- * the default event action in the bowser component should be omitted.
285
- *
286
- * Note: The method will change values of the given event object
287
- * to fullfill protocol and encoding restrictions.
288
- */
289
- public triggerMouseEvent(e: ICoreMouseEvent): boolean {
290
- // range check for col/row
291
- if (e.col < 0 || e.col >= this._bufferService.cols
292
- || e.row < 0 || e.row >= this._bufferService.rows) {
293
- return false;
294
- }
295
-
296
- // filter nonsense combinations of button + action
297
- if (e.button === CoreMouseButton.WHEEL && e.action === CoreMouseAction.MOVE) {
298
- return false;
299
- }
300
- if (e.button === CoreMouseButton.NONE && e.action !== CoreMouseAction.MOVE) {
301
- return false;
302
- }
303
- if (e.button !== CoreMouseButton.WHEEL && (e.action === CoreMouseAction.LEFT || e.action === CoreMouseAction.RIGHT)) {
304
- return false;
305
- }
306
-
307
- // report 1-based coords
308
- e.col++;
309
- e.row++;
310
-
311
- // debounce move events at grid or pixel level
312
- if (e.action === CoreMouseAction.MOVE
313
- && this._lastEvent
314
- && this._equalEvents(this._lastEvent, e, this._activeEncoding === 'SGR_PIXELS')
315
- ) {
316
- return false;
317
- }
318
-
319
- // apply protocol restrictions
320
- if (!this._protocols[this._activeProtocol].restrict(e)) {
321
- return false;
322
- }
323
-
324
- // encode report and send
325
- const report = this._encodings[this._activeEncoding](e);
326
- if (report) {
327
- // always send DEFAULT as binary data
328
- if (this._activeEncoding === 'DEFAULT') {
329
- this._coreService.triggerBinaryEvent(report);
330
- } else {
331
- this._coreService.triggerDataEvent(report, true);
332
- }
333
- }
334
-
335
- this._lastEvent = e;
239
+ public restrictMouseEvent(e: ICoreMouseEvent): boolean {
240
+ return this._protocols[this._activeProtocol].restrict(e);
241
+ }
336
242
 
337
- return true;
243
+ public encodeMouseEvent(e: ICoreMouseEvent): string {
244
+ return this._encodings[this._activeEncoding](e);
338
245
  }
339
246
 
340
- public explainEvents(events: CoreMouseEventType): { [event: string]: boolean } {
341
- return {
342
- down: !!(events & CoreMouseEventType.DOWN),
343
- up: !!(events & CoreMouseEventType.UP),
344
- drag: !!(events & CoreMouseEventType.DRAG),
345
- move: !!(events & CoreMouseEventType.MOVE),
346
- wheel: !!(events & CoreMouseEventType.WHEEL)
347
- };
247
+ public get isDefaultEncoding(): boolean {
248
+ return this._activeEncoding === 'DEFAULT';
348
249
  }
349
250
 
350
- private _equalEvents(e1: ICoreMouseEvent, e2: ICoreMouseEvent, pixels: boolean): boolean {
351
- if (pixels) {
352
- if (e1.x !== e2.x) return false;
353
- if (e1.y !== e2.y) return false;
354
- } else {
355
- if (e1.col !== e2.col) return false;
356
- if (e1.row !== e2.row) return false;
357
- }
358
- if (e1.button !== e2.button) return false;
359
- if (e1.action !== e2.action) return false;
360
- if (e1.ctrl !== e2.ctrl) return false;
361
- if (e1.alt !== e2.alt) return false;
362
- if (e1.shift !== e2.shift) return false;
363
- return true;
251
+ public get isPixelEncoding(): boolean {
252
+ return this._activeEncoding === 'SGR_PIXELS';
364
253
  }
365
254
  }
@@ -33,8 +33,8 @@ export interface IBufferResizeEvent {
33
33
  rowsChanged: boolean;
34
34
  }
35
35
 
36
- export const ICoreMouseService = createDecorator<ICoreMouseService>('CoreMouseService');
37
- export interface ICoreMouseService {
36
+ export const IMouseStateService = createDecorator<IMouseStateService>('MouseStateService');
37
+ export interface IMouseStateService {
38
38
  serviceBrand: undefined;
39
39
 
40
40
  activeProtocol: string;
@@ -43,33 +43,17 @@ export interface ICoreMouseService {
43
43
  addProtocol(name: string, protocol: ICoreMouseProtocol): void;
44
44
  addEncoding(name: string, encoding: CoreMouseEncoding): void;
45
45
  reset(): void;
46
-
47
- /**
48
- * Triggers a mouse event to be sent.
49
- *
50
- * Returns true if the event passed all protocol restrictions and a report
51
- * was sent, otherwise false. The return value may be used to decide whether
52
- * the default event action in the bowser component should be omitted.
53
- *
54
- * Note: The method will change values of the given event object
55
- * to fullfill protocol and encoding restrictions.
56
- */
57
- triggerMouseEvent(event: ICoreMouseEvent): boolean;
46
+ setCustomWheelEventHandler(customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined): void;
47
+ allowCustomWheelEvent(ev: WheelEvent): boolean;
58
48
 
59
49
  /**
60
50
  * Event to announce changes in mouse tracking.
61
51
  */
62
52
  onProtocolChange: IEvent<CoreMouseEventType>;
63
-
64
- /**
65
- * Human readable version of mouse events.
66
- */
67
- explainEvents(events: CoreMouseEventType): { [event: string]: boolean };
68
-
69
- /**
70
- * Process wheel event taking partial scroll into account.
71
- */
72
- consumeWheelEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number;
53
+ restrictMouseEvent(event: ICoreMouseEvent): boolean;
54
+ encodeMouseEvent(event: ICoreMouseEvent): string;
55
+ readonly isDefaultEncoding: boolean;
56
+ readonly isPixelEncoding: boolean;
73
57
  }
74
58
 
75
59
  export const ICoreService = createDecorator<ICoreService>('CoreService');