Skip to content

Commit d63445b

Browse files
committed
Move custom mouse event handler into MouseStateService
Don't need to keep it in CoreBrowserTerminal
1 parent 2331abe commit d63445b

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

src/browser/CoreBrowserTerminal.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
7878
public browser: IBrowser = Browser as any;
7979

8080
private _customKeyEventHandler: CustomKeyEventHandler | undefined;
81-
private _customWheelEventHandler: CustomWheelEventHandler | undefined;
8281

8382
// Browser services
8483
private readonly _decorationService: DecorationService;
@@ -582,7 +581,6 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
582581
));
583582
this._instantiationService.setService(ISelectionService, this._selectionService);
584583
this._mouseService = this._instantiationService.createInstance(MouseService);
585-
this._mouseService.setCustomWheelEventHandler(this._customWheelEventHandler);
586584
this._instantiationService.setService(IMouseService, this._mouseService);
587585
this._register(this._selectionService.onRequestScrollLines(e => this.scrollLines(e.amount, e.suppressScrollEvent)));
588586
this._register(this._selectionService.onSelectionChange(() => this._onSelectionChange.fire()));
@@ -727,8 +725,7 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
727725
}
728726

729727
public attachCustomWheelEventHandler(customWheelEventHandler: CustomWheelEventHandler): void {
730-
this._customWheelEventHandler = customWheelEventHandler;
731-
this._mouseService?.setCustomWheelEventHandler(customWheelEventHandler);
728+
this.mouseStateService.setCustomWheelEventHandler(customWheelEventHandler);
732729
}
733730

