Skip to content

Commit ebddd5d

Browse files
committed
Move from CoreBrowerTerminal to CoreMouseService. Still slow though
1 parent 5bd7b4a commit ebddd5d

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

src/browser/CoreBrowserTerminal.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,11 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
646646
if (deltaY === 0) {
647647
return false;
648648
}
649-
const lines = self._consumeWheelEvent(ev as WheelEvent);
649+
const lines = self.coreMouseService.consumeWhellEvent(
650+
ev as WheelEvent,
651+
self._renderService?.dimensions?.device?.cell?.height,
652+
self._coreBrowserService?.dpr
653+
);
650654
if (lines === 0) {
651655
return false;
652656
}
@@ -821,7 +825,11 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
821825
return false;
822826
}
823827

824-
const lines = self._consumeWheelEvent(ev as WheelEvent);
828+
const lines = self.coreMouseService.consumeWhellEvent(
829+
ev as WheelEvent,
830+
self._renderService?.dimensions?.device?.cell?.height,
831+
self._coreBrowserService?.dpr
832+
);
825833
if (lines === 0) {
826834
return false;
827835
}
@@ -834,38 +842,7 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
834842
}, { passive: false }));
835843
}
836844

837-
// Stores a partial line amount when scrolling, this is used to keep track of how much of a line
838-
// is scrolled so we can "scroll" over partial lines and feel natural on touchpads. This is a
839-
// quick fix and could have a more robust solution in place that reset the value when needed.
840-
private _wheelPartialScroll: number = 0;
841845

842-
/**
843-
* Gets the number of pixels scrolled by the mouse event taking into account what type of delta
844-
* is being used.
845-
* @param ev The mouse wheel event.
846-
*/
847-
private _consumeWheelEvent(ev: WheelEvent): number {
848-
// Do nothing if it's not a vertical scroll event
849-
if (ev.deltaY === 0 || ev.shiftKey) {
850-
return 0;
851-
}
852-
853-
if (!this._coreBrowserService || !this._renderService) {
854-
return 0;
855-
}
856-
// Fallback to WheelEvent.DOM_DELTA_LINE
857-
const targetWheelEventPixels = this._renderService.dimensions.device.cell.height / this._coreBrowserService.dpr;
858-
let amount = 1;
859-
if (ev.deltaMode === WheelEvent.DOM_DELTA_PIXEL) {
860-
amount /= targetWheelEventPixels + 0.0; // Prevent integer division
861-
this._wheelPartialScroll += amount;
862-
amount = Math.floor(Math.abs(this._wheelPartialScroll)) * (this._wheelPartialScroll > 0 ? 1 : -1);
863-
this._wheelPartialScroll %= 1;
864-
} else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
865-
amount *= this._bufferService.rows;
866-
}
867-
return amount;
868-
}
869846

870847
/**
871848
* Tells the renderer to refresh terminal content between two rows (inclusive) at the next

src/common/TestUtils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ export class MockCoreMouseService implements ICoreMouseService {
6666
public explainEvents(events: CoreMouseEventType): { [event: string]: boolean } {
6767
throw new Error('Method not implemented.');
6868
}
69+
public consumeWhellEvent(ev: WheelEvent, cellHeight: number, dpr: number): number {
70+
return 1; // Return a simple mock value
71+
}
6972
}
7073

7174
export class MockCharsetService implements ICharsetService {

src/common/services/CoreMouseService.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export class CoreMouseService extends Disposable implements ICoreMouseService {
174174
private _activeProtocol: string = '';
175175
private _activeEncoding: string = '';
176176
private _lastEvent: ICoreMouseEvent | null = null;
177+
private _wheelPartialScroll: number = 0;
177178

178179
private readonly _onProtocolChange = this._register(new Emitter<CoreMouseEventType>());
179180
public readonly onProtocolChange = this._onProtocolChange.event;
@@ -229,6 +230,35 @@ export class CoreMouseService extends Disposable implements ICoreMouseService {
229230
this.activeProtocol = 'NONE';
230231
this.activeEncoding = 'DEFAULT';
231232
this._lastEvent = null;
233+
this._wheelPartialScroll = 0;
234+
}
235+
236+
/**
237+
* Processes a wheel event, accounting for partial scrolls for trackpad, mouse scrolls.
238+
* This prevents hyper-sensitive scrolling in alt buffer.
239+
*/
240+
public consumeWhellEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number {
241+
// Do nothing if it's not a vertical scroll event
242+
if (ev.deltaY === 0 || ev.shiftKey) {
243+
return 0;
244+
}
245+
246+
if (cellHeight === undefined || dpr === undefined) {
247+
return 0;
248+
}
249+
250+
// Fallback to WheelEvent.DOM_DELTA_LINE
251+
const targetWheelEventPixels = cellHeight / dpr;
252+
let amount = 1;
253+
if (ev.deltaMode === WheelEvent.DOM_DELTA_PIXEL) {
254+
amount /= targetWheelEventPixels + 0.0; // Prevent integer division
255+
this._wheelPartialScroll += amount;
256+
amount = Math.floor(Math.abs(this._wheelPartialScroll)) * (this._wheelPartialScroll > 0 ? 1 : -1);
257+
this._wheelPartialScroll %= 1;
258+
} else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
259+
amount *= this._bufferService.rows;
260+
}
261+
return amount;
232262
}
233263

234264
/**

src/common/services/Services.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export interface ICoreMouseService {
5858
* Human readable version of mouse events.
5959
*/
6060
explainEvents(events: CoreMouseEventType): { [event: string]: boolean };
61+
62+
/**
63+
* Process wheel event taking partial scroll into account.
64+
*/
65+
consumeWhellEvent(ev: WheelEvent, cellHeight?: number, dpr?: number): number;
6166
}
6267

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

0 commit comments

Comments
 (0)