viewImpl.ts 21.6 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.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6
import { onUnexpectedError } from 'vs/base/common/errors';
A
Alex Dima 已提交
7
import { IDisposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
8
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
9
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
A
Alex Dima 已提交
10
import { Position } from 'vs/editor/common/core/position';
J
Johannes Rieken 已提交
11
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
A
Alex Dima 已提交
12
import { IConfiguration } from 'vs/editor/common/editorCommon';
13
import { TextAreaHandler, ITextAreaHandlerHelper } from 'vs/editor/browser/controller/textAreaHandler';
J
Johannes Rieken 已提交
14
import { PointerHandler } from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
15
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
16
import { ViewController, ICommandDelegate } from 'vs/editor/browser/view/viewController';
17
import { ViewEventDispatcher } from 'vs/editor/common/view/viewEventDispatcher';
J
Johannes Rieken 已提交
18 19 20
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';
21
import { CurrentLineMarginHighlightOverlay } from 'vs/editor/browser/viewParts/currentLineMarginHighlight/currentLineMarginHighlight';
J
Johannes Rieken 已提交
22 23 24 25 26
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';
27
import { Margin } from 'vs/editor/browser/viewParts/margin/margin';
J
Johannes Rieken 已提交
28
import { LinesDecorationsOverlay } from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
29
import { MarginViewLineDecorationsOverlay } from 'vs/editor/browser/viewParts/marginDecorations/marginDecorations';
J
Johannes Rieken 已提交
30 31 32 33 34 35 36 37
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 已提交
38
import { ViewPart, PartFingerprint, PartFingerprints } from 'vs/editor/browser/view/viewPart';
39
import { ViewContext } from 'vs/editor/common/view/viewContext';
J
Johannes Rieken 已提交
40
import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
A
Alex Dima 已提交
41
import { RenderingContext } from 'vs/editor/common/view/renderingContext';
J
Johannes Rieken 已提交
42
import { IPointerHandlerHelper } from 'vs/editor/browser/controller/mouseHandler';
A
Alex Dima 已提交
43
import { ViewOutgoingEvents } from 'vs/editor/browser/view/viewOutgoingEvents';
44
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
A
Alex Dima 已提交
45
import { EditorScrollbar } from 'vs/editor/browser/viewParts/editorScrollbar/editorScrollbar';
A
Alex Dima 已提交
46
import { Minimap } from 'vs/editor/browser/viewParts/minimap/minimap';
47
import * as viewEvents from 'vs/editor/common/view/viewEvents';
48
import { IThemeService, getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
49
import { Cursor } from 'vs/editor/common/controller/cursor';
B
Benjamin Pasero 已提交
50
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
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

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

68
	private _scrollbar: EditorScrollbar;
69
	private _context: ViewContext;
70
	private _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

A
Alex Dima 已提交
108
		let 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

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
		this._register(this._cursor.addEventListener((events: viewEvents.ViewEvent[]) => {
142 143
			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
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
181
		contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));
182
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(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);

A
Alex Dima 已提交
211
		let minimap = new Minimap(this._context);
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
		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());
230 231
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textArea);
		this.overflowGuardContainer.appendChild(this._textAreaHandler.textAreaCover);
232
		this.overflowGuardContainer.appendChild(this.overlayWidgets.getDomNode());
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();
A
Alex Dima 已提交
267
				return this.viewLines.visibleRangeForPosition(new Position(lineNumber, column));
E
Erich Gamma 已提交
268 269 270 271 272 273 274 275 276
			},

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

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

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

A
Alex Dima 已提交
291 292
		this.overflowGuardContainer.setWidth(layoutInfo.width);
		this.overflowGuardContainer.setHeight(layoutInfo.height);
E
Erich Gamma 已提交
293

A
Alex Dima 已提交
294 295
		this.linesContent.setWidth(1000000);
		this.linesContent.setHeight(1000000);
E
Erich Gamma 已提交
296 297

	}
298

299
	private getEditorClassName() {
S
smoke:  
Sandeep Somavarapu 已提交
300 301
		let focused = this._textAreaHandler.isFocused() ? ' focused' : '';
		return this._context.configuration.editor.editorClassName + ' ' + getThemeTypeSelector(this._context.theme.type) + focused;
302 303
	}

304 305
	// --- begin event handlers

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

E
Erich Gamma 已提交
334 335 336 337 338 339 340 341 342
	// --- end event handlers

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

		this.eventDispatcher.removeEventHandler(this);
A
Alex Dima 已提交
343
		this.outgoingEvents.dispose();
E
Erich Gamma 已提交
344 345 346 347 348

		this.pointerHandler.dispose();

		this.viewLines.dispose();

A
Alex Dima 已提交
349
		// Destroy view parts
