editorScrollbar.ts 6.7 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 21
	private readonly scrollbar: SmoothScrollableElement;
	private readonly scrollbarDomNode: FastDomNode<HTMLElement>;
A
Alex Dima 已提交
22 23 24 25 26 27 28

	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

M
Matt Bierner 已提交
34
		const 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,
52
			scrollPredominantAxisOnly: configScrollbarOpts.scrollPredominantAxisOnly,
E
Erich Gamma 已提交
53 54
		};

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

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

E
Erich Gamma 已提交
62 63 64 65
		// 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

M
Matt Bierner 已提交
66 67
		const onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
			const newScrollPosition: INewScrollPosition = {};
68

E
Erich Gamma 已提交
69
			if (lookAtScrollTop) {
M
Matt Bierner 已提交
70
				const deltaTop = domNode.scrollTop;
E
Erich Gamma 已提交
71
				if (deltaTop) {
72
					newScrollPosition.scrollTop = this._context.viewLayout.getCurrentScrollTop() + deltaTop;
E
Erich Gamma 已提交
73 74 75 76 77
					domNode.scrollTop = 0;
				}
			}

			if (lookAtScrollLeft) {
M
Matt Bierner 已提交
78
				const deltaLeft = domNode.scrollLeft;
E
Erich Gamma 已提交
79
				if (deltaLeft) {
80
					newScrollPosition.scrollLeft = this._context.viewLayout.getCurrentScrollLeft() + deltaLeft;
E
Erich Gamma 已提交
81 82 83
					domNode.scrollLeft = 0;
				}
			}
84

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

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

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

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

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

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

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

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

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

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

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

	// --- end event handlers

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

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