editorScrollbar.ts 6.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';
J
Johannes Rieken 已提交
7
import { ScrollableElementCreationOptions, ScrollableElementChangeOptions } from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
8
import { IOverviewRulerLayoutInfo, SmoothScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
A
Alex Dima 已提交
9 10 11 12
import { INewScrollPosition } from 'vs/editor/common/editorCommon';
import { ViewPart, PartFingerprint, PartFingerprints } from 'vs/editor/browser/view/viewPart';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import * as viewEvents from 'vs/editor/common/view/viewEvents';
A
Alex Dima 已提交
13
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/common/view/renderingContext';
A
Alex Dima 已提交
14
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
15
import { getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
B
Benjamin Pasero 已提交
16
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
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,
E
Erich Gamma 已提交
51 52
		};

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

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

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

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

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

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

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

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

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

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

		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 已提交
108 109 110
		this.scrollbarDomNode.setHeight(layoutInfo.contentHeight);
	}

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

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

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

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

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

	// --- end event handlers

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

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