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

A
Alex Dima 已提交
7 8 9 10 11 12
import {onUnexpectedError} from 'vs/base/common/errors';
import {EventEmitter, IEmitterEvent, IEventEmitter, ListenerUnbind} from 'vs/base/common/eventEmitter';
import {IDisposable, disposeAll} from 'vs/base/common/lifecycle';
import * as timer from 'vs/base/common/timer';
import * as browser from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
13
import {StyleMutator} from 'vs/base/browser/styleMutator';
A
Alex Dima 已提交
14 15 16
import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {Range} from 'vs/editor/common/core/range';
import * as editorCommon from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
17
import {ViewEventHandler} from 'vs/editor/common/viewModel/viewEventHandler';
A
Alex Dima 已提交
18
import {Configuration} from 'vs/editor/browser/config/configuration';
E
Erich Gamma 已提交
19 20
import {KeyboardHandler} from 'vs/editor/browser/controller/keyboardHandler';
import {PointerHandler} from 'vs/editor/browser/controller/pointerHandler';
A
Alex Dima 已提交
21
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
E
Erich Gamma 已提交
22
import {ViewController} from 'vs/editor/browser/view/viewController';
A
Alex Dima 已提交
23
import {ViewEventDispatcher} from 'vs/editor/browser/view/viewEventDispatcher';
E
Erich Gamma 已提交
24
import {ContentViewOverlays, MarginViewOverlays} from 'vs/editor/browser/view/viewOverlays';
A
Alex Dima 已提交
25
import {LayoutProvider} from 'vs/editor/browser/viewLayout/layoutProvider';
E
Erich Gamma 已提交
26 27 28 29 30
import {ViewContentWidgets} from 'vs/editor/browser/viewParts/contentWidgets/contentWidgets';
import {CurrentLineHighlightOverlay} from 'vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight';
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';
A
Alex Dima 已提交
31 32 33 34 35
import {ViewLines} from 'vs/editor/browser/viewParts/lines/viewLines';
import {LinesDecorationsOverlay} from 'vs/editor/browser/viewParts/linesDecorations/linesDecorations';
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';
36
import {Rulers} from 'vs/editor/browser/viewParts/rulers/rulers';
A
Alex Dima 已提交
37 38 39 40
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';
E
Erich Gamma 已提交
41

A
Alex Dima 已提交
42
export class View extends ViewEventHandler implements editorBrowser.IView, IDisposable {
E
Erich Gamma 已提交
43 44 45

	private eventDispatcher:ViewEventDispatcher;

A
Alex Dima 已提交
46 47
	private listenersToRemove:ListenerUnbind[];
	private listenersToDispose:IDisposable[];
E
Erich Gamma 已提交
48 49

	private layoutProvider: LayoutProvider;
A
Alex Dima 已提交
50
	public context: editorBrowser.IViewContext;
E
Erich Gamma 已提交
51 52 53 54 55 56 57 58

	// 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;
A
Alex Dima 已提交
59
	private viewParts: editorBrowser.IViewPart[];
E
Erich Gamma 已提交
60 61 62 63

	private keyboardHandler: KeyboardHandler;
	private pointerHandler: PointerHandler;

A
Alex Dima 已提交
64
	private outgoingEventBus: EventEmitter;
E
Erich Gamma 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78

	// Dom nodes
	private linesContent: HTMLElement;
	public domNode: HTMLElement;
	public textArea: HTMLTextAreaElement;
	private textAreaCover: HTMLElement;
	private linesContentContainer: HTMLElement;
	private overflowGuardContainer: HTMLElement;

	// Actual mutable state
	private hasFocus:boolean;
	private _isDisposed: boolean;

	private handleAccumulatedModelEventsTimeout:number;
A
Alex Dima 已提交
79 80
	private accumulatedModelEvents: IEmitterEvent[];
	private _renderAnimationFrame: IDisposable;
E
Erich Gamma 已提交
81 82 83 84 85

