viewImpl.ts 21.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
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
A
Alex Dima 已提交
8 9 10 11
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
J
Johannes Rieken 已提交
12
import { PointerHandler } from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
13
import { ITextAreaHandlerHelper, TextAreaHandler } from 'vs/editor/browser/controller/textAreaHandler';
A
Alex Dima 已提交
14
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
15 16
import { ICommandDelegate, ViewController } from 'vs/editor/browser/view/viewController';
import { ViewOutgoingEvents } from 'vs/editor/browser/view/viewOutgoingEvents';
J
Johannes Rieken 已提交
17
import { ContentViewOverlays, MarginViewOverlays } from 'vs/editor/browser/view/viewOverlays';
A
Alex Dima 已提交
18
import { PartFingerprint, PartFingerprints, ViewPart } from 'vs/editor/browser/view/viewPart';
J
Johannes Rieken 已提交
19 20
import { ViewContentWidgets } from 'vs/editor/browser/viewParts/contentWidgets/contentWidgets';
import { CurrentLineHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight';
21
import { CurrentLineMarginHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineMarginHighlight/currentLineMarginHighlight';
J
Johannes Rieken 已提交
22
import { DecorationsOverlay } from 'vs/editor/browser/viewParts/decorations/decorations';
A
Alex Dima 已提交
23
import { EditorScrollbar } from 'vs/editor/browser/viewParts/editorScrollbar/editorScrollbar';
J
Johannes Rieken 已提交
24 25
import { GlyphMarginOverlay } from 'vs/editor/browser/viewParts/glyphMargin/glyphMargin';
import { IndentGuidesOverlay } from 'vs/editor/browser/viewParts/indentGuides/indentGuides';
A
Alex Dima 已提交
26
import { LineNumbersOverlay } from 'vs/editor/browser/viewParts/lineNumbers/lineNumbers';
J
Johannes Rieken 已提交
27 28
import { ViewLines } from 'vs/editor/browser/viewParts/lines/viewLines';
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
A
Alex Dima 已提交
29
import { Margin } from 'vs/editor/browser/viewParts/margin/margin';
30
import { MarginViewLineDecorationsOverlay } from 'vs/editor/browser/viewParts/marginDecorations/marginDecorations';
A
Alex Dima 已提交
31
import { Minimap } from 'vs/editor/browser/viewParts/minimap/minimap';
J
Johannes Rieken 已提交
32 33 34 35 36 37 38 39
import { ViewOverlayWidgets } from 'vs/editor/browser/viewParts/overlayWidgets/overlayWidgets';
import { DecorationsOverviewRuler } from 'vs/editor/browser/viewParts/overviewRuler/decorationsOverviewRuler';
import { OverviewRuler } from 'vs/editor/browser/viewParts/overviewRuler/overviewRuler';
import { Rulers } from 'vs/editor/browser/viewParts/rulers/rulers';
import { ScrollDecorationViewPart } from 'vs/editor/browser/viewParts/scrollDecoration/scrollDecoration';
import { SelectionsOverlay } from 'vs/editor/browser/viewParts/selections/selections';
import { ViewCursors } from 'vs/editor/browser/viewParts/viewCursors/viewCursors';
import { ViewZones } from 'vs/editor/browser/viewParts/viewZones/viewZones';
A
Alex Dima 已提交
40 41 42
import { Cursor } from 'vs/editor/common/controller/cursor';
import { Position } from 'vs/editor/common/core/position';
import { IConfiguration } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
43
import { RenderingContext } from 'vs/editor/common/view/renderingContext';
A
Alex Dima 已提交
44 45
import { ViewContext } from 'vs/editor/common/view/viewContext';
import { ViewEventDispatcher } from 'vs/editor/common/view/viewEventDispatcher';
46
import * as viewEvents from 'vs/editor/common/view/viewEvents';
A
Alex Dima 已提交
47 48 49
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
50
import { IThemeService, getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
E
Erich Gamma 已提交
51

A
Alex Dima 已提交
52 53
export interface IContentWidgetData {
	widget: editorBrowser.IContentWidget;
A
Alex Dima 已提交
54
	position: editorBrowser.IContentWidgetPosition | null;
A
Alex Dima 已提交
55 56 57 58
}

export interface IOverlayWidgetData {
	widget: editorBrowser.IOverlayWidget;
A
Alex Dima 已提交
59
	position: editorBrowser.IOverlayWidgetPosition | null;
A
Alex Dima 已提交
60 61
}

A
Alex Dima 已提交
62 63
const invalidFunc = () => { throw new Error(`Invalid change accessor`); };

A
Alex Dima 已提交
64
export class View extends ViewEventHandler {
E
Erich Gamma 已提交
65

66
	private readonly eventDispatcher: ViewEventDispatcher;
E
Erich Gamma 已提交
67

68
	private _scrollbar: EditorScrollbar;
69 70
	private readonly _context: ViewContext;
	private readonly _cursor: Cursor;
E
Erich Gamma 已提交
71 72 73 74 75 76 77 78

	// The view lines
	private viewLines: ViewLines;

	// These are parts, but we must do some API related calls on them, so we keep a reference
	private viewZones: ViewZones;
	private contentWidgets: ViewContentWidgets;
	private overlayWidgets: ViewOverlayWidgets;
79
	private viewCursors: ViewCursors;
A
Alex Dima 已提交
80
	private viewParts: ViewPart[];
E
Erich Gamma 已提交
81

82 83
	private readonly _textAreaHandler: TextAreaHandler;
	private readonly pointerHandler: PointerHandler;
E
Erich Gamma 已提交
84

85
	private readonly outgoingEvents: ViewOutgoingEvents;
E
Erich Gamma 已提交
86 87

	// Dom nodes
A
Alex Dima 已提交
88 89 90
	private linesContent: FastDomNode<HTMLElement>;
	public domNode: FastDomNode<HTMLElement>;
	private overflowGuardContainer: FastDomNode<HTMLElement>;
E
Erich Gamma 已提交
91 92

	// Actual mutable state
A
Alex Dima 已提交
93
	private _renderAnimationFrame: IDisposable | null;
E
Erich Gamma 已提交
94

A
Alex Dima 已提交
95
	constructor(
96
		commandDelegate: ICommandDelegate,
A
Alex Dima 已提交
97
		configuration: IConfiguration,
98
		themeService: IThemeService,
J
Johannes Rieken 已提交
99
		model: IViewModel,
100
		cursor: Cursor,
A
Alex Dima 已提交
101
		outgoingEvents: ViewOutgoingEvents
A
Alex Dima 已提交
102
	) {
E
Erich Gamma 已提交
103
		super();
104
		this._cursor = cursor;
E
Erich Gamma 已提交
105
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
106
		this.outgoingEvents = outgoingEvents;
E
Erich Gamma 已提交
107

M
Matt Bierner 已提交
108
		const viewController = new ViewController(configuration, model, this.outgoingEvents, commandDelegate);
E
Erich Gamma 已提交
109 110

		// The event dispatcher will always go through _renderOnce before dispatching any events
J
Johannes Rieken 已提交
111
		this.eventDispatcher = new ViewEventDispatcher((callback: () => void) => this._renderOnce(callback));
E
Erich Gamma 已提交
112

113 114 115
		// Ensure the view is the first event handler in order to update the layout
		this.eventDispatcher.addEventHandler(this);

E
Erich Gamma 已提交
116
		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
117 118 119
		this._context = new ViewContext(configuration, themeService.getTheme(), model, this.eventDispatcher);

		this._register(themeService.onThemeChange(theme => {
120
			this._context.theme = theme;
121
			this.eventDispatcher.emit(new viewEvents.ViewThemeChangedEvent());
122
			this.render(true, false);
123
		}));
E
Erich Gamma 已提交
124

125 126
		this.viewParts = [];

127 128
		// Keyboard handler
		this._textAreaHandler = new TextAreaHandler(this._context, viewController, this.createTextAreaHandlerHelper());
129
		this.viewParts.push(this._textAreaHandler);
130

A
Alex Dima 已提交
131
		// These two dom nodes must be constructed up front, since references are needed in the layout provider (scrolling & co.)
A
Alex Dima 已提交
132
		this.linesContent = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
133
		this.linesContent.setClassName('lines-content' + ' monaco-editor-background');
A
Alex Dima 已提交
134
		this.linesContent.setPosition('absolute');
A
Alex Dima 已提交
135

A
Alex Dima 已提交
136
		this.domNode = createFastDomNode(document.createElement('div'));
137
		this.domNode.setClassName(this.getEditorClassName());
A
Alex Dima 已提交
138

A
Alex Dima 已提交
139
		this.overflowGuardContainer = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
140
		PartFingerprints.write(this.overflowGuardContainer, PartFingerprint.OverflowGuard);
A
Alex Dima 已提交
141
		this.overflowGuardContainer.setClassName('overflow-guard');
A
Alex Dima 已提交
142

143
		this._scrollbar = new EditorScrollbar(this._context, this.linesContent, this.domNode, this.overflowGuardContainer);
A
Alex Dima 已提交
144 145
		this.viewParts.push(this._scrollbar);

E
Erich Gamma 已提交
146
		// View Lines
147
		this.viewLines = new ViewLines(this._context, this.linesContent);
E
Erich Gamma 已提交
148 149

		// View Zones
150
		this.viewZones = new ViewZones(this._context);
E
Erich Gamma 已提交
151 152 153
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
M
Matt Bierner 已提交
154
		const decorationsOverviewRuler = new DecorationsOverviewRuler(this._context);
E
Erich Gamma 已提交
155 156 157
		this.viewParts.push(decorationsOverviewRuler);


M
Matt Bierner 已提交
158
		const scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
159 160
		this.viewParts.push(scrollDecoration);

M
Matt Bierner 已提交
161
		const contentViewOverlays = new ContentViewOverlays(this._context);
E
Erich Gamma 已提交
162
		this.viewParts.push(contentViewOverlays);
A
Alex Dima 已提交
163
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
164
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
165
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
166
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
E
Erich Gamma 已提交
167

M
Matt Bierner 已提交
168
		const marginViewOverlays = new MarginViewOverlays(this._context);
E
Erich Gamma 已提交
169
		this.viewParts.push(marginViewOverlays);
A
Alex Dima 已提交
170
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
171
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
172
		marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
173 174
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
175

M
Matt Bierner 已提交
176
		const margin = new Margin(this._context);
A
Alex Dima 已提交
177
		margin.getDomNode().appendChild(this.viewZones.marginDomNode);
A
Alex Dima 已提交
178
		margin.getDomNode().appendChild(marginViewOverlays.getDomNode());
179
		this.viewParts.push(margin);
E
Erich Gamma 已提交
180 181

		// Content widgets
182
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
183 184
		this.viewParts.push(this.contentWidgets);

185 186
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
187 188

		// Overlay widgets
189
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
190 191
		this.viewParts.push(this.overlayWidgets);

M
Matt Bierner 已提交
192
		const rulers = new Rulers(this._context);
193 194
		this.viewParts.push(rulers);

M
Matt Bierner 已提交
195
		const minimap = new Minimap(this._context);
A
Alex Dima 已提交
196 197
		this.viewParts.push(minimap);

E
Erich Gamma 已提交
198 199 200
		// -------------- Wire dom nodes up

		if (decorationsOverviewRuler) {
M
Matt Bierner 已提交
201
			const overviewRulerData = this._scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
202 203 204
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

A
Alex Dima 已提交
205 206 207 208 209 210 211 212 213
		this.linesContent.appendChild(contentViewOverlays.getDomNode());
		this.linesContent.appendChild(rulers.domNode);
		this.linesContent.appendChild(this.viewZones.domNode);
		this.linesContent.appendChild(this.viewLines.getDomNode());
		this.linesContent.appendChild(this.contentWidgets.domNode);
		this.linesContent.appendChild(this.viewCursors.getDomNode());
		this.overflowGuardContainer.appendChild(margin.getDomNode());
		this.overflowGuardContainer.appendChild(this._scrollbar.getDomNode());
		this.overflowGuardContainer.appendChild(scrollDecoration.getDomNode());
214 215
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textArea);
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textAreaCover);
216
		this.overflowGuardContainer.appendChild(this.overlayWidgets.getDomNode());
A
Alex Dima 已提交
217 218 219
		this.overflowGuardContainer.appendChild(minimap.getDomNode());
		this.domNode.appendChild(this.overflowGuardContainer);
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
A
Alex Dima 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232

		this._setLayout();

		// Pointer handler
		this.pointerHandler = this._register(new PointerHandler(this._context, viewController, this.createPointerHandlerHelper()));

		this._register(model.addEventListener((events: viewEvents.ViewEvent[]) => {
			this.eventDispatcher.emitMany(events);
		}));

		this._register(this._cursor.addEventListener((events: viewEvents.ViewEvent[]) => {
			this.eventDispatcher.emitMany(events);
		}));
E
Erich Gamma 已提交
233 234 235 236 237 238
	}

	private _flushAccumulatedAndRenderNow(): void {
		this._renderNow();
	}

239
	private createPointerHandlerHelper(): IPointerHandlerHelper {
E
Erich Gamma 已提交
240
		return {
A
Alex Dima 已提交
241 242
			viewDomNode: this.domNode.domNode,
			linesContentDomNode: this.linesContent.domNode,
E
Erich Gamma 已提交
243 244 245 246 247

			focusTextArea: () => {
				this.focus();
			},

248 249 250
			getLastViewCursorsRenderData: () => {
				return this.viewCursors.getLastRenderData() || [];
			},
A
Alex Dima 已提交
251
			shouldSuppressMouseDownOnViewZone: (viewZoneId: string) => {
E
Erich Gamma 已提交
252 253
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},
254 255 256
			shouldSuppressMouseDownOnWidget: (widgetId: string) => {
				return this.contentWidgets.shouldSuppressMouseDownOnWidget(widgetId);
			},
E
Erich Gamma 已提交
257 258 259 260 261 262 263
			getPositionFromDOMInfo: (spanNode: HTMLElement, offset: number) => {
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getPositionFromDOMInfo(spanNode, offset);
			},

			visibleRangeForPosition2: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
264
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
265 266 267 268 269 270 271 272 273
			},

			getLineWidth: (lineNumber: number) => {
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getLineWidth(lineNumber);
			}
		};
	}

274
	private createTextAreaHandlerHelper(): ITextAreaHandlerHelper {
E
Erich Gamma 已提交
275 276 277
		return {
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
278
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
279 280 281 282
			}
		};
	}

A
Alex Dima 已提交
283 284
	private _setLayout(): void {
		const layoutInfo = this._context.configuration.editor.layoutInfo;
A
Alex Dima 已提交
285 286
		this.domNode.setWidth(layoutInfo.width);
		this.domNode.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
287

A
Alex Dima 已提交
288 289
		this.overflowGuardContainer.setWidth(layoutInfo.width);
		this.overflowGuardContainer.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
290

A
Alex Dima 已提交
291 292
		this.linesContent.setWidth(1000000);
		this.linesContent.setHeight(1000000);
E
Erich Gamma 已提交
293 294

	}
295

296
	private getEditorClassName() {
M
Matt Bierner 已提交
297
		const focused = this._textAreaHandler.isFocused() ? ' focused' : '';
S
smoke:  
Sandeep Somavarapu 已提交
298
		return this._context.configuration.editor.editorClassName + ' ' + getThemeTypeSelector(this._context.theme.type) + focused;
299 300
	}

301 302
	// --- begin event handlers

A
Alex Dima 已提交
303
	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
304
		if (e.editorClassName) {
305
			this.domNode.setClassName(this.getEditorClassName());
E
Erich Gamma 已提交
306
		}
A
Alex Dima 已提交
307 308 309
		if (e.layoutInfo) {
			this._setLayout();
		}
310
		return false;
E
Erich Gamma 已提交
311
	}
