editorScrollbar.ts 7.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
7 8
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
9
import { IOverviewRulerLayoutInfo, SmoothScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
A
Alex Dima 已提交
10 11
import { ScrollableElementChangeOptions, ScrollableElementCreationOptions } from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
import { PartFingerprint, PartFingerprints, ViewPart } from 'vs/editor/browser/view/viewPart';
12
import { INewScrollPosition, ScrollType } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/common/view/renderingContext';
A
Alex Dima 已提交
14 15
import { ViewContext } from 'vs/editor/common/view/viewContext';
import * as viewEvents from 'vs/editor/common/view/viewEvents';
16
import { getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
A
renames  
Alex Dima 已提交
17
import { EditorOption } from 'vs/editor/common/config/editorOptions';
E
Erich Gamma 已提交
18

A
Alex Dima 已提交
19
export class EditorScrollbar extends ViewPart {
E
Erich Gamma 已提交
20

21 22
	private readonly scrollbar: SmoothScrollableElement;
	private readonly scrollbarDomNode: FastDomNode<HTMLElement>;
A
Alex Dima 已提交
23 24 25 26 27 28 29

	constructor(
		context: ViewContext,
		linesContent: FastDomNode<HTMLElement>,
		viewDomNode: FastDomNode<HTMLElement>,
		overflowGuardDomNode: FastDomNode<HTMLElement>
	) {
A
Alex Dima 已提交
30 31
		super(context);

A
Alex Dima 已提交
32 33

		const options = this._context.configuration.options;
A
renames  
Alex Dima 已提交
34 35 36
		const scrollbar = options.get(EditorOption.scrollbar);
		const mouseWheelScrollSensitivity = options.get(EditorOption.mouseWheelScrollSensitivity);
		const fastScrollSensitivity = options.get(EditorOption.fastScrollSensitivity);
37
		const scrollPredominantAxis = options.get(EditorOption.scrollPredominantAxis);
E
Erich Gamma 已提交
38

M
Matt Bierner 已提交
39
		const scrollbarOptions: ScrollableElementCreationOptions = {
A
Alex Dima 已提交
40
			listenOnDomNode: viewDomNode.domNode,
41
			className: 'editor-scrollable' + ' ' + getThemeTypeSelector(context.theme.type),
A
Alex Dima 已提交
42 43 44
			useShadows: false,
			lazyRender: true,

A
Alex Dima 已提交
45 46
			vertical: scrollbar.vertical,
			horizontal: scrollbar.horizontal,
47 48
			verticalHasArrows: scrollbar.verticalHasArrows,
			horizontalHasArrows: scrollbar.horizontalHasArrows,
A
Alex Dima 已提交
49 50 51 52 53
			verticalScrollbarSize: scrollbar.verticalScrollbarSize,
			verticalSliderSize: scrollbar.verticalSliderSize,
			horizontalScrollbarSize: scrollbar.horizontalScrollbarSize,
			horizontalSliderSize: scrollbar.horizontalSliderSize,
			handleMouseWheel: scrollbar.handleMouseWheel,
54
			alwaysConsumeMouseWheel: scrollbar.alwaysConsumeMouseWheel,
A
Alex Dima 已提交
55 56 57
			arrowSize: scrollbar.arrowSize,
			mouseWheelScrollSensitivity: mouseWheelScrollSensitivity,
			fastScrollSensitivity: fastScrollSensitivity,
58
			scrollPredominantAxis: scrollPredominantAxis,
E
Erich Gamma 已提交
59 60
		};

61
		this.scrollbar = this._register(new SmoothScrollableElement(linesContent.domNode, scrollbarOptions, this._context.viewLayout.getScrollable()));
A
Alex Dima 已提交
62 63
		PartFingerprints.write(this.scrollbar.getDomNode(), PartFingerprint.ScrollableElement);

A
Alex Dima 已提交
64 65 66 67
		this.scrollbarDomNode = createFastDomNode(this.scrollbar.getDomNode());
		this.scrollbarDomNode.setPosition('absolute');
		this._setLayout();

E
Erich Gamma 已提交
68 69 70 71
		// When having a zone widget that calls .focus() on one of its dom elements,
		// the browser will try desperately to reveal that dom node, unexpectedly
		// changing the .scrollTop of this.linesContent

M
Matt Bierner 已提交
72 73
		const onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
			const newScrollPosition: INewScrollPosition = {};
74

E
Erich Gamma 已提交
75
			if (lookAtScrollTop) {
M
Matt Bierner 已提交
76
				const deltaTop = domNode.scrollTop;
E
Erich Gamma 已提交
77
				if (deltaTop) {
78
					newScrollPosition.scrollTop = this._context.viewLayout.getCurrentScrollTop() + deltaTop;
E
Erich Gamma 已提交
79 80 81 82 83
					domNode.scrollTop = 0;
				}
			}

			if (lookAtScrollLeft) {
M
Matt Bierner 已提交
84
				const deltaLeft = domNode.scrollLeft;
E
Erich Gamma 已提交
85
				if (deltaLeft) {
86
					newScrollPosition.scrollLeft = this._context.viewLayout.getCurrentScrollLeft() + deltaLeft;
E
Erich Gamma 已提交
87 88 89
					domNode.scrollLeft = 0;
				}
			}
90

91
			this._context.model.setScrollPosition(newScrollPosition, ScrollType.Immediate);
E
Erich Gamma 已提交
92 93 94
		};

		// I've seen this happen both on the view dom node & on the lines content dom node.
A
Alex Dima 已提交
95 96 97
		this._register(dom.addDisposableListener(viewDomNode.domNode, 'scroll', (e: Event) => onBrowserDesperateReveal(viewDomNode.domNode, true, true)));
		this._register(dom.addDisposableListener(linesContent.domNode, 'scroll', (e: Event) => onBrowserDesperateReveal(linesContent.domNode, true, false)));
		this._register(dom.addDisposableListener(overflowGuardDomNode.domNode, 'scroll', (e: Event) => onBrowserDesperateReveal(overflowGuardDomNode.domNode, true, false)));
S
Sandeep Somavarapu 已提交
98
		this._register(dom.addDisposableListener(this.scrollbarDomNode.domNode, 'scroll', (e: Event) => onBrowserDesperateReveal(this.scrollbarDomNode.domNode, true, false)));
E
Erich Gamma 已提交
99 100 101
	}

	public dispose(): void {
A
Alex Dima 已提交
102
		super.dispose();
E
Erich Gamma 已提交
103 104
	}

A
Alex Dima 已提交
105
	private _setLayout(): void {
A
Alex Dima 已提交
106
		const options = this._context.configuration.options;
A
renames  
Alex Dima 已提交
107
		const layoutInfo = options.get(EditorOption.layoutInfo);
A
Alex Dima 已提交
108 109

		this.scrollbarDomNode.setLeft(layoutInfo.contentLeft);
110

A
renames  
Alex Dima 已提交
111
		const minimap = options.get(EditorOption.minimap);
A
Alex Dima 已提交
112
		const side = minimap.side;
113
		if (side === 'right') {
A
Alex Dima 已提交
114
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth + layoutInfo.minimap.minimapWidth);
115 116 117
		} else {
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth);
		}
118
		this.scrollbarDomNode.setHeight(layoutInfo.height);
A
Alex Dima 已提交
119 120
	}