	private _editorId: number;
	private _keybindingService: IKeybindingService;
	private _editorTextFocusContextKey: IKeybindingContextKey<boolean>;

A
Alex Dima 已提交
86
	constructor(editorId:number, configuration:Configuration, model:editorCommon.IViewModel, keybindingService: IKeybindingService) {
E
Erich Gamma 已提交
87 88 89 90
		super();
		this._isDisposed = false;
		this._editorId = editorId;
		this._renderAnimationFrame = null;
A
Alex Dima 已提交
91
		this.outgoingEventBus = new EventEmitter();
E
Erich Gamma 已提交
92 93 94 95 96 97 98 99 100 101 102

		var viewController = new ViewController(model, configuration, this.outgoingEventBus);

		this.listenersToRemove = [];
		this.listenersToDispose = [];

		// The event dispatcher will always go through _renderOnce before dispatching any events
		this.eventDispatcher = new ViewEventDispatcher((callback:()=>void) => this._renderOnce(callback));

		// These two dom nodes must be constructed up front, since references are needed in the layout provider (scrolling & co.)
		this.linesContent = document.createElement('div');
A
Alex Dima 已提交
103
		this.linesContent.className = editorBrowser.ClassNames.LINES_CONTENT + ' monaco-editor-background';
E
Erich Gamma 已提交
104 105 106 107
		this.domNode = document.createElement('div');
		Configuration.applyEditorStyling(this.domNode, configuration.editor.stylingInfo);

		this.overflowGuardContainer = document.createElement('div');
A
Alex Dima 已提交
108
		this.overflowGuardContainer.className = editorBrowser.ClassNames.OVERFLOW_GUARD;
E
Erich Gamma 已提交
109 110 111 112 113 114 115 116 117 118 119

		// The layout provider has such responsibilities as:
		// - scrolling (i.e. viewport / full size) & co.
		// - whitespaces (a.k.a. view zones) management & co.
		// - line heights updating & co.
		this.layoutProvider = new LayoutProvider(configuration, model, this.eventDispatcher, this.linesContent, this.domNode, this.overflowGuardContainer);
		this.eventDispatcher.addEventHandler(this.layoutProvider);

		// The view context is passed on to most classes (basically to reduce param. counts in ctors)
		this.context = new ViewContext(
				editorId, configuration, model, this.eventDispatcher,
A
Alex Dima 已提交
120 121
				(eventHandler:editorBrowser.IViewEventHandler) => this.eventDispatcher.addEventHandler(eventHandler),
				(eventHandler:editorBrowser.IViewEventHandler) => this.eventDispatcher.removeEventHandler(eventHandler)
E
Erich Gamma 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		);

		this.createTextArea(keybindingService);
		this.createViewParts();

		// Keyboard handler
		this.keyboardHandler = new KeyboardHandler(this.context, viewController, this.createKeyboardHandlerHelper());

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

		this.hasFocus = false;
		this.codeEditorHelper = null;

		this.eventDispatcher.addEventHandler(this);

		// The view lines rendering calls model.getLineTokens() that might emit events that its tokens have changed.
		// This delayed processing of incoming model events acts as a guard against undesired/unexpected recursion.
		this.handleAccumulatedModelEventsTimeout = -1;
		this.accumulatedModelEvents = [];
A
Alex Dima 已提交
142
		this.listenersToRemove.push(model.addBulkListener((events:IEmitterEvent[]) => {
E
Erich Gamma 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			this.accumulatedModelEvents = this.accumulatedModelEvents.concat(events);
			if (this.handleAccumulatedModelEventsTimeout === -1) {
				this.handleAccumulatedModelEventsTimeout = setTimeout(() => {
					this.handleAccumulatedModelEventsTimeout = -1;
					this._flushAnyAccumulatedEvents();
				});
			}
		}));
	}

	private _flushAnyAccumulatedEvents(): void {
		var toEmit = this.accumulatedModelEvents;
		this.accumulatedModelEvents = [];
		if (toEmit.length > 0) {
			this.eventDispatcher.emitMany(toEmit);
		}
	}

	private createTextArea(keybindingService: IKeybindingService): void {
		// Text Area (The focus will always be in the textarea when the cursor is blinking)
		this.textArea = <HTMLTextAreaElement>document.createElement('textarea');
		this._keybindingService = keybindingService.createScoped(this.textArea);
A
Alex Dima 已提交
165 166
		this._editorTextFocusContextKey = this._keybindingService.createKey(editorCommon.KEYBINDING_CONTEXT_EDITOR_TEXT_FOCUS, undefined);
		this.textArea.className = editorBrowser.ClassNames.TEXTAREA;
E
Erich Gamma 已提交
167 168 169 170
		this.textArea.setAttribute('wrap', 'off');
		this.textArea.setAttribute('autocorrect', 'off');
		this.textArea.setAttribute('autocapitalize', 'off');
		this.textArea.setAttribute('spellcheck', 'false');
171
		this.textArea.setAttribute('aria-label', this.context.configuration.editor.ariaLabel);
E
Erich Gamma 已提交
172 173
		this.textArea.setAttribute('role', 'textbox');
		this.textArea.setAttribute('aria-multiline', 'true');
A
Alex Dima 已提交
174 175
		StyleMutator.setTop(this.textArea, 0);
		StyleMutator.setLeft(this.textArea, 0);
E
Erich Gamma 已提交
176
		// Give textarea same font size & line height as editor, for the IME case (when the textarea is visible)
A
Alex Dima 已提交
177 178
		StyleMutator.setFontSize(this.textArea, this.context.configuration.editor.fontSize);
		StyleMutator.setLineHeight(this.textArea, this.context.configuration.editor.lineHeight);
E
Erich Gamma 已提交
179

A
Alex Dima 已提交
180 181
		this.listenersToDispose.push(dom.addDisposableListener(this.textArea, 'focus', () => this._setHasFocus(true)));
		this.listenersToDispose.push(dom.addDisposableListener(this.textArea, 'blur', () => this._setHasFocus(false)));
E
Erich Gamma 已提交
182 183 184 185 186 187

		// On top of the text area, we position a dom node to cover it up
		// (there have been reports of tiny blinking cursors)
		// (in WebKit the textarea is 1px by 1px because it cannot handle input to a 0x0 textarea)
		this.textAreaCover = document.createElement('div');
		if (this.context.configuration.editor.glyphMargin) {
A
Alex Dima 已提交
188
			this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.GLYPH_MARGIN + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
189 190
		} else {
			if (this.context.configuration.editor.lineNumbers) {
A
Alex Dima 已提交
191
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.LINE_NUMBERS + ' ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
192
			} else {
A
Alex Dima 已提交
193
				this.textAreaCover.className = 'monaco-editor-background ' + editorBrowser.ClassNames.TEXTAREA_COVER;
E
Erich Gamma 已提交
194 195 196
			}
		}
		this.textAreaCover.style.position = 'absolute';
A
Alex Dima 已提交
197 198 199 200
		StyleMutator.setWidth(this.textAreaCover, 1);
		StyleMutator.setHeight(this.textAreaCover, 1);
		StyleMutator.setTop(this.textAreaCover, 0);
		StyleMutator.setLeft(this.textAreaCover, 0);
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	}