A
Alex Dima 已提交
312
	public onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {
S
smoke:  
Sandeep Somavarapu 已提交
313
		this.domNode.setClassName(this.getEditorClassName());
314
		this._context.model.setHasFocus(e.isFocused);
A
Alex Dima 已提交
315
		if (e.isFocused) {
A
Alex Dima 已提交
316
			this.outgoingEvents.emitViewFocusGained();
E
Erich Gamma 已提交
317
		} else {
A
Alex Dima 已提交
318
			this.outgoingEvents.emitViewFocusLost();
E
Erich Gamma 已提交
319 320 321
		}
		return false;
	}
322 323 324 325
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		this.outgoingEvents.emitScrollChanged(e);
		return false;
	}
326 327 328 329
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.domNode.setClassName(this.getEditorClassName());
		return false;
	}
330

E
Erich Gamma 已提交
331 332 333 334 335 336 337 338 339
	// --- end event handlers

	public dispose(): void {
		if (this._renderAnimationFrame !== null) {
			this._renderAnimationFrame.dispose();
			this._renderAnimationFrame = null;
		}

		this.eventDispatcher.removeEventHandler(this);
A
Alex Dima 已提交
340
		this.outgoingEvents.dispose();
E
Erich Gamma 已提交
341 342 343

		this.viewLines.dispose();

A
Alex Dima 已提交
344
		// Destroy view parts
A
Alex Dima 已提交
345
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
346 347 348 349
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

A
Alex Dima 已提交
350
		super.dispose();
E
Erich Gamma 已提交
351 352
	}

