openEditorsView.ts 4.7 KB
Newer Older
I
isidor 已提交
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.
 *--------------------------------------------------------------------------------------------*/

import nls = require('vs/nls');
I
isidor 已提交
7
import errors = require('vs/base/common/errors');
I
isidor 已提交
8 9
import {IActionRunner} from 'vs/base/common/actions';
import dom = require('vs/base/browser/dom');
I
isidor 已提交
10
import {Tree} from 'vs/base/parts/tree/browser/treeImpl';
I
isidor 已提交
11 12 13 14 15 16
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IMessageService} from 'vs/platform/message/common/message';
import {AdaptiveCollapsibleViewletView} from 'vs/workbench/browser/viewlet';
import {ITextFileService} from 'vs/workbench/parts/files/common/files';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IEditorStacksModel} from 'vs/workbench/common/editor/editorStacksModel';
I
isidor 已提交
17 18
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {Renderer, DataSource} from 'vs/workbench/parts/files/browser/views/openEditorsViewer';
I
isidor 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31

const $ = dom.emmet;

export class OpenEditorsView extends AdaptiveCollapsibleViewletView {

	private static MEMENTO_COLLAPSED = 'openEditors.memento.collapsed';
	private static DEFAULT_MAX_VISIBLE_EDITORS = 9;
	private static DEFAULT_DYNAMIC_HEIGHT = true;

	private settings: any;
	private maxVisibleOpenEditors: number;
	private dynamicHeight: boolean;

I
isidor 已提交
32
	private model: IEditorStacksModel;
I
isidor 已提交
33 34 35 36 37
	private dirtyCountElement: HTMLElement;
	private lastDirtyCount: number;

	constructor(actionRunner: IActionRunner, settings: any,
		@IMessageService messageService: IMessageService,
I
isidor 已提交
38
		@IInstantiationService private instantiationService: IInstantiationService,
I
isidor 已提交
39 40 41 42 43 44 45
		@IContextMenuService contextMenuService: IContextMenuService,
		@ITextFileService private textFileService: ITextFileService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(actionRunner, OpenEditorsView.computeExpandedBodySize(editorService.getStacksModel()), !!settings[OpenEditorsView.MEMENTO_COLLAPSED], nls.localize('openEditosrSection', "Open Editors Section"), messageService, contextMenuService);

		this.settings = settings;
I
isidor 已提交
46
		this.model = editorService.getStacksModel();
I
isidor 已提交
47
		this.lastDirtyCount = 0;
I
isidor 已提交
48
		console.log(this.getExpandedBodySize(this.model));
I
isidor 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	}

	public renderHeader(container: HTMLElement): void {
		const titleDiv = dom.append(container, $('.title'));
		const titleSpan = dom.append(titleDiv, $('span'));
		titleSpan.textContent = nls.localize('openEditors', "Open Editors");

		this.dirtyCountElement = dom.append(titleDiv, $('.monaco-count-badge'));
		this.updateDirtyIndicator();

		super.renderHeader(container);
	}

	public renderBody(container: HTMLElement): void {
		this.treeContainer = super.renderViewTree(container);
		dom.addClass(this.treeContainer, 'explorer-open-editors');

I
isidor 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		const dataSource = this.instantiationService.createInstance(DataSource);
		const renderer = this.instantiationService.createInstance(Renderer);

		this.tree = new Tree(this.treeContainer, {
			dataSource,
			renderer
		}, {
			indentPixels: 0,
			twistiePixels: 8,
			ariaLabel: nls.localize('treeAriaLabel', "Open Editors")
		});

		// Show groups only if there is more than 1
		const treeInput = this.model.groups.length === 1 ? this.model.groups[0] : this.model;
		this.tree.setInput(treeInput).done(null, errors.onUnexpectedError);
I
isidor 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
	}

	private updateDirtyIndicator(): void {
		let dirty = this.textFileService.getDirty().length;
		this.lastDirtyCount = dirty;
		if (dirty === 0) {
			this.dirtyCountElement.hidden = true;
		} else {
			this.dirtyCountElement.textContent = nls.localize('dirtyCounter', "{0} unsaved", dirty);
			this.dirtyCountElement.hidden = false;
		}
	}

I
isidor 已提交
94
	private getExpandedBodySize(model: IEditorStacksModel): number {
I
isidor 已提交
95 96 97 98
		return OpenEditorsView.computeExpandedBodySize(model, this.maxVisibleOpenEditors, this.dynamicHeight);
	}

	private static computeExpandedBodySize(model: IEditorStacksModel, maxVisibleOpenEditors = OpenEditorsView.DEFAULT_MAX_VISIBLE_EDITORS, dynamicHeight = OpenEditorsView.DEFAULT_DYNAMIC_HEIGHT): number {
I
isidor 已提交
99
		const entryCount = model.groups.reduce((sum, group) => sum + group.count, 0);
I
isidor 已提交
100 101 102 103 104 105 106 107 108 109 110 111

		let itemsToShow: number;
		if (dynamicHeight) {
			itemsToShow = Math.min(Math.max(maxVisibleOpenEditors, 1), entryCount);
		} else {
			itemsToShow = Math.max(maxVisibleOpenEditors, 1);
		}
		// We only show the group labels if there is more than 1 group
		if (model.groups.length > 1) {
			itemsToShow += model.groups.length;
		}

I
isidor 已提交
112
		return itemsToShow * Renderer.ITEM_HEIGHT;
I
isidor 已提交
113 114
	}
}