	private createViewParts(): void {
		this.viewParts = [];

		// View Lines
		this.viewLines = new ViewLines(this.context, this.layoutProvider);

		// View Zones
		this.viewZones = new ViewZones(this.context, this.layoutProvider);
		this.viewParts.push(this.viewZones);

		// Decorations overview ruler
		var decorationsOverviewRuler = new DecorationsOverviewRuler(
				this.context, this.layoutProvider.getScrollHeight(),
				(lineNumber:number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
		);
		this.viewParts.push(decorationsOverviewRuler);


		var scrollDecoration = new ScrollDecorationViewPart(this.context);
		this.viewParts.push(scrollDecoration);

		var contentViewOverlays = new ContentViewOverlays(this.context, this.layoutProvider);
		this.viewParts.push(contentViewOverlays);
		contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this.context, this.layoutProvider));
		contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this.context));
		contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this.context));

		var marginViewOverlays = new MarginViewOverlays(this.context, this.layoutProvider);
		this.viewParts.push(marginViewOverlays);
		marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this.context));
		marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this.context));
		marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this.context));


		// Content widgets
		this.contentWidgets = new ViewContentWidgets(this.context, this.domNode);
		this.viewParts.push(this.contentWidgets);

		var viewCursors = new ViewCursors(this.context);
		this.viewParts.push(viewCursors);

		// Overlay widgets
		this.overlayWidgets = new ViewOverlayWidgets(this.context);
		this.viewParts.push(this.overlayWidgets);

248 249 250
		var rulers = new Rulers(this.context, this.layoutProvider);
		this.viewParts.push(rulers);

E
Erich Gamma 已提交
251 252 253 254 255 256 257 258 259 260 261
		// -------------- Wire dom nodes up

		this.linesContentContainer = this.layoutProvider.getScrollbarContainerDomNode();
		this.linesContentContainer.style.position = 'absolute';

		if (decorationsOverviewRuler) {
			var overviewRulerData = this.layoutProvider.getOverviewRulerInsertData();
			overviewRulerData.parent.insertBefore(decorationsOverviewRuler.getDomNode(), overviewRulerData.insertBefore);
		}

		this.linesContent.appendChild(contentViewOverlays.getDomNode());
262
		this.linesContent.appendChild(rulers.domNode);
E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273
		this.linesContent.appendChild(this.viewZones.domNode);
		this.linesContent.appendChild(this.viewLines.domNode);
		this.linesContent.appendChild(this.contentWidgets.domNode);
		this.linesContent.appendChild(viewCursors.getDomNode());
		this.overflowGuardContainer.appendChild(marginViewOverlays.getDomNode());
		this.overflowGuardContainer.appendChild(this.linesContentContainer);
		this.overflowGuardContainer.appendChild(scrollDecoration.getDomNode());
		this.overflowGuardContainer.appendChild(this.overlayWidgets.domNode);
		this.overflowGuardContainer.appendChild(this.textArea);
		this.overflowGuardContainer.appendChild(this.textAreaCover);
		this.domNode.appendChild(this.overflowGuardContainer);
274
		this.domNode.appendChild(this.contentWidgets.overflowingContentWidgetsDomNode);
E
Erich Gamma 已提交
275 276 277 278 279 280 281
	}

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

A
Alex Dima 已提交
282
	private createPointerHandlerHelper(): editorBrowser.IPointerHandlerHelper {
E
Erich Gamma 已提交
283 284 285 286 287 288 289 290 291 292 293
		return {
			viewDomNode: this.domNode,
			linesContentDomNode: this.linesContent,

			focusTextArea: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.focusTextArea: View is disposed');
				}
				this.focus();
			},

