explorerViewlet.ts 6.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import 'vs/css!./media/explorerviewlet';
9
import {IDisposable} from 'vs/base/common/lifecycle';
10
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import {Dimension, Builder} from 'vs/base/browser/builder';
import {Scope} from 'vs/workbench/common/memento';
import {VIEWLET_ID} from 'vs/workbench/parts/files/common/files';
import {CollapsibleViewletView, IViewletView, Viewlet} from 'vs/workbench/browser/viewlet';
import {IActionRunner} from 'vs/base/common/actions';
import {SplitView} from 'vs/base/browser/ui/splitview/splitview';
import {ActionRunner, FileViewletState} from 'vs/workbench/parts/files/browser/views/explorerViewer';
import {ExplorerView} from 'vs/workbench/parts/files/browser/views/explorerView';
import {EmptyView} from 'vs/workbench/parts/files/browser/views/emptyView';
import {WorkingFilesView} from 'vs/workbench/parts/files/browser/views/workingFilesView';
import {IStorageService} from 'vs/platform/storage/common/storage';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {StructuredSelection} from 'vs/platform/selection/common/selection';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';

export class ExplorerViewlet extends Viewlet {
	private viewletContainer: Builder;
	private splitView: SplitView;
	private views: IViewletView[];

	private explorerView: ExplorerView;
	private workingFilesView: WorkingFilesView;
34
	private lastFocusedView: ExplorerView | WorkingFilesView | EmptyView;
35
	private focusListener: IDisposable;
E
Erich Gamma 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

	private viewletSettings: any;
	private viewletState: FileViewletState;

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IStorageService private storageService: IStorageService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(VIEWLET_ID, telemetryService);

		this.views = [];
		this.viewletState = new FileViewletState();

		this.viewletSettings = this.getMemento(storageService, Scope.WORKSPACE);
	}

	public create(parent: Builder): TPromise<void> {
		super.create(parent);

		this.viewletContainer = parent.div().addClass('explorer-viewlet');

		this.splitView = new SplitView(this.viewletContainer.getHTMLElement());

		// Working files view
		this.addWorkingFilesView();

		// Explorer view
		this.addExplorerView();

67
		// Track focus
68
		this.focusListener = this.splitView.onFocus((view: ExplorerView | WorkingFilesView | EmptyView) => {
69 70 71
			this.lastFocusedView = view;
		});

72
		return TPromise.join(this.views.map((view) => view.create())).then(() => void 0);
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	}

	private addWorkingFilesView(): void {
		this.workingFilesView = this.instantiationService.createInstance(WorkingFilesView, this.getActionRunner(), this.viewletSettings);
		this.splitView.addView(this.workingFilesView);

		this.views.push(this.workingFilesView);
	}

	private addExplorerView(): void {
		let explorerView: CollapsibleViewletView | EmptyView;

		// With a Workspace
		if (this.contextService.getWorkspace()) {
			this.explorerView = explorerView = this.instantiationService.createInstance(ExplorerView, this.viewletState, this.getActionRunner(), this.viewletSettings);
		}

		// No workspace
		else {
			explorerView = this.instantiationService.createInstance(EmptyView);
		}

		this.splitView.addView(explorerView);
		this.views.push(explorerView);
	}

	public getExplorerView(): ExplorerView {
		return this.explorerView;
	}

103 104 105 106
	public getWorkingFilesView(): WorkingFilesView {
		return this.workingFilesView;
	}

E
Erich Gamma 已提交
107 108
	public setVisible(visible: boolean): TPromise<void> {
		return super.setVisible(visible).then(() => {
109
			return TPromise.join(this.views.map((view) => view.setVisible(visible))).then(() => void 0);
E
Erich Gamma 已提交
110 111 112 113 114 115
		});
	}

	public focus(): void {
		super.focus();

116
		if (this.lastFocusedView && this.lastFocusedView.isExpanded() && this.hasSelectionOrFocus(this.lastFocusedView)) {
117 118 119 120
			this.lastFocusedView.focusBody();
			return;
		}

121
		if (this.hasSelectionOrFocus(this.workingFilesView)) {
122 123 124
			return this.workingFilesView.focusBody();
		}

125
		if (this.hasSelectionOrFocus(this.explorerView)) {
126
			return this.explorerView.focusBody();
E
Erich Gamma 已提交
127
		}
128 129 130 131 132 133 134 135 136 137 138 139

		if (this.workingFilesView && this.workingFilesView.isExpanded()) {
			return this.workingFilesView.focusBody();
		}

		if (this.explorerView && this.explorerView.isExpanded()) {
			return this.explorerView.focusBody();
		}

		return this.workingFilesView.focus();
	}

140
	private hasSelectionOrFocus(view: ExplorerView | WorkingFilesView | EmptyView): boolean {
141 142 143 144 145 146 147 148
		if (!view) {
			return false;
		}

		if (!view.isExpanded()) {
			return false;
		}

149 150 151 152 153 154 155 156
		if (view instanceof ExplorerView || view instanceof WorkingFilesView) {
			const viewer = view.getViewer();
			if (!viewer) {
				return false;
			}

			return !!viewer.getFocus() || (viewer.getSelection() && viewer.getSelection().length > 0);

157 158
		}

159
		return false;
E
Erich Gamma 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	}

	public layout(dimension: Dimension): void {
		this.splitView.layout(dimension.height);
	}

	public getSelection(): StructuredSelection {
		return this.explorerView ? this.explorerView.getSelection() : this.workingFilesView.getSelection();
	}

	public getActionRunner(): IActionRunner {
		if (!this.actionRunner) {
			this.actionRunner = new ActionRunner(this.viewletState);
		}

		return this.actionRunner;
	}

	public shutdown(): void {
		this.views.forEach((view) => view.shutdown());

		super.shutdown();
	}

	public dispose(): void {
		if (this.splitView) {
			this.splitView.dispose();
187 188 189 190 191 192
			this.splitView = null;
		}

		if (this.focusListener) {
			this.focusListener.dispose();
			this.focusListener = null;
E
Erich Gamma 已提交
193 194 195
		}
	}
}