openEditorsView.ts 10.2 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
import {TPromise} from 'vs/base/common/winjs.base';
I
isidor 已提交
9 10
import {IActionRunner} from 'vs/base/common/actions';
import dom = require('vs/base/browser/dom');
I
isidor 已提交
11
import {CollapsibleState} from 'vs/base/browser/ui/splitview/splitview';
I
isidor 已提交
12
import {Tree} from 'vs/base/parts/tree/browser/treeImpl';
I
isidor 已提交
13 14
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IMessageService} from 'vs/platform/message/common/message';
I
isidor 已提交
15 16 17 18
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IEventService} from 'vs/platform/event/common/event';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {EventType as WorkbenchEventType, UntitledEditorEvent} from 'vs/workbench/common/events';
19
import {EditorInput} from 'vs/workbench/common/editor';
I
isidor 已提交
20
import {AdaptiveCollapsibleViewletView} from 'vs/workbench/browser/viewlet';
I
isidor 已提交
21
import {ITextFileService, TextFileChangeEvent, EventType as FileEventType, AutoSaveMode, IFilesConfiguration} from 'vs/workbench/parts/files/common/files';
I
isidor 已提交
22
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
23
import {IEditorStacksModel, IEditorGroup} from 'vs/workbench/common/editor/editorStacksModel';
I
isidor 已提交
24
import {Renderer, DataSource, Controller} from 'vs/workbench/parts/files/browser/views/openEditorsViewer';
I
isidor 已提交
25 26 27 28 29 30

const $ = dom.emmet;

export class OpenEditorsView extends AdaptiveCollapsibleViewletView {

	private static MEMENTO_COLLAPSED = 'openEditors.memento.collapsed';
I
isidor 已提交
31
	private static DEFAULT_MAX_VISIBLE_OPEN_EDITORS = 9;
I
isidor 已提交
32 33 34 35 36 37
	private static DEFAULT_DYNAMIC_HEIGHT = true;

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

I
isidor 已提交
38
	private model: IEditorStacksModel;
I
isidor 已提交
39 40 41 42 43
	private dirtyCountElement: HTMLElement;
	private lastDirtyCount: number;

	constructor(actionRunner: IActionRunner, settings: any,
		@IMessageService messageService: IMessageService,
I
isidor 已提交
44
		@IEventService private eventService: IEventService,
I
isidor 已提交
45
		@IInstantiationService private instantiationService: IInstantiationService,
I
isidor 已提交
46 47
		@IContextMenuService contextMenuService: IContextMenuService,
		@ITextFileService private textFileService: ITextFileService,
I
isidor 已提交
48
		@IConfigurationService private configurationService: IConfigurationService,
I
isidor 已提交
49 50 51 52 53
		@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 已提交
54
		this.model = editorService.getStacksModel();
I
isidor 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		this.lastDirtyCount = 0;
	}

	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 已提交
73 74
		const dataSource = this.instantiationService.createInstance(DataSource);
		const renderer = this.instantiationService.createInstance(Renderer);
I
isidor 已提交
75
		const controller = this.instantiationService.createInstance(Controller);
I
isidor 已提交
76 77 78

		this.tree = new Tree(this.treeContainer, {
			dataSource,
I
isidor 已提交
79 80
			renderer,
			controller
I
isidor 已提交
81 82
		}, {
			indentPixels: 0,
83
			twistiePixels: 20,
I
isidor 已提交
84 85 86 87 88 89
			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 已提交
90
		this.tree.expandAll(this.model.groups).done(null, errors.onUnexpectedError);
I
isidor 已提交
91 92
	}

I
isidor 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	public create(): TPromise<void> {

		// Load Config
		const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
		this.onConfigurationUpdated(configuration);

		// listeners
		this.registerListeners();

		return super.create();
	}

	private registerListeners(): void {

		// update on model changes
108 109 110 111 112
		this.toDispose.push(this.model.onGroupActivated(e => this.onEditorGroupUpdated(e)));
		this.toDispose.push(this.model.onGroupClosed(e => this.onEditorGroupUpdated(e)));
		this.toDispose.push(this.model.onGroupMoved(e => this.onEditorGroupUpdated(e)));
		this.toDispose.push(this.model.onGroupOpened(e => this.onEditorGroupUpdated(e)));
		this.toDispose.push(this.model.onGroupRenamed(e => this.onEditorGroupUpdated(e)));
I
isidor 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

		// listen to untitled
		this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onUntitledFileDirty()));
		this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DELETED, (e: UntitledEditorEvent) => this.onUntitledFileDeleted()));

		// listen to files being changed locally
		this.toDispose.push(this.eventService.addListener2(FileEventType.FILE_DIRTY, (e: TextFileChangeEvent) => this.onTextFileDirty(e)));
		this.toDispose.push(this.eventService.addListener2(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onTextFileSaved(e)));
		this.toDispose.push(this.eventService.addListener2(FileEventType.FILE_SAVE_ERROR, (e: TextFileChangeEvent) => this.onTextFileSaveError(e)));
		this.toDispose.push(this.eventService.addListener2(FileEventType.FILE_REVERTED, (e: TextFileChangeEvent) => this.onTextFileReverted(e)));

		// Also handle configuration updates
		this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config)));
	}