294 295 296 297
			isDirty: (): boolean => {
				return (this.accumulatedModelEvents.length > 0);
			},

E
Erich Gamma 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
			getScrollTop: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollTop: View is disposed');
				}
				return this.layoutProvider.getScrollTop();
			},
			setScrollTop: (scrollTop: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.setScrollTop: View is disposed');
				}
				this.layoutProvider.setScrollTop(scrollTop);
			},
			getScrollLeft: () => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getScrollLeft: View is disposed');
				}
				return this.layoutProvider.getScrollLeft();
			},
			setScrollLeft: (scrollLeft: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.setScrollLeft: View is disposed');
				}
				this.layoutProvider.setScrollLeft(scrollLeft);
			},

			isAfterLines: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.isAfterLines: View is disposed');
				}
				return this.layoutProvider.isAfterLines(verticalOffset);
			},
			getLineNumberAtVerticalOffset: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLineNumberAtVerticalOffset: View is disposed');
				}
				return this.layoutProvider.getLineNumberAtVerticalOffset(verticalOffset);
			},
			getVerticalOffsetForLineNumber: (lineNumber: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getVerticalOffsetForLineNumber: View is disposed');
				}
				return this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber);
			},
			getWhitespaceAtVerticalOffset: (verticalOffset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getWhitespaceAtVerticalOffset: View is disposed');
				}
				return this.layoutProvider.getWhitespaceAtVerticalOffset(verticalOffset);
			},
			shouldSuppressMouseDownOnViewZone: (viewZoneId: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.shouldSuppressMouseDownOnViewZone: View is disposed');
				}
				return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
			},

			getPositionFromDOMInfo: (spanNode: HTMLElement, offset: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getPositionFromDOMInfo: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getPositionFromDOMInfo(spanNode, offset);
			},

			visibleRangeForPosition2: (lineNumber: number, column: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.visibleRangeForPosition2: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
367
				var visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), 0);
E
Erich Gamma 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			},

			getLineWidth: (lineNumber: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.pointerHandler.getLineWidth: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
				return this.viewLines.getLineWidth(lineNumber);
			}
		};
	}

A
Alex Dima 已提交
384
	private createKeyboardHandlerHelper(): editorBrowser.IKeyboardHandlerHelper {
E
Erich Gamma 已提交
385 386 387 388 389 390 391 392 393
		return {
			viewDomNode: this.domNode,
			textArea: this.textArea,
			visibleRangeForPositionRelativeToEditor: (lineNumber: number, column: number) => {
				if (this._isDisposed) {
					throw new Error('ViewImpl.keyboardHandler.visibleRangeForPositionRelativeToEditor: View is disposed');
				}
				this._flushAccumulatedAndRenderNow();
				var linesViewPortData = this.layoutProvider.getLinesViewportData();
A
Alex Dima 已提交
394
				var visibleRanges = this.viewLines.visibleRangesForRange2(new Range(lineNumber, column, lineNumber, column), linesViewPortData.visibleRangesDeltaTop);
E
Erich Gamma 已提交
395 396 397 398 399 400 401 402 403 404
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			}
		};
	}

	// --- begin event handlers

A
Alex Dima 已提交
405 406
	public onLayoutChanged(layoutInfo:editorCommon.IEditorLayoutInfo): boolean {
		if (browser.isChrome) {
A
tslint  
Alex Dima 已提交
407
			/* tslint:disable:no-unused-variable */
E
Erich Gamma 已提交
408 409 410
			// Access overflowGuardContainer.clientWidth to prevent relayouting bug in Chrome
			// See Bug 19676: Editor misses a layout event
			var clientWidth = this.overflowGuardContainer.clientWidth + 'px';
A
tslint  
Alex Dima 已提交
411
			/* tslint:enable:no-unused-variable */
E
Erich Gamma 已提交
412
		}
A
Alex Dima 已提交
413 414
		StyleMutator.setWidth(this.domNode, layoutInfo.width);
		StyleMutator.setHeight(this.domNode, layoutInfo.height);
E
Erich Gamma 已提交
415

A
Alex Dima 已提交
416 417
		StyleMutator.setWidth(this.overflowGuardContainer, layoutInfo.width);
		StyleMutator.setHeight(this.overflowGuardContainer, layoutInfo.height);
E
Erich Gamma 已提交
418

A
Alex Dima 已提交
419 420
		StyleMutator.setWidth(this.linesContent, 1000000);
		StyleMutator.setHeight(this.linesContent, 1000000);
E
Erich Gamma 已提交
421

A
Alex Dima 已提交
422 423 424
		StyleMutator.setLeft(this.linesContentContainer, layoutInfo.contentLeft);
		StyleMutator.setWidth(this.linesContentContainer, layoutInfo.contentWidth);
		StyleMutator.setHeight(this.linesContentContainer, layoutInfo.contentHeight);
E
Erich Gamma 已提交
425

A
Alex Dima 已提交
426
		this.outgoingEventBus.emit(editorCommon.EventType.ViewLayoutChanged, layoutInfo);
E
Erich Gamma 已提交
427 428
		return false;
	}
A
Alex Dima 已提交
429
	public onConfigurationChanged(e: editorCommon.IConfigurationChangedEvent): boolean {
E
Erich Gamma 已提交
430 431 432
		if (e.stylingInfo) {
			Configuration.applyEditorStyling(this.domNode, this.context.configuration.editor.stylingInfo);
		}
433 434 435
		if (e.ariaLabel) {
			this.textArea.setAttribute('aria-label', this.context.configuration.editor.ariaLabel);
		}
E
Erich Gamma 已提交
436 437
		return false;
	}
A
Alex Dima 已提交
438
	public onScrollChanged(e:editorCommon.IScrollEvent): boolean {
E
Erich Gamma 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452
		this.outgoingEventBus.emit('scroll', {
			scrollTop: this.layoutProvider.getScrollTop(),
			scrollLeft: this.layoutProvider.getScrollLeft()
		});
		return false;
	}
	public onScrollHeightChanged(scrollHeight:number): boolean {
		this.outgoingEventBus.emit('scrollSize', {
			scrollWidth: this.layoutProvider.getScrollWidth(),
			scrollHeight: this.layoutProvider.getScrollHeight()
		});
		return super.onScrollHeightChanged(scrollHeight);
	}
	public onViewFocusChanged(isFocused:boolean): boolean {
A
Alex Dima 已提交
453
		dom.toggleClass(this.domNode, 'focused', isFocused);
E
Erich Gamma 已提交
454 455
		if (isFocused) {
			this._editorTextFocusContextKey.set(true);
A
Alex Dima 已提交
456
			this.outgoingEventBus.emit(editorCommon.EventType.ViewFocusGained, {});
E
Erich Gamma 已提交
457 458
		} else {
			this._editorTextFocusContextKey.reset();
A
Alex Dima 已提交
459
			this.outgoingEventBus.emit(editorCommon.EventType.ViewFocusLost, {});
E
Erich Gamma 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
		}
		return false;
	}
	// --- end event handlers

	public dispose(): void {
		this._isDisposed = true;
		if (this.handleAccumulatedModelEventsTimeout !== -1) {
			clearTimeout(this.handleAccumulatedModelEventsTimeout);
			this.handleAccumulatedModelEventsTimeout = -1;
		}
		if (this._renderAnimationFrame !== null) {
			this._renderAnimationFrame.dispose();
			this._renderAnimationFrame = null;
		}
		this.accumulatedModelEvents = [];

		this.eventDispatcher.removeEventHandler(this);
		this.outgoingEventBus.dispose();
		this.listenersToRemove.forEach((element) => {
			element();
		});
		this.listenersToRemove = [];

A
Alex Dima 已提交
484
		this.listenersToDispose = disposeAll(this.listenersToDispose);
E
Erich Gamma 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

		this.keyboardHandler.dispose();
		this.pointerHandler.dispose();

		this.viewLines.dispose();

		// Destroy IViewPart second
		for (var i = 0, len = this.viewParts.length; i < len; i++) {
			this.viewParts[i].dispose();
		}
		this.viewParts = [];

		this.layoutProvider.dispose();
		this._keybindingService.dispose();
	}

	// --- begin Code Editor APIs

A
Alex Dima 已提交
503 504
	private codeEditorHelper:editorBrowser.ICodeEditorHelper;
	public getCodeEditorHelper(): editorBrowser.ICodeEditorHelper {
E
Erich Gamma 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
		if (!this.codeEditorHelper) {
			this.codeEditorHelper = {
				getScrollTop: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollTop: View is disposed');
					}
					return this.layoutProvider.getScrollTop();
				},
				setScrollTop: (scrollTop: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.setScrollTop: View is disposed');
					}
					this.layoutProvider.setScrollTop(scrollTop);
				},
				getScrollLeft: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollLeft: View is disposed');
					}
					return this.layoutProvider.getScrollLeft();
				},
				setScrollLeft: (scrollLeft: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.setScrollLeft: View is disposed');
					}
					this.layoutProvider.setScrollLeft(scrollLeft);
				},
				getScrollHeight: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollHeight: View is disposed');
					}
					return this.layoutProvider.getScrollHeight();
				},
				getScrollWidth: () => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getScrollWidth: View is disposed');
					}
					return this.layoutProvider.getScrollWidth();
				},
				getVerticalOffsetForPosition: (modelLineNumber:number, modelColumn:number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getVerticalOffsetForPosition: View is disposed');
					}
					var modelPosition = this.context.model.validateModelPosition({
						lineNumber: modelLineNumber,
						column: modelColumn
					});
					var viewPosition = this.context.model.convertModelPositionToViewPosition(modelPosition.lineNumber, modelPosition.column);
					return this.layoutProvider.getVerticalOffsetForLineNumber(viewPosition.lineNumber);
				},
				delegateVerticalScrollbarMouseDown: (browserEvent: MouseEvent) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.delegateVerticalScrollbarMouseDown: View is disposed');
					}
					this.layoutProvider.delegateVerticalScrollbarMouseDown(browserEvent);
				},
				getOffsetForColumn: (modelLineNumber: number, modelColumn: number) => {
					if (this._isDisposed) {
						throw new Error('ViewImpl.codeEditorHelper.getOffsetForColumn: View is disposed');
					}
					var modelPosition = this.context.model.validateModelPosition({
						lineNumber: modelLineNumber,
						column: modelColumn
					});
					var viewPosition = this.context.model.convertModelPositionToViewPosition(modelPosition.lineNumber, modelPosition.column);
					this._flushAccumulatedAndRenderNow();
A
Alex Dima 已提交
570
					var visibleRanges = this.viewLines.visibleRangesForRange2(new Range(viewPosition.lineNumber, viewPosition.column, viewPosition.lineNumber, viewPosition.column), 0);
E
Erich Gamma 已提交
571 572 573 574 575 576 577 578 579 580
					if (!visibleRanges) {
						return -1;
					}
					return visibleRanges[0].left;
				}
			};
		}
		return this.codeEditorHelper;
	}

A
Alex Dima 已提交
581
	public getCenteredRangeInViewport(): editorCommon.IEditorRange {
E
Erich Gamma 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594
		if (this._isDisposed) {
			throw new Error('ViewImpl.getCenteredRangeInViewport: View is disposed');
		}
		var viewLineNumber = this.layoutProvider.getCenteredViewLineNumberInViewport();
		var viewModel = this.context.model;
		var currentCenteredViewRange = new Range(viewLineNumber, 1, viewLineNumber, viewModel.getLineMaxColumn(viewLineNumber));
		return viewModel.convertViewRangeToModelRange(currentCenteredViewRange);
	}

//	public getLineInfoProvider():view.ILineInfoProvider {
//		return this.viewLines;
//	}

A
Alex Dima 已提交
595
	public getInternalEventBus(): IEventEmitter {
E
Erich Gamma 已提交
596 597 598 599 600 601
		if (this._isDisposed) {
			throw new Error('ViewImpl.getInternalEventBus: View is disposed');
		}
		return this.outgoingEventBus;
	}

A
Alex Dima 已提交
602
	public saveState(): editorCommon.IViewState {
E
Erich Gamma 已提交
603 604 605 606 607 608
		if (this._isDisposed) {
			throw new Error('ViewImpl.saveState: View is disposed');
		}
		return this.layoutProvider.saveState();
	}

A
Alex Dima 已提交
609
	public restoreState(state: editorCommon.IViewState): void {
E
Erich Gamma 已提交
610 611 612 613 614 615 616 617 618 619 620
		if (this._isDisposed) {
			throw new Error('ViewImpl.restoreState: View is disposed');
		}
		this._flushAnyAccumulatedEvents();
		return this.layoutProvider.restoreState(state);
	}

	public focus(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl.focus: View is disposed');
		}
621
		this.keyboardHandler.focusTextArea();
E
Erich Gamma 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

		// IE does not trigger the focus event immediately, so we must help it a little bit
		this._setHasFocus(true);
	}

	public isFocused(): boolean {
		if (this._isDisposed) {
			throw new Error('ViewImpl.isFocused: View is disposed');
		}
		return this.hasFocus;
	}

	public createOverviewRuler(cssClassName: string, minimumHeight: number, maximumHeight: number): OverviewRuler {
		if (this._isDisposed) {
			throw new Error('ViewImpl.createOverviewRuler: View is disposed');
		}
		return new OverviewRuler(
				this.context, cssClassName, this.layoutProvider.getScrollHeight(), minimumHeight, maximumHeight,
				(lineNumber:number) => this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber)
		);
	}

A
Alex Dima 已提交
644
	public change(callback: (changeAccessor: editorBrowser.IViewZoneChangeAccessor) => any): boolean {
E
Erich Gamma 已提交
645 646 647 648 649 650 651
		if (this._isDisposed) {
			throw new Error('ViewImpl.change: View is disposed');
		}
		var zonesHaveChanged = false;
		this._renderOnce(() => {
			// Handle events to avoid "adjusting" newly inserted view zones
			this._flushAnyAccumulatedEvents();
A
Alex Dima 已提交
652 653
			var changeAccessor:editorBrowser.IViewZoneChangeAccessor = {
				addZone: (zone:editorBrowser.IViewZone): number => {
E
Erich Gamma 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
					zonesHaveChanged = true;
					return this.viewZones.addZone(zone);
				},
				removeZone: (id:number): void => {
					zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
				},
				layoutZone: (id: number): void => {
					zonesHaveChanged = this.viewZones.layoutZone(id) || zonesHaveChanged;
				}
			};

			var r: any = null;
			try {
				r = callback(changeAccessor);
			} catch (e) {
A
Alex Dima 已提交
669
				onUnexpectedError(e);
E
Erich Gamma 已提交
670 671 672 673 674 675 676
			}

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

			if (zonesHaveChanged) {
A
Alex Dima 已提交
677
				this.context.privateViewEventBus.emit(editorCommon.EventType.ViewZonesChanged, null);
E
Erich Gamma 已提交
678 679 680 681 682 683 684
			}

			return r;
		});
		return zonesHaveChanged;
	}

A
Alex Dima 已提交
685
	public getWhitespaces(): editorCommon.IEditorWhitespace[]{
E
Erich Gamma 已提交
686 687 688 689 690 691
		if (this._isDisposed) {
			throw new Error('ViewImpl.getWhitespaces: View is disposed');
		}
		return this.layoutProvider.getWhitespaces();
	}

A
Alex Dima 已提交
692
	public addContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
693 694 695 696 697 698 699 700 701
		if (this._isDisposed) {
			throw new Error('ViewImpl.addContentWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.contentWidgets.addWidget(widgetData.widget);
			this.layoutContentWidget(widgetData);
		});
	}

A
Alex Dima 已提交
702
	public layoutContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
703 704 705 706 707 708 709 710 711 712
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutContentWidget: View is disposed');
		}
		this._renderOnce(() => {
			var position1 = widgetData.position ? widgetData.position.position : null;
			var preference1 = widgetData.position ? widgetData.position.preference : null;
			this.contentWidgets.setWidgetPosition(widgetData.widget, position1, preference1);
		});
	}

A
Alex Dima 已提交
713
	public removeContentWidget(widgetData: editorBrowser.IContentWidgetData): void {
E
Erich Gamma 已提交
714 715 716 717 718 719 720 721
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeContentWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.contentWidgets.removeWidget(widgetData.widget);
		});
	}

A
Alex Dima 已提交
722
	public addOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
723 724 725 726 727 728 729 730 731
		if (this._isDisposed) {
			throw new Error('ViewImpl.addOverlayWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.overlayWidgets.addWidget(widgetData.widget);
			this.layoutOverlayWidget(widgetData);
		});
	}

A
Alex Dima 已提交
732
	public layoutOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
733 734 735 736 737 738 739 740 741
		if (this._isDisposed) {
			throw new Error('ViewImpl.layoutOverlayWidget: View is disposed');
		}
		this._renderOnce(() => {
			var preference2 = widgetData.position ? widgetData.position.preference : null;
			this.overlayWidgets.setWidgetPosition(widgetData.widget, preference2);
		});
	}

A
Alex Dima 已提交
742
	public removeOverlayWidget(widgetData: editorBrowser.IOverlayWidgetData): void {
E
Erich Gamma 已提交
743 744 745 746 747 748 749 750
		if (this._isDisposed) {
			throw new Error('ViewImpl.removeOverlayWidget: View is disposed');
		}
		this._renderOnce(() => {
			this.overlayWidgets.removeWidget(widgetData.widget);
		});
	}

751
	public render(now:boolean): void {
E
Erich Gamma 已提交
752 753 754 755 756
		if (this._isDisposed) {
			throw new Error('ViewImpl.render: View is disposed');
		}
		// Force a render with a layout event
		this.layoutProvider.emitLayoutChangedEvent();
757 758 759
		if (now) {
			this._flushAccumulatedAndRenderNow();
		}
E
Erich Gamma 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	}

	public renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl.renderOnce: View is disposed');
		}
		return this._renderOnce(callback);
	}

	// --- end Code Editor APIs

	private _renderOnce(callback: () => any): any {
		if (this._isDisposed) {
			throw new Error('ViewImpl._renderOnce: View is disposed');
		}
		return this.outgoingEventBus.deferredEmit(() => {
			try {
				var r = callback ? callback() : null;
			} finally {
				this._scheduleRender();
			}

			return r;
		});
	}

	private _scheduleRender(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl._scheduleRender: View is disposed');
		}
		if (this._renderAnimationFrame === null) {
A
Alex Dima 已提交
791
			this._renderAnimationFrame = dom.runAtThisOrScheduleAtNextAnimationFrame(this._onRenderScheduled.bind(this), 100);
E
Erich Gamma 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
		}
	}

	private _onRenderScheduled(): void {
		this._renderAnimationFrame = null;
		this._flushAccumulatedAndRenderNow();
	}

	private _renderNow(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl._renderNow: View is disposed');
		}
		this.actualRender();
	}

A
Alex Dima 已提交
807
	private createRenderingContext(linesViewportData:editorCommon.IViewLinesViewportData): editorBrowser.IRenderingContext {
E
Erich Gamma 已提交
808 809 810 811 812

		var vInfo = this.layoutProvider.getCurrentViewport();

		var deltaTop = linesViewportData.visibleRangesDeltaTop;

A
Alex Dima 已提交
813
		var r:editorBrowser.IRenderingContext = {
E
Erich Gamma 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
			linesViewportData: linesViewportData,
			scrollWidth: this.layoutProvider.getScrollWidth(),
			scrollHeight: this.layoutProvider.getScrollHeight(),

			visibleRange: linesViewportData.visibleRange,
			bigNumbersDelta: linesViewportData.bigNumbersDelta,

			viewportWidth: vInfo.width,
			viewportHeight: vInfo.height,
			viewportLeft: vInfo.left,
			viewportTop: vInfo.top,

			getScrolledTopFromAbsoluteTop: (absoluteTop:number) => {
				return this.layoutProvider.getScrolledTopFromAbsoluteTop(absoluteTop);
			},

			getViewportVerticalOffsetForLineNumber: (lineNumber:number) => {
				var verticalOffset = this.layoutProvider.getVerticalOffsetForLineNumber(lineNumber);
				var scrolledTop = this.layoutProvider.getScrolledTopFromAbsoluteTop(verticalOffset);
				return scrolledTop;
			},

			getDecorationsInViewport: () => linesViewportData.getDecorationsInViewport(),

A
Alex Dima 已提交
838
			linesVisibleRangesForRange: (range:editorCommon.IRange, includeNewLines:boolean) => {
E
Erich Gamma 已提交
839 840 841
				return this.viewLines.linesVisibleRangesForRange(range, includeNewLines);
			},

A
Alex Dima 已提交
842
			visibleRangeForPosition: (position:editorCommon.IPosition) => {
A
Alex Dima 已提交
843
				var visibleRanges = this.viewLines.visibleRangesForRange2(new Range(position.lineNumber, position.column, position.lineNumber, position.column), deltaTop);
E
Erich Gamma 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
				if (!visibleRanges) {
					return null;
				}
				return visibleRanges[0];
			},

			lineIsVisible: (lineNumber:number) => {
				return linesViewportData.visibleRange.startLineNumber <= lineNumber && lineNumber <= linesViewportData.visibleRange.endLineNumber;
			}
		};
		return r;
	}

	private actualRender(): void {
		if (this._isDisposed) {
			throw new Error('ViewImpl.actualRender: View is disposed');
		}
A
Alex Dima 已提交
861
		if (!dom.isInDOM(this.domNode)) {
E
Erich Gamma 已提交
862 863 864
			return;
		}

A
Alex Dima 已提交
865
		var t = timer.start(timer.Topic.EDITOR, 'View.render');
E
Erich Gamma 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

		var i:number,
			len:number;

		try {

			for (i = 0, len = this.viewParts.length; i < len; i++) {
				this.viewParts[i].onBeforeForcedLayout();
			}

			var linesViewportData = this.viewLines.render();

			var renderingContext = this.createRenderingContext(linesViewportData);

			// Render the rest of the parts
			for (i = 0, len = this.viewParts.length; i < len; i++) {
				this.viewParts[i].onReadAfterForcedLayout(renderingContext);
			}

			for (i = 0, len = this.viewParts.length; i < len; i++) {
				this.viewParts[i].onWriteAfterForcedLayout();
			}
		} catch (err) {
A
Alex Dima 已提交
889
			onUnexpectedError(err);
E
Erich Gamma 已提交
890 891 892 893 894 895 896 897
		}

		t.stop();
	}

	private _setHasFocus(newHasFocus:boolean): void {
		if (this.hasFocus !== newHasFocus) {
			this.hasFocus = newHasFocus;
A
Alex Dima 已提交
898
			this.context.privateViewEventBus.emit(editorCommon.EventType.ViewFocusChanged, this.hasFocus);
E
Erich Gamma 已提交
899 900 901 902
		}
	}
}

A
Alex Dima 已提交
903
class ViewContext implements editorBrowser.IViewContext {
E
Erich Gamma 已提交
904 905

	public editorId:number;
A
Alex Dima 已提交
906 907 908 909 910
	public configuration:editorCommon.IConfiguration;
	public model: editorCommon.IViewModel;
	public privateViewEventBus:editorCommon.IViewEventBus;
	public addEventHandler:(eventHandler:editorBrowser.IViewEventHandler)=>void;
	public removeEventHandler:(eventHandler:editorBrowser.IViewEventHandler)=>void;
E
Erich Gamma 已提交
911 912 913

	constructor(
					editorId:number,
A
Alex Dima 已提交
914 915 916 917 918
					configuration:editorCommon.IConfiguration,
					model: editorCommon.IViewModel,
					privateViewEventBus:editorCommon.IViewEventBus,
					addEventHandler:(eventHandler:editorBrowser.IViewEventHandler)=>void,
					removeEventHandler:(eventHandler:editorBrowser.IViewEventHandler)=>void
E
Erich Gamma 已提交
919 920 921 922 923 924 925 926 927 928
				)
	{
		this.editorId = editorId;
		this.configuration = configuration;
		this.model = model;
		this.privateViewEventBus = privateViewEventBus;
		this.addEventHandler = addEventHandler;
		this.removeEventHandler = removeEventHandler;
	}
}