A
Alex Dima 已提交
353
	private _renderOnce(callback: () => any): any {
M
Matt Bierner 已提交
354
		const r = safeInvokeNoArg(callback);
A
Alex Dima 已提交
355 356 357
		this._scheduleRender();
		return r;
	}
E
Erich Gamma 已提交
358

A
Alex Dima 已提交
359 360 361 362 363
	private _scheduleRender(): void {
		if (this._renderAnimationFrame === null) {
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
		}
	}
A
Alex Dima 已提交
364

A
Alex Dima 已提交
365 366 367 368
	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}
369

A
Alex Dima 已提交
370 371 372
	private _renderNow(): void {
		safeInvokeNoArg(() => this._actualRender());
	}
A
Alex Dima 已提交
373

A
Alex Dima 已提交
374
	private _getViewPartsToRender(): ViewPart[] {
A
Alex Dima 已提交
375
		let result: ViewPart[] = [], resultLen = 0;
A
Alex Dima 已提交
376
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
M
Matt Bierner 已提交
377
			const viewPart = this.viewParts[i];
A
Alex Dima 已提交
378
			if (viewPart.shouldRender()) {
A
Alex Dima 已提交
379
				result[resultLen++] = viewPart;
A
Alex Dima 已提交
380 381 382 383
			}
		}
		return result;
	}
