editorScrollbar.ts 6.0 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';

A
Alex Dima 已提交
7
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
8 9
import { ScrollableElementCreationOptions, ScrollableElementChangeOptions } from 'vs/base/browser/ui/scrollbar/scrollableElementOptions';
import { IOverviewRulerLayoutInfo, ScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
A
Alex Dima 已提交
10 11 12 13
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 已提交
14
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/common/view/renderingContext';
A
Alex Dima 已提交
15
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
E
Erich Gamma 已提交
16

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

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

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

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

A
Alex Dima 已提交
33
		let scrollbarOptions: ScrollableElementCreationOptions = {
A
Alex Dima 已提交
34
			canUseTranslate3d: editor.canUseTranslate3d,
A
Alex Dima 已提交
35
			listenOnDomNode: viewDomNode.domNode,
A
Alex Dima 已提交
36
			className: 'editor-scrollable' + ' ' + editor.viewInfo.theme,
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 ScrollableElement(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.getScrollTop() + 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.getScrollLeft() + deltaLeft;
E
Erich Gamma 已提交
79 80 81
					domNode.scrollLeft = 0;
				}
			}
82

83
			this._context.viewLayout.setScrollPosition(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)));
E
Erich Gamma 已提交
90 91 92
	}

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

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

		this.scrollbarDomNode.setLeft(layoutInfo.contentLeft);
		this.scrollbarDomNode.setWidth(layoutInfo.contentWidth + layoutInfo.minimapWidth);
		this.scrollbarDomNode.setHeight(layoutInfo.contentHeight);
	}

A
Alex Dima 已提交
104
	public getOverviewRulerLayoutInfo(): IOverviewRulerLayoutInfo {
105
		return this.scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
106 107
	}

A
Alex Dima 已提交
108 109
	public getDomNode(): FastDomNode<HTMLElement> {
		return this.scrollbarDomNode;
E
Erich Gamma 已提交
110 111
	}

J
Johannes Rieken 已提交
112
	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
113 114
		this.scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}
115 116 117 118

	public getVerticalSliderVerticalCenter(): number {
		return this.scrollbar.getVerticalSliderVerticalCenter();
	}
A
Alex Dima 已提交
119 120 121 122

	// --- begin event handlers

	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
123
		const editor = this._context.configuration.editor;
A
Alex Dima 已提交
124

A
Alex Dima 已提交
125 126
		this.scrollbar.updateClassName('editor-scrollable' + ' ' + editor.viewInfo.theme);
		if (e.viewInfo || e.canUseTranslate3d) {
A
Alex Dima 已提交
127
			let newOpts: ScrollableElementChangeOptions = {
A
Alex Dima 已提交
128 129 130
				canUseTranslate3d: editor.canUseTranslate3d,
				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 142 143 144
		return true;
	}
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		return true;
	}

	// --- end event handlers

A
Alex Dima 已提交
145
	public prepareRender(ctx: RenderingContext): void {
A
Alex Dima 已提交
146 147 148
		// Nothing to do
	}

A
Alex Dima 已提交
149
	public render(ctx: RestrictedRenderingContext): void {
A
Alex Dima 已提交
150 151
		this.scrollbar.renderNow();
	}
E
Erich Gamma 已提交
152
}