editorScrollbar.ts 7.1 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';
A
Alex Dima 已提交
12
import { INewScrollPosition } 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);
E
Erich Gamma 已提交
37

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

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

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

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

E
Erich Gamma 已提交
66 67 68 69
		// 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 已提交
70 71
		const onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
			const newScrollPosition: INewScrollPosition = {};
72

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

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

89
			this._context.viewLayout.setScrollPositionNow(newScrollPosition);
E
Erich Gamma 已提交
90 91 92
		};

		// I've seen this happen both on the view dom node & on the lines content dom node.
A
Alex Dima 已提交
93 94 95
		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 已提交
96
		this._register(dom.addDisposableListener(this.scrollbarDomNode.domNode, 'scroll', (e: Event) => onBrowserDesperateReveal(this.scrollbarDomNode.domNode, true, false)));
E
Erich Gamma 已提交
97 98 99
	}

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

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

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

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

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

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

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

A
Alex Dima 已提交
131 132 133
	// --- begin event handlers

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

	// --- end event handlers

A
Alex Dima 已提交
165
	public prepareRender(ctx: RenderingContext): void {
A
Alex Dima 已提交
166 167 168
		// Nothing to do
	}

A
Alex Dima 已提交
169
	public render(ctx: RestrictedRenderingContext): void {
A
Alex Dima 已提交
170 171
		this.scrollbar.renderNow();
	}
E
Erich Gamma 已提交
172
}