384

A
Alex Dima 已提交
385 386 387 388
	private _actualRender(): void {
		if (!dom.isInDOM(this.domNode.domNode)) {
			return;
		}
389

A
Alex Dima 已提交
390
		let viewPartsToRender = this._getViewPartsToRender();
A
Alex Dima 已提交
391

A
Alex Dima 已提交
392 393 394 395
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
			return;
		}
A
Alex Dima 已提交
396

397
		const partialViewportData = this._context.viewLayout.getLinesViewportData();
A
Alex Dima 已提交
398
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
399

M
Matt Bierner 已提交
400
		const viewportData = new ViewportData(
401 402 403 404 405
			this._cursor.getViewSelections(),
			partialViewportData,
			this._context.viewLayout.getWhitespaceViewportData(),
			this._context.model
		);
A
Alex Dima 已提交
406

407 408 409 410 411
		if (this.contentWidgets.shouldRender()) {
			// Give the content widgets a chance to set their max width before a possible synchronous layout
			this.contentWidgets.onBeforeRender(viewportData);
		}

A
Alex Dima 已提交
412
		if (this.viewLines.shouldRender()) {
413
			this.viewLines.renderText(viewportData);
A
Alex Dima 已提交
414
			this.viewLines.onDidRender();
A
Alex Dima 已提交
415

A
Alex Dima 已提交
416 417 418
			// Rendering of viewLines might cause scroll events to occur, so collect view parts to render again
			viewPartsToRender = this._getViewPartsToRender();
		}