A
Alex Dima 已提交
121
	public getOverviewRulerLayoutInfo(): IOverviewRulerLayoutInfo {
122
		return this.scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
123 124
	}

A
Alex Dima 已提交
125 126
	public getDomNode(): FastDomNode<HTMLElement> {
		return this.scrollbarDomNode;
E
Erich Gamma 已提交
127 128
	}

129
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
130 131
		this.scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}
132

A
Alex Dima 已提交
133 134 135
	// --- begin event handlers

	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
136
		if (
A
renames  
Alex Dima 已提交
137 138 139
			e.hasChanged(EditorOption.scrollbar)
			|| e.hasChanged(EditorOption.mouseWheelScrollSensitivity)
			|| e.hasChanged(EditorOption.fastScrollSensitivity)
A
Alex Dima 已提交
140 141
		) {
			const options = this._context.configuration.options;
A
renames  
Alex Dima 已提交
142 143 144
			const scrollbar = options.get(EditorOption.scrollbar);
			const mouseWheelScrollSensitivity = options.get(EditorOption.mouseWheelScrollSensitivity);
			const fastScrollSensitivity = options.get(EditorOption.fastScrollSensitivity);
145
			const scrollPredominantAxis = options.get(EditorOption.scrollPredominantAxis);
M
Matt Bierner 已提交
146
			const newOpts: ScrollableElementChangeOptions = {
A
Alex Dima 已提交
147 148
				handleMouseWheel: scrollbar.handleMouseWheel,
				mouseWheelScrollSensitivity: mouseWheelScrollSensitivity,
149
				fastScrollSensitivity: fastScrollSensitivity,
150
				scrollPredominantAxis: scrollPredominantAxis
A
Alex Dima 已提交
151 152 153
			};
			this.scrollbar.updateOptions(newOpts);
		}
A
renames  
Alex Dima 已提交
154
		if (e.hasChanged(EditorOption.layoutInfo)) {
A
Alex Dima 已提交
155 156
			this._setLayout();
		}
A
Alex Dima 已提交
157 158 159 160 161
		return true;
	}
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		return true;
	}
162 163 164 165
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.scrollbar.updateClassName('editor-scrollable' + ' ' + getThemeTypeSelector(this._context.theme.type));
		return true;
	}
A
Alex Dima 已提交
166 167 168

	// --- end event handlers

A
Alex Dima 已提交
169
	public prepareRender(ctx: RenderingContext): void {
A
Alex Dima 已提交
170 171 172
		// Nothing to do
	}

A
Alex Dima 已提交
173
	public render(ctx: RestrictedRenderingContext): void {
A
Alex Dima 已提交
174 175
		this.scrollbar.renderNow();
	}
E
Erich Gamma 已提交
176
}