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

6
import * as nls from 'vs/nls';
7
import * as errors from 'vs/base/common/errors';
8
import * as DOM from 'vs/base/browser/dom';
9
import { IAction } from 'vs/base/common/actions';
J
Johannes Rieken 已提交
10
import { Button } from 'vs/base/browser/ui/button/button';
11
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
I
isidor 已提交
13
import { OpenFolderAction, AddRootFolderAction } from 'vs/workbench/browser/actions/workspaceActions';
14 15
import { attachButtonStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
16 17
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
18
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
19
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
20
import { ViewletPanel, IViewletPanelOptions } from 'vs/workbench/browser/parts/views/panelViewlet';
I
isidor 已提交
21
import { ResourcesDropHandler, DragAndDropObserver } from 'vs/workbench/browser/dnd';
I
isidor 已提交
22 23
import { listDropBackground } from 'vs/platform/theme/common/colorRegistry';
import { SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
24 25 26
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ILabelService } from 'vs/platform/label/common/label';
import { Schemas } from 'vs/base/common/network';
E
Erich Gamma 已提交
27

28
export class EmptyView extends ViewletPanel {
E
Erich Gamma 已提交
29

30 31
	static readonly ID: string = 'workbench.explorer.emptyView';
	static readonly NAME = nls.localize('noWorkspace', "No Folder Opened");
32

33
	private button: Button;
34 35
	private messageElement: HTMLElement;
	private titleElement: HTMLElement;
36

37
	constructor(
S
#27823  
Sandeep Somavarapu 已提交
38
		options: IViewletViewOptions,
39 40
		@IThemeService private readonly themeService: IThemeService,
		@IInstantiationService private readonly instantiationService: IInstantiationService,
41
		@IKeybindingService keybindingService: IKeybindingService,
42
		@IContextMenuService contextMenuService: IContextMenuService,
43
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
44 45 46
		@IConfigurationService configurationService: IConfigurationService,
		@IWorkbenchEnvironmentService private environmentService: IWorkbenchEnvironmentService,
		@ILabelService private labelService: ILabelService
47
	) {
48
		super({ ...(options as IViewletPanelOptions), ariaHeaderLabel: nls.localize('explorerSection', "Files Explorer Section") }, keybindingService, contextMenuService, configurationService);
49 50
		this._register(this.contextService.onDidChangeWorkbenchState(() => this.setLabels()));
		this._register(this.labelService.onDidChangeFormatters(() => this.setLabels()));
E
Erich Gamma 已提交
51 52
	}

53
	renderHeader(container: HTMLElement): void {
54 55 56 57 58 59 60
		const titleContainer = document.createElement('div');
		DOM.addClass(titleContainer, 'title');
		container.appendChild(titleContainer);

		this.titleElement = document.createElement('span');
		this.titleElement.textContent = name;
		titleContainer.appendChild(this.titleElement);
E
Erich Gamma 已提交
61 62
	}

J
Joao Moreno 已提交
63
	protected renderBody(container: HTMLElement): void {
E
Erich Gamma 已提交
64
		DOM.addClass(container, 'explorer-empty-view');
I
isidor 已提交
65
		container.tabIndex = 0;
E
Erich Gamma 已提交
66

67 68 69
		const messageContainer = document.createElement('div');
		DOM.addClass(messageContainer, 'section');
		container.appendChild(messageContainer);
E
Erich Gamma 已提交
70

71 72
		this.messageElement = document.createElement('p');
		messageContainer.appendChild(this.messageElement);
E
Erich Gamma 已提交
73

74
		this.button = new Button(messageContainer);
75
		attachButtonStyler(this.button, this.themeService);
J
Joao Moreno 已提交
76

M
Matt Bierner 已提交
77
		this._register(this.button.onDidClick(() => {
M
Matt Bierner 已提交
78 79 80
			if (!this.actionRunner) {
				return;
			}
I
isidor 已提交
81
			const actionClass = this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE ? AddRootFolderAction : OpenFolderAction;
82
			const action = this.instantiationService.createInstance<string, string, IAction>(actionClass, actionClass.ID, actionClass.LABEL);
83
			this.actionRunner.run(action).then(() => {
84 85 86 87 88
				action.dispose();
			}, err => {
				action.dispose();
				errors.onUnexpectedError(err);
			});
I
isidor 已提交
89
		}));
90

M
Matt Bierner 已提交
91
		this._register(new DragAndDropObserver(container, {
I
isidor 已提交
92
			onDrop: e => {
M
Matt Bierner 已提交
93 94
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
95 96 97 98
				const dropHandler = this.instantiationService.createInstance(ResourcesDropHandler, { allowWorkspaceOpen: true });
				dropHandler.handleDrop(e, () => undefined, targetGroup => undefined);
			},
			onDragEnter: (e) => {
M
Matt Bierner 已提交
99 100
				const color = this.themeService.getTheme().getColor(listDropBackground);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
101 102
			},
			onDragEnd: () => {
M
Matt Bierner 已提交
103 104
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
105 106
			},
			onDragLeave: () => {
M
Matt Bierner 已提交
107 108
				const color = this.themeService.getTheme().getColor(SIDE_BAR_BACKGROUND);
				container.style.backgroundColor = color ? color.toString() : '';
I
isidor 已提交
109 110
			},
			onDragOver: e => {
M
Matt Bierner 已提交
111
				e.dataTransfer!.dropEffect = 'copy';
I
isidor 已提交
112
			}
I
isidor 已提交
113
		}));
114

I
isidor 已提交
115
		this.setLabels();
116 117
	}

I
isidor 已提交
118 119
	private setLabels(): void {
		if (this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
120
			this.messageElement.textContent = nls.localize('noWorkspaceHelp', "You have not yet added a folder to the workspace.");
I
isidor 已提交
121 122 123
			if (this.button) {
				this.button.label = nls.localize('addFolder', "Add Folder");
			}
124
			this.titleElement.textContent = EmptyView.NAME;
I
isidor 已提交
125
		} else {
126 127 128 129 130 131
			if (this.environmentService.configuration.remoteAuthority) {
				const hostLabel = this.labelService.getHostLabel(Schemas.vscodeRemote, this.environmentService.configuration.remoteAuthority);
				this.messageElement.textContent = hostLabel ? nls.localize('remoteNoFolderHelp', "Connected to {0}", hostLabel) : nls.localize('connecting', "Connecting...");
			} else {
				this.messageElement.textContent = nls.localize('noFolderHelp', "You have not yet opened a folder.");
			}
I
isidor 已提交
132 133 134
			if (this.button) {
				this.button.label = nls.localize('openFolder', "Open Folder");
			}
135
			this.titleElement.textContent = this.title;
136
		}
E
Erich Gamma 已提交
137 138
	}

139
	layoutBody(size: number): void {
J
Joao Moreno 已提交
140 141 142
		// no-op
	}

143
	focusBody(): void {
144
		if (this.button) {
145
			this.button.element.focus();
B
Benjamin Pasero 已提交
146
		}
E
Erich Gamma 已提交
147
	}
148
}