editorScrollbar.ts 6.5 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';
E
Erich Gamma 已提交
17

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

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

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

A
Alex Dima 已提交
31 32
		const editor = this._context.configuration.editor;
		const configScrollbarOpts = editor.viewInfo.scrollbar;
E
Erich Gamma 已提交
33

A
Alex Dima 已提交
34
		let scrollbarOptions: ScrollableElementCreationOptions = {
A
Alex Dima 已提交
35
			listenOnDomNode: viewDomNode.domNode,
36
			className: 'editor-scrollable' + ' ' + getThemeTypeSelector(context.theme.type),
A
Alex Dima 已提交
37 38 39
			useShadows: false,
			lazyRender: true,

E
Erich Gamma 已提交
40 41
			vertical: configScrollbarOpts.vertical,
			horizontal: configScrollbarOpts.horizontal,
A
Alex Dima 已提交
42 43 44 45 46 47 48 49 50
			verticalHasArrows: configScrollbarOpts.verticalHasArrows,
			horizontalHasArrows: configScrollbarOpts.horizontalHasArrows,
			verticalScrollbarSize: configScrollbarOpts.verticalScrollbarSize,
			verticalSliderSize: configScrollbarOpts.verticalSliderSize,
			horizontalScrollbarSize: configScrollbarOpts.horizontalScrollbarSize,
			horizontalSliderSize: configScrollbarOpts.horizontalSliderSize,
			handleMouseWheel: configScrollbarOpts.handleMouseWheel,
			arrowSize: configScrollbarOpts.arrowSize,
			mouseWheelScrollSensitivity: configScrollbarOpts.mouseWheelScrollSensitivity,
T
Tiago Ribeiro 已提交
51
			fastScrollSensitivity: configScrollbarOpts.fastScrollSensitivity,
E
Erich Gamma 已提交
52 53
		};

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

A
Alex Dima 已提交
57 58 59 60
		this.scrollbarDomNode = createFastDomNode(this.scrollbar.getDomNode());
		this.scrollbarDomNode.setPosition('absolute');
		this._setLayout();

E
Erich Gamma 已提交
61 62 63 64
		// 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 已提交
65
		let onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
J
Johannes Rieken 已提交
66
			let newScrollPosition: INewScrollPosition = {};
67

E
Erich Gamma 已提交
68
			if (lookAtScrollTop) {
A
Alex Dima 已提交
69
				let deltaTop = domNode.scrollTop;
E
Erich Gamma 已提交
70
				if (deltaTop) {
71
					newScrollPosition.scrollTop = this._context.viewLayout.getCurrentScrollTop() + deltaTop;
E
Erich Gamma 已提交
72 73 74 75 76
					domNode.scrollTop = 0;
				}
			}

			if (lookAtScrollLeft) {
A
Alex Dima 已提交
77
				let deltaLeft = domNode.scrollLeft;
E
Erich Gamma 已提交
78
				if (deltaLeft) {
79
					newScrollPosition.scrollLeft = this._context.viewLayout.getCurrentScrollLeft() + deltaLeft;
E
Erich Gamma 已提交
80 81 82
					domNode.scrollLeft = 0;
				}
			}
83

84
			this._context.viewLayout.setScrollPositionNow(newScrollPosition);
E
Erich Gamma 已提交
85 86 87
		};

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

	public dispose(): void {
A
Alex Dima 已提交
95
		super.dispose();
E
Erich Gamma 已提交
96 97
	}

A
Alex Dima 已提交
98 99 100 101
	private _setLayout(): void {
		const layoutInfo = this._context.configuration.editor.layoutInfo;

		this.scrollbarDomNode.setLeft(layoutInfo.contentLeft);
102 103 104 105 106 107 108

		const side = this._context.configuration.editor.viewInfo.minimap.side;
		if (side === 'right') {
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth + layoutInfo.minimapWidth);
		} else {
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth);
		}
A
Alex Dima 已提交
109 110 111
		this.scrollbarDomNode.setHeight(layoutInfo.contentHeight);
	}

A
Alex Dima 已提交
112
	public getOverviewRulerLayoutInfo(): IOverviewRulerLayoutInfo {
113
		return this.scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
114 115
	}

A
Alex Dima 已提交
116 117
	public getDomNode(): FastDomNode<HTMLElement> {
		return this.scrollbarDomNode;
E
Erich Gamma 已提交
118 119
	}

120
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
121 122
		this.scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}
123

A
Alex Dima 已提交
124 125 126
	// --- begin event handlers

	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
127
		if (e.viewInfo) {
128
			const editor = this._context.configuration.editor;
A
Alex Dima 已提交
129
			let newOpts: ScrollableElementChangeOptions = {
A
Alex Dima 已提交
130
				handleMouseWheel: editor.viewInfo.scrollbar.handleMouseWheel,
T
Tiago Ribeiro 已提交
131 132
				mouseWheelScrollSensitivity: editor.viewInfo.scrollbar.mouseWheelScrollSensitivity,
				fastScrollSensitivity: editor.viewInfo.scrollbar.fastScrollSensitivity
A
Alex Dima 已提交
133 134 135
			};
			this.scrollbar.updateOptions(newOpts);
		}
A
Alex Dima 已提交
136 137 138
		if (e.layoutInfo) {
			this._setLayout();
		}
A
Alex Dima 已提交
139 140 141 142 143
		return true;
	}
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		return true;
	}
144 145 146 147
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.scrollbar.updateClassName('editor-scrollable' + ' ' + getThemeTypeSelector(this._context.theme.type));
		return true;
	}
A
Alex Dima 已提交
148 149 150

	// --- end event handlers

A
Alex Dima 已提交
151
	public prepareRender(ctx: RenderingContext): void {
A
Alex Dima 已提交
152 153 154
		// Nothing to do
	}

A
Alex Dima 已提交
155
	public render(ctx: RestrictedRenderingContext): void {
A
Alex Dima 已提交
156 157
		this.scrollbar.renderNow();
	}
E
Erich Gamma 已提交
158
}