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,
A
Alex Dima 已提交
47
			saveLastScrollTimeOnClassName: ClassNames.VIEW_LINE
E
Erich Gamma 已提交
48 49 50 51 52 53 54 55 56 57 58
		};
		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');

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

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

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

		// 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 已提交
80
		let onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
A
Alex Dima 已提交
81
			const scrollState = this.scrollable.getState();
J
Johannes Rieken 已提交
82
			let newScrollPosition: INewScrollPosition = {};
83

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

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

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

		// I've seen this happen both on the view dom node & on the lines content dom node.
J
Johannes Rieken 已提交
104 105 106
		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 已提交
107 108 109
	}

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

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

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

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

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