A
Alex Dima 已提交
350
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
E
Erich Gamma 已提交
351 352 353 354
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

A
Alex Dima 已提交
355
		super.dispose();
E
Erich Gamma 已提交
356 357
	}

A
Alex Dima 已提交
358 359 360 361 362
	private _renderOnce(callback: () => any): any {
		let r = safeInvokeNoArg(callback);
		this._scheduleRender();
		return r;
	}
E
Erich Gamma 已提交
363

A
Alex Dima 已提交
364 365 366 367 368
	private _scheduleRender(): void {
		if (this._renderAnimationFrame === null) {
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
		}
	}
A
Alex Dima 已提交
369

A
Alex Dima 已提交
370 371 372 373
	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}
374

A
Alex Dima 已提交
375 376 377
	private _renderNow(): void {
		safeInvokeNoArg(() => this._actualRender());
	}
A
Alex Dima 已提交
378

A
Alex Dima 已提交
379
	private _getViewPartsToRender(): ViewPart[] {
A
Alex Dima 已提交
380
		let result: ViewPart[] = [], resultLen = 0;
A
Alex Dima 已提交
381 382 383
		for (let i = 0, len = this.viewParts.length; i < len; i++) {
			let viewPart = this.viewParts[i];
			if (viewPart.shouldRender()) {
A
Alex Dima 已提交
384
				result[resultLen++] = viewPart;
A
Alex Dima 已提交
385 386 387 388
			}
		}
		return result;
	}
389

A
Alex Dima 已提交
390 391 392 393
	private _actualRender(): void {
		if (!dom.isInDOM(this.domNode.domNode)) {
			return;
		}
394

A
Alex Dima 已提交
395
		let viewPartsToRender = this._getViewPartsToRender();
A
Alex Dima 已提交
396

A
Alex Dima 已提交
397 398 399 400
		if (!this.viewLines.shouldRender() && viewPartsToRender.length === 0) {
			// Nothing to render
			return;
		}
A
Alex Dima 已提交
401

402
		const partialViewportData = this._context.viewLayout.getLinesViewportData();
A
Alex Dima 已提交
403
		this._context.model.setViewport(partialViewportData.startLineNumber, partialViewportData.endLineNumber, partialViewportData.centeredLineNumber);
404

405 406 407 408 409 410
		let viewportData = new ViewportData(
			this._cursor.getViewSelections(),
			partialViewportData,
			this._context.viewLayout.getWhitespaceViewportData(),
			this._context.model
		);
A
Alex Dima 已提交
411

412 413 414 415 416
		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 已提交
417
		if (this.viewLines.shouldRender()) {
418
			this.viewLines.renderText(viewportData);
A
Alex Dima 已提交
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();
		}
A
Alex Dima 已提交
424

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

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

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

A
Alex Dima 已提交
440 441
	// --- BEGIN CodeEditor helpers

442
	public delegateVerticalScrollbarMouseDown(browserEvent: IMouseEvent): void {
A
Alex Dima 已提交
443 444 445
		this._scrollbar.delegateVerticalScrollbarMouseDown(browserEvent);
	}

446 447 448 449 450 451 452
	public restoreState(scrollPosition: { scrollLeft: number; scrollTop: number; }): void {
		this._context.viewLayout.setScrollPositionNow({ scrollTop: scrollPosition.scrollTop });
		this._renderNow();
		this.viewLines.updateLineWidths();
		this._context.viewLayout.setScrollPositionNow({ scrollLeft: scrollPosition.scrollLeft });
	}

A
Alex Dima 已提交
453 454 455 456 457 458 459
	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();
A
Alex Dima 已提交
460 461
		const visibleRange = this.viewLines.visibleRangeForPosition(new Position(viewPosition.lineNumber, viewPosition.column));
		if (!visibleRange) {
A
Alex Dima 已提交
462
			return -1;
E
Erich Gamma 已提交
463
		}
A
Alex Dima 已提交
464
		return visibleRange.left;
A
Alex Dima 已提交
465 466
	}

A
Alex Dima 已提交
467
	public getTargetAtClientPoint(clientX: number, clientY: number): editorBrowser.IMouseTarget | null {
A
Alex Dima 已提交
468 469 470
		return this.pointerHandler.getTargetAtClientPoint(clientX, clientY);
	}

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

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

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

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

			// Invalidate changeAccessor
A
Alex Dima 已提交
501 502 503
			changeAccessor.addZone = invalidFunc;
			changeAccessor.removeZone = invalidFunc;
			changeAccessor.layoutZone = invalidFunc;
E
Erich Gamma 已提交
504 505

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

A
Alex Dima 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	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 focus(): void {
530
		this._textAreaHandler.focusTextArea();
A
Alex Dima 已提交
531 532 533
	}

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

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

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

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

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

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

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

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

}

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

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