editorScrollbar.ts 7.0 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';
17
import { EditorOptionId } from 'vs/editor/common/config/editorOptions';
E
Erich Gamma 已提交
18

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

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

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

A
Alex Dima 已提交
32 33

		const options = this._context.configuration.options;
34 35 36
		const scrollbar = options.get(EditorOptionId.scrollbar);
		const mouseWheelScrollSensitivity = options.get(EditorOptionId.mouseWheelScrollSensitivity);
		const fastScrollSensitivity = options.get(EditorOptionId.fastScrollSensitivity);
E
Erich Gamma 已提交
37

M
Matt Bierner 已提交
38
		const scrollbarOptions: ScrollableElementCreationOptions = {
A
Alex Dima 已提交
39
			listenOnDomNode: viewDomNode.domNode,
40
			className: 'editor-scrollable' + ' ' + getThemeTypeSelector(context.theme.type),
A
Alex Dima 已提交
41 42 43
			useShadows: false,
			lazyRender: true,

A
Alex Dima 已提交
44 45 46 47 48 49 50 51 52 53 54 55
			vertical: scrollbar.vertical,
			horizontal: scrollbar.horizontal,
			verticalHasArrows: scrollbar.verticalHasArrows,
			horizontalHasArrows: scrollbar.horizontalHasArrows,
			verticalScrollbarSize: scrollbar.verticalScrollbarSize,
			verticalSliderSize: scrollbar.verticalSliderSize,
			horizontalScrollbarSize: scrollbar.horizontalScrollbarSize,
			horizontalSliderSize: scrollbar.horizontalSliderSize,
			handleMouseWheel: scrollbar.handleMouseWheel,
			arrowSize: scrollbar.arrowSize,
			mouseWheelScrollSensitivity: mouseWheelScrollSensitivity,
			fastScrollSensitivity: fastScrollSensitivity,
E
Erich Gamma 已提交
56 57
		};

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

A
Alex Dima 已提交
61 62 63 64
		this.scrollbarDomNode = createFastDomNode(this.scrollbar.getDomNode());
		this.scrollbarDomNode.setPosition('absolute');
		this._setLayout();

E
Erich Gamma 已提交
65 66 67 68
		// 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 已提交
69 70
		const onBrowserDesperateReveal = (domNode: HTMLElement, lookAtScrollTop: boolean, lookAtScrollLeft: boolean) => {
			const newScrollPosition: INewScrollPosition = {};
71

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

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

88
			this._context.viewLayout.setScrollPositionNow(newScrollPosition);
E
Erich Gamma 已提交
89 90 91
		};

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

	public dispose(): void {
A
Alex Dima 已提交
99
		super.dispose();
E
Erich Gamma 已提交
100 101
	}

A
Alex Dima 已提交
102
	private _setLayout(): void {
A
Alex Dima 已提交
103
		const options = this._context.configuration.options;
104
		const layoutInfo = options.get(EditorOptionId.layoutInfo);
A
Alex Dima 已提交
105 106

		this.scrollbarDomNode.setLeft(layoutInfo.contentLeft);
107

108
		const minimap = options.get(EditorOptionId.minimap);
A
Alex Dima 已提交
109
		const side = minimap.side;
110 111 112 113 114
		if (side === 'right') {
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth + layoutInfo.minimapWidth);
		} else {
			this.scrollbarDomNode.setWidth(layoutInfo.contentWidth);
		}
A
Alex Dima 已提交
115 116 117
		this.scrollbarDomNode.setHeight(layoutInfo.contentHeight);
	}

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

A
Alex Dima 已提交
122 123
	public getDomNode(): FastDomNode<HTMLElement> {
		return this.scrollbarDomNode;
E
Erich Gamma 已提交
124 125
	}

126
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
127 128
		this.scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}
129

A
Alex Dima 已提交
130 131 132
	// --- begin event handlers

	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
133 134 135 136 137 138
		if (
			e.hasChanged(EditorOptionId.scrollbar)
			|| e.hasChanged(EditorOptionId.mouseWheelScrollSensitivity)
			|| e.hasChanged(EditorOptionId.fastScrollSensitivity)
		) {
			const options = this._context.configuration.options;
139 140 141
			const scrollbar = options.get(EditorOptionId.scrollbar);
			const mouseWheelScrollSensitivity = options.get(EditorOptionId.mouseWheelScrollSensitivity);
			const fastScrollSensitivity = options.get(EditorOptionId.fastScrollSensitivity);
M
Matt Bierner 已提交
142
			const newOpts: ScrollableElementChangeOptions = {
A
Alex Dima 已提交
143 144 145
				handleMouseWheel: scrollbar.handleMouseWheel,
				mouseWheelScrollSensitivity: mouseWheelScrollSensitivity,
				fastScrollSensitivity: fastScrollSensitivity
A
Alex Dima 已提交
146 147 148
			};
			this.scrollbar.updateOptions(newOpts);
		}
A
Alex Dima 已提交
149
		if (e.hasChanged(EditorOptionId.layoutInfo)) {
A
Alex Dima 已提交
150 151
			this._setLayout();
		}
A
Alex Dima 已提交
152 153 154 155 156
		return true;
	}
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		return true;
	}
157 158 159 160
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.scrollbar.updateClassName('editor-scrollable' + ' ' + getThemeTypeSelector(this._context.theme.type));
		return true;
	}
A
Alex Dima 已提交
161 162 163

	// --- end event handlers

A
Alex Dima 已提交
164
	public prepareRender(ctx: RenderingContext): void {
A
Alex Dima 已提交
165 166 167
		// Nothing to do
	}

A
Alex Dima 已提交
168
	public render(ctx: RestrictedRenderingContext): void {
A
Alex Dima 已提交
169 170
		this.scrollbar.renderNow();
	}
E
Erich Gamma 已提交
171
}