734731
public registerLinkProvider(linkProvider: ILinkProvider): IDisposable {

src/browser/TestUtils.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,6 @@ export class MockMouseService implements IMouseService {
393393
throw new Error('Not implemented');
394394
}
395395

396-
public setCustomWheelEventHandler(): void {
397-
throw new Error('Not implemented');
398-
}
399-
400396
public bindMouse(): void {
401397
throw new Error('Not implemented');
402398
}

src/browser/services/MouseService.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { IBufferService, IMouseStateService, ICoreService, ILogService, IOptions
88
import { CoreMouseAction, CoreMouseButton, CoreMouseEventType, IDisposable } from 'common/Types';
99
import { C0 } from 'common/data/EscapeSequences';
1010
import { toDisposable } from 'common/Lifecycle';
11-
import { CustomWheelEventHandler } from 'browser/Types';
1211
import { ICoreBrowserService, IMouseCoordsService, IMouseService, IMouseServiceTarget, IRenderService, ISelectionService } from './Services';
1312

1413
type RequestedMouseEvents = Record<'mouseup' | 'wheel' | 'mousedrag' | 'mousemove', EventListener | null>;
@@ -22,8 +21,6 @@ interface IMouseBindContext {
2221
export class MouseService implements IMouseService {
2322
public serviceBrand: undefined;
2423

25-
private _customWheelEventHandler: CustomWheelEventHandler | undefined;
26-
2724
constructor(
2825
@IRenderService private readonly _renderService: IRenderService,
2926
@IMouseCoordsService private readonly _mouseCoordsService: IMouseCoordsService,
@@ -119,7 +116,7 @@ export class MouseService implements IMouseService {
119116
but = ev.button < 3 ? ev.button : CoreMouseButton.NONE;
120117
break;
121118
case 'wheel':
122-
if (this._customWheelEventHandler && this._customWheelEventHandler(ev as WheelEvent) === false) {
119+
if (!this._mouseStateService.allowCustomWheelEvent(ev as WheelEvent)) {
123120
return false;
124121
}
125122
const deltaY = (ev as WheelEvent).deltaY;
@@ -226,7 +223,7 @@ export class MouseService implements IMouseService {
226223
return;
227224
}
228225

229-
if (this._customWheelEventHandler && this._customWheelEventHandler(ev) === false) {
226+
if (!this._mouseStateService.allowCustomWheelEvent(ev)) {
230227
return false;
231228
}
232229

@@ -320,7 +317,4 @@ export class MouseService implements IMouseService {
320317
}
321318
}
322319

323-
public setCustomWheelEventHandler(customWheelEventHandler: CustomWheelEventHandler | undefined): void {
324-
this._customWheelEventHandler = customWheelEventHandler;
325-
}
326320
}

src/browser/services/Services.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { IRenderDimensions, IRenderer } from 'browser/renderer/shared/Types';
7-
import { CustomWheelEventHandler, IColorSet, ILink, ReadonlyColorSet } from 'browser/Types';
7+
import { IColorSet, ILink, ReadonlyColorSet } from 'browser/Types';
88
import { ISelectionRedrawRequestEvent as ISelectionRequestRedrawEvent, ISelectionRequestScrollLinesEvent } from 'browser/selection/Types';
99
import { createDecorator } from 'common/services/ServiceRegistry';
1010
import { AllColorIndex, IDisposable, IKeyboardResult } from 'common/Types';
@@ -61,7 +61,6 @@ export const IMouseService = createDecorator<IMouseService>('MouseService');
6161
export interface IMouseService {
6262
serviceBrand: undefined;
6363

64-
setCustomWheelEventHandler(customWheelEventHandler: CustomWheelEventHandler | undefined): void;
6564
bindMouse(target: IMouseServiceTarget, register: (disposable: IDisposable) => void, focus: () => void): void;
6665
}
6766
export interface IMouseServiceTarget {

src/common/TestUtils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export class MockMouseStateService implements IMouseStateService {
8282
public consumeWheelEvent(ev: WheelEvent, cellHeight: number, dpr: number): number {
8383
return 1;
8484
}
85+
public setCustomWheelEventHandler(customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined): void { }
86+
public allowCustomWheelEvent(ev: WheelEvent): boolean { return true; }
8587
}
8688

8789
export class MockCharsetService implements ICharsetService {

src/common/services/MouseStateService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export class MouseStateService extends Disposable implements IMouseStateService
175175
private _activeEncoding: string = '';
176176
private _lastEvent: ICoreMouseEvent | null = null;
177177
private _wheelPartialScroll: number = 0;
178+
private _customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined;
178179

179180
private readonly _onProtocolChange = this._register(new Emitter<CoreMouseEventType>());
180181
public readonly onProtocolChange = this._onProtocolChange.event;
@@ -268,6 +269,14 @@ export class MouseStateService extends Disposable implements IMouseStateService
268269
return amount;
269270
}
270271

272+
public setCustomWheelEventHandler(customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined): void {
273+
this._customWheelEventHandler = customWheelEventHandler;
274+
}
275+
276+
public allowCustomWheelEvent(ev: WheelEvent): boolean {
277+
return this._customWheelEventHandler ? this._customWheelEventHandler(ev) !== false : true;
278+
}
279+
271280
private _applyScrollModifier(amount: number, ev: WheelEvent): number {
272281
// Multiply the scroll speed when the modifier key is pressed
273282
if (ev.altKey || ev.ctrlKey || ev.shiftKey) {

src/common/services/Services.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export interface IMouseStateService {
7070
* Process wheel event taking partial scroll into account.
7171
*/
7272
consumeWheelEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number;
73+
74+
/**
75+
* Sets a custom wheel event handler that can veto wheel processing.
76+
*/
77+
setCustomWheelEventHandler(customWheelEventHandler: ((event: WheelEvent) => boolean) | undefined): void;
78+
79+
/**
80+
* Checks whether a wheel event should be processed.
81+
*/
82+
allowCustomWheelEvent(ev: WheelEvent): boolean;
7383
}
7484

7585
export const ICoreService = createDecorator<ICoreService>('CoreService');

0 commit comments

Comments
 (0)