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

J
Johannes Rieken 已提交
7
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
8
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
9 10
import { ScrollableElementCreationOptions, ScrollableElementChangeOptions } from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
import { IOverviewRulerLayoutInfo, ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
A
Alex Dima 已提交
11
import { IConfiguration, IConfigurationChangedEvent, INewScrollPosition } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
12
import { ClassNames } from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
13
import { PartFingerprint, PartFingerprints } from 'vs/editor/browser/view/viewPart';
14
import { Scrollable } from 'vs/base/common/scrollable';
E
Erich Gamma 已提交
15

J
Johannes Rieken 已提交
16
function addPropertyIfPresent(src: any, dst: any, prop: string): void {
E
Erich Gamma 已提交
17 18 19 20 21
	if (src.hasOwnProperty(prop)) {
		dst[prop] = src[prop];
	}
}

22
export class EditorScrollbar implements IDisposable {
E
Erich Gamma 已提交
23

24
	private scrollable: Scrollable;
A
Alex Dima 已提交
25
	private configuration: IConfiguration;
E
Erich Gamma 已提交
26

J
Johannes Rieken 已提交
27
	private toDispose: IDisposable[];
E
Erich Gamma 已提交
28
	private linesContent: HTMLElement;
A
Alex Dima 已提交
29
	private scrollbar: ScrollableElement;
E
Erich Gamma 已提交
30

A
Alex Dima 已提交
31
	constructor(scrollable: Scrollable, configuration: IConfiguration, linesContent: HTMLElement, viewDomNode: HTMLElement, overflowGuardDomNode: HTMLElement) {
E
Erich Gamma 已提交
32
		this.toDispose = [];
33
		this.scrollable = scrollable;
E
Erich Gamma 已提交
34 35 36
		this.configuration = configuration;
		this.linesContent = linesContent;

A
Alex Dima 已提交
37
		let configScrollbarOpts = this.configuration.editor.viewInfo.scrollbar;
E
Erich Gamma 已提交
38

A
Alex Dima 已提交
39
		let scrollbarOptions: ScrollableElementCreationOptions = {
40
			canUseTranslate3d: this.configuration.editor.viewInfo.canUseTranslate3d,
E
Erich Gamma 已提交
41 42 43
			listenOnDomNode: viewDomNode,
			vertical: configScrollbarOpts.vertical,
			horizontal: configScrollbarOpts.horizontal,
44
			className: ClassNames.SCROLLABLE_ELEMENT + ' ' + this.configuration.editor.viewInfo.theme,
E
Erich Gamma 已提交
45
			useShadows: false,
46
			lazyRender: true
E
Erich Gamma 已提交
47 48 49 50 51 52 53 54 55 56 57
		};
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'verticalHasArrows');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'horizontalHasArrows');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'verticalScrollbarSize');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'verticalSliderSize');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'horizontalScrollbarSize');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'horizontalSliderSize');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'handleMouseWheel');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'arrowSize');
		addPropertyIfPresent(configScrollbarOpts, scrollbarOptions, 'mouseWheelScrollSensitivity');

58
		this.scrollbar = new ScrollableElement(linesContent, scrollbarOptions, this.scrollable);
A
Alex Dima 已提交
59 60
		PartFingerprints.write(this.scrollbar.getDomNode(), PartFingerprint.ScrollableElement);

E
Erich Gamma 已提交
61 62
		this.toDispose.push(this.scrollbar);

J
Johannes Rieken 已提交
63
		this.toDispose.push(this.configuration.onDidChange((e: IConfigurationChangedEvent) => {
64
			this.scrollbar.updateClassName(ClassNames.SCROLLABLE_ELEMENT + ' ' + this.configuration.editor.viewInfo.theme);
65
			if (e.viewInfo.scrollbar || e.viewInfo.canUseTranslate3d) {
J
Johannes Rieken 已提交
66
				let newOpts: ScrollableElementChangeOptions = {
67
					canUseTranslate3d: this.configuration.editor.viewInfo.canUseTranslate3d,
68 69
					handleMouseWheel: this.configuration.editor.viewInfo.scrollbar.handleMouseWheel,
					mouseWheelScrollSensitivity: this.configuration.editor.viewInfo.scrollbar.mouseWheelScrollSensitivity
70 71
				};
				this.scrollbar.updateOptions(newOpts);
E
Erich Gamma 已提交
72 73 74 75 76 77 78
			}
		}));

		// 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

A
Alex Dima 已提交
79
		let onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
A
Alex Dima 已提交
80
			const scrollState = this.scrollable.getState();
J
Johannes Rieken 已提交
81
			let newScrollPosition: INewScrollPosition = {};
82

E
Erich Gamma 已提交
83
			if (lookAtScrollTop) {
A
Alex Dima 已提交
84
				let deltaTop = domNode.scrollTop;
E
Erich Gamma 已提交
85
				if (deltaTop) {
A
Alex Dima 已提交
86
					newScrollPosition.scrollTop = scrollState.scrollTop + deltaTop;
E
Erich Gamma 已提交
87 88 89 90 91
					domNode.scrollTop = 0;
				}
			}

			if (lookAtScrollLeft) {
A
Alex Dima 已提交
92
				let deltaLeft = domNode.scrollLeft;
E
Erich Gamma 已提交
93
				if (deltaLeft) {
A
Alex Dima 已提交
94
					newScrollPosition.scrollLeft = scrollState.scrollLeft + deltaLeft;
E
Erich Gamma 已提交
95 96 97
					domNode.scrollLeft = 0;
				}
			}
98

99
			this.scrollable.updateState(newScrollPosition);
E
Erich Gamma 已提交
100 101 102
		};

		// I've seen this happen both on the view dom node & on the lines content dom node.
J
Johannes Rieken 已提交
103 104 105
		this.toDispose.push(dom.addDisposableListener(viewDomNode, 'scroll', (e: Event) => onBrowserDesperateReveal(viewDomNode, true, true)));
		this.toDispose.push(dom.addDisposableListener(linesContent, 'scroll', (e: Event) => onBrowserDesperateReveal(linesContent, true, false)));
		this.toDispose.push(dom.addDisposableListener(overflowGuardDomNode, 'scroll', (e: Event) => onBrowserDesperateReveal(overflowGuardDomNode, true, false)));
E
Erich Gamma 已提交
106 107 108
	}

	public dispose(): void {
J
Joao Moreno 已提交
109
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
110 111
	}

112 113 114 115
	public renderScrollbar(): void {
		this.scrollbar.renderNow();
	}

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

	public getScrollbarContainerDomNode(): HTMLElement {
121
		return this.scrollbar.getDomNode();
E
Erich Gamma 已提交
122 123
	}

J
Johannes Rieken 已提交
124
	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
125 126
		this.scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}
127 128 129 130

	public getVerticalSliderVerticalCenter(): number {
		return this.scrollbar.getVerticalSliderVerticalCenter();
	}
E
Erich Gamma 已提交
131
}