128 129 130 131 132 133 134 135 136 137
	private onEditorGroupUpdated(e: IEditorGroup): void {
		if (this.isDisposed) {
			return;
		}

		// View size
		this.expandedBodySize = this.getExpandedBodySize(this.model);

		if (this.tree) {
			// Show in tree
138 139 140 141 142 143
			const treeInput = this.model.groups.length === 1 ? this.model.groups[0] : this.model;
			if (treeInput !== this.tree.getInput()) {
				this.tree.setInput(treeInput).done(null, errors.onUnexpectedError);
			} else {
				this.tree.refresh().done(null, errors.onUnexpectedError);
			}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

			// Make sure to keep active editor input highlighted
			if (this.model.activeGroup) {
				this.highlightEntry(this.model.activeGroup.activeEditor);
			}
		}
	}

	private highlightEntry(entry: EditorInput): void {
		this.tree.clearFocus();
		this.tree.clearSelection();

		if (entry) {
			this.tree.setFocus(entry);
			this.tree.setSelection([entry]);
			this.tree.reveal(entry).done(null, errors.onUnexpectedError);
		}
	}

I
isidor 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	private onConfigurationUpdated(configuration: IFilesConfiguration): void {
		// TODO@isidor change configuration name
		let visibleOpenEditors = configuration && configuration.explorer && configuration.explorer.workingFiles && configuration.explorer.workingFiles.maxVisible;
		if (typeof visibleOpenEditors === 'number') {
			this.maxVisibleOpenEditors = visibleOpenEditors;
		} else {
			this.maxVisibleOpenEditors = OpenEditorsView.DEFAULT_MAX_VISIBLE_OPEN_EDITORS;
		}

		// TODO@isidor change configuration name
		let dynamicHeight = configuration && configuration.explorer && configuration.explorer.workingFiles && configuration.explorer.workingFiles.dynamicHeight;
		if (typeof dynamicHeight === 'boolean') {
			this.dynamicHeight = dynamicHeight;
		} else {
			this.dynamicHeight = OpenEditorsView.DEFAULT_DYNAMIC_HEIGHT;
		}

		// Adjust expanded body size
		this.expandedBodySize = this.getExpandedBodySize(this.model);
	}

	private onTextFileDirty(e: TextFileChangeEvent): void {
		if (this.textFileService.getAutoSaveMode() !== AutoSaveMode.AFTER_SHORT_DELAY) {
			this.updateDirtyIndicator(); // no indication needed when auto save is enabled for short delay
		}
	}

	private onTextFileSaved(e: TextFileChangeEvent): void {
		if (this.lastDirtyCount > 0) {
			this.updateDirtyIndicator();
		}
	}

	private onTextFileSaveError(e: TextFileChangeEvent): void {
		this.updateDirtyIndicator();
	}

	private onTextFileReverted(e: TextFileChangeEvent): void {
		if (this.lastDirtyCount > 0) {
			this.updateDirtyIndicator();
		}
	}

	private onUntitledFileDirty(): void {
		this.updateDirtyIndicator();
	}

	private onUntitledFileDeleted(): void {
		if (this.lastDirtyCount > 0) {
			this.updateDirtyIndicator();
		}
	}

I
isidor 已提交
216 217 218 219 220 221 222 223 224 225 226
	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 已提交
227
	private getExpandedBodySize(model: IEditorStacksModel): number {
I
isidor 已提交
228 229 230
		return OpenEditorsView.computeExpandedBodySize(model, this.maxVisibleOpenEditors, this.dynamicHeight);
	}

I
isidor 已提交
231
	private static computeExpandedBodySize(model: IEditorStacksModel, maxVisibleOpenEditors = OpenEditorsView.DEFAULT_MAX_VISIBLE_OPEN_EDITORS, dynamicHeight = OpenEditorsView.DEFAULT_DYNAMIC_HEIGHT): number {
I
isidor 已提交
232
		const entryCount = model.groups.reduce((sum, group) => sum + group.count, 0);
I
isidor 已提交
233 234 235 236 237 238 239 240 241 242 243 244

		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 已提交
245
		return itemsToShow * Renderer.ITEM_HEIGHT;
I
isidor 已提交
246
	}
I
isidor 已提交
247 248 249 250 251 252 253 254 255 256 257 258

	public getOptimalWidth():number {
		let parentNode = this.tree.getHTMLElement();
		let childNodes = [].slice.call(parentNode.querySelectorAll('.monaco-file-label > .file-name'));
		return dom.getLargestChildWidth(parentNode, childNodes);
	}

	public shutdown(): void {
		this.settings[OpenEditorsView.MEMENTO_COLLAPSED] = (this.state === CollapsibleState.COLLAPSED);

		super.shutdown();
	}
I
isidor 已提交
259
}