Skip to content

Commit

Permalink
Reduce IViewLayout usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Feb 5, 2017
1 parent 5ab4210 commit ad605ea
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 126 deletions.
18 changes: 9 additions & 9 deletions src/vs/editor/browser/view/viewImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export class View extends ViewEventHandler implements editorBrowser.IView, IDisp
// - 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.layoutProvider = new LayoutProvider(configuration, model.getLineCount(), this.eventDispatcher);

this._scrollbar = new EditorScrollbar(this.layoutProvider.getScrollable(), configuration, this.eventDispatcher, this.linesContent, this.domNode, this.overflowGuardContainer);
this._scrollbar = new EditorScrollbar(this.layoutProvider.getScrollable(), configuration, this.linesContent, this.domNode, this.overflowGuardContainer);

// The view context is passed on to most classes (basically to reduce param. counts in ctors)
this._context = new ViewContext(
Expand Down Expand Up @@ -236,22 +236,22 @@ export class View extends ViewEventHandler implements editorBrowser.IView, IDisp
let scrollDecoration = new ScrollDecorationViewPart(this._context);
this.viewParts.push(scrollDecoration);

let contentViewOverlays = new ContentViewOverlays(this._context, this.layoutProvider);
let contentViewOverlays = new ContentViewOverlays(this._context);
this.viewParts.push(contentViewOverlays);
contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context, this.layoutProvider));
contentViewOverlays.addDynamicOverlay(new CurrentLineHighlightOverlay(this._context));
contentViewOverlays.addDynamicOverlay(new SelectionsOverlay(this._context));
contentViewOverlays.addDynamicOverlay(new DecorationsOverlay(this._context));
contentViewOverlays.addDynamicOverlay(new IndentGuidesOverlay(this._context));

let marginViewOverlays = new MarginViewOverlays(this._context, this.layoutProvider);
let marginViewOverlays = new MarginViewOverlays(this._context);
this.viewParts.push(marginViewOverlays);
marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context, this.layoutProvider));
marginViewOverlays.addDynamicOverlay(new CurrentLineMarginHighlightOverlay(this._context));
marginViewOverlays.addDynamicOverlay(new GlyphMarginOverlay(this._context));
marginViewOverlays.addDynamicOverlay(new MarginViewLineDecorationsOverlay(this._context));
marginViewOverlays.addDynamicOverlay(new LinesDecorationsOverlay(this._context));
marginViewOverlays.addDynamicOverlay(new LineNumbersOverlay(this._context));

let margin = new Margin(this._context, this.layoutProvider);
let margin = new Margin(this._context);
margin.domNode.appendChild(this.viewZones.marginDomNode);
margin.domNode.appendChild(marginViewOverlays.getDomNode());
this.viewParts.push(margin);
Expand All @@ -267,7 +267,7 @@ export class View extends ViewEventHandler implements editorBrowser.IView, IDisp
this.overlayWidgets = new ViewOverlayWidgets(this._context);
this.viewParts.push(this.overlayWidgets);

let rulers = new Rulers(this._context, this.layoutProvider);
let rulers = new Rulers(this._context);
this.viewParts.push(rulers);

// -------------- Wire dom nodes up
Expand Down Expand Up @@ -448,7 +448,7 @@ export class View extends ViewEventHandler implements editorBrowser.IView, IDisp
// --- begin event handlers

public onModelFlushed(): boolean {
this.layoutProvider.onModelFlushed();
this.layoutProvider.onModelFlushed(this._context.model.getLineCount());
return false;
}
public onModelLinesDeleted(e: editorCommon.IViewLinesDeletedEvent): boolean {
Expand Down
22 changes: 7 additions & 15 deletions src/vs/editor/browser/view/viewOverlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ import { DynamicViewOverlay } from 'vs/editor/browser/view/dynamicViewOverlay';
import { Configuration } from 'vs/editor/browser/config/configuration';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import { IRenderingContext, IRestrictedRenderingContext } from 'vs/editor/common/view/renderingContext';
import { IViewLayout } from 'vs/editor/common/viewModel/viewModel';
import { ViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';

export class ViewOverlays extends ViewLayer<ViewOverlayLine> {

private _dynamicOverlays: DynamicViewOverlay[];
private _isFocused: boolean;
protected _viewLayout: IViewLayout;

constructor(context: ViewContext, viewLayout: IViewLayout) {
constructor(context: ViewContext) {
super(context);

this._dynamicOverlays = [];
this._isFocused = false;
this._viewLayout = viewLayout;

this.domNode.setClassName('view-overlays');
}
Expand All @@ -48,7 +45,6 @@ export class ViewOverlays extends ViewLayer<ViewOverlayLine> {

public dispose(): void {
super.dispose();
this._viewLayout = null;

for (let i = 0, len = this._dynamicOverlays.length; i < len; i++) {
let dynamicOverlay = this._dynamicOverlays[i];
Expand Down Expand Up @@ -180,16 +176,13 @@ export class ViewOverlayLine implements IVisibleLine {

export class ContentViewOverlays extends ViewOverlays {

private _scrollWidth: number;
private _contentWidth: number;

constructor(context: ViewContext, viewLayout: IViewLayout) {
super(context, viewLayout);
constructor(context: ViewContext) {
super(context);

this._scrollWidth = this._viewLayout.getScrollWidth();
this._contentWidth = this._context.configuration.editor.layoutInfo.contentWidth;

this.domNode.setWidth(this._scrollWidth);
this.domNode.setHeight(0);
}

Expand All @@ -200,14 +193,13 @@ export class ContentViewOverlays extends ViewOverlays {
return super.onConfigurationChanged(e);
}
public onScrollChanged(e: IScrollEvent): boolean {
this._scrollWidth = e.scrollWidth;
return super.onScrollChanged(e) || e.scrollWidthChanged;
}

_viewOverlaysRender(ctx: IRestrictedRenderingContext): void {
super._viewOverlaysRender(ctx);

this.domNode.setWidth(Math.max(this._scrollWidth, this._contentWidth));
this.domNode.setWidth(Math.max(ctx.scrollWidth, this._contentWidth));
}
}

Expand All @@ -216,8 +208,8 @@ export class MarginViewOverlays extends ViewOverlays {
private _contentLeft: number;
private _canUseTranslate3d: boolean;

constructor(context: ViewContext, viewLayout: IViewLayout) {
super(context, viewLayout);
constructor(context: ViewContext) {
super(context);

this._contentLeft = context.configuration.editor.layoutInfo.contentLeft;
this._canUseTranslate3d = context.configuration.editor.viewInfo.canUseTranslate3d;
Expand Down Expand Up @@ -250,7 +242,7 @@ export class MarginViewOverlays extends ViewOverlays {

_viewOverlaysRender(ctx: IRestrictedRenderingContext): void {
super._viewOverlaysRender(ctx);
let height = Math.min(this._viewLayout.getTotalHeight(), 1000000);
let height = Math.min(ctx.scrollHeight, 1000000);
this.domNode.setHeight(height);
this.domNode.setWidth(this._contentLeft);
}
Expand Down
104 changes: 46 additions & 58 deletions src/vs/editor/browser/viewLayout/layoutProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,44 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Scrollable, ScrollbarVisibility } from 'vs/base/common/scrollable';
import { Disposable } from 'vs/base/common/lifecycle';
import { Scrollable, ScrollState, ScrollEvent, ScrollbarVisibility } from 'vs/base/common/scrollable';
import * as editorCommon from 'vs/editor/common/editorCommon';
import { LinesLayout } from 'vs/editor/common/viewLayout/linesLayout';
import { IViewLayout, IViewModel } from 'vs/editor/common/viewModel/viewModel';
import { IViewLayout } from 'vs/editor/common/viewModel/viewModel';
import { IPartialViewLinesViewportData } from 'vs/editor/common/viewLayout/viewLinesViewportData';
import { IViewEventBus } from 'vs/editor/common/view/viewContext';

export class LayoutProvider implements IDisposable, IViewLayout {
export class LayoutProvider extends Disposable implements IViewLayout {

static LINES_HORIZONTAL_EXTRA_PX = 30;

private _toDispose: IDisposable[];
private _configuration: editorCommon.IConfiguration;
private _privateViewEventBus: IViewEventBus;
private _model: IViewModel;
private _linesLayout: LinesLayout;
private _scrollable: Scrollable;

constructor(configuration: editorCommon.IConfiguration, model: IViewModel, privateViewEventBus: IViewEventBus) {
this._scrollable = new Scrollable();
this._scrollable.updateState({
width: configuration.editor.layoutInfo.contentWidth,
height: configuration.editor.layoutInfo.contentHeight
});
this._toDispose = [];
this._toDispose.push(this._scrollable);
constructor(configuration: editorCommon.IConfiguration, lineCount: number, privateViewEventBus: IViewEventBus) {
super();

this._configuration = configuration;
this._privateViewEventBus = privateViewEventBus;
this._model = model;

this._configuration.setMaxLineNumber(this._model.getMaxLineNumber());
this._linesLayout = new LinesLayout(lineCount, this._configuration.editor.lineHeight);

this._linesLayout = new LinesLayout(this._model.getLineCount(), this._configuration.editor.lineHeight);
this._scrollable = this._register(new Scrollable());
this._scrollable.updateState({
width: configuration.editor.layoutInfo.contentWidth,
height: configuration.editor.layoutInfo.contentHeight
});
this._register(this._scrollable.onScroll((e: ScrollEvent) => {
this._privateViewEventBus.emit(editorCommon.EventType.ViewScrollChanged, e);
}));

this._updateHeight();
}

public dispose(): void {
this._toDispose = dispose(this._toDispose);
super.dispose();
}

public getScrollable(): Scrollable {
Expand All @@ -57,21 +54,18 @@ export class LayoutProvider implements IDisposable, IViewLayout {

// ---- begin view event handlers

public onModelFlushed(): void {
this._linesLayout.onModelFlushed(this._model.getLineCount());
this._configuration.setMaxLineNumber(this._model.getMaxLineNumber());
public onModelFlushed(lineCount: number): void {
this._linesLayout.onModelFlushed(lineCount);
this._updateHeight();
}

public onModelLinesDeleted(e: editorCommon.IViewLinesDeletedEvent): void {
this._linesLayout.onModelLinesDeleted(e.fromLineNumber, e.toLineNumber);
this._configuration.setMaxLineNumber(this._model.getMaxLineNumber());
this._updateHeight();
}

public onModelLinesInserted(e: editorCommon.IViewLinesInsertedEvent): void {
this._linesLayout.onModelLinesInserted(e.fromLineNumber, e.toLineNumber);
this._configuration.setMaxLineNumber(this._model.getMaxLineNumber());
this._updateHeight();
}

Expand All @@ -89,14 +83,39 @@ export class LayoutProvider implements IDisposable, IViewLayout {
this._updateHeight();
}

// ---- end view event handlers

private _getHorizontalScrollbarHeight(scrollState: ScrollState): number {
if (this._configuration.editor.viewInfo.scrollbar.horizontal === ScrollbarVisibility.Hidden) {
// horizontal scrollbar not visible
return 0;
}
if (scrollState.width <= scrollState.scrollWidth) {
// horizontal scrollbar not visible
return 0;
}
return this._configuration.editor.viewInfo.scrollbar.horizontalScrollbarSize;
}

private _getTotalHeight(): number {
const scrollState = this._scrollable.getState();

let result = this._linesLayout.getLinesTotalHeight();
if (this._configuration.editor.viewInfo.scrollBeyondLastLine) {
result += scrollState.height - this._configuration.editor.lineHeight;
} else {
result += this._getHorizontalScrollbarHeight(scrollState);
}

return Math.max(scrollState.height, result);
}

private _updateHeight(): void {
this._scrollable.updateState({
scrollHeight: this.getTotalHeight()
scrollHeight: this._getTotalHeight()
});
}

// ---- end view event handlers

// ---- Layouting logic

public getCurrentViewport(): editorCommon.Viewport {
Expand Down Expand Up @@ -181,37 +200,6 @@ export class LayoutProvider implements IDisposable, IViewLayout {
return this._linesLayout.getLineNumberAtOrAfterVerticalOffset(verticalOffset);
}

/**
* Get the sum of heights for all objects and compute basically the `scrollHeight` for the editor content.
*
* Take into account the `scrollBeyondLastLine` and `reserveHorizontalScrollbarHeight` and produce a scrollHeight that is at least as large as `viewport`.height.
*
* @param viewport The viewport.
* @param reserveHorizontalScrollbarHeight The height of the horizontal scrollbar.
* @return Basically, the `scrollHeight` for the editor content.
*/
private _getTotalHeight(viewport: editorCommon.Viewport, reserveHorizontalScrollbarHeight: number): number {
var totalLinesHeight = this._linesLayout.getLinesTotalHeight();

if (this._configuration.editor.viewInfo.scrollBeyondLastLine) {
totalLinesHeight += viewport.height - this._configuration.editor.lineHeight;
} else {
totalLinesHeight += reserveHorizontalScrollbarHeight;
}

return Math.max(viewport.height, totalLinesHeight);
}

public getTotalHeight(): number {
const scrollState = this._scrollable.getState();
let reserveHorizontalScrollbarHeight = 0;
if (scrollState.scrollWidth > scrollState.width) {
if (this._configuration.editor.viewInfo.scrollbar.horizontal !== ScrollbarVisibility.Hidden) {
reserveHorizontalScrollbarHeight = this._configuration.editor.viewInfo.scrollbar.horizontalScrollbarSize;
}
}
return this._getTotalHeight(this.getCurrentViewport(), reserveHorizontalScrollbarHeight);
}
public getWhitespaceAtVerticalOffset(verticalOffset: number): editorCommon.IViewWhitespaceViewportData {
return this._linesLayout.getWhitespaceAtVerticalOffset(verticalOffset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,29 @@ import * as editorCommon from 'vs/editor/common/editorCommon';
import { DynamicViewOverlay } from 'vs/editor/browser/view/dynamicViewOverlay';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import { IRenderingContext } from 'vs/editor/common/view/renderingContext';
import { IViewLayout } from 'vs/editor/common/viewModel/viewModel';

export class CurrentLineHighlightOverlay extends DynamicViewOverlay {
private _context: ViewContext;
private _lineHeight: number;
private _readOnly: boolean;
private _renderLineHighlight: 'none' | 'gutter' | 'line' | 'all';
private _viewLayout: IViewLayout;
private _selectionIsEmpty: boolean;
private _primaryCursorIsInEditableRange: boolean;
private _primaryCursorLineNumber: number;
private _scrollWidth: number;
private _contentWidth: number;

constructor(context: ViewContext, viewLayout: IViewLayout) {
constructor(context: ViewContext) {
super();
this._context = context;
this._lineHeight = this._context.configuration.editor.lineHeight;
this._readOnly = this._context.configuration.editor.readOnly;
this._renderLineHighlight = this._context.configuration.editor.viewInfo.renderLineHighlight;

this._viewLayout = viewLayout;

this._selectionIsEmpty = true;
this._primaryCursorIsInEditableRange = true;
this._primaryCursorLineNumber = 1;
this._scrollWidth = this._viewLayout.getScrollWidth();
this._scrollWidth = 0;
this._contentWidth = this._context.configuration.editor.layoutInfo.contentWidth;

this._context.addEventHandler(this);
Expand All @@ -53,7 +49,6 @@ export class CurrentLineHighlightOverlay extends DynamicViewOverlay {
this._primaryCursorIsInEditableRange = true;
this._selectionIsEmpty = true;
this._primaryCursorLineNumber = 1;
this._scrollWidth = this._viewLayout.getScrollWidth();
return true;
}
public onModelLinesDeleted(e: editorCommon.IViewLinesDeletedEvent): boolean {
Expand Down Expand Up @@ -101,7 +96,6 @@ export class CurrentLineHighlightOverlay extends DynamicViewOverlay {
return true;
}
public onScrollChanged(e: editorCommon.IScrollEvent): boolean {
this._scrollWidth = e.scrollWidth;
return e.scrollWidthChanged;
}
public onZonesChanged(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ import * as editorCommon from 'vs/editor/common/editorCommon';
import { DynamicViewOverlay } from 'vs/editor/browser/view/dynamicViewOverlay';
import { ViewContext } from 'vs/editor/common/view/viewContext';
import { IRenderingContext } from 'vs/editor/common/view/renderingContext';
import { IViewLayout } from 'vs/editor/common/viewModel/viewModel';

export class CurrentLineMarginHighlightOverlay extends DynamicViewOverlay {
private _context: ViewContext;
private _lineHeight: number;
private _renderLineHighlight: 'none' | 'gutter' | 'line' | 'all';
private _viewLayout: IViewLayout;
private _primaryCursorIsInEditableRange: boolean;
private _primaryCursorLineNumber: number;
private _contentLeft: number;

constructor(context: ViewContext, viewLayout: IViewLayout) {
constructor(context: ViewContext) {
super();
this._context = context;
this._lineHeight = this._context.configuration.editor.lineHeight;
this._renderLineHighlight = this._context.configuration.editor.viewInfo.renderLineHighlight;

this._viewLayout = viewLayout;

this._primaryCursorIsInEditableRange = true;
this._primaryCursorLineNumber = 1;
this._contentLeft = this._context.configuration.editor.layoutInfo.contentLeft;
Expand Down
Loading

0 comments on commit ad605ea

Please sign in to comment.