A
Alex Dima 已提交
419

M
Matt Bierner 已提交
420
		const renderingContext = new RenderingContext(this._context.viewLayout, viewportData, this.viewLines);
A
Alex Dima 已提交
421

A
Alex Dima 已提交
422 423
		// Render the rest of the parts
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
424
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
425 426
			viewPart.prepareRender(renderingContext);
		}
427

A
Alex Dima 已提交
428
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
M
Matt Bierner 已提交
429
			const viewPart = viewPartsToRender[i];
A
Alex Dima 已提交
430 431
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
432 433 434
		}
	}

A
Alex Dima 已提交
435 436
	// --- BEGIN CodeEditor helpers

437
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
A
Alex Dima 已提交
438 439 440
		this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}

441 442
	public restoreState(scrollPosition: { scrollLeft: number; scrollTop: number; }): void {
		this._context.viewLayout.setScrollPositionNow({ scrollTop: scrollPosition.scrollTop });
443
		this._context.model.tokenizeViewport();
444 445 446 447 448
		this._renderNow();
		this.viewLines.updateLineWidths();
		this._context.viewLayout.setScrollPositionNow({ scrollLeft: scrollPosition.scrollLeft });
	}

A
Alex Dima 已提交
449
	public getOffsetForColumn(modelLineNumber: number, modelColumn: number): number {
M
Matt Bierner 已提交
450
		const modelPosition = this._context.model.validateModelPosition({
A
Alex Dima 已提交
451 452 453
			lineNumber: modelLineNumber,
			column: modelColumn
		});
M
Matt Bierner 已提交
454
		const viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
A
Alex Dima 已提交
455
		this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
456 457
		const visibleRange = this.viewLines.visibleRangeForPosition(new Position(viewPosition.lineNumber, viewPosition.column));
		if (!visibleRange) {
A
Alex Dima 已提交
458
			return -1;
E
Erich Gamma 已提交
459
		}
A
Alex Dima 已提交
460
		return visibleRange.left;
A
Alex Dima 已提交
461 462
	}

A
Alex Dima 已提交
463
	public getTargetAtClientPoint(clientX: number, clientY: number): editorBrowser.IMouseTarget | null {
A
Alex Dima 已提交
464 465 466
		return this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
	}

467 468
	public createOverviewRuler(cssClassName: string): OverviewRuler {
		return new OverviewRuler(this._context, cssClassName);
E
Erich Gamma 已提交
469 470
	}

A
Alex Dima 已提交
471
	public change(callback: (changeAccessor: editorBrowser.IViewZoneChangeAccessor) => any): boolean {
A
Alex Dima 已提交
472
		let zonesHaveChanged = false;
A
Alex Dima 已提交
473

E
Erich Gamma 已提交
474
		this._renderOnce(() => {
M
Matt Bierner 已提交
475
			const changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
A
Alex Dima 已提交
476
				addZone: (zone: editorBrowser.IViewZone): string => {
E
Erich Gamma 已提交
477 478 479
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
A
Alex Dima 已提交
480
				removeZone: (id: string): void => {
481 482 483
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
484 485
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
A
Alex Dima 已提交
486
				layoutZone: (id: string): void => {
487 488 489
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
490 491 492 493
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

A
Alex Dima 已提交
494
			safeInvoke1Arg(callback, changeAccessor);
E
Erich Gamma 已提交
495 496

			// Invalidate changeAccessor
A
Alex Dima 已提交
497 498 499
			changeAccessor.addZone = invalidFunc;
			changeAccessor.removeZone = invalidFunc;
			changeAccessor.layoutZone = invalidFunc;
E
Erich Gamma 已提交
500 501

			if (zonesHaveChanged) {
502
				this._context.viewLayout.onHeightMaybeChanged();
503
				this._context.privateViewEventBus.emit(new viewEvents.ViewZonesChangedEvent());
E
Erich Gamma 已提交
504 505 506 507 508
			}
		});
		return zonesHaveChanged;
	}

A
Alex Dima 已提交
509 510 511 512 513
	public render(now: boolean, everything: boolean): void {
		if (everything) {
			// Force everything to render...
			this.viewLines.forceShouldRender();
			for (let i = 0, len = this.viewParts.length; i < len; i++) {
M
Matt Bierner 已提交
514
				const viewPart = this.viewParts[i];
A
Alex Dima 已提交
515 516 517 518 519 520 521 522 523 524 525
				viewPart.forceShouldRender();
			}
		}
		if (now) {
			this._flushAccumulatedAndRenderNow();
		} else {
			this._scheduleRender();
		}
	}

	public focus(): void {
526
		this._textAreaHandler.focusTextArea();
A
Alex Dima 已提交
527 528 529
	}

	public isFocused(): boolean {
530
		return this._textAreaHandler.isFocused();
A
Alex Dima 已提交
531 532
	}

A
Alex Dima 已提交
533
	public addContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
534 535 536
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
537 538
	}

A
Alex Dima 已提交
539
	public layoutContentWidget(widgetData: IContentWidgetData): void {
M
Matt Bierner 已提交
540
		const newPosition = widgetData.position ? widgetData.position.position : null;
A
Alex Dima 已提交
541
		const newRange = widgetData.position ? widgetData.position.range || null : null;
M
Matt Bierner 已提交
542
		const newPreference = widgetData.position ? widgetData.position.preference : null;
A
Alex Dima 已提交
543
		this.contentWidgets.setWidgetPosition(widgetData.widget, newPosition, newRange, newPreference);
A
Alex Dima 已提交
544
		this._scheduleRender();
E
Erich Gamma 已提交
545 546
	}

A
Alex Dima 已提交
547
	public removeContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
548 549
		this.contentWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
550 551
	}

A
Alex Dima 已提交
552
	public addOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
553 554 555
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
556 557
	}

A
Alex Dima 已提交
558
	public layoutOverlayWidget(widgetData: IOverlayWidgetData): void {
M
Matt Bierner 已提交
559 560
		const newPreference = widgetData.position ? widgetData.position.preference : null;
		const shouldRender = this.overlayWidgets.setWidgetPosition(widgetData.widget, newPreference);
561 562 563
		if (shouldRender) {
			this._scheduleRender();
		}
E
Erich Gamma 已提交
564 565
	}

A
Alex Dima 已提交
566
	public removeOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
567 568
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
569 570
	}

A
Alex Dima 已提交
571
	// --- END CodeEditor helpers
E
Erich Gamma 已提交
572 573 574

}

J
Johannes Rieken 已提交
575
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
576 577
	try {
		return func();
J
Johannes Rieken 已提交
578
	} catch (e) {
A
Alex Dima 已提交
579 580 581 582
		onUnexpectedError(e);
	}
}

J
Johannes Rieken 已提交
583
function safeInvoke1Arg(func: Function, arg1: any): any {
A
Alex Dima 已提交
584 585
	try {
		return func(arg1);
J
Johannes Rieken 已提交
586
	} catch (e) {
A
Alex Dima 已提交
587 588 589
		onUnexpectedError(e);
	}
}