Skip to content

Commit 4f083a0

Browse files
committed
Fix microsoft#62003, add option for predominant axis scrolling
1 parent fa93b07 commit 4f083a0

6 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/vs/base/browser/ui/scrollbar/scrollableElement.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export abstract class AbstractScrollableElement extends Widget {
282282
this._options.handleMouseWheel = massagedOptions.handleMouseWheel;
283283
this._options.mouseWheelScrollSensitivity = massagedOptions.mouseWheelScrollSensitivity;
284284
this._options.fastScrollSensitivity = massagedOptions.fastScrollSensitivity;
285+
this._options.scrollPredominantAxisOnly = massagedOptions.scrollPredominantAxisOnly;
285286
this._setListeningToMouseWheel(this._options.handleMouseWheel);
286287

287288
if (!this._options.lazyRender) {
@@ -329,6 +330,14 @@ export abstract class AbstractScrollableElement extends Widget {
329330
let deltaY = e.deltaY * this._options.mouseWheelScrollSensitivity;
330331
let deltaX = e.deltaX * this._options.mouseWheelScrollSensitivity;
331332

333+
if (this._options.scrollPredominantAxisOnly) {
334+
if (Math.abs(deltaY) >= Math.abs(deltaX)) {
335+
deltaX = 0;
336+
} else {
337+
deltaY = 0;
338+
}
339+
}
340+
332341
if (this._options.flipAxes) {
333342
[deltaY, deltaX] = [deltaX, deltaY];
334343
}
@@ -548,6 +557,7 @@ function resolveOptions(opts: ScrollableElementCreationOptions): ScrollableEleme
548557
scrollYToX: (typeof opts.scrollYToX !== 'undefined' ? opts.scrollYToX : false),
549558
mouseWheelScrollSensitivity: (typeof opts.mouseWheelScrollSensitivity !== 'undefined' ? opts.mouseWheelScrollSensitivity : 1),
550559
fastScrollSensitivity: (typeof opts.fastScrollSensitivity !== 'undefined' ? opts.fastScrollSensitivity : 5),
560+
scrollPredominantAxisOnly: (typeof opts.scrollPredominantAxisOnly !== 'undefined' ? opts.scrollPredominantAxisOnly : true),
551561
mouseWheelSmoothScroll: (typeof opts.mouseWheelSmoothScroll !== 'undefined' ? opts.mouseWheelSmoothScroll : true),
552562
arrowSize: (typeof opts.arrowSize !== 'undefined' ? opts.arrowSize : 11),
553563

src/vs/base/browser/ui/scrollbar/scrollableElementOptions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export interface ScrollableElementCreationOptions {
5555
* Defaults to 5.
5656
*/
5757
fastScrollSensitivity?: number;
58+
/**
59+
* Whether the editor will only scroll along the predominant axis when scrolling both
60+
* vertically and horizontally at the same time.
61+
* Prevents horizontal drift when scrolling vertically on a trackpad.
62+
* Defaults to true.
63+
*/
64+
scrollPredominantAxisOnly?: boolean;
5865
/**
5966
* Height for vertical arrows (top/bottom) and width for horizontal arrows (left/right).
6067
* Defaults to 11.
@@ -113,6 +120,7 @@ export interface ScrollableElementChangeOptions {
113120
handleMouseWheel?: boolean;
114121
mouseWheelScrollSensitivity?: number;
115122
fastScrollSensitivity: number;
123+
scrollPredominantAxisOnly: boolean;
116124
}
117125

118126
export interface ScrollableElementResolvedOptions {
@@ -125,6 +133,7 @@ export interface ScrollableElementResolvedOptions {
125133
alwaysConsumeMouseWheel: boolean;
126134
mouseWheelScrollSensitivity: number;
127135
fastScrollSensitivity: number;
136+
scrollPredominantAxisOnly: boolean;
128137
mouseWheelSmoothScroll: boolean;
129138
arrowSize: number;
130139
listenOnDomNode: HTMLElement | null;

src/vs/editor/browser/viewParts/editorScrollbar/editorScrollbar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class EditorScrollbar extends ViewPart {
4949
arrowSize: configScrollbarOpts.arrowSize,
5050
mouseWheelScrollSensitivity: configScrollbarOpts.mouseWheelScrollSensitivity,
5151
fastScrollSensitivity: configScrollbarOpts.fastScrollSensitivity,
52+
scrollPredominantAxisOnly: configScrollbarOpts.scrollPredominantAxisOnly,
5253
};
5354

5455
this.scrollbar = this._register(new SmoothScrollableElement(linesContent.domNode, scrollbarOptions, this._context.viewLayout.scrollable));
@@ -129,7 +130,8 @@ export class EditorScrollbar extends ViewPart {
129130
const newOpts: ScrollableElementChangeOptions = {
130131
handleMouseWheel: editor.viewInfo.scrollbar.handleMouseWheel,
131132
mouseWheelScrollSensitivity: editor.viewInfo.scrollbar.mouseWheelScrollSensitivity,
132-
fastScrollSensitivity: editor.viewInfo.scrollbar.fastScrollSensitivity
133+
fastScrollSensitivity: editor.viewInfo.scrollbar.fastScrollSensitivity,
134+
scrollPredominantAxisOnly: editor.viewInfo.scrollbar.scrollPredominantAxisOnly
133135
};
134136
this.scrollbar.updateOptions(newOpts);
135137
}

src/vs/editor/common/config/commonEditorConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ const editorConfiguration: IConfigurationNode = {
459459
'default': EDITOR_DEFAULTS.viewInfo.scrollbar.fastScrollSensitivity,
460460
'markdownDescription': nls.localize('fastScrollSensitivity', "Scrolling speed mulitiplier when pressing `Alt`.")
461461
},
462+
'editor.scrollPredominantAxisOnly': {
463+
'type': 'boolean',
464+
'default': EDITOR_DEFAULTS.viewInfo.scrollbar.scrollPredominantAxisOnly,
465+
'description': nls.localize('scrollPredominantAxisOnly', "Scroll only along the predominant axis when scrolling both vertically and horizontally at the same time. Prevents horizontal drift when scrolling vertically on a trackpad.")
466+
},
462467
'editor.multiCursorModifier': {
463468
'type': 'string',
464469
'enum': ['ctrlCmd', 'alt'],

src/vs/editor/common/config/editorOptions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ export interface IEditorOptions {
485485
* Defaults to 5.
486486
*/
487487
fastScrollSensitivity?: number;
488+
/**
489+
* Enable that the editor scrolls only the predominant axis. Prevents horizontal drift when scrolling vertically on a trackpad.
490+
* Defaults to true.
491+
*/
492+
scrollPredominantAxisOnly?: boolean;
488493
/**
489494
* The modifier to be used to add multiple cursors with the mouse.
490495
* Defaults to 'alt'
@@ -898,6 +903,7 @@ export interface InternalEditorScrollbarOptions {
898903
readonly verticalSliderSize: number;
899904
readonly mouseWheelScrollSensitivity: number;
900905
readonly fastScrollSensitivity: number;
906+
readonly scrollPredominantAxisOnly: boolean;
901907
}
902908

903909
export interface InternalEditorMinimapOptions {
@@ -1321,6 +1327,7 @@ export class InternalEditorOptions {
13211327
&& a.verticalSliderSize === b.verticalSliderSize
13221328
&& a.mouseWheelScrollSensitivity === b.mouseWheelScrollSensitivity
13231329
&& a.fastScrollSensitivity === b.fastScrollSensitivity
1330+
&& a.scrollPredominantAxisOnly === b.scrollPredominantAxisOnly
13241331
);
13251332
}
13261333

@@ -1826,7 +1833,7 @@ export class EditorOptionsValidator {
18261833
};
18271834
}
18281835

1829-
private static _sanitizeScrollbarOpts(opts: IEditorScrollbarOptions | undefined, defaults: InternalEditorScrollbarOptions, mouseWheelScrollSensitivity: number, fastScrollSensitivity: number): InternalEditorScrollbarOptions {
1836+
private static _sanitizeScrollbarOpts(opts: IEditorScrollbarOptions | undefined, defaults: InternalEditorScrollbarOptions, mouseWheelScrollSensitivity: number, fastScrollSensitivity: number, scrollPredominantAxisOnly: boolean): InternalEditorScrollbarOptions {
18301837
if (typeof opts !== 'object') {
18311838
return defaults;
18321839
}
@@ -1851,6 +1858,7 @@ export class EditorOptionsValidator {
18511858
handleMouseWheel: _boolean(opts.handleMouseWheel, defaults.handleMouseWheel),
18521859
mouseWheelScrollSensitivity: mouseWheelScrollSensitivity,
18531860
fastScrollSensitivity: fastScrollSensitivity,
1861+
scrollPredominantAxisOnly: scrollPredominantAxisOnly,
18541862
};
18551863
}
18561864

@@ -2006,7 +2014,8 @@ export class EditorOptionsValidator {
20062014
if (fastScrollSensitivity <= 0) {
20072015
fastScrollSensitivity = defaults.scrollbar.fastScrollSensitivity;
20082016
}
2009-
const scrollbar = this._sanitizeScrollbarOpts(opts.scrollbar, defaults.scrollbar, mouseWheelScrollSensitivity, fastScrollSensitivity);
2017+
let scrollPredominantAxisOnly = _boolean(opts.scrollPredominantAxisOnly, defaults.scrollbar.scrollPredominantAxisOnly);
2018+
const scrollbar = this._sanitizeScrollbarOpts(opts.scrollbar, defaults.scrollbar, mouseWheelScrollSensitivity, fastScrollSensitivity, scrollPredominantAxisOnly);
20102019
const minimap = this._sanitizeMinimapOpts(opts.minimap, defaults.minimap);
20112020

20122021
return {
@@ -2636,6 +2645,7 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
26362645
handleMouseWheel: true,
26372646
mouseWheelScrollSensitivity: 1,
26382647
fastScrollSensitivity: 5,
2648+
scrollPredominantAxisOnly: true,
26392649
},
26402650
minimap: {
26412651
enabled: true,

src/vs/monaco.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,11 @@ declare namespace monaco.editor {
28162816
* Defaults to 5.
28172817
*/
28182818
fastScrollSensitivity?: number;
2819+
/**
2820+
* Enable that the editor scrolls only the predominant axis. Prevents horizontal drift when scrolling vertically on a trackpad.
2821+
* Defaults to true.
2822+
*/
2823+
scrollPredominantAxisOnly?: boolean;
28192824
/**
28202825
* The modifier to be used to add multiple cursors with the mouse.
28212826
* Defaults to 'alt'
@@ -3174,6 +3179,7 @@ declare namespace monaco.editor {
31743179
readonly verticalSliderSize: number;
31753180
readonly mouseWheelScrollSensitivity: number;
31763181
readonly fastScrollSensitivity: number;
3182+
readonly scrollPredominantAxisOnly: boolean;
31773183
}
31783184

31793185
export interface InternalEditorMinimapOptions {

0 commit comments

Comments
 (0)