viewImpl.ts 21.4 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';

J
Johannes Rieken 已提交
7
import { onUnexpectedError } from 'vs/base/common/errors';
A
Alex Dima 已提交
8
import { IDisposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
9
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
10
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
J
Johannes Rieken 已提交
11 12 13 14
import { ICommandService } from 'vs/platform/commands/common/commands';
import { Range } from 'vs/editor/common/core/range';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
import { Configuration } from 'vs/editor/browser/config/configuration';
15
import { TextAreaHandler, ITextAreaHandlerHelper } from 'vs/editor/browser/controller/textAreaHandler';
J
Johannes Rieken 已提交
16
import { PointerHandler } from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
17
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
18
import { ViewController, ExecCoreEditorCommandFunc } from 'vs/editor/browser/view/viewController';
19
import { ViewEventDispatcher } from 'vs/editor/common/view/viewEventDispatcher';
J
Johannes Rieken 已提交
20 21 22
import { ContentViewOverlays, MarginViewOverlays } from 'vs/editor/browser/view/viewOverlays';
import { ViewContentWidgets } from 'vs/editor/browser/viewParts/contentWidgets/contentWidgets';
import { CurrentLineHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight';
23
import { CurrentLineMarginHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineMarginHighlight/currentLineMarginHighlight';
J
Johannes Rieken 已提交
24 25 26 27 28
import { DecorationsOverlay } from 'vs/editor/browser/viewParts/decorations/decorations';
import { GlyphMarginOverlay } from 'vs/editor/browser/viewParts/glyphMargin/glyphMargin';
import { LineNumbersOverlay } from 'vs/editor/browser/viewParts/lineNumbers/lineNumbers';
import { IndentGuidesOverlay } from 'vs/editor/browser/viewParts/indentGuides/indentGuides';
import { ViewLines } from 'vs/editor/browser/viewParts/lines/viewLines';
29
import { Margin } from 'vs/editor/browser/viewParts/margin/margin';
J
Johannes Rieken 已提交
30
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
31
import { MarginViewLineDecorationsOverlay } from 'vs/editor/browser/viewParts/marginDecorations/marginDecorations';
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
import { ViewPart, PartFingerprint, PartFingerprints } from 'vs/editor/browser/view/viewPart';
41
import { ViewContext } from 'vs/editor/common/view/viewContext';
J
Johannes Rieken 已提交
42
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
A
Alex Dima 已提交
43
import { RenderingContext } from 'vs/editor/common/view/renderingContext';
J
Johannes Rieken 已提交
44
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
A
Alex Dima 已提交
45
import { ViewOutgoingEvents } from 'vs/editor/browser/view/viewOutgoingEvents';
46
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
A
Alex Dima 已提交
47
import { EditorScrollbar } from 'vs/editor/browser/viewParts/editorScrollbar/editorScrollbar';
A
Alex Dima 已提交
48
import { Minimap } from 'vs/editor/browser/viewParts/minimap/minimap';
49
import * as viewEvents from 'vs/editor/common/view/viewEvents';
50
import { IThemeService, getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
51
import { Cursor } from "vs/editor/common/controller/cursor";
E
Erich Gamma 已提交
52

A
Alex Dima 已提交
53 54 55 56 57 58 59 60 61 62 63
export interface IContentWidgetData {
	widget: editorBrowser.IContentWidget;
	position: editorBrowser.IContentWidgetPosition;
}

export interface IOverlayWidgetData {
	widget: editorBrowser.IOverlayWidget;
	position: editorBrowser.IOverlayWidgetPosition;
}

export class View extends ViewEventHandler {
E
Erich Gamma 已提交
64

J
Johannes Rieken 已提交
65
	private eventDispatcher: ViewEventDispatcher;
E
Erich Gamma 已提交
66

67
	private _scrollbar: EditorScrollbar;
68
	private _context: ViewContext;
E
Erich Gamma 已提交
69 70 71 72 73 74 75 76

	// 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;
77
	private viewCursors: ViewCursors;
A
Alex Dima 已提交
78
	private viewParts: ViewPart[];
E
Erich Gamma 已提交
79

80 81
	private readonly _textAreaHandler: TextAreaHandler;
	private readonly pointerHandler: PointerHandler;
E
Erich Gamma 已提交
82

83
	private readonly outgoingEvents: ViewOutgoingEvents;
E
Erich Gamma 已提交
84 85

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

	// Actual mutable state
	private _isDisposed: boolean;

A
Alex Dima 已提交
93
	private _renderAnimationFrame: IDisposable;
E
Erich Gamma 已提交
94

A
Alex Dima 已提交
95
	constructor(
96
		commandService: ICommandService,
J
Johannes Rieken 已提交
97
		configuration: Configuration,
98
		themeService: IThemeService,
J
Johannes Rieken 已提交
99
		model: IViewModel,
100
		cursor: Cursor,
101
		execCoreEditorCommandFunc: ExecCoreEditorCommandFunc
A
Alex Dima 已提交
102
	) {
E
Erich Gamma 已提交
103 104 105
		super();
		this._isDisposed = false;
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
106
		this.outgoingEvents = new ViewOutgoingEvents(model);
E
Erich Gamma 已提交
107

108
		let viewController = new ViewController(model, execCoreEditorCommandFunc, this.outgoingEvents, commandService);
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

E
Erich Gamma 已提交
131
		this.createViewParts();
A
Alex Dima 已提交
132
		this._setLayout();
E
Erich Gamma 已提交
133 134

		// Pointer handler
135
		this.pointerHandler = new PointerHandler(this._context, viewController, this.createPointerHandlerHelper());
E
Erich Gamma 已提交
136

A
Alex Dima 已提交
137 138 139
		this._register(model.addEventListener((events: viewEvents.ViewEvent[]) => {
			this.eventDispatcher.emitMany(events);
		}));
140 141 142 143

		this._register(cursor.addEventListener((events: viewEvents.ViewEvent[]) => {
			this.eventDispatcher.emitMany(events);
		}));
E
Erich Gamma 已提交
144 145 146
	}

	private createViewParts(): void {
A
Alex Dima 已提交
147
		// These two dom nodes must be constructed up front, since references are needed in the layout provider (scrolling & co.)
A
Alex Dima 已提交
148
		this.linesContent = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
149
		this.linesContent.setClassName('lines-content' + ' monaco-editor-background');
A
Alex Dima 已提交
150
		this.linesContent.setPosition('absolute');
A
Alex Dima 已提交
151

A
Alex Dima 已提交
152
		this.domNode = createFastDomNode(document.createElement('div'));
153
		this.domNode.setClassName(this.getEditorClassName());
A
Alex Dima 已提交
154

A
Alex Dima 已提交
155
		this.overflowGuardContainer = createFastDomNode(document.createElement('div'));
A
Alex Dima 已提交
156
		PartFingerprints.write(this.overflowGuardContainer, PartFingerprint.OverflowGuard);
A
Alex Dima 已提交
157
		this.overflowGuardContainer.setClassName('overflow-guard');
A
Alex Dima 已提交
158

159
		this._scrollbar = new EditorScrollbar(this._context, this.linesContent, this.domNode, this.overflowGuardContainer);
A
Alex Dima 已提交
160 161
		this.viewParts.push(this._scrollbar);

E
Erich Gamma 已提交
162
		// View Lines
163
		this.viewLines = new ViewLines(this._context, this.linesContent);
E
Erich Gamma 已提交
164 165

		// View Zones
166
		this.viewZones = new ViewZones(this._context);
E
Erich Gamma 已提交
167 168 169
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
170
		let decorationsOverviewRuler = new DecorationsOverviewRuler(this._context);
E
Erich Gamma 已提交
171 172 173
		this.viewParts.push(decorationsOverviewRuler);


A
Alex Dima 已提交
174
		let scrollDecoration = new ScrollDecorationViewPart(this._context);
E
Erich Gamma 已提交
175 176
		this.viewParts.push(scrollDecoration);

A
Alex Dima 已提交
177
		let contentViewOverlays = new ContentViewOverlays(this._context);
E
Erich Gamma 已提交
178
		this.viewParts.push(contentViewOverlays);
A
Alex Dima 已提交
179
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
180 181
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
182
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
E
Erich Gamma 已提交
183

A
Alex Dima 已提交
184
		let marginViewOverlays = new MarginViewOverlays(this._context);
E
Erich Gamma 已提交
185
		this.viewParts.push(marginViewOverlays);
A
Alex Dima 已提交
186
		marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
187
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
188
		marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
189 190
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));
E
Erich Gamma 已提交
191

A
Alex Dima 已提交
192
		let margin = new Margin(this._context);
A
Alex Dima 已提交
193
		margin.getDomNode().appendChild(this.viewZones.marginDomNode);
A
Alex Dima 已提交
194
		margin.getDomNode().appendChild(marginViewOverlays.getDomNode());
195
		this.viewParts.push(margin);
E
Erich Gamma 已提交
196 197

		// Content widgets
198
		this.contentWidgets = new ViewContentWidgets(this._context, this.domNode);
E
Erich Gamma 已提交
199 200
		this.viewParts.push(this.contentWidgets);

201 202
		this.viewCursors = new ViewCursors(this._context);
		this.viewParts.push(this.viewCursors);
E
Erich Gamma 已提交
203 204

		// Overlay widgets
205
		this.overlayWidgets = new ViewOverlayWidgets(this._context);
E
Erich Gamma 已提交
206 207
		this.viewParts.push(this.overlayWidgets);

A
Alex Dima 已提交
208
		let rulers = new Rulers(this._context);
209 210
		this.viewParts.push(rulers);

211
		let minimap = new Minimap(this._context, this._scrollbar);
A
Alex Dima 已提交
212 213
		this.viewParts.push(minimap);

E
Erich Gamma 已提交
214 215 216
		// -------------- Wire dom nodes up

		if (decorationsOverviewRuler) {
217
			let overviewRulerData = this._scrollbar.getOverviewRulerLayoutInfo();
E
Erich Gamma 已提交
218 219 220
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

A
Alex Dima 已提交
221 222 223 224 225 226 227 228 229 230
		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());
		this.overflowGuardContainer.appendChild(this.overlayWidgets.getDomNode());
231 232
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textArea);
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textAreaCover);
A
Alex Dima 已提交
233 234 235
		this.overflowGuardContainer.appendChild(minimap.getDomNode());
		this.domNode.appendChild(this.overflowGuardContainer);
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
E
Erich Gamma 已提交
236 237 238 239 240 241
	}

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

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

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

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

			visibleRangeForPosition2: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
267
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column));
E
Erich Gamma 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			},

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

281
	private createTextAreaHandlerHelper(): ITextAreaHandlerHelper {
E
Erich Gamma 已提交
282 283 284
		return {
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				this._flushAccumulatedAndRenderNow();
285
				let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column));
E
Erich Gamma 已提交
286 287 288 289 290 291 292 293
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			}
		};
	}

A
Alex Dima 已提交
294 295
	private _setLayout(): void {
		const layoutInfo = this._context.configuration.editor.layoutInfo;
A
Alex Dima 已提交
296 297
		this.domNode.setWidth(layoutInfo.width);
		this.domNode.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
298

A
Alex Dima 已提交
299 300
		this.overflowGuardContainer.setWidth(layoutInfo.width);
		this.overflowGuardContainer.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
301

A
Alex Dima 已提交
302 303
		this.linesContent.setWidth(1000000);
		this.linesContent.setHeight(1000000);
E
Erich Gamma 已提交
304 305

	}
306

307 308 309 310
	private getEditorClassName() {
		return this._context.configuration.editor.editorClassName + ' ' + getThemeTypeSelector(this._context.theme.type);
	}

311 312
	// --- begin event handlers

A
Alex Dima 已提交
313
	public onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
A
Alex Dima 已提交
314
		if (e.editorClassName) {
315
			this.domNode.setClassName(this.getEditorClassName());
E
Erich Gamma 已提交
316
		}
A
Alex Dima 已提交
317 318 319
		if (e.layoutInfo) {
			this._setLayout();
		}
320
		return false;
E
Erich Gamma 已提交
321
	}
A
Alex Dima 已提交
322
	public onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {
A
Alex Dima 已提交
323
		this.domNode.toggleClassName('focused', e.isFocused);
A
Alex Dima 已提交
324
		if (e.isFocused) {
A
Alex Dima 已提交
325
			this.outgoingEvents.emitViewFocusGained();
E
Erich Gamma 已提交
326
		} else {
A
Alex Dima 已提交
327
			this.outgoingEvents.emitViewFocusLost();
E
Erich Gamma 已提交
328 329 330
		}
		return false;
	}
331 332 333 334
	public onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
		this.outgoingEvents.emitScrollChanged(e);
		return false;
	}
335 336 337 338
	public onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
		this.domNode.setClassName(this.getEditorClassName());
		return false;
	}
339

E
Erich Gamma 已提交
340 341 342 343 344 345 346 347 348 349
	// --- end event handlers

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

		this.eventDispatcher.removeEventHandler(this);
A
Alex Dima 已提交
350
		this.outgoingEvents.dispose();
E
Erich Gamma 已提交
351 352 353 354 355

		this.pointerHandler.dispose();

		this.viewLines.dispose();

A
Alex Dima 已提交
356
		// Destroy view parts
A
Alex Dima 已提交
357
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
358 359 360 361
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

A
Alex Dima 已提交
362
		super.dispose();
E
Erich Gamma 已提交
363 364
	}

A
Alex Dima 已提交
365 366 367 368 369
	private _renderOnce(callback: () => any): any {
		let r = safeInvokeNoArg(callback);
		this._scheduleRender();
		return r;
	}
E
Erich Gamma 已提交
370

A
Alex Dima 已提交
371 372 373 374 375
	private _scheduleRender(): void {
		if (this._renderAnimationFrame === null) {
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
		}
	}
A
Alex Dima 已提交
376

A
Alex Dima 已提交
377 378 379 380
	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}
381

A
Alex Dima 已提交
382 383 384
	private _renderNow(): void {
		safeInvokeNoArg(() => this._actualRender());
	}
A
Alex Dima 已提交
385

A
Alex Dima 已提交
386 387 388 389 390 391 392 393 394 395
	private _getViewPartsToRender(): ViewPart[] {
		let result: ViewPart[] = [];
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
			let viewPart = this.viewParts[i];
			if (viewPart.shouldRender()) {
				result.push(viewPart);
			}
		}
		return result;
	}
396

A
Alex Dima 已提交
397 398 399 400
	private _actualRender(): void {
		if (!dom.isInDOM(this.domNode.domNode)) {
			return;
		}
401

A
Alex Dima 已提交
402
		let viewPartsToRender = this._getViewPartsToRender();
A
Alex Dima 已提交
403

A
Alex Dima 已提交
404 405
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
406
			this._textAreaHandler.writeToTextArea();
A
Alex Dima 已提交
407 408
			return;
		}
A
Alex Dima 已提交
409

410
		const partialViewportData = this._context.viewLayout.getLinesViewportData();
A
Alex Dima 已提交
411
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
412

A
Alex Dima 已提交
413
		let viewportData = new ViewportData(partialViewportData, this._context.model);
A
Alex Dima 已提交
414

A
Alex Dima 已提交
415 416
		if (this.viewLines.shouldRender()) {
			this.viewLines.renderText(viewportData, () => {
417
				this._textAreaHandler.writeToTextArea();
A
Alex Dima 已提交
418 419
			});
			this.viewLines.onDidRender();
A
Alex Dima 已提交
420

A
Alex Dima 已提交
421 422 423
			// Rendering of viewLines might cause scroll events to occur, so collect view parts to render again
			viewPartsToRender = this._getViewPartsToRender();
		} else {
424
			this._textAreaHandler.writeToTextArea();
A
Alex Dima 已提交
425
		}
A
Alex Dima 已提交
426

427
		let renderingContext = new RenderingContext(this._context.viewLayout, viewportData, this.viewLines);
A
Alex Dima 已提交
428

A
Alex Dima 已提交
429 430 431 432 433
		// Render the rest of the parts
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.prepareRender(renderingContext);
		}
434

A
Alex Dima 已提交
435 436 437 438
		for (let i = 0, len = viewPartsToRender.length; i < len; i++) {
			let viewPart = viewPartsToRender[i];
			viewPart.render(renderingContext);
			viewPart.onDidRender();
E
Erich Gamma 已提交
439 440 441
		}
	}

A
Alex Dima 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	// --- BEGIN CodeEditor helpers

	public delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void {
		this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}

	public getOffsetForColumn(modelLineNumber: number, modelColumn: number): number {
		let modelPosition = this._context.model.validateModelPosition({
			lineNumber: modelLineNumber,
			column: modelColumn
		});
		let viewPosition = this._context.model.coordinatesConverter.convertModelPositionToViewPosition(modelPosition);
		this._flushAccumulatedAndRenderNow();
		let visibleRanges = this.viewLines.visibleRangesForRange2(new Range(viewPosition.lineNumber, viewPosition.column, viewPosition.lineNumber, viewPosition.column));
		if (!visibleRanges) {
			return -1;
E
Erich Gamma 已提交
458
		}
A
Alex Dima 已提交
459 460 461 462 463 464 465 466 467
		return visibleRanges[0].left;
	}

	public getTargetAtClientPoint(clientX: number, clientY: number): editorBrowser.IMouseTarget {
		return this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
	}

	public getInternalEventBus(): ViewOutgoingEvents {
		return this.outgoingEvents;
E
Erich Gamma 已提交
468 469 470
	}

	public createOverviewRuler(cssClassName: string, minimumHeight: number, maximumHeight: number): OverviewRuler {
471
		return new OverviewRuler(this._context, cssClassName, minimumHeight, maximumHeight);
E
Erich Gamma 已提交
472 473
	}

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

E
Erich Gamma 已提交
477
		this._renderOnce(() => {
A
Alex Dima 已提交
478
			let changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
J
Johannes Rieken 已提交
479
				addZone: (zone: editorBrowser.IViewZone): number => {
E
Erich Gamma 已提交
480 481 482
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
J
Johannes Rieken 已提交
483
				removeZone: (id: number): void => {
484 485 486
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
487 488 489
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
				layoutZone: (id: number): void => {
490 491 492
					if (!id) {
						return;
					}
E
Erich Gamma 已提交
493 494 495 496
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

A
Alex Dima 已提交
497
			safeInvoke1Arg(callback, changeAccessor);
E
Erich Gamma 已提交
498 499 500 501 502 503

			// Invalidate changeAccessor
			changeAccessor.addZone = null;
			changeAccessor.removeZone = null;

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

A
Alex Dima 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	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++) {
				let viewPart = this.viewParts[i];
				viewPart.forceShouldRender();
			}
		}
		if (now) {
			this._flushAccumulatedAndRenderNow();
		} else {
			this._scheduleRender();
		}
	}

	public setAriaActiveDescendant(id: string): void {
528
		this._textAreaHandler.setAriaActiveDescendant(id);
A
Alex Dima 已提交
529 530 531
	}

	public focus(): void {
532
		this._textAreaHandler.focusTextArea();
A
Alex Dima 已提交
533 534 535
	}

	public isFocused(): boolean {
536
		return this._textAreaHandler.isFocused();
A
Alex Dima 已提交
537 538
	}

A
Alex Dima 已提交
539
	public addContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
540 541 542
		this.contentWidgets.addWidget(widgetData.widget);
		this.layoutContentWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
543 544
	}

A
Alex Dima 已提交
545
	public layoutContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
546 547 548 549
		let newPosition = widgetData.position ? widgetData.position.position : null;
		let newPreference = widgetData.position ? widgetData.position.preference : null;
		this.contentWidgets.setWidgetPosition(widgetData.widget, newPosition, newPreference);
		this._scheduleRender();
E
Erich Gamma 已提交
550 551
	}

A
Alex Dima 已提交
552
	public removeContentWidget(widgetData: IContentWidgetData): void {
A
Alex Dima 已提交
553 554
		this.contentWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
555 556
	}

A
Alex Dima 已提交
557
	public addOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
558 559 560
		this.overlayWidgets.addWidget(widgetData.widget);
		this.layoutOverlayWidget(widgetData);
		this._scheduleRender();
E
Erich Gamma 已提交
561 562
	}

A
Alex Dima 已提交
563
	public layoutOverlayWidget(widgetData: IOverlayWidgetData): void {
564 565 566 567 568
		let newPreference = widgetData.position ? widgetData.position.preference : null;
		let shouldRender = this.overlayWidgets.setWidgetPosition(widgetData.widget, newPreference);
		if (shouldRender) {
			this._scheduleRender();
		}
E
Erich Gamma 已提交
569 570
	}

A
Alex Dima 已提交
571
	public removeOverlayWidget(widgetData: IOverlayWidgetData): void {
A
Alex Dima 已提交
572 573
		this.overlayWidgets.removeWidget(widgetData.widget);
		this._scheduleRender();
E
Erich Gamma 已提交
574 575
	}

A
Alex Dima 已提交
576
	// --- END CodeEditor helpers
E
Erich Gamma 已提交
577 578 579

}

J
Johannes Rieken 已提交
580
function safeInvokeNoArg(func: Function): any {
A
Alex Dima 已提交
581 582
	try {
		return func();
J
Johannes Rieken 已提交
583
	} catch (e) {
A
Alex Dima 已提交
584 585 586 587
		onUnexpectedError(e);
	}
}

J
Johannes Rieken 已提交
588
function safeInvoke1Arg(func: Function, arg1: any): any {
A
Alex Dima 已提交
589 590
	try {
		return func(arg1);
J
Johannes Rieken 已提交
591
	} catch (e) {
A
Alex Dima 已提交
592 593 594
		onUnexpectedError(